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'
165 void Package::set_temp_dir(FileName const & temp_dir) const
167 if (temp_dir.empty())
168 temp_dir_ = system_temp_dir_;
170 temp_dir_ = temp_dir;
176 // These next functions contain the stuff that is substituted at
177 // configuration-time.
178 FileName const hardcoded_localedir()
181 // The build system needs to make sure that this is in utf8 encoding.
182 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
186 FileName const hardcoded_system_support_dir()
189 // The build system needs to make sure that this is in utf8 encoding.
190 return FileName(LYX_ABS_INSTALLED_DATADIR);
194 string const & with_version_suffix()
196 static string const program_suffix = PROGRAM_SUFFIX;
197 static string const with_version_suffix =
198 " --with-version-suffix=" PROGRAM_SUFFIX;
199 return program_suffix.empty() ? program_suffix : with_version_suffix;
205 FileName const & Package::top_srcdir()
208 // The build system needs to make sure that this is in utf8 encoding.
209 static FileName const dir(LYX_ABS_TOP_SRCDIR);
216 bool check_command_line_dir(string const & dir,
218 string const & command_line_switch);
220 FileName const extract_env_var_dir(string const & env_var);
222 bool check_env_var_dir(FileName const & dir,
223 string const & env_var);
225 bool check_env_var_dir(FileName const & dir,
227 string const & env_var);
229 string const relative_locale_dir();
231 string const relative_system_support_dir();
235 * Convert \p name to internal path and strip a trailing slash, since it
236 * comes from user input (commandline or environment).
237 * \p name is encoded in utf8.
239 string const fix_dir_name(string const & name)
241 return rtrim(os::internal_path(name), "/");
245 FileName buildSupportDir(string const & binary_dir,
246 exe_build_dir_to_top_build_dir top_build_dir_location)
249 switch (top_build_dir_location) {
250 case top_build_dir_is_one_level_up:
251 indirection = "../lib";
253 case top_build_dir_is_two_levels_up:
254 indirection = "../../lib";
257 return FileName(addPath(binary_dir, indirection));
261 void buildDirs(FileName const & abs_binary,
262 exe_build_dir_to_top_build_dir top_build_dir_location,
263 FileName & build_support_dir, FileName & system_support_dir)
265 string const check_text = "Checking whether LyX is run in place...";
267 // We're looking for "Makefile" in a directory
269 // We're also looking for "chkconfig.ltx" in a directory
271 // If both are found, then we're running LyX in-place.
273 // Note that the name of the lyx binary may be a symbolic link.
274 // If that is the case, then we follow the links too.
275 FileName binary = abs_binary;
277 // Try and find "lyxrc.defaults".
278 string binary_dir = onlyPath(binary.absFilename());
279 // We may be using libtools with static linking.
280 if (suffixIs(binary_dir, ".libs/"))
281 binary_dir = addPath(binary_dir, "../");
282 build_support_dir = buildSupportDir(binary_dir, top_build_dir_location);
283 if (!fileSearch(build_support_dir.absFilename(), "Makefile").empty()) {
284 // Try and find "chkconfig.ltx".
286 FileName(addPath(Package::top_srcdir().absFilename(), "lib"));
288 if (!fileSearch(system_support_dir.absFilename(), "chkconfig.ltx").empty()) {
289 LYXERR(Debug::INIT, check_text << " yes");
294 // Check whether binary is a symbolic link.
295 // If so, resolve it and repeat the exercise.
296 if (!binary.isSymLink())
300 if (readLink(binary, link)) {
303 // Unable to resolve the link.
308 LYXERR(Debug::INIT, check_text << " no");
309 system_support_dir = FileName();
310 build_support_dir = FileName();
314 // Specification of document_dir_ may be reset by LyXRC,
315 // but the default is fixed for a given OS.
316 FileName const get_document_dir(FileName const & home_dir)
318 #if defined (USE_WINDOWS_PACKAGING)
319 (void)home_dir; // Silence warning about unused variable.
320 os::GetFolderPath win32_folder_path;
321 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
328 // The specification of home_dir_ is fixed for a given OS.
329 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
330 // and on a Posix-like machine: "/home/USERNAME".
331 FileName const get_home_dir()
333 #if defined (USE_WINDOWS_PACKAGING)
334 string const home_dir = getEnv("USERPROFILE");
336 string const home_dir = getEnv("HOME");
339 return FileName(fix_dir_name(home_dir));
343 // Several sources are probed to ascertain the locale directory.
344 // The only requirement is that the result is indeed a directory.
345 FileName const get_locale_dir(FileName const & system_support_dir)
347 // 1. Use the "LYX_LOCALEDIR" environment variable.
348 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
349 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
352 // 2. Search for system_support_dir / <relative locale dir>
353 // The <relative locale dir> is OS-dependent. (On Unix, it will
355 FileName path(addPath(system_support_dir.absFilename(),
356 relative_locale_dir()));
358 if (path.exists() && path.isDirectory())
361 // 3. Fall back to the hard-coded LOCALEDIR.
362 path = hardcoded_localedir();
363 if (path.exists() && path.isDirectory())
366 // 4. Search for source tree "po/" in case LyX was launched with
367 // '-sysdir' argument.
368 path.set(addPath(system_support_dir.absFilename(), "../po"));
369 if (path.exists() && path.isDirectory())
376 // Specification of temp_dir_ may be reset by LyXRC,
377 // but the default is fixed for a given OS.
378 FileName const get_temp_dir()
380 #if defined (USE_WINDOWS_PACKAGING)
381 // Typical example: C:/TEMP/.
383 GetTempPath(MAX_PATH, path);
384 return FileName(to_utf8(from_local8bit(path)));
386 return FileName("/tmp");
391 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
392 FileName const abs_path_from_command_line(string const & command_line)
394 if (command_line.empty())
397 string const str_path = fix_dir_name(command_line);
398 FileName path(str_path);
399 return path.isAbsolute() ? path : makeAbsPath(str_path);
403 // Does the grunt work for abs_path_from_binary_name()
404 FileName const get_binary_path(string const & exe)
406 #if defined (USE_WINDOWS_PACKAGING)
407 // The executable may have been invoked either with or
408 // without the .exe extension.
409 // Ensure that it is present.
410 string const as_internal_path = os::internal_path(exe);
411 string const exe_path = suffixIs(as_internal_path, ".exe") ?
412 as_internal_path : as_internal_path + ".exe";
414 string const exe_path = os::internal_path(exe);
416 FileName exepath(exe_path);
417 if (exepath.isAbsolute())
420 // Two possibilities present themselves.
421 // 1. The binary is relative to the CWD.
422 FileName const abs_exe_path = makeAbsPath(exe_path);
423 if (abs_exe_path.exists())
426 // 2. exe must be the name of the binary only and it
427 // can be found on the PATH.
428 string const exe_name = onlyFilename(exe_path);
429 if (exe_name != exe_path)
432 vector<string> const path = getEnvPath("PATH");
433 vector<string>::const_iterator it = path.begin();
434 vector<string>::const_iterator const end = path.end();
435 for (; it != end; ++it) {
436 // This will do nothing if *it is already absolute.
437 string const exe_dir = makeAbsPath(*it).absFilename();
439 FileName const exe_path(addName(exe_dir, exe_name));
440 if (exe_path.exists())
444 // Didn't find anything.
449 // Extracts the absolute path to the binary name received as argv[0].
450 FileName const abs_path_from_binary_name(string const & exe)
452 FileName const abs_binary = get_binary_path(exe);
453 if (abs_binary.empty()) {
455 throw ExceptionMessage(ErrorException,
456 _("LyX binary not found"),
457 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
464 // A plethora of directories is searched to ascertain the system
465 // lyxdir which is defined as the first directory to contain
468 get_system_support_dir(FileName const & abs_binary,
469 string const & command_line_system_support_dir)
471 string const chkconfig_ltx = "chkconfig.ltx";
473 // searched_dirs is used for diagnostic purposes only in the case
474 // that "chkconfig.ltx" is not found.
475 list<FileName> searched_dirs;
477 // 1. Use the -sysdir command line parameter.
478 FileName path = abs_path_from_command_line(command_line_system_support_dir);
480 searched_dirs.push_back(path);
481 if (check_command_line_dir(path.absFilename(), chkconfig_ltx, "-sysdir"))
485 // 2. Use the "LYX_DIR_15x" environment variable.
486 path = extract_env_var_dir("LYX_DIR_15x");
488 searched_dirs.push_back(path);
489 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
493 // 3. Search relative to the lyx binary.
494 // We're looking for "chkconfig.ltx" in a directory
495 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
496 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
497 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
498 string const relative_lyxdir = relative_system_support_dir();
500 // One subtlety to be aware of. The name of the lyx binary may be
501 // a symbolic link. If that is the case, then we follow the links too.
502 FileName binary = abs_binary;
504 // Try and find "chkconfig.ltx".
505 string const binary_dir = onlyPath(binary.absFilename());
507 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
508 searched_dirs.push_back(lyxdir);
510 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
511 // Success! "chkconfig.ltx" has been found.
515 // Check whether binary is a symbolic link.
516 // If so, resolve it and repeat the exercise.
517 if (!binary.isSymLink())
521 if (readLink(binary, link)) {
524 // Unable to resolve the link.
529 // 4. Repeat the exercise on the directory itself.
530 FileName binary_dir(onlyPath(abs_binary.absFilename()));
532 // This time test whether the directory is a symbolic link
533 // *before* looking for "chkconfig.ltx".
534 // (We've looked relative to the original already.)
535 if (!binary.isSymLink())
539 if (readLink(binary_dir, link)) {
542 // Unable to resolve the link.
546 // Try and find "chkconfig.ltx".
547 FileName const lyxdir(addPath(binary_dir.absFilename(),
549 searched_dirs.push_back(lyxdir);
551 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
552 // Success! "chkconfig.ltx" has been found.
557 // 5. In desparation, try the hard-coded system support dir.
558 path = hardcoded_system_support_dir();
559 if (!fileSearch(path.absFilename(), chkconfig_ltx).empty())
562 // Everything has failed :-(
563 // So inform the user and exit.
564 string searched_dirs_str;
565 typedef list<FileName>::const_iterator iterator;
566 iterator const begin = searched_dirs.begin();
567 iterator const end = searched_dirs.end();
568 for (iterator it = begin; it != end; ++it) {
570 searched_dirs_str += "\n\t";
571 searched_dirs_str += it->absFilename();
575 throw ExceptionMessage(ErrorException, _("No system directory"),
576 bformat(_("Unable to determine the system directory "
579 "Use the '-sysdir' command line parameter or "
580 "set the environment variable LYX_DIR_15x to "
581 "the LyX system directory containing the file "
583 from_utf8(searched_dirs_str)));
585 // Keep the compiler happy.
590 // Returns the absolute path to the user lyxdir, together with a flag
591 // indicating whether this directory was specified explicitly (as -userdir
592 // or through an environment variable) or whether it was deduced.
593 bool userSupportDir(FileName const & default_user_support_dir,
594 string const & command_line_user_support_dir, FileName & result)
596 // 1. Use the -userdir command line parameter.
597 result = abs_path_from_command_line(command_line_user_support_dir);
601 // 2. Use the LYX_USERDIR_15x environment variable.
602 result = extract_env_var_dir("LYX_USERDIR_15x");
606 // 3. Use the OS-dependent default_user_support_dir
607 result = default_user_support_dir;
612 // $HOME/.lyx on POSIX but on Win32 it will be something like
613 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
614 FileName const get_default_user_support_dir(FileName const & home_dir)
616 #if defined (USE_WINDOWS_PACKAGING)
617 (void)home_dir; // Silence warning about unused variable.
619 os::GetFolderPath win32_folder_path;
620 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
622 #elif defined (USE_MACOSX_PACKAGING)
623 (void)home_dir; // Silence warning about unused variable.
626 OSErr const error_code =
627 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
628 kDontCreateFolder, &fsref);
632 // FSRefMakePath returns the result in utf8
633 char store[PATH_MAX + 1];
634 OSStatus const status_code =
635 FSRefMakePath(&fsref,
636 reinterpret_cast<UInt8*>(store), PATH_MAX);
637 if (status_code != 0)
640 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
642 #else // USE_POSIX_PACKAGING
643 return FileName(addPath(home_dir.absFilename(), string(".") + PACKAGE));
648 // Check that directory @c dir contains @c file.
649 // Else emit an error message about an invalid @c command_line_switch.
650 bool check_command_line_dir(string const & dir,
652 string const & command_line_switch)
654 FileName const abs_path = fileSearch(dir, file);
655 if (abs_path.empty()) {
657 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
658 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
659 from_utf8(command_line_switch), from_utf8(dir),
663 return !abs_path.empty();
667 // The environment variable @c env_var expands to a (single) file path.
668 FileName const extract_env_var_dir(string const & env_var)
670 string const dir = fix_dir_name(getEnv(env_var));
671 return dir.empty() ? FileName() : makeAbsPath(dir);
675 // Check that directory @c dir contains @c file.
676 // Else emit a warning about an invalid @c env_var.
677 bool check_env_var_dir(FileName const & dir,
679 string const & env_var)
681 FileName const abs_path = fileSearch(dir.absFilename(), file);
682 if (abs_path.empty()) {
684 throw ExceptionMessage(WarningException, _("File not found"), bformat(
685 _("Invalid %1$s environment variable.\n"
686 "Directory %2$s does not contain %3$s."),
687 from_utf8(env_var), from_utf8(dir.absFilename()),
691 return !abs_path.empty();
695 // Check that directory @c dir is indeed a directory.
696 // Else emit a warning about an invalid @c env_var.
697 bool check_env_var_dir(FileName const & dir,
698 string const & env_var)
700 bool const success = dir.exists() && dir.isDirectory();
703 // Put this string on a single line so that the gettext
704 // search mechanism in po/Makefile.in.in will register
705 // Package.cpp.in as a file containing strings that need
708 docstring const fmt =
709 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
711 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
712 fmt, from_utf8(env_var), from_utf8(dir.absFilename())));
719 // The locale directory relative to the LyX system directory.
720 string const relative_locale_dir()
722 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
730 // The system lyxdir is relative to the directory containing the LyX binary.
731 string const relative_system_support_dir()
735 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
736 result = "../Resources/";
738 result = addPath("../share/", PACKAGE);
746 } // namespace support