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/lstrings.h"
22 #include "support/os.h"
24 #if defined (USE_WINDOWS_PACKAGING)
25 # include "support/os_win32.h"
31 #if !defined (USE_WINDOWS_PACKAGING) && \
32 !defined (USE_MACOSX_PACKAGING) && \
33 !defined (USE_POSIX_PACKAGING)
34 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
37 #if defined (USE_MACOSX_PACKAGING)
38 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
49 bool initialised_ = false;
54 void init_package(string const & command_line_arg0,
55 string const & command_line_system_support_dir,
56 string const & command_line_user_support_dir,
57 exe_build_dir_to_top_build_dir top_build_dir_location)
59 // Can do so only once.
63 package_ = Package(command_line_arg0,
64 command_line_system_support_dir,
65 command_line_user_support_dir,
66 top_build_dir_location);
71 Package const & package()
73 // Commented out because package().locale_dir() can be called
74 // from the message translation code in Messages.cpp before
75 // init_package() is called. Lars is on the case...
76 // LASSERT(initialised_, /**/);
83 FileName const abs_path_from_binary_name(string const & exe);
85 void buildDirs(FileName const & abs_binary,
86 exe_build_dir_to_top_build_dir top_build_dir_location,
87 FileName &, FileName &);
89 FileName const get_document_dir(FileName const & home_dir);
91 FileName const get_home_dir();
93 FileName const get_locale_dir(FileName const & system_support_dir);
95 FileName const get_system_support_dir(FileName const & abs_binary,
96 string const & command_line_system_support_dir);
98 FileName const get_temp_dir();
100 FileName const get_default_user_support_dir(FileName const & home_dir);
102 bool userSupportDir(FileName const & default_user_support_dir,
103 string const & command_line_user_support_dir, FileName & result);
106 string const & with_version_suffix();
111 Package::Package(string const & command_line_arg0,
112 string const & command_line_system_support_dir,
113 string const & command_line_user_support_dir,
114 exe_build_dir_to_top_build_dir top_build_dir_location)
115 : explicit_user_support_dir_(false)
117 home_dir_ = get_home_dir();
118 system_temp_dir_ = get_temp_dir();
119 temp_dir_ = system_temp_dir_;
120 document_dir_ = get_document_dir(home_dir_);
122 FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
123 string const bdir = onlyPath(abs_binary.absFilename());
124 // We may be using libtools
125 if (suffixIs(bdir, ".libs/"))
126 binary_dir_ = FileName(addPath(bdir, "../"));
128 binary_dir_ = FileName(bdir);
130 // Is LyX being run in-place from the build tree?
131 buildDirs(abs_binary, top_build_dir_location,
132 build_support_dir_, system_support_dir_);
134 if (build_support_dir_.empty())
135 system_support_dir_ =
136 get_system_support_dir(abs_binary,
137 command_line_system_support_dir);
139 locale_dir_ = get_locale_dir(system_support_dir_);
141 FileName const default_user_support_dir =
142 get_default_user_support_dir(home_dir_);
144 explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
145 command_line_user_support_dir, user_support_dir_);
147 FileName const configure_script(addName(system_support().absFilename(), "configure.py"));
148 configure_command_ = os::python() + ' ' +
149 quoteName(configure_script.toFilesystemEncoding()) +
150 with_version_suffix();
152 LYXERR(Debug::INIT, "<package>\n"
153 << "\tbinary_dir " << binary_dir().absFilename() << '\n'
154 << "\tsystem_support " << system_support().absFilename() << '\n'
155 << "\tbuild_support " << build_support().absFilename() << '\n'
156 << "\tuser_support " << user_support().absFilename() << '\n'
157 << "\tlocale_dir " << locale_dir().absFilename() << '\n'
158 << "\tdocument_dir " << document_dir().absFilename() << '\n'
159 << "\ttemp_dir " << temp_dir().absFilename() << '\n'
160 << "\thome_dir " << home_dir().absFilename() << '\n'
167 // These next functions contain the stuff that is substituted at
168 // configuration-time.
169 FileName const hardcoded_localedir()
172 // The build system needs to make sure that this is in utf8 encoding.
173 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
177 FileName const hardcoded_system_support_dir()
180 // The build system needs to make sure that this is in utf8 encoding.
181 return FileName(LYX_ABS_INSTALLED_DATADIR);
185 string const & with_version_suffix()
187 static string const program_suffix = PROGRAM_SUFFIX;
188 static string const with_version_suffix =
189 " --with-version-suffix=" PROGRAM_SUFFIX;
190 return program_suffix.empty() ? program_suffix : with_version_suffix;
196 FileName const & Package::top_srcdir()
199 // The build system needs to make sure that this is in utf8 encoding.
200 static FileName const dir(LYX_ABS_TOP_SRCDIR);
207 bool check_command_line_dir(string const & dir,
209 string const & command_line_switch);
211 FileName const extract_env_var_dir(string const & env_var);
213 bool check_env_var_dir(FileName const & dir,
214 string const & env_var);
216 bool check_env_var_dir(FileName const & dir,
218 string const & env_var);
220 string const relative_locale_dir();
222 string const relative_system_support_dir();
226 * Convert \p name to internal path and strip a trailing slash, since it
227 * comes from user input (commandline or environment).
228 * \p name is encoded in utf8.
230 string const fix_dir_name(string const & name)
232 return rtrim(os::internal_path(name), "/");
236 FileName buildSupportDir(string const & binary_dir,
237 exe_build_dir_to_top_build_dir top_build_dir_location)
240 switch (top_build_dir_location) {
241 case top_build_dir_is_one_level_up:
242 indirection = "../lib";
244 case top_build_dir_is_two_levels_up:
245 indirection = "../../lib";
248 return FileName(addPath(binary_dir, indirection));
252 void buildDirs(FileName const & abs_binary,
253 exe_build_dir_to_top_build_dir top_build_dir_location,
254 FileName & build_support_dir, FileName & system_support_dir)
256 string const check_text = "Checking whether LyX is run in place...";
258 // We're looking for "Makefile" in a directory
260 // We're also looking for "chkconfig.ltx" in a directory
262 // If both are found, then we're running LyX in-place.
264 // Note that the name of the lyx binary may be a symbolic link.
265 // If that is the case, then we follow the links too.
266 FileName binary = abs_binary;
268 // Try and find "lyxrc.defaults".
269 string binary_dir = onlyPath(binary.absFilename());
270 // We may be using libtools with static linking.
271 if (suffixIs(binary_dir, ".libs/"))
272 binary_dir = addPath(binary_dir, "../");
273 build_support_dir = buildSupportDir(binary_dir, top_build_dir_location);
274 if (!fileSearch(build_support_dir.absFilename(), "Makefile").empty()) {
275 // Try and find "chkconfig.ltx".
277 FileName(addPath(Package::top_srcdir().absFilename(), "lib"));
279 if (!fileSearch(system_support_dir.absFilename(), "chkconfig.ltx").empty()) {
280 LYXERR(Debug::INIT, check_text << " yes");
285 // Check whether binary is a symbolic link.
286 // If so, resolve it and repeat the exercise.
287 if (!binary.isSymLink())
291 if (readLink(binary, link)) {
294 // Unable to resolve the link.
299 LYXERR(Debug::INIT, check_text << " no");
300 system_support_dir = FileName();
301 build_support_dir = FileName();
305 // Specification of document_dir_ may be reset by LyXRC,
306 // but the default is fixed for a given OS.
307 FileName const get_document_dir(FileName const & home_dir)
309 #if defined (USE_WINDOWS_PACKAGING)
310 (void)home_dir; // Silence warning about unused variable.
311 os::GetFolderPath win32_folder_path;
312 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
319 // The specification of home_dir_ is fixed for a given OS.
320 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
321 // and on a Posix-like machine: "/home/USERNAME".
322 FileName const get_home_dir()
324 #if defined (USE_WINDOWS_PACKAGING)
325 string const home_dir = getEnv("USERPROFILE");
327 string const home_dir = getEnv("HOME");
330 return FileName(fix_dir_name(home_dir));
334 // Several sources are probed to ascertain the locale directory.
335 // The only requirement is that the result is indeed a directory.
336 FileName const get_locale_dir(FileName const & system_support_dir)
338 // 1. Use the "LYX_LOCALEDIR" environment variable.
339 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
340 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
343 // 2. Search for system_support_dir / <relative locale dir>
344 // The <relative locale dir> is OS-dependent. (On Unix, it will
346 FileName path(addPath(system_support_dir.absFilename(),
347 relative_locale_dir()));
349 if (path.exists() && path.isDirectory())
352 // 3. Fall back to the hard-coded LOCALEDIR.
353 path = hardcoded_localedir();
354 if (path.exists() && path.isDirectory())
361 // Specification of temp_dir_ may be reset by LyXRC,
362 // but the default is fixed for a given OS.
363 FileName const get_temp_dir()
365 #if defined (USE_WINDOWS_PACKAGING)
366 // Typical example: C:/TEMP/.
368 GetTempPath(MAX_PATH, path);
369 return FileName(to_utf8(from_local8bit(path)));
371 return FileName("/tmp");
376 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
377 FileName const abs_path_from_command_line(string const & command_line)
379 if (command_line.empty())
382 string const str_path = fix_dir_name(command_line);
383 FileName path(str_path);
384 return path.isAbsolute() ? path : makeAbsPath(str_path);
388 // Does the grunt work for abs_path_from_binary_name()
389 FileName const get_binary_path(string const & exe)
391 #if defined (USE_WINDOWS_PACKAGING)
392 // The executable may have been invoked either with or
393 // without the .exe extension.
394 // Ensure that it is present.
395 string const as_internal_path = os::internal_path(exe);
396 string const exe_path = suffixIs(as_internal_path, ".exe") ?
397 as_internal_path : as_internal_path + ".exe";
399 string const exe_path = os::internal_path(exe);
401 FileName exepath(exe_path);
402 if (exepath.isAbsolute())
405 // Two possibilities present themselves.
406 // 1. The binary is relative to the CWD.
407 FileName const abs_exe_path = makeAbsPath(exe_path);
408 if (abs_exe_path.exists())
411 // 2. exe must be the name of the binary only and it
412 // can be found on the PATH.
413 string const exe_name = onlyFilename(exe_path);
414 if (exe_name != exe_path)
417 vector<string> const path = getEnvPath("PATH");
418 vector<string>::const_iterator it = path.begin();
419 vector<string>::const_iterator const end = path.end();
420 for (; it != end; ++it) {
421 // This will do nothing if *it is already absolute.
422 string const exe_dir = makeAbsPath(*it).absFilename();
424 FileName const exe_path(addName(exe_dir, exe_name));
425 if (exe_path.exists())
429 // Didn't find anything.
434 // Extracts the absolute path to the binary name received as argv[0].
435 FileName const abs_path_from_binary_name(string const & exe)
437 FileName const abs_binary = get_binary_path(exe);
438 if (abs_binary.empty()) {
440 throw ExceptionMessage(ErrorException,
441 _("LyX binary not found"),
442 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
449 // A plethora of directories is searched to ascertain the system
450 // lyxdir which is defined as the first directory to contain
453 get_system_support_dir(FileName const & abs_binary,
454 string const & command_line_system_support_dir)
456 string const chkconfig_ltx = "chkconfig.ltx";
458 // searched_dirs is used for diagnostic purposes only in the case
459 // that "chkconfig.ltx" is not found.
460 list<FileName> searched_dirs;
462 // 1. Use the -sysdir command line parameter.
463 FileName path = abs_path_from_command_line(command_line_system_support_dir);
465 searched_dirs.push_back(path);
466 if (check_command_line_dir(path.absFilename(), chkconfig_ltx, "-sysdir"))
470 // 2. Use the "LYX_DIR_15x" environment variable.
471 path = extract_env_var_dir("LYX_DIR_15x");
473 searched_dirs.push_back(path);
474 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
478 // 3. Search relative to the lyx binary.
479 // We're looking for "chkconfig.ltx" in a directory
480 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
481 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
482 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
483 string const relative_lyxdir = relative_system_support_dir();
485 // One subtlety to be aware of. The name of the lyx binary may be
486 // a symbolic link. If that is the case, then we follow the links too.
487 FileName binary = abs_binary;
489 // Try and find "chkconfig.ltx".
490 string const binary_dir = onlyPath(binary.absFilename());
492 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
493 searched_dirs.push_back(lyxdir);
495 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
496 // Success! "chkconfig.ltx" has been found.
500 // Check whether binary is a symbolic link.
501 // If so, resolve it and repeat the exercise.
502 if (!binary.isSymLink())
506 if (readLink(binary, link)) {
509 // Unable to resolve the link.
514 // 4. Repeat the exercise on the directory itself.
515 FileName binary_dir(onlyPath(abs_binary.absFilename()));
517 // This time test whether the directory is a symbolic link
518 // *before* looking for "chkconfig.ltx".
519 // (We've looked relative to the original already.)
520 if (!binary.isSymLink())
524 if (readLink(binary_dir, link)) {
527 // Unable to resolve the link.
531 // Try and find "chkconfig.ltx".
532 FileName const lyxdir(addPath(binary_dir.absFilename(),
534 searched_dirs.push_back(lyxdir);
536 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
537 // Success! "chkconfig.ltx" has been found.
542 // 5. In desparation, try the hard-coded system support dir.
543 path = hardcoded_system_support_dir();
544 if (!fileSearch(path.absFilename(), chkconfig_ltx).empty())
547 // Everything has failed :-(
548 // So inform the user and exit.
549 string searched_dirs_str;
550 typedef list<FileName>::const_iterator iterator;
551 iterator const begin = searched_dirs.begin();
552 iterator const end = searched_dirs.end();
553 for (iterator it = begin; it != end; ++it) {
555 searched_dirs_str += "\n\t";
556 searched_dirs_str += it->absFilename();
560 throw ExceptionMessage(ErrorException, _("No system directory"),
561 bformat(_("Unable to determine the system directory "
564 "Use the '-sysdir' command line parameter or "
565 "set the environment variable LYX_DIR_15x to "
566 "the LyX system directory containing the file "
568 from_utf8(searched_dirs_str)));
570 // Keep the compiler happy.
575 // Returns the absolute path to the user lyxdir, together with a flag
576 // indicating whether this directory was specified explicitly (as -userdir
577 // or through an environment variable) or whether it was deduced.
578 bool userSupportDir(FileName const & default_user_support_dir,
579 string const & command_line_user_support_dir, FileName & result)
581 // 1. Use the -userdir command line parameter.
582 result = abs_path_from_command_line(command_line_user_support_dir);
586 // 2. Use the LYX_USERDIR_15x environment variable.
587 result = extract_env_var_dir("LYX_USERDIR_15x");
591 // 3. Use the OS-dependent default_user_support_dir
592 result = default_user_support_dir;
597 // $HOME/.lyx on POSIX but on Win32 it will be something like
598 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
599 FileName const get_default_user_support_dir(FileName const & home_dir)
601 #if defined (USE_WINDOWS_PACKAGING)
602 (void)home_dir; // Silence warning about unused variable.
604 os::GetFolderPath win32_folder_path;
605 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
607 #elif defined (USE_MACOSX_PACKAGING)
608 (void)home_dir; // Silence warning about unused variable.
611 OSErr const error_code =
612 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
613 kDontCreateFolder, &fsref);
617 // FSRefMakePath returns the result in utf8
618 char store[PATH_MAX + 1];
619 OSStatus const status_code =
620 FSRefMakePath(&fsref,
621 reinterpret_cast<UInt8*>(store), PATH_MAX);
622 if (status_code != 0)
625 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
627 #else // USE_POSIX_PACKAGING
628 return FileName(addPath(home_dir.absFilename(), string(".") + PACKAGE));
633 // Check that directory @c dir contains @c file.
634 // Else emit a warning about an invalid @c command_line_switch.
635 bool check_command_line_dir(string const & dir,
637 string const & command_line_switch)
639 FileName const abs_path = fileSearch(dir, file);
640 if (abs_path.empty()) {
642 throw ExceptionMessage(WarningException, _("File not found"), bformat(
643 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
644 from_utf8(command_line_switch), from_utf8(dir),
648 return !abs_path.empty();
652 // The environment variable @c env_var expands to a (single) file path.
653 FileName const extract_env_var_dir(string const & env_var)
655 string const dir = fix_dir_name(getEnv(env_var));
656 return dir.empty() ? FileName() : makeAbsPath(dir);
660 // Check that directory @c dir contains @c file.
661 // Else emit a warning about an invalid @c env_var.
662 bool check_env_var_dir(FileName const & dir,
664 string const & env_var)
666 FileName const abs_path = fileSearch(dir.absFilename(), file);
667 if (abs_path.empty()) {
669 throw ExceptionMessage(WarningException, _("File not found"), bformat(
670 _("Invalid %1$s environment variable.\n"
671 "Directory %2$s does not contain %3$s."),
672 from_utf8(env_var), from_utf8(dir.absFilename()),
676 return !abs_path.empty();
680 // Check that directory @c dir is indeed a directory.
681 // Else emit a warning about an invalid @c env_var.
682 bool check_env_var_dir(FileName const & dir,
683 string const & env_var)
685 bool const success = dir.exists() && dir.isDirectory();
688 // Put this string on a single line so that the gettext
689 // search mechanism in po/Makefile.in.in will register
690 // Package.cpp.in as a file containing strings that need
693 docstring const fmt =
694 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
696 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
697 fmt, from_utf8(env_var), from_utf8(dir.absFilename())));
704 // The locale directory relative to the LyX system directory.
705 string const relative_locale_dir()
707 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
715 // The system lyxdir is relative to the directory containing the LyX binary.
716 string const relative_system_support_dir()
720 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
721 result = "../Resources/";
723 result = addPath("../share/", PACKAGE);
731 } // namespace support