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 binary_dir_ = FileName(onlyPath(abs_binary.absFilename()));
135 // Is LyX being run in-place from the build tree?
136 boost::tie(build_support_dir_, system_support_dir_) =
137 get_build_dirs(abs_binary, top_build_dir_location);
139 if (build_support_dir_.empty())
140 system_support_dir_ =
141 get_system_support_dir(abs_binary,
142 command_line_system_support_dir);
144 locale_dir_ = get_locale_dir(system_support_dir_);
146 FileName const default_user_support_dir =
147 get_default_user_support_dir(home_dir_);
148 boost::tie(user_support_dir_, explicit_user_support_dir_) =
149 get_user_support_dir(default_user_support_dir,
150 command_line_user_support_dir);
152 FileName const configure_script(addName(system_support().absFilename(), "configure.py"));
153 configure_command_ = os::python() + ' ' +
154 quoteName(configure_script.toFilesystemEncoding()) +
155 with_version_suffix();
159 << "\tbinary_dir " << binary_dir().absFilename() << '\n'
160 << "\tsystem_support " << system_support().absFilename() << '\n'
161 << "\tbuild_support " << build_support().absFilename() << '\n'
162 << "\tuser_support " << user_support().absFilename() << '\n'
163 << "\tlocale_dir " << locale_dir().absFilename() << '\n'
164 << "\tdocument_dir " << document_dir().absFilename() << '\n'
165 << "\ttemp_dir " << temp_dir().absFilename() << '\n'
166 << "\thome_dir " << home_dir().absFilename() << '\n'
167 << "</package>\n" << std::endl;
173 // These next functions contain the stuff that is substituted at
174 // configuration-time.
175 FileName const hardcoded_localedir()
178 // The build system needs to make sure that this is in utf8 encoding.
179 return FileName("@LOCALEDIR@");
183 FileName const hardcoded_system_support_dir()
186 // The build system needs to make sure that this is in utf8 encoding.
187 return FileName("@LYX_DIR@");
191 string const & with_version_suffix()
193 static string const program_suffix("@PROGRAM_SUFFIX@");
195 with_version_suffix(" --with-version-suffix=@PROGRAM_SUFFIX@");
196 return program_suffix.empty() ? program_suffix : with_version_suffix;
202 FileName const & Package::top_srcdir()
205 // The build system needs to make sure that this is in utf8 encoding.
206 static FileName const dir("@TOP_SRCDIR@");
213 bool check_command_line_dir(string const & dir,
215 string const & command_line_switch);
217 FileName const extract_env_var_dir(string const & env_var);
219 bool check_env_var_dir(FileName const & dir,
220 string const & env_var);
222 bool check_env_var_dir(FileName const & dir,
224 string const & env_var);
226 string const relative_locale_dir();
228 string const relative_system_support_dir();
232 * Convert \p name to internal path and strip a trailing slash, since it
233 * comes from user input (commandline or environment).
234 * \p name is encoded in utf8.
236 string const fix_dir_name(string const & name)
238 return rtrim(os::internal_path(name), "/");
243 get_build_support_dir(string const & binary_dir,
244 exe_build_dir_to_top_build_dir top_build_dir_location)
247 switch (top_build_dir_location) {
248 case top_build_dir_is_one_level_up:
249 indirection = "../lib";
251 case top_build_dir_is_two_levels_up:
252 indirection = "../../lib";
256 return FileName(normalizePath(addPath(binary_dir, indirection)));
260 std::pair<FileName, FileName> const
261 get_build_dirs(FileName const & abs_binary,
262 exe_build_dir_to_top_build_dir top_build_dir_location)
264 string const check_text = "Checking whether LyX is run in place...";
266 // We're looking for "Makefile" in a directory
268 // We're also looking for "chkconfig.ltx" in a directory
270 // If both are found, then we're running LyX in-place.
272 // Note that the name of the lyx binary may be a symbolic link.
273 // If that is the case, then we follow the links too.
274 FileName binary = abs_binary;
276 // Try and find "lyxrc.defaults".
277 string const binary_dir = onlyPath(binary.absFilename());
278 FileName const build_support_dir =
279 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 (!fs::symbolic_link_exists(binary.toFilesystemEncoding()))
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 (fs::exists(path.toFilesystemEncoding()) &&
357 fs::is_directory(path.toFilesystemEncoding()))
360 // 3. Fall back to the hard-coded LOCALEDIR.
361 path = hardcoded_localedir();
362 if (fs::exists(path.toFilesystemEncoding()) &&
363 fs::is_directory(path.toFilesystemEncoding()))
370 // Specification of temp_dir_ may be reset by LyXRC,
371 // but the default is fixed for a given OS.
372 FileName const get_temp_dir()
374 #if defined (USE_WINDOWS_PACKAGING)
375 // Typical example: C:/TEMP/.
377 GetTempPath(MAX_PATH, path);
378 return FileName(os::internal_path(to_utf8(from_local8bit(path))));
380 return FileName("/tmp");
385 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
386 FileName const abs_path_from_command_line(string const & command_line)
388 if (command_line.empty())
391 string const path = fix_dir_name(command_line);
392 return os::is_absolute_path(path) ? FileName(path) : makeAbsPath(path);
396 // Does the grunt work for abs_path_from_binary_name()
397 FileName const get_binary_path(string const & exe)
399 #if defined (USE_WINDOWS_PACKAGING)
400 // The executable may have been invoked either with or
401 // without the .exe extension.
402 // Ensure that it is present.
403 string const as_internal_path = os::internal_path(exe);
404 string const exe_path = suffixIs(as_internal_path, ".exe") ?
405 as_internal_path : as_internal_path + ".exe";
407 string const exe_path = os::internal_path(exe);
409 if (os::is_absolute_path(exe_path))
410 return FileName(exe_path);
412 // Two possibilities present themselves.
413 // 1. The binary is relative to the CWD.
414 FileName const abs_exe_path = makeAbsPath(exe_path);
415 if (fs::exists(abs_exe_path.toFilesystemEncoding()))
418 // 2. exe must be the name of the binary only and it
419 // can be found on the PATH.
420 string const exe_name = onlyFilename(exe_path);
421 if (exe_name != exe_path)
424 std::vector<string> const path = getEnvPath("PATH");
425 std::vector<string>::const_iterator it = path.begin();
426 std::vector<string>::const_iterator const end = path.end();
427 for (; it != end; ++it) {
428 // This will do nothing if *it is already absolute.
429 string const exe_dir = makeAbsPath(*it).absFilename();
431 FileName const exe_path(addName(exe_dir, exe_name));
432 if (fs::exists(exe_path.toFilesystemEncoding()))
436 // Didn't find anything.
441 // Extracts the absolute path to the binary name received as argv[0].
442 FileName const abs_path_from_binary_name(string const & exe)
444 FileName const abs_binary = get_binary_path(exe);
445 if (abs_binary.empty()) {
447 throw ExceptionMessage(ErrorException,
448 _("LyX binary not found"),
449 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
456 // A plethora of directories is searched to ascertain the system
457 // lyxdir which is defined as the first directory to contain
460 get_system_support_dir(FileName const & abs_binary,
461 string const & command_line_system_support_dir)
463 string const chkconfig_ltx = "chkconfig.ltx";
465 // searched_dirs is used for diagnostic purposes only in the case
466 // that "chkconfig.ltx" is not found.
467 std::list<FileName> searched_dirs;
469 // 1. Use the -sysdir command line parameter.
470 FileName path = abs_path_from_command_line(command_line_system_support_dir);
472 searched_dirs.push_back(path);
473 if (check_command_line_dir(path.absFilename(), chkconfig_ltx, "-sysdir"))
477 // 2. Use the "LYX_DIR_15x" environment variable.
478 path = extract_env_var_dir("LYX_DIR_15x");
480 searched_dirs.push_back(path);
481 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
485 // 3. Search relative to the lyx binary.
486 // We're looking for "chkconfig.ltx" in a directory
487 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
488 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
489 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
490 string const relative_lyxdir = relative_system_support_dir();
492 // One subtlety to be aware of. The name of the lyx binary may be
493 // a symbolic link. If that is the case, then we follow the links too.
494 FileName binary = abs_binary;
496 // Try and find "chkconfig.ltx".
497 string const binary_dir = onlyPath(binary.absFilename());
499 FileName const lyxdir(
500 normalizePath(addPath(binary_dir, relative_lyxdir)));
501 searched_dirs.push_back(lyxdir);
503 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
504 // Success! "chkconfig.ltx" has been found.
508 // Check whether binary is a symbolic link.
509 // If so, resolve it and repeat the exercise.
510 if (!fs::symbolic_link_exists(binary.toFilesystemEncoding()))
514 if (readLink(binary, link)) {
517 // Unable to resolve the link.
522 // 4. Repeat the exercise on the directory itself.
523 FileName binary_dir(onlyPath(abs_binary.absFilename()));
525 // This time test whether the directory is a symbolic link
526 // *before* looking for "chkconfig.ltx".
527 // (We've looked relative to the original already.)
528 if (!fs::symbolic_link_exists(binary.toFilesystemEncoding()))
532 if (readLink(binary_dir, link)) {
535 // Unable to resolve the link.
539 // Try and find "chkconfig.ltx".
540 FileName const lyxdir(
541 normalizePath(addPath(binary_dir.absFilename(), relative_lyxdir)));
542 searched_dirs.push_back(lyxdir);
544 if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
545 // Success! "chkconfig.ltx" has been found.
550 // 5. In desparation, try the hard-coded system support dir.
551 path = hardcoded_system_support_dir();
552 if (!fileSearch(path.absFilename(), chkconfig_ltx).empty())
555 // Everything has failed :-(
556 // So inform the user and exit.
557 string searched_dirs_str;
558 typedef std::list<FileName>::const_iterator iterator;
559 iterator const begin = searched_dirs.begin();
560 iterator const end = searched_dirs.end();
561 for (iterator it = begin; it != end; ++it) {
563 searched_dirs_str += "\n\t";
564 searched_dirs_str += it->absFilename();
568 throw ExceptionMessage(ErrorException, _("No system directory"),
569 bformat(_("Unable to determine the system directory "
572 "Use the '-sysdir' command line parameter or "
573 "set the environment variable LYX_DIR_15x to "
574 "the LyX system directory containing the file "
576 from_utf8(searched_dirs_str)));
578 // Keep the compiler happy.
583 // Returns the absolute path to the user lyxdir, together with a flag
584 // indicating whether this directory was specified explicitly (as -userdir
585 // or through an environment variable) or whether it was deduced.
586 std::pair<FileName, bool> const
587 get_user_support_dir(FileName const & default_user_support_dir,
588 string const & command_line_user_support_dir)
590 bool explicit_userdir = true;
592 // 1. Use the -userdir command line parameter.
594 abs_path_from_command_line(command_line_user_support_dir);
596 return std::make_pair(path, explicit_userdir);
598 // 2. Use the LYX_USERDIR_15x environment variable.
599 path = extract_env_var_dir("LYX_USERDIR_15x");
601 return std::make_pair(path, explicit_userdir);
603 // 3. Use the OS-dependent default_user_support_dir
604 explicit_userdir = false;
605 return std::make_pair(default_user_support_dir, explicit_userdir);
609 // $HOME/.lyx on POSIX but on Win32 it will be something like
610 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
611 FileName const get_default_user_support_dir(FileName const & home_dir)
613 #if defined (USE_WINDOWS_PACKAGING)
614 (void)home_dir; // Silence warning about unused variable.
616 os::GetFolderPath win32_folder_path;
617 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
619 #elif defined (USE_MACOSX_PACKAGING)
620 (void)home_dir; // Silence warning about unused variable.
623 OSErr const error_code =
624 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
625 kDontCreateFolder, &fsref);
629 // FSRefMakePath returns the result in utf8
630 char store[PATH_MAX + 1];
631 OSStatus const status_code =
632 FSRefMakePath(&fsref,
633 reinterpret_cast<UInt8*>(store), PATH_MAX);
634 if (status_code != 0)
637 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
639 #else // USE_POSIX_PACKAGING
640 return FileName(addPath(home_dir.absFilename(), string(".") + PACKAGE));
645 // Check that directory @c dir contains @c file.
646 // Else emit a warning about an invalid @c command_line_switch.
647 bool check_command_line_dir(string const & dir,
649 string const & command_line_switch)
651 FileName const abs_path = fileSearch(dir, file);
652 if (abs_path.empty()) {
654 throw ExceptionMessage(WarningException, _("File not found"), bformat(
655 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
656 from_utf8(command_line_switch), from_utf8(dir),
660 return !abs_path.empty();
664 // The environment variable @c env_var expands to a (single) file path.
665 FileName const extract_env_var_dir(string const & env_var)
667 string const dir = fix_dir_name(getEnv(env_var));
668 return dir.empty() ? FileName() : makeAbsPath(dir);
672 // Check that directory @c dir contains @c file.
673 // Else emit a warning about an invalid @c env_var.
674 bool check_env_var_dir(FileName const & dir,
676 string const & env_var)
678 FileName const abs_path = fileSearch(dir.absFilename(), file);
679 if (abs_path.empty()) {
681 throw ExceptionMessage(WarningException, _("File not found"), bformat(
682 _("Invalid %1$s environment variable.\n"
683 "Directory %2$s does not contain %3$s."),
684 from_utf8(env_var), from_utf8(dir.absFilename()),
688 return !abs_path.empty();
692 // Check that directory @c dir is indeed a directory.
693 // Else emit a warning about an invalid @c env_var.
694 bool check_env_var_dir(FileName const & dir,
695 string const & env_var)
697 string const encoded(dir.toFilesystemEncoding());
698 bool const success = (fs::exists(encoded) && fs::is_directory(encoded));
701 // Put this string on a single line so that the gettext
702 // search mechanism in po/Makefile.in.in will register
703 // Package.cpp.in as a file containing strings that need
706 docstring const fmt =
707 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
709 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
710 fmt, from_utf8(env_var), from_utf8(dir.absFilename())));
717 // The locale directory relative to the LyX system directory.
718 string const relative_locale_dir()
720 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
728 // The system lyxdir is relative to the directory containing the LyX binary.
729 string const relative_system_support_dir()
733 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
734 result = "../Resources/";
736 result = addPath("../share/", PACKAGE);
744 } // namespace support