4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "support/Package.h"
16 #include "support/debug.h"
17 #include "support/environment.h"
18 #include "support/ExceptionMessage.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
25 #if defined (USE_WINDOWS_PACKAGING)
26 # include "support/os_win32.h"
32 #if !defined (USE_WINDOWS_PACKAGING) && \
33 !defined (USE_MACOSX_PACKAGING) && \
34 !defined (USE_POSIX_PACKAGING)
35 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
38 #if defined (USE_MACOSX_PACKAGING)
39 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
50 bool initialised_ = false;
55 void init_package(string const & command_line_arg0,
56 string const & command_line_system_support_dir,
57 string const & command_line_user_support_dir,
58 exe_build_dir_to_top_build_dir top_build_dir_location)
60 package_ = Package(command_line_arg0,
61 command_line_system_support_dir,
62 command_line_user_support_dir,
63 top_build_dir_location);
68 Package const & package()
70 LASSERT(initialised_, /**/);
77 FileName const abs_path_from_binary_name(string const & exe);
79 void buildDirs(FileName const & abs_binary,
80 exe_build_dir_to_top_build_dir top_build_dir_location,
81 FileName &, FileName &);
83 FileName const get_document_dir(FileName const & home_dir);
85 FileName const get_home_dir();
87 FileName const get_locale_dir(FileName const & system_support_dir);
89 FileName const get_system_support_dir(FileName const & abs_binary,
90 string const & command_line_system_support_dir);
92 FileName const get_default_user_support_dir(FileName const & home_dir);
94 bool userSupportDir(FileName const & default_user_support_dir,
95 string const & command_line_user_support_dir, FileName & result);
98 string const & with_version_suffix();
103 Package::Package(string const & command_line_arg0,
104 string const & command_line_system_support_dir,
105 string const & command_line_user_support_dir,
106 exe_build_dir_to_top_build_dir top_build_dir_location)
107 : explicit_user_support_dir_(false)
109 home_dir_ = get_home_dir();
110 // Specification of temp_dir_ may be reset by LyXRC,
111 // but the default is fixed for a given OS.
112 system_temp_dir_ = FileName::tempPath();
113 temp_dir_ = system_temp_dir_;
114 document_dir_ = get_document_dir(home_dir_);
116 FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
117 binary_dir_ = FileName(onlyPath(abs_binary.absFileName()));
119 // Is LyX being run in-place from the build tree?
120 buildDirs(abs_binary, top_build_dir_location,
121 build_support_dir_, system_support_dir_);
123 if (build_support_dir_.empty())
124 system_support_dir_ =
125 get_system_support_dir(abs_binary,
126 command_line_system_support_dir);
128 // The LyX executable is one level above binary_dir_ if we are running
129 // tex2lyx in place. Otherwise it is in binary_dir_.
131 if (build_support_dir_.empty() ||
132 top_build_dir_location == top_build_dir_is_one_level_up)
133 abs_lyx_dir = binary_dir_.absFileName();
135 FileName fn(addPath(binary_dir_.absFileName(), "../"));
136 abs_lyx_dir = fn.realPath();
138 // The LyX executable may have a package suffix if we are not running
140 if (build_support_dir_.empty())
141 lyx_binary_ = FileName(addName(abs_lyx_dir,
142 "lyx" + string(PROGRAM_SUFFIX)));
144 lyx_binary_ = FileName(addName(abs_lyx_dir, "lyx"));
146 locale_dir_ = get_locale_dir(system_support_dir_);
148 FileName const default_user_support_dir =
149 get_default_user_support_dir(home_dir_);
151 explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
152 command_line_user_support_dir, user_support_dir_);
154 FileName const configure_script(addName(system_support().absFileName(), "configure.py"));
155 configure_command_ = os::python() + ' ' +
156 quoteName(configure_script.toFilesystemEncoding(), quote_python) +
157 with_version_suffix();
159 LYXERR(Debug::INIT, "<package>\n"
160 << "\tbinary_dir " << binary_dir().absFileName() << '\n'
161 << "\tsystem_support " << system_support().absFileName() << '\n'
162 << "\tbuild_support " << build_support().absFileName() << '\n'
163 << "\tuser_support " << user_support().absFileName() << '\n'
164 << "\tlocale_dir " << locale_dir().absFileName() << '\n'
165 << "\tdocument_dir " << document_dir().absFileName() << '\n'
166 << "\ttemp_dir " << temp_dir().absFileName() << '\n'
167 << "\thome_dir " << home_dir().absFileName() << '\n'
172 void Package::set_temp_dir(FileName const & temp_dir) const
174 if (temp_dir.empty())
175 temp_dir_ = system_temp_dir_;
177 temp_dir_ = temp_dir;
183 // These next functions contain the stuff that is substituted at
184 // configuration-time.
185 FileName const hardcoded_localedir()
188 // The build system needs to make sure that this is in utf8 encoding.
189 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
193 FileName const hardcoded_system_support_dir()
196 // The build system needs to make sure that this is in utf8 encoding.
197 return FileName(LYX_ABS_INSTALLED_DATADIR);
201 string const & with_version_suffix()
203 static string const program_suffix = PROGRAM_SUFFIX;
204 static string const with_version_suffix =
205 " --with-version-suffix=" PROGRAM_SUFFIX;
206 return program_suffix.empty() ? program_suffix : with_version_suffix;
212 FileName const & Package::top_srcdir()
215 // The build system needs to make sure that this is in utf8 encoding.
216 static FileName const dir(LYX_ABS_TOP_SRCDIR);
223 bool check_command_line_dir(string const & dir,
225 string const & command_line_switch);
227 FileName const extract_env_var_dir(string const & env_var);
229 bool check_env_var_dir(FileName const & dir,
230 string const & env_var);
232 bool check_env_var_dir(FileName const & dir,
234 string const & env_var);
236 string const relative_locale_dir();
238 string const relative_system_support_dir();
242 * Convert \p name to internal path and strip a trailing slash, since it
243 * comes from user input (commandline or environment).
244 * \p name is encoded in utf8.
246 string const fix_dir_name(string const & name)
248 return rtrim(os::internal_path(name), "/");
252 FileName buildSupportDir(string const & binary_dir,
253 exe_build_dir_to_top_build_dir top_build_dir_location)
256 switch (top_build_dir_location) {
257 case top_build_dir_is_one_level_up:
258 indirection = "../lib";
260 case top_build_dir_is_two_levels_up:
261 indirection = "../../lib";
264 return FileName(addPath(binary_dir, indirection));
268 void buildDirs(FileName const & abs_binary,
269 exe_build_dir_to_top_build_dir top_build_dir_location,
270 FileName & build_support_dir, FileName & system_support_dir)
272 string const check_text = "Checking whether LyX is run in place...";
274 // We're looking for "Makefile" in a directory
276 // We're also looking for "chkconfig.ltx" in a directory
278 // If both are found, then we're running LyX in-place.
280 // Note that the name of the lyx binary may be a symbolic link.
281 // If that is the case, then we follow the links too.
282 FileName binary = abs_binary;
284 // Try and find "lyxrc.defaults".
285 string binary_dir = onlyPath(binary.absFileName());
286 build_support_dir = buildSupportDir(binary_dir, top_build_dir_location);
287 if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
288 // Try and find "chkconfig.ltx".
290 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
292 if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
293 LYXERR(Debug::INIT, check_text << " yes");
298 // Check whether binary is a symbolic link.
299 // If so, resolve it and repeat the exercise.
300 if (!binary.isSymLink())
304 if (readLink(binary, link)) {
307 // Unable to resolve the link.
312 LYXERR(Debug::INIT, check_text << " no");
313 system_support_dir = FileName();
314 build_support_dir = FileName();
318 // Specification of document_dir_ may be reset by LyXRC,
319 // but the default is fixed for a given OS.
320 FileName const get_document_dir(FileName const & home_dir)
322 #if defined (USE_WINDOWS_PACKAGING)
323 (void)home_dir; // Silence warning about unused variable.
324 os::GetFolderPath win32_folder_path;
325 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
332 // The specification of home_dir_ is fixed for a given OS.
333 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
334 // and on a Posix-like machine: "/home/USERNAME".
335 FileName const get_home_dir()
337 #if defined (USE_WINDOWS_PACKAGING)
338 string const home_dir = getEnv("USERPROFILE");
340 string const home_dir = getEnv("HOME");
343 return FileName(fix_dir_name(home_dir));
347 // Several sources are probed to ascertain the locale directory.
348 // The only requirement is that the result is indeed a directory.
349 FileName const get_locale_dir(FileName const & system_support_dir)
351 // 1. Use the "LYX_LOCALEDIR" environment variable.
352 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
353 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
356 // 2. Search for system_support_dir / <relative locale dir>
357 // The <relative locale dir> is OS-dependent. (On Unix, it will
359 FileName path(addPath(system_support_dir.absFileName(),
360 relative_locale_dir()));
362 if (path.exists() && path.isDirectory())
365 // 3. Fall back to the hard-coded LOCALEDIR.
366 path = hardcoded_localedir();
367 if (path.exists() && path.isDirectory())
374 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
375 FileName const abs_path_from_command_line(string const & command_line)
377 if (command_line.empty())
380 string const str_path = fix_dir_name(command_line);
381 return makeAbsPath(str_path);
385 // Does the grunt work for abs_path_from_binary_name()
386 FileName const get_binary_path(string const & exe)
388 #if defined (USE_WINDOWS_PACKAGING)
389 // The executable may have been invoked either with or
390 // without the .exe extension.
391 // Ensure that it is present.
392 string const as_internal_path = os::internal_path(exe);
393 string const exe_path = suffixIs(as_internal_path, ".exe") ?
394 as_internal_path : as_internal_path + ".exe";
396 string const exe_path = os::internal_path(exe);
398 if (FileName::isAbsolute(exe_path))
399 return FileName(exe_path);
401 // Two possibilities present themselves.
402 // 1. The binary is relative to the CWD.
403 FileName const abs_exe_path = makeAbsPath(exe_path);
404 if (abs_exe_path.exists())
407 // 2. exe must be the name of the binary only and it
408 // can be found on the PATH.
409 string const exe_name = onlyFileName(exe_path);
410 if (exe_name != exe_path)
413 vector<string> const path = getEnvPath("PATH");
414 vector<string>::const_iterator it = path.begin();
415 vector<string>::const_iterator const end = path.end();
416 for (; it != end; ++it) {
417 // This will do nothing if *it is already absolute.
418 string const exe_dir = makeAbsPath(*it).absFileName();
420 FileName const exe_path(addName(exe_dir, exe_name));
421 if (exe_path.exists())
425 // Didn't find anything.
430 // Extracts the absolute path to the binary name received as argv[0].
431 FileName const abs_path_from_binary_name(string const & exe)
433 FileName const abs_binary = get_binary_path(exe);
434 if (abs_binary.empty()) {
436 throw ExceptionMessage(ErrorException,
437 _("LyX binary not found"),
438 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
445 // A plethora of directories is searched to ascertain the system
446 // lyxdir which is defined as the first directory to contain
449 get_system_support_dir(FileName const & abs_binary,
450 string const & command_line_system_support_dir)
452 string const chkconfig_ltx = "chkconfig.ltx";
454 // searched_dirs is used for diagnostic purposes only in the case
455 // that "chkconfig.ltx" is not found.
456 list<FileName> searched_dirs;
458 // 1. Use the -sysdir command line parameter.
459 FileName path = abs_path_from_command_line(command_line_system_support_dir);
461 searched_dirs.push_back(path);
462 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
466 // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
467 path = extract_env_var_dir(LYX_DIR_VER);
469 searched_dirs.push_back(path);
470 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
474 // 3. Search relative to the lyx binary.
475 // We're looking for "chkconfig.ltx" in a directory
476 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
477 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
478 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
479 string const relative_lyxdir = relative_system_support_dir();
481 // One subtlety to be aware of. The name of the lyx binary may be
482 // a symbolic link. If that is the case, then we follow the links too.
483 FileName binary = abs_binary;
485 // Try and find "chkconfig.ltx".
486 string const binary_dir = onlyPath(binary.absFileName());
488 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
489 searched_dirs.push_back(lyxdir);
491 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
492 // Success! "chkconfig.ltx" has been found.
496 // Check whether binary is a symbolic link.
497 // If so, resolve it and repeat the exercise.
498 if (!binary.isSymLink())
502 if (readLink(binary, link)) {
505 // Unable to resolve the link.
510 // 4. Repeat the exercise on the directory itself.
511 FileName binary_dir(onlyPath(abs_binary.absFileName()));
513 // This time test whether the directory is a symbolic link
514 // *before* looking for "chkconfig.ltx".
515 // (We've looked relative to the original already.)
516 if (!binary.isSymLink())
520 if (readLink(binary_dir, link)) {
523 // Unable to resolve the link.
527 // Try and find "chkconfig.ltx".
528 FileName const lyxdir(addPath(binary_dir.absFileName(),
530 searched_dirs.push_back(lyxdir);
532 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
533 // Success! "chkconfig.ltx" has been found.
538 // 5. In desparation, try the hard-coded system support dir.
539 path = hardcoded_system_support_dir();
540 if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
543 // Everything has failed :-(
544 // So inform the user and exit.
545 string searched_dirs_str;
546 typedef list<FileName>::const_iterator iterator;
547 iterator const begin = searched_dirs.begin();
548 iterator const end = searched_dirs.end();
549 for (iterator it = begin; it != end; ++it) {
551 searched_dirs_str += "\n\t";
552 searched_dirs_str += it->absFileName();
556 throw ExceptionMessage(ErrorException, _("No system directory"),
557 bformat(_("Unable to determine the system directory "
560 "Use the '-sysdir' command line parameter or "
561 "set the environment variable\n%2$s "
562 "to the LyX system directory containing the "
563 "file `chkconfig.ltx'."),
564 from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
566 // Keep the compiler happy.
571 // Returns the absolute path to the user lyxdir, together with a flag
572 // indicating whether this directory was specified explicitly (as -userdir
573 // or through an environment variable) or whether it was deduced.
574 bool userSupportDir(FileName const & default_user_support_dir,
575 string const & command_line_user_support_dir, FileName & result)
577 // 1. Use the -userdir command line parameter.
578 result = abs_path_from_command_line(command_line_user_support_dir);
582 // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
583 result = extract_env_var_dir(LYX_USERDIR_VER);
587 // 3. Use the OS-dependent default_user_support_dir
588 result = default_user_support_dir;
593 // $HOME/.lyx on POSIX but on Win32 it will be something like
594 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
595 FileName const get_default_user_support_dir(FileName const & home_dir)
597 #if defined (USE_WINDOWS_PACKAGING)
598 (void)home_dir; // Silence warning about unused variable.
600 os::GetFolderPath win32_folder_path;
601 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
603 #elif defined (USE_MACOSX_PACKAGING)
604 (void)home_dir; // Silence warning about unused variable.
607 OSErr const error_code =
608 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
609 kDontCreateFolder, &fsref);
613 // FSRefMakePath returns the result in utf8
614 char store[PATH_MAX + 1];
615 OSStatus const status_code =
616 FSRefMakePath(&fsref,
617 reinterpret_cast<UInt8*>(store), PATH_MAX);
618 if (status_code != 0)
621 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
623 #else // USE_POSIX_PACKAGING
624 return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
629 // Check that directory @c dir contains @c file.
630 // Else emit an error message about an invalid @c command_line_switch.
631 bool check_command_line_dir(string const & dir,
633 string const & command_line_switch)
635 FileName const abs_path = fileSearch(dir, file);
636 if (abs_path.empty()) {
638 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
639 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
640 from_utf8(command_line_switch), from_utf8(dir),
644 return !abs_path.empty();
648 // The environment variable @c env_var expands to a (single) file path.
649 FileName const extract_env_var_dir(string const & env_var)
651 string const dir = fix_dir_name(getEnv(env_var));
652 return dir.empty() ? FileName() : makeAbsPath(dir);
656 // Check that directory @c dir contains @c file.
657 // Else emit a warning about an invalid @c env_var.
658 bool check_env_var_dir(FileName const & dir,
660 string const & env_var)
662 FileName const abs_path = fileSearch(dir.absFileName(), file);
663 if (abs_path.empty()) {
665 throw ExceptionMessage(WarningException, _("File not found"), bformat(
666 _("Invalid %1$s environment variable.\n"
667 "Directory %2$s does not contain %3$s."),
668 from_utf8(env_var), from_utf8(dir.absFileName()),
672 return !abs_path.empty();
676 // Check that directory @c dir is indeed a directory.
677 // Else emit a warning about an invalid @c env_var.
678 bool check_env_var_dir(FileName const & dir,
679 string const & env_var)
681 bool const success = dir.exists() && dir.isDirectory();
684 // Put this string on a single line so that the gettext
685 // search mechanism in po/Makefile.in.in will register
686 // Package.cpp.in as a file containing strings that need
689 docstring const fmt =
690 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
692 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
693 fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
700 // The locale directory relative to the LyX system directory.
701 string const relative_locale_dir()
703 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
711 // The system lyxdir is relative to the directory containing the LyX binary.
712 string const relative_system_support_dir()
716 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
717 result = "../Resources/";
719 result = addPath("../share/", PACKAGE);
727 } // namespace support