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"
19 #include "support/environment.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/ExceptionMessage.h"
23 #include "support/os.h"
25 #if defined (USE_WINDOWS_PACKAGING)
26 # include "support/os_win32.h"
29 #include <boost/tuple/tuple.hpp>
34 #if !defined (USE_WINDOWS_PACKAGING) && \
35 !defined (USE_MACOSX_PACKAGING) && \
36 !defined (USE_POSIX_PACKAGING)
37 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
40 #if defined (USE_MACOSX_PACKAGING)
41 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
52 bool initialised_ = false;
57 void init_package(string const & command_line_arg0,
58 string const & command_line_system_support_dir,
59 string const & command_line_user_support_dir,
60 exe_build_dir_to_top_build_dir top_build_dir_location)
62 // Can do so only once.
66 package_ = Package(command_line_arg0,
67 command_line_system_support_dir,
68 command_line_user_support_dir,
69 top_build_dir_location);
74 Package const & package()
76 // Commented out because package().locale_dir() can be called
77 // from the message translation code in Messages.cpp before
78 // init_package() is called. Lars is on the case...
79 // BOOST_ASSERT(initialised_);
86 FileName const abs_path_from_binary_name(string const & exe);
88 std::pair<FileName, FileName> const
89 get_build_dirs(FileName const & abs_binary,
90 exe_build_dir_to_top_build_dir top_build_dir_location);
92 FileName const get_document_dir(FileName const & home_dir);
94 FileName const get_home_dir();
96 FileName const get_locale_dir(FileName const & system_support_dir);
98 FileName const get_system_support_dir(FileName const & abs_binary,
99 string const & command_line_system_support_dir);
101 FileName const get_temp_dir();
103 FileName const get_default_user_support_dir(FileName const & home_dir);
105 std::pair<FileName, bool> const
106 get_user_support_dir(FileName const & default_user_support_dir,
107 string const & command_line_user_support_dir);
110 string const & with_version_suffix();
115 Package::Package(string const & command_line_arg0,
116 string const & command_line_system_support_dir,
117 string const & command_line_user_support_dir,
118 exe_build_dir_to_top_build_dir top_build_dir_location)
119 : explicit_user_support_dir_(false)
121 home_dir_ = get_home_dir();
122 system_temp_dir_ = get_temp_dir();
123 temp_dir_ = system_temp_dir_;
124 document_dir_ = get_document_dir(home_dir_);
126 FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
127 string const bdir = onlyPath(abs_binary.absFilename());
128 // We may be using libtools
129 if (suffixIs(bdir, ".libs/"))
130 binary_dir_ = FileName(addPath(bdir, "../"));
132 binary_dir_ = FileName(bdir);
134 // Is LyX being run in-place from the build tree?
135 boost::tie(build_support_dir_, system_support_dir_) =
136 get_build_dirs(abs_binary, top_build_dir_location);
138 if (build_support_dir_.empty())
139 system_support_dir_ =
140 get_system_support_dir(abs_binary,
141 command_line_system_support_dir);
143 locale_dir_ = get_locale_dir(system_support_dir_);
145 FileName const default_user_support_dir =
146 get_default_user_support_dir(home_dir_);
147 boost::tie(user_support_dir_, explicit_user_support_dir_) =
148 get_user_support_dir(default_user_support_dir,
149 command_line_user_support_dir);
151 FileName const configure_script(addName(system_support().absFilename(), "configure.py"));
152 configure_command_ = os::python() + ' ' +
153 quoteName(configure_script.toFilesystemEncoding()) +
154 with_version_suffix();
158 << "\tbinary_dir " << binary_dir().absFilename() << '\n'
159 << "\tsystem_support " << system_support().absFilename() << '\n'
160 << "\tbuild_support " << build_support().absFilename() << '\n'
161 << "\tuser_support " << user_support().absFilename() << '\n'
162 << "\tlocale_dir " << locale_dir().absFilename() << '\n'
163 << "\tdocument_dir " << document_dir().absFilename() << '\n'
164 << "\ttemp_dir " << temp_dir().absFilename() << '\n'
165 << "\thome_dir " << home_dir().absFilename() << '\n'
166 << "</package>\n" << std::endl;
172 // These next functions contain the stuff that is substituted at
173 // configuration-time.
174 FileName const hardcoded_localedir()
177 // The build system needs to make sure that this is in utf8 encoding.
178 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
182 FileName const hardcoded_system_support_dir()
185 // The build system needs to make sure that this is in utf8 encoding.
186 return FileName(LYX_ABS_INSTALLED_DATADIR);
190 string const & with_version_suffix()
192 static string const program_suffix = PROGRAM_SUFFIX;
193 static string const with_version_suffix =
194 " --with-version-suffix=" PROGRAM_SUFFIX;
195 return program_suffix.empty() ? program_suffix : with_version_suffix;
201 FileName const & Package::top_srcdir()
204 // The build system needs to make sure that this is in utf8 encoding.
205 static FileName const dir(LYX_ABS_TOP_SRCDIR);
212 bool check_command_line_dir(string const & dir,
214 string const & command_line_switch);
216 FileName const extract_env_var_dir(string const & env_var);
218 bool check_env_var_dir(FileName const & dir,
219 string const & env_var);
221 bool check_env_var_dir(FileName const & dir,
223 string const & env_var);
225 string const relative_locale_dir();
227 string const relative_system_support_dir();
231 * Convert \p name to internal path and strip a trailing slash, since it
232 * comes from user input (commandline or environment).
233 * \p name is encoded in utf8.
235 string const fix_dir_name(string const & name)
237 return rtrim(os::internal_path(name), "/");
242 get_build_support_dir(string const & binary_dir,
243 exe_build_dir_to_top_build_dir top_build_dir_location)
246 switch (top_build_dir_location) {
247 case top_build_dir_is_one_level_up:
248 indirection = "../lib";
250 case top_build_dir_is_two_levels_up:
251 indirection = "../../lib";
254 return FileName(normalizePath(addPath(binary_dir, indirection)));
258 std::pair<FileName, FileName> const
259 get_build_dirs(FileName const & abs_binary,
260 exe_build_dir_to_top_build_dir top_build_dir_location)
262 string const check_text = "Checking whether LyX is run in place...";
264 // We're looking for "Makefile" in a directory
266 // We're also looking for "chkconfig.ltx" in a directory
268 // If both are found, then we're running LyX in-place.
270 // Note that the name of the lyx binary may be a symbolic link.
271 // If that is the case, then we follow the links too.
272 FileName binary = abs_binary;
274 // Try and find "lyxrc.defaults".
275 string binary_dir = onlyPath(binary.absFilename());
276 // We may be using libtools with static linking.
277 if (suffixIs(binary_dir, ".libs/"))
278 binary_dir = addPath(binary_dir, "../");
279 FileName const build_support_dir =
280 get_build_support_dir(binary_dir, top_build_dir_location);
281 if (!fileSearch(build_support_dir.absFilename(), "Makefile").empty()) {
282 // Try and find "chkconfig.ltx".
283 string const system_support_dir =
284 addPath(Package::top_srcdir().absFilename(), "lib");
286 if (!fileSearch(system_support_dir, "chkconfig.ltx").empty()) {
287 lyxerr[Debug::INIT] << check_text << " yes"
289 return std::make_pair(build_support_dir, system_support_dir);
293 // Check whether binary is a symbolic link.
294 // If so, resolve it and repeat the exercise.
295 if (!binary.isSymLink())
299 if (readLink(binary, link)) {
302 // Unable to resolve the link.
307 lyxerr[Debug::INIT] << check_text << " no" << std::endl;
308 return std::make_pair(FileName(), FileName());
312 // Specification of document_dir_ may be reset by LyXRC,
313 // but the default is fixed for a given OS.
314 FileName const get_document_dir(FileName const & home_dir)
316 #if defined (USE_WINDOWS_PACKAGING)
317 (void)home_dir; // Silence warning about unused variable.
318 os::GetFolderPath win32_folder_path;
319 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
326 // The specification of home_dir_ is fixed for a given OS.
327 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
328 // and on a Posix-like machine: "/home/USERNAME".
329 FileName const get_home_dir()
331 #if defined (USE_WINDOWS_PACKAGING)
332 string const home_dir = getEnv("USERPROFILE");
334 string const home_dir = getEnv("HOME");
337 return FileName(fix_dir_name(home_dir));
341 // Several sources are probed to ascertain the locale directory.
342 // The only requirement is that the result is indeed a directory.
343 FileName const get_locale_dir(FileName const & system_support_dir)
345 // 1. Use the "LYX_LOCALEDIR" environment variable.
346 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
347 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
350 // 2. Search for system_support_dir / <relative locale dir>
351 // The <relative locale dir> is OS-dependent. (On Unix, it will
353 FileName path(normalizePath(addPath(system_support_dir.absFilename(),
354 relative_locale_dir())));
356 if (path.exists() && path.isDirectory())
359 // 3. Fall back to the hard-coded LOCALEDIR.
360 path = hardcoded_localedir();
361 if (path.exists() && path.isDirectory())
368 // Specification of temp_dir_ may be reset by LyXRC,
369 // but the default is fixed for a given OS.
370 FileName const get_temp_dir()
372 #if defined (USE_WINDOWS_PACKAGING)
373 // Typical example: C:/TEMP/.
375 GetTempPath(MAX_PATH, path);
376 return FileName(os::internal_path(to_utf8(from_local8bit(path))));
378 return FileName("/tmp");
383 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
384 FileName const abs_path_from_command_line(string const & command_line)
386 if (command_line.empty())
389 string const path = fix_dir_name(command_line);
390 return os::is_absolute_path(path) ? FileName(path) : makeAbsPath(path);
394 // Does the grunt work for abs_path_from_binary_name()
395 FileName const get_binary_path(string const & exe)
397 #if defined (USE_WINDOWS_PACKAGING)
398 // The executable may have been invoked either with or
399 // without the .exe extension.
400 // Ensure that it is present.
401 string const as_internal_path = os::internal_path(exe);
402 string const exe_path = suffixIs(as_internal_path, ".exe") ?
403 as_internal_path : as_internal_path + ".exe";
405 string const exe_path = os::internal_path(exe);
407 if (os::is_absolute_path(exe_path))
408 return FileName(exe_path);
410 // Two possibilities present themselves.
411 // 1. The binary is relative to the CWD.
412 FileName const abs_exe_path = makeAbsPath(exe_path);
413 if (abs_exe_path.exists())
416 // 2. exe must be the name of the binary only and it
417 // can be found on the PATH.
418 string const exe_name = onlyFilename(exe_path);
419 if (exe_name != exe_path)
422 std::vector<string> const path = getEnvPath("PATH");
423 std::vector<string>::const_iterator it = path.begin();
424 std::vector<string>::const_iterator const end = path.end();
425 for (; it != end; ++it) {
426 // This will do nothing if *it is already absolute.
427 string const exe_dir = makeAbsPath(*it).absFilename();
429 FileName const exe_path(addName(exe_dir, exe_name));
430 if (exe_path.exists())
434 // Didn't find anything.
439 // Extracts the absolute path to the binary name received as argv[0].
440 FileName const abs_path_from_binary_name(string const & exe)
442 FileName const abs_binary = get_binary_path(exe);
443 if (abs_binary.empty()) {
445 throw ExceptionMessage(ErrorException,
446 _("LyX binary not found"),
447 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
454 // A plethora of directories is searched to ascertain the system
455 // lyxdir which is defined as the first directory to contain
458 get_system_support_dir(FileName const & abs_binary,
459 string const & command_line_system_support_dir)
461 string const chkconfig_ltx = "chkconfig.ltx";
463 // searched_dirs is used for diagnostic purposes only in the case
464 // that "chkconfig.ltx" is not found.
465 std::list<FileName> searched_dirs;
467 // 1. Use the -sysdir command line parameter.
468 FileName path = abs_path_from_command_line(command_line_system_support_dir);
470 searched_dirs.push_back(path);
471 if (check_command_line_dir(path.absFilename(), chkconfig_ltx, "-sysdir"))
475 // 2. Use the "LYX_DIR_15x" environment variable.
476 path = extract_env_var_dir("LYX_DIR_15x");
478 searched_dirs.push_back(path);
479 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
483 // 3. Search relative to the lyx binary.
484 // We're looking for "chkconfig.ltx" in a directory
485 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
486 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
487 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
488 string const relative_lyxdir = relative_system_support_dir();
490 // One subtlety to be aware of. The name of the lyx binary may be
491 // a symbolic link. If that is the case, then we follow the links too.
492 FileName binary = abs_binary;
494 // Try and find "chkconfig.ltx".
495 string const binary_dir = onlyPath(binary.absFilename());
497 FileName const lyxdir(
498 normalizePath(addPath(binary_dir, relative_lyxdir)));
499 searched_dirs.push_back(lyxdir);
501 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
502 // Success! "chkconfig.ltx" has been found.
506 // Check whether binary is a symbolic link.
507 // If so, resolve it and repeat the exercise.
508 if (!binary.isSymLink())
512 if (readLink(binary, link)) {
515 // Unable to resolve the link.
520 // 4. Repeat the exercise on the directory itself.
521 FileName binary_dir(onlyPath(abs_binary.absFilename()));
523 // This time test whether the directory is a symbolic link
524 // *before* looking for "chkconfig.ltx".
525 // (We've looked relative to the original already.)
526 if (!binary.isSymLink())
530 if (readLink(binary_dir, link)) {
533 // Unable to resolve the link.
537 // Try and find "chkconfig.ltx".
538 FileName const lyxdir(
539 normalizePath(addPath(binary_dir.absFilename(), relative_lyxdir)));
540 searched_dirs.push_back(lyxdir);
542 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
543 // Success! "chkconfig.ltx" has been found.
548 // 5. In desparation, try the hard-coded system support dir.
549 path = hardcoded_system_support_dir();
550 if (!fileSearch(path.absFilename(), chkconfig_ltx).empty())
553 // Everything has failed :-(
554 // So inform the user and exit.
555 string searched_dirs_str;
556 typedef std::list<FileName>::const_iterator iterator;
557 iterator const begin = searched_dirs.begin();
558 iterator const end = searched_dirs.end();
559 for (iterator it = begin; it != end; ++it) {
561 searched_dirs_str += "\n\t";
562 searched_dirs_str += it->absFilename();
566 throw ExceptionMessage(ErrorException, _("No system directory"),
567 bformat(_("Unable to determine the system directory "
570 "Use the '-sysdir' command line parameter or "
571 "set the environment variable LYX_DIR_15x to "
572 "the LyX system directory containing the file "
574 from_utf8(searched_dirs_str)));
576 // Keep the compiler happy.
581 // Returns the absolute path to the user lyxdir, together with a flag
582 // indicating whether this directory was specified explicitly (as -userdir
583 // or through an environment variable) or whether it was deduced.
584 std::pair<FileName, bool> const
585 get_user_support_dir(FileName const & default_user_support_dir,
586 string const & command_line_user_support_dir)
588 bool explicit_userdir = true;
590 // 1. Use the -userdir command line parameter.
592 abs_path_from_command_line(command_line_user_support_dir);
594 return std::make_pair(path, explicit_userdir);
596 // 2. Use the LYX_USERDIR_15x environment variable.
597 path = extract_env_var_dir("LYX_USERDIR_15x");
599 return std::make_pair(path, explicit_userdir);
601 // 3. Use the OS-dependent default_user_support_dir
602 explicit_userdir = false;
603 return std::make_pair(default_user_support_dir, explicit_userdir);
607 // $HOME/.lyx on POSIX but on Win32 it will be something like
608 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
609 FileName const get_default_user_support_dir(FileName const & home_dir)
611 #if defined (USE_WINDOWS_PACKAGING)
612 (void)home_dir; // Silence warning about unused variable.
614 os::GetFolderPath win32_folder_path;
615 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
617 #elif defined (USE_MACOSX_PACKAGING)
618 (void)home_dir; // Silence warning about unused variable.
621 OSErr const error_code =
622 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
623 kDontCreateFolder, &fsref);
627 // FSRefMakePath returns the result in utf8
628 char store[PATH_MAX + 1];
629 OSStatus const status_code =
630 FSRefMakePath(&fsref,
631 reinterpret_cast<UInt8*>(store), PATH_MAX);
632 if (status_code != 0)
635 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
637 #else // USE_POSIX_PACKAGING
638 return FileName(addPath(home_dir.absFilename(), string(".") + PACKAGE));
643 // Check that directory @c dir contains @c file.
644 // Else emit a warning about an invalid @c command_line_switch.
645 bool check_command_line_dir(string const & dir,
647 string const & command_line_switch)
649 FileName const abs_path = fileSearch(dir, file);
650 if (abs_path.empty()) {
652 throw ExceptionMessage(WarningException, _("File not found"), bformat(
653 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
654 from_utf8(command_line_switch), from_utf8(dir),
658 return !abs_path.empty();
662 // The environment variable @c env_var expands to a (single) file path.
663 FileName const extract_env_var_dir(string const & env_var)
665 string const dir = fix_dir_name(getEnv(env_var));
666 return dir.empty() ? FileName() : makeAbsPath(dir);
670 // Check that directory @c dir contains @c file.
671 // Else emit a warning about an invalid @c env_var.
672 bool check_env_var_dir(FileName const & dir,
674 string const & env_var)
676 FileName const abs_path = fileSearch(dir.absFilename(), file);
677 if (abs_path.empty()) {
679 throw ExceptionMessage(WarningException, _("File not found"), bformat(
680 _("Invalid %1$s environment variable.\n"
681 "Directory %2$s does not contain %3$s."),
682 from_utf8(env_var), from_utf8(dir.absFilename()),
686 return !abs_path.empty();
690 // Check that directory @c dir is indeed a directory.
691 // Else emit a warning about an invalid @c env_var.
692 bool check_env_var_dir(FileName const & dir,
693 string const & env_var)
695 bool const success = dir.exists() && dir.isDirectory();
698 // Put this string on a single line so that the gettext
699 // search mechanism in po/Makefile.in.in will register
700 // Package.cpp.in as a file containing strings that need
703 docstring const fmt =
704 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
706 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
707 fmt, from_utf8(env_var), from_utf8(dir.absFilename())));
714 // The locale directory relative to the LyX system directory.
715 string const relative_locale_dir()
717 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
725 // The system lyxdir is relative to the directory containing the LyX binary.
726 string const relative_system_support_dir()
730 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
731 result = "../Resources/";
733 result = addPath("../share/", PACKAGE);
741 } // namespace support