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.
11 * Warning! This file is autogenerated from Package.cpp.in.
12 * All changes to this file will be lost.
17 #include "support/Package.h"
22 #include "support/environment.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/ExceptionMessage.h"
26 #include "support/os.h"
28 #if defined (USE_WINDOWS_PACKAGING)
29 # include "support/os_win32.h"
32 #include <boost/filesystem/operations.hpp>
33 #include <boost/tuple/tuple.hpp>
38 #if !defined (USE_WINDOWS_PACKAGING) && \
39 !defined (USE_MACOSX_PACKAGING) && \
40 !defined (USE_POSIX_PACKAGING)
41 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
44 #if defined (USE_MACOSX_PACKAGING)
45 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
50 namespace fs = boost::filesystem;
58 bool initialised_ = false;
63 void init_package(string const & command_line_arg0,
64 string const & command_line_system_support_dir,
65 string const & command_line_user_support_dir,
66 exe_build_dir_to_top_build_dir top_build_dir_location)
68 // Can do so only once.
72 package_ = Package(command_line_arg0,
73 command_line_system_support_dir,
74 command_line_user_support_dir,
75 top_build_dir_location);
80 Package const & package()
82 // Commented out because package().locale_dir() can be called
83 // from the message translation code in Messages.cpp before
84 // init_package() is called. Lars is on the case...
85 // BOOST_ASSERT(initialised_);
92 FileName const abs_path_from_binary_name(string const & exe);
94 std::pair<FileName, FileName> const
95 get_build_dirs(FileName const & abs_binary,
96 exe_build_dir_to_top_build_dir top_build_dir_location);
98 FileName const get_document_dir(FileName const & home_dir);
100 FileName const get_home_dir();
102 FileName const get_locale_dir(FileName const & system_support_dir);
104 FileName const get_system_support_dir(FileName const & abs_binary,
105 string const & command_line_system_support_dir);
107 FileName const get_temp_dir();
109 FileName const get_default_user_support_dir(FileName const & home_dir);
111 std::pair<FileName, bool> const
112 get_user_support_dir(FileName const & default_user_support_dir,
113 string const & command_line_user_support_dir);
116 string const & with_version_suffix();
121 Package::Package(string const & command_line_arg0,
122 string const & command_line_system_support_dir,
123 string const & command_line_user_support_dir,
124 exe_build_dir_to_top_build_dir top_build_dir_location)
125 : explicit_user_support_dir_(false)
127 home_dir_ = get_home_dir();
128 system_temp_dir_ = get_temp_dir();
129 temp_dir_ = system_temp_dir_;
130 document_dir_ = get_document_dir(home_dir_);
132 FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
133 string const bdir = onlyPath(abs_binary.absFilename());
134 // We may be using libtools
135 if (suffixIs(bdir, ".libs/"))
136 binary_dir_ = FileName(addPath(bdir, "../"));
138 binary_dir_ = FileName(bdir);
140 // Is LyX being run in-place from the build tree?
141 boost::tie(build_support_dir_, system_support_dir_) =
142 get_build_dirs(abs_binary, top_build_dir_location);
144 if (build_support_dir_.empty())
145 system_support_dir_ =
146 get_system_support_dir(abs_binary,
147 command_line_system_support_dir);
149 locale_dir_ = get_locale_dir(system_support_dir_);
151 FileName const default_user_support_dir =
152 get_default_user_support_dir(home_dir_);
153 boost::tie(user_support_dir_, explicit_user_support_dir_) =
154 get_user_support_dir(default_user_support_dir,
155 command_line_user_support_dir);
157 FileName const configure_script(addName(system_support().absFilename(), "configure.py"));
158 configure_command_ = os::python() + ' ' +
159 quoteName(configure_script.toFilesystemEncoding()) +
160 with_version_suffix();
164 << "\tbinary_dir " << binary_dir().absFilename() << '\n'
165 << "\tsystem_support " << system_support().absFilename() << '\n'
166 << "\tbuild_support " << build_support().absFilename() << '\n'
167 << "\tuser_support " << user_support().absFilename() << '\n'
168 << "\tlocale_dir " << locale_dir().absFilename() << '\n'
169 << "\tdocument_dir " << document_dir().absFilename() << '\n'
170 << "\ttemp_dir " << temp_dir().absFilename() << '\n'
171 << "\thome_dir " << home_dir().absFilename() << '\n'
172 << "</package>\n" << std::endl;
178 // These next functions contain the stuff that is substituted at
179 // configuration-time.
180 FileName const hardcoded_localedir()
183 // The build system needs to make sure that this is in utf8 encoding.
184 return FileName("@LOCALEDIR@");
188 FileName const hardcoded_system_support_dir()
191 // The build system needs to make sure that this is in utf8 encoding.
192 return FileName("@LYX_DIR@");
196 string const & with_version_suffix()
198 static string const program_suffix("@PROGRAM_SUFFIX@");
200 with_version_suffix(" --with-version-suffix=@PROGRAM_SUFFIX@");
201 return program_suffix.empty() ? program_suffix : with_version_suffix;
207 FileName const & Package::top_srcdir()
210 // The build system needs to make sure that this is in utf8 encoding.
211 static FileName const dir("@TOP_SRCDIR@");
218 bool check_command_line_dir(string const & dir,
220 string const & command_line_switch);
222 FileName const extract_env_var_dir(string const & env_var);
224 bool check_env_var_dir(FileName const & dir,
225 string const & env_var);
227 bool check_env_var_dir(FileName const & dir,
229 string const & env_var);
231 string const relative_locale_dir();
233 string const relative_system_support_dir();
237 * Convert \p name to internal path and strip a trailing slash, since it
238 * comes from user input (commandline or environment).
239 * \p name is encoded in utf8.
241 string const fix_dir_name(string const & name)
243 return rtrim(os::internal_path(name), "/");
248 get_build_support_dir(string const & binary_dir,
249 exe_build_dir_to_top_build_dir top_build_dir_location)
252 switch (top_build_dir_location) {
253 case top_build_dir_is_one_level_up:
254 indirection = "../lib";
256 case top_build_dir_is_two_levels_up:
257 indirection = "../../lib";
260 return FileName(normalizePath(addPath(binary_dir, indirection)));
264 std::pair<FileName, FileName> const
265 get_build_dirs(FileName const & abs_binary,
266 exe_build_dir_to_top_build_dir top_build_dir_location)
268 string const check_text = "Checking whether LyX is run in place...";
270 // We're looking for "Makefile" in a directory
272 // We're also looking for "chkconfig.ltx" in a directory
274 // If both are found, then we're running LyX in-place.
276 // Note that the name of the lyx binary may be a symbolic link.
277 // If that is the case, then we follow the links too.
278 FileName binary = abs_binary;
280 // Try and find "lyxrc.defaults".
281 string binary_dir = onlyPath(binary.absFilename());
282 // We may be using libtools with static linking.
283 if (suffixIs(binary_dir, ".libs/"))
284 binary_dir = addPath(binary_dir, "../");
285 FileName const build_support_dir =
286 get_build_support_dir(binary_dir, top_build_dir_location);
287 if (!fileSearch(build_support_dir.absFilename(), "Makefile").empty()) {
288 // Try and find "chkconfig.ltx".
289 string const system_support_dir =
290 addPath(Package::top_srcdir().absFilename(), "lib");
292 if (!fileSearch(system_support_dir, "chkconfig.ltx").empty()) {
293 lyxerr[Debug::INIT] << check_text << " yes"
295 return std::make_pair(build_support_dir, system_support_dir);
299 // Check whether binary is a symbolic link.
300 // If so, resolve it and repeat the exercise.
301 if (!fs::symbolic_link_exists(binary.toFilesystemEncoding()))
305 if (readLink(binary, link)) {
308 // Unable to resolve the link.
313 lyxerr[Debug::INIT] << check_text << " no" << std::endl;
314 return std::make_pair(FileName(), 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(normalizePath(addPath(system_support_dir.absFilename(),
360 relative_locale_dir())));
362 if (fs::exists(path.toFilesystemEncoding()) &&
363 fs::is_directory(path.toFilesystemEncoding()))
366 // 3. Fall back to the hard-coded LOCALEDIR.
367 path = hardcoded_localedir();
368 if (fs::exists(path.toFilesystemEncoding()) &&
369 fs::is_directory(path.toFilesystemEncoding()))
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(os::internal_path(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 path = fix_dir_name(command_line);
398 return os::is_absolute_path(path) ? FileName(path) : makeAbsPath(path);
402 // Does the grunt work for abs_path_from_binary_name()
403 FileName const get_binary_path(string const & exe)
405 #if defined (USE_WINDOWS_PACKAGING)
406 // The executable may have been invoked either with or
407 // without the .exe extension.
408 // Ensure that it is present.
409 string const as_internal_path = os::internal_path(exe);
410 string const exe_path = suffixIs(as_internal_path, ".exe") ?
411 as_internal_path : as_internal_path + ".exe";
413 string const exe_path = os::internal_path(exe);
415 if (os::is_absolute_path(exe_path))
416 return FileName(exe_path);
418 // Two possibilities present themselves.
419 // 1. The binary is relative to the CWD.
420 FileName const abs_exe_path = makeAbsPath(exe_path);
421 if (fs::exists(abs_exe_path.toFilesystemEncoding()))
424 // 2. exe must be the name of the binary only and it
425 // can be found on the PATH.
426 string const exe_name = onlyFilename(exe_path);
427 if (exe_name != exe_path)
430 std::vector<string> const path = getEnvPath("PATH");
431 std::vector<string>::const_iterator it = path.begin();
432 std::vector<string>::const_iterator const end = path.end();
433 for (; it != end; ++it) {
434 // This will do nothing if *it is already absolute.
435 string const exe_dir = makeAbsPath(*it).absFilename();
437 FileName const exe_path(addName(exe_dir, exe_name));
438 if (fs::exists(exe_path.toFilesystemEncoding()))
442 // Didn't find anything.
447 // Extracts the absolute path to the binary name received as argv[0].
448 FileName const abs_path_from_binary_name(string const & exe)
450 FileName const abs_binary = get_binary_path(exe);
451 if (abs_binary.empty()) {
453 throw ExceptionMessage(ErrorException,
454 _("LyX binary not found"),
455 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
462 // A plethora of directories is searched to ascertain the system
463 // lyxdir which is defined as the first directory to contain
466 get_system_support_dir(FileName const & abs_binary,
467 string const & command_line_system_support_dir)
469 string const chkconfig_ltx = "chkconfig.ltx";
471 // searched_dirs is used for diagnostic purposes only in the case
472 // that "chkconfig.ltx" is not found.
473 std::list<FileName> searched_dirs;
475 // 1. Use the -sysdir command line parameter.
476 FileName path = abs_path_from_command_line(command_line_system_support_dir);
478 searched_dirs.push_back(path);
479 if (check_command_line_dir(path.absFilename(), chkconfig_ltx, "-sysdir"))
483 // 2. Use the "LYX_DIR_15x" environment variable.
484 path = extract_env_var_dir("LYX_DIR_15x");
486 searched_dirs.push_back(path);
487 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
491 // 3. Search relative to the lyx binary.
492 // We're looking for "chkconfig.ltx" in a directory
493 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
494 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
495 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
496 string const relative_lyxdir = relative_system_support_dir();
498 // One subtlety to be aware of. The name of the lyx binary may be
499 // a symbolic link. If that is the case, then we follow the links too.
500 FileName binary = abs_binary;
502 // Try and find "chkconfig.ltx".
503 string const binary_dir = onlyPath(binary.absFilename());
505 FileName const lyxdir(
506 normalizePath(addPath(binary_dir, relative_lyxdir)));
507 searched_dirs.push_back(lyxdir);
509 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
510 // Success! "chkconfig.ltx" has been found.
514 // Check whether binary is a symbolic link.
515 // If so, resolve it and repeat the exercise.
516 if (!fs::symbolic_link_exists(binary.toFilesystemEncoding()))
520 if (readLink(binary, link)) {
523 // Unable to resolve the link.
528 // 4. Repeat the exercise on the directory itself.
529 FileName binary_dir(onlyPath(abs_binary.absFilename()));
531 // This time test whether the directory is a symbolic link
532 // *before* looking for "chkconfig.ltx".
533 // (We've looked relative to the original already.)
534 if (!fs::symbolic_link_exists(binary.toFilesystemEncoding()))
538 if (readLink(binary_dir, link)) {
541 // Unable to resolve the link.
545 // Try and find "chkconfig.ltx".
546 FileName const lyxdir(
547 normalizePath(addPath(binary_dir.absFilename(), relative_lyxdir)));
548 searched_dirs.push_back(lyxdir);
550 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
551 // Success! "chkconfig.ltx" has been found.
556 // 5. In desparation, try the hard-coded system support dir.
557 path = hardcoded_system_support_dir();
558 if (!fileSearch(path.absFilename(), chkconfig_ltx).empty())
561 // Everything has failed :-(
562 // So inform the user and exit.
563 string searched_dirs_str;
564 typedef std::list<FileName>::const_iterator iterator;
565 iterator const begin = searched_dirs.begin();
566 iterator const end = searched_dirs.end();
567 for (iterator it = begin; it != end; ++it) {
569 searched_dirs_str += "\n\t";
570 searched_dirs_str += it->absFilename();
574 throw ExceptionMessage(ErrorException, _("No system directory"),
575 bformat(_("Unable to determine the system directory "
578 "Use the '-sysdir' command line parameter or "
579 "set the environment variable LYX_DIR_15x to "
580 "the LyX system directory containing the file "
582 from_utf8(searched_dirs_str)));
584 // Keep the compiler happy.
589 // Returns the absolute path to the user lyxdir, together with a flag
590 // indicating whether this directory was specified explicitly (as -userdir
591 // or through an environment variable) or whether it was deduced.
592 std::pair<FileName, bool> const
593 get_user_support_dir(FileName const & default_user_support_dir,
594 string const & command_line_user_support_dir)
596 bool explicit_userdir = true;
598 // 1. Use the -userdir command line parameter.
600 abs_path_from_command_line(command_line_user_support_dir);
602 return std::make_pair(path, explicit_userdir);
604 // 2. Use the LYX_USERDIR_15x environment variable.
605 path = extract_env_var_dir("LYX_USERDIR_15x");
607 return std::make_pair(path, explicit_userdir);
609 // 3. Use the OS-dependent default_user_support_dir
610 explicit_userdir = false;
611 return std::make_pair(default_user_support_dir, explicit_userdir);
615 // $HOME/.lyx on POSIX but on Win32 it will be something like
616 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
617 FileName const get_default_user_support_dir(FileName const & home_dir)
619 #if defined (USE_WINDOWS_PACKAGING)
620 (void)home_dir; // Silence warning about unused variable.
622 os::GetFolderPath win32_folder_path;
623 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
625 #elif defined (USE_MACOSX_PACKAGING)
626 (void)home_dir; // Silence warning about unused variable.
629 OSErr const error_code =
630 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
631 kDontCreateFolder, &fsref);
635 // FSRefMakePath returns the result in utf8
636 char store[PATH_MAX + 1];
637 OSStatus const status_code =
638 FSRefMakePath(&fsref,
639 reinterpret_cast<UInt8*>(store), PATH_MAX);
640 if (status_code != 0)
643 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
645 #else // USE_POSIX_PACKAGING
646 return FileName(addPath(home_dir.absFilename(), string(".") + PACKAGE));
651 // Check that directory @c dir contains @c file.
652 // Else emit a warning about an invalid @c command_line_switch.
653 bool check_command_line_dir(string const & dir,
655 string const & command_line_switch)
657 FileName const abs_path = fileSearch(dir, file);
658 if (abs_path.empty()) {
660 throw ExceptionMessage(WarningException, _("File not found"), bformat(
661 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
662 from_utf8(command_line_switch), from_utf8(dir),
666 return !abs_path.empty();
670 // The environment variable @c env_var expands to a (single) file path.
671 FileName const extract_env_var_dir(string const & env_var)
673 string const dir = fix_dir_name(getEnv(env_var));
674 return dir.empty() ? FileName() : makeAbsPath(dir);
678 // Check that directory @c dir contains @c file.
679 // Else emit a warning about an invalid @c env_var.
680 bool check_env_var_dir(FileName const & dir,
682 string const & env_var)
684 FileName const abs_path = fileSearch(dir.absFilename(), file);
685 if (abs_path.empty()) {
687 throw ExceptionMessage(WarningException, _("File not found"), bformat(
688 _("Invalid %1$s environment variable.\n"
689 "Directory %2$s does not contain %3$s."),
690 from_utf8(env_var), from_utf8(dir.absFilename()),
694 return !abs_path.empty();
698 // Check that directory @c dir is indeed a directory.
699 // Else emit a warning about an invalid @c env_var.
700 bool check_env_var_dir(FileName const & dir,
701 string const & env_var)
703 string const encoded(dir.toFilesystemEncoding());
704 bool const success = (fs::exists(encoded) && fs::is_directory(encoded));
707 // Put this string on a single line so that the gettext
708 // search mechanism in po/Makefile.in.in will register
709 // Package.cpp.in as a file containing strings that need
712 docstring const fmt =
713 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
715 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
716 fmt, from_utf8(env_var), from_utf8(dir.absFilename())));
723 // The locale directory relative to the LyX system directory.
724 string const relative_locale_dir()
726 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
734 // The system lyxdir is relative to the directory containing the LyX binary.
735 string const relative_system_support_dir()
739 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
740 result = "../Resources/";
742 result = addPath("../share/", PACKAGE);
750 } // namespace support