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/lassert.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
25 #if defined (USE_WINDOWS_PACKAGING)
26 # include "support/os_win32.h"
32 #if !defined (USE_WINDOWS_PACKAGING) && \
33 !defined (USE_MACOSX_PACKAGING) && \
34 !defined (USE_POSIX_PACKAGING)
35 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
38 #if defined (USE_MACOSX_PACKAGING)
39 # include "support/qstring_helpers.h"
41 # include <QDesktopServices>
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)
61 package_ = Package(command_line_arg0,
62 command_line_system_support_dir,
63 command_line_user_support_dir);
68 Package const & package()
70 LASSERT(initialised_, /**/);
77 FileName const abs_path_from_binary_name(string const & exe);
80 bool inBuildDir(FileName const & abs_binary, FileName &, FileName &);
82 FileName findLyxBinary(FileName const & abs_binary);
84 FileName const get_document_dir(FileName const & home_dir);
86 FileName const get_locale_dir(FileName const & system_support_dir);
88 FileName const get_system_support_dir(FileName const & abs_binary,
89 string const & command_line_system_support_dir);
91 FileName const get_default_user_support_dir(FileName const & home_dir);
93 bool userSupportDir(FileName const & default_user_support_dir,
94 string const & command_line_user_support_dir, FileName & result);
96 string const & with_version_suffix();
98 string const fix_dir_name(string const & name);
103 Package::Package(string const & command_line_arg0,
104 string const & command_line_system_support_dir,
105 string const & command_line_user_support_dir)
106 : explicit_user_support_dir_(false)
108 // Specification of temp_dir_ may be reset by LyXRC,
109 // but the default is fixed for a given OS.
110 system_temp_dir_ = FileName::tempPath();
111 temp_dir_ = system_temp_dir_;
112 document_dir_ = get_document_dir(get_home_dir());
114 FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
115 binary_dir_ = FileName(onlyPath(abs_binary.absFileName()));
117 // the LyX package directory
118 lyx_dir_ = FileName(addPath(binary_dir_.absFileName(), "../"));
119 lyx_dir_ = FileName(lyx_dir_.realPath());
121 // Is LyX being run in-place from the build tree?
122 bool in_build_dir = inBuildDir(abs_binary, build_support_dir_, system_support_dir_);
125 system_support_dir_ =
126 get_system_support_dir(abs_binary,
127 command_line_system_support_dir);
130 // Find the LyX executable
131 lyx_binary_ = findLyxBinary(abs_binary);
133 locale_dir_ = get_locale_dir(system_support_dir_);
135 FileName const default_user_support_dir =
136 get_default_user_support_dir(get_home_dir());
138 explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
139 command_line_user_support_dir, user_support_dir_);
141 FileName const configure_script(addName(system_support().absFileName(), "configure.py"));
142 configure_command_ = os::python() + ' ' +
143 quoteName(configure_script.toFilesystemEncoding(), quote_python) +
144 with_version_suffix();
146 LYXERR(Debug::INIT, "<package>\n"
147 << "\tbinary_dir " << binary_dir().absFileName() << '\n'
148 << "\tsystem_support " << system_support().absFileName() << '\n'
149 << "\tbuild_support " << build_support().absFileName() << '\n'
150 << "\tuser_support " << user_support().absFileName() << '\n'
151 << "\tlocale_dir " << locale_dir().absFileName() << '\n'
152 << "\tdocument_dir " << document_dir().absFileName() << '\n'
153 << "\ttemp_dir " << temp_dir().absFileName() << '\n'
154 << "\thome_dir " << get_home_dir().absFileName() << '\n'
159 void Package::set_temp_dir(FileName const & temp_dir) const
161 if (temp_dir.empty())
162 temp_dir_ = system_temp_dir_;
164 temp_dir_ = temp_dir;
167 // The specification of home_dir_ is fixed for a given OS.
168 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
169 // and on a Posix-like machine: "/home/USERNAME".
170 FileName const & Package::get_home_dir()
172 #if defined (USE_WINDOWS_PACKAGING)
173 static FileName const home_dir(getEnv("USERPROFILE"));
174 #elif defined (USE_MACOSX_PACKAGING)
175 static FileName const home_dir(fromqstr(QDir::homePath()));
177 static FileName const home_dir(getEnv("HOME"));
185 // These next functions contain the stuff that is substituted at
186 // configuration-time.
187 FileName const hardcoded_localedir()
190 // The build system needs to make sure that this is in utf8 encoding.
191 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
195 FileName const hardcoded_system_support_dir()
198 // The build system needs to make sure that this is in utf8 encoding.
199 return FileName(LYX_ABS_INSTALLED_DATADIR);
203 string const & with_version_suffix()
205 static string const program_suffix = PROGRAM_SUFFIX;
206 static string const with_version_suffix =
207 " --with-version-suffix=" PROGRAM_SUFFIX;
208 return program_suffix.empty() ? program_suffix : with_version_suffix;
214 FileName const & Package::top_srcdir()
217 // The build system needs to make sure that this is in utf8 encoding.
218 static FileName const dir(LYX_ABS_TOP_SRCDIR);
225 bool check_command_line_dir(string const & dir,
227 string const & command_line_switch);
229 FileName const extract_env_var_dir(string const & env_var);
231 bool check_env_var_dir(FileName const & dir,
232 string const & env_var);
234 bool check_env_var_dir(FileName const & dir,
236 string const & env_var);
238 string const relative_locale_dir();
240 string const relative_system_support_dir();
244 * Convert \p name to internal path and strip a trailing slash, since it
245 * comes from user input (commandline or environment).
246 * \p name is encoded in utf8.
248 string const fix_dir_name(string const & name)
250 return rtrim(os::internal_path(name), "/");
255 bool isBuildDir(FileName const & abs_binary, string const & dir_location,
256 FileName & build_support_dir)
258 string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
260 // Makefile by automake
261 build_support_dir = FileName(addPath(search_dir, "lib"));
262 if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
265 // cmake file, no Makefile in lib
266 FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
267 if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
274 bool inBuildDir(FileName const & abs_binary,
275 FileName & build_support_dir, FileName & system_support_dir)
277 string const check_text = "Checking whether LyX is run in place...";
279 // We're looking for "Makefile" in a directory
281 // We're also looking for "chkconfig.ltx" in a directory
283 // If both are found, then we're running LyX in-place.
285 // Note that the name of the lyx binary may be a symbolic link.
286 // If that is the case, then we follow the links too.
287 FileName binary = abs_binary;
289 // Try and find "lyxrc.defaults".
290 if( isBuildDir(binary, "../", build_support_dir) ||
291 isBuildDir(binary, "../../", build_support_dir))
293 // Try and find "chkconfig.ltx".
295 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
297 if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
298 LYXERR(Debug::INIT, check_text << " yes");
303 // Check whether binary is a symbolic link.
304 // If so, resolve it and repeat the exercise.
305 if (!binary.isSymLink())
309 if (readLink(binary, link)) {
312 // Unable to resolve the link.
317 LYXERR(Debug::INIT, check_text << " no");
318 system_support_dir = FileName();
319 build_support_dir = FileName();
325 bool doesFileExist(FileName & result, string const & search_dir, string const & name)
327 result = fileSearch(search_dir, name);
328 if (!result.empty()) {
335 bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
337 lyx_binary = FileName();
339 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
340 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
341 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
342 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
344 return !lyx_binary.empty() ? true : false;
348 FileName findLyxBinary(FileName const & abs_binary)
351 string checkname = abs_binary.toFilesystemEncoding();
352 int check_len = checkname.length();
353 int prgsuffixlen = string(PROGRAM_SUFFIX).length();
354 if ((prgsuffixlen > 0) && (check_len > prgsuffixlen) &&
355 (checkname.substr(check_len-prgsuffixlen) == string(PROGRAM_SUFFIX))) {
358 else if (!abs_binary.extension().empty()) {
359 ext = "." + abs_binary.extension();
362 string binary_dir = onlyPath(abs_binary.absFileName());
365 if (lyxBinaryPath(lyx_binary, binary_dir, ext))
368 string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
369 if (lyxBinaryPath(lyx_binary, search_dir, ext))
376 // Specification of document_dir_ may be reset by LyXRC,
377 // but the default is fixed for a given OS.
378 FileName const get_document_dir(FileName const & home_dir)
380 #if defined (USE_WINDOWS_PACKAGING)
381 (void)home_dir; // Silence warning about unused variable.
382 os::GetFolderPath win32_folder_path;
383 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
384 #elif defined (USE_MACOSX_PACKAGING)
385 (void)home_dir; // Silence warning about unused variable.
386 return FileName(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)));
394 // Several sources are probed to ascertain the locale directory.
395 // The only requirement is that the result is indeed a directory.
396 FileName const get_locale_dir(FileName const & system_support_dir)
398 // 1. Use the "LYX_LOCALEDIR" environment variable.
399 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
400 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
403 // 2. Search for system_support_dir / <relative locale dir>
404 // The <relative locale dir> is OS-dependent. (On Unix, it will
406 FileName path(addPath(system_support_dir.absFileName(),
407 relative_locale_dir()));
409 if (path.exists() && path.isDirectory())
412 // 3. Fall back to the hard-coded LOCALEDIR.
413 path = hardcoded_localedir();
414 if (path.exists() && path.isDirectory())
421 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
422 FileName const abs_path_from_command_line(string const & command_line)
424 if (command_line.empty())
427 string const str_path = fix_dir_name(command_line);
428 return makeAbsPath(str_path);
432 // Does the grunt work for abs_path_from_binary_name()
433 FileName const get_binary_path(string const & exe)
435 #if defined (USE_WINDOWS_PACKAGING)
436 // The executable may have been invoked either with or
437 // without the .exe extension.
438 // Ensure that it is present.
439 string const as_internal_path = os::internal_path(exe);
440 string const exe_path = suffixIs(as_internal_path, ".exe") ?
441 as_internal_path : as_internal_path + ".exe";
443 string const exe_path = os::internal_path(exe);
445 if (FileName::isAbsolute(exe_path))
446 return FileName(exe_path);
448 // Two possibilities present themselves.
449 // 1. The binary is relative to the CWD.
450 FileName const abs_exe_path = makeAbsPath(exe_path);
451 if (abs_exe_path.exists())
454 // 2. exe must be the name of the binary only and it
455 // can be found on the PATH.
456 string const exe_name = onlyFileName(exe_path);
457 if (exe_name != exe_path)
460 vector<string> const path = getEnvPath("PATH");
461 vector<string>::const_iterator it = path.begin();
462 vector<string>::const_iterator const end = path.end();
463 for (; it != end; ++it) {
464 // This will do nothing if *it is already absolute.
465 string const exe_dir = makeAbsPath(*it).absFileName();
467 FileName const exe_path(addName(exe_dir, exe_name));
468 if (exe_path.exists())
472 // Didn't find anything.
477 // Extracts the absolute path to the binary name received as argv[0].
478 FileName const abs_path_from_binary_name(string const & exe)
480 FileName const abs_binary = get_binary_path(exe);
481 if (abs_binary.empty()) {
483 throw ExceptionMessage(ErrorException,
484 _("LyX binary not found"),
485 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
492 // A plethora of directories is searched to ascertain the system
493 // lyxdir which is defined as the first directory to contain
496 get_system_support_dir(FileName const & abs_binary,
497 string const & command_line_system_support_dir)
499 string const chkconfig_ltx = "chkconfig.ltx";
501 // searched_dirs is used for diagnostic purposes only in the case
502 // that "chkconfig.ltx" is not found.
503 list<FileName> searched_dirs;
505 // 1. Use the -sysdir command line parameter.
506 FileName path = abs_path_from_command_line(command_line_system_support_dir);
508 searched_dirs.push_back(path);
509 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
513 // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
514 path = extract_env_var_dir(LYX_DIR_VER);
516 searched_dirs.push_back(path);
517 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
521 // 3. Search relative to the lyx binary.
522 // We're looking for "chkconfig.ltx" in a directory
523 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
524 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
525 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
526 string const relative_lyxdir = relative_system_support_dir();
528 // One subtlety to be aware of. The name of the lyx binary may be
529 // a symbolic link. If that is the case, then we follow the links too.
530 FileName binary = abs_binary;
532 // Try and find "chkconfig.ltx".
533 string const binary_dir = onlyPath(binary.absFileName());
535 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
536 searched_dirs.push_back(lyxdir);
538 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
539 // Success! "chkconfig.ltx" has been found.
543 // Check whether binary is a symbolic link.
544 // If so, resolve it and repeat the exercise.
545 if (!binary.isSymLink())
549 if (readLink(binary, link)) {
552 // Unable to resolve the link.
557 // 4. Repeat the exercise on the directory itself.
558 FileName binary_dir(onlyPath(abs_binary.absFileName()));
560 // This time test whether the directory is a symbolic link
561 // *before* looking for "chkconfig.ltx".
562 // (We've looked relative to the original already.)
563 if (!binary.isSymLink())
567 if (readLink(binary_dir, link)) {
570 // Unable to resolve the link.
574 // Try and find "chkconfig.ltx".
575 FileName const lyxdir(addPath(binary_dir.absFileName(),
577 searched_dirs.push_back(lyxdir);
579 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
580 // Success! "chkconfig.ltx" has been found.
585 // 5. In desparation, try the hard-coded system support dir.
586 path = hardcoded_system_support_dir();
587 if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
590 // Everything has failed :-(
591 // So inform the user and exit.
592 string searched_dirs_str;
593 typedef list<FileName>::const_iterator iterator;
594 iterator const begin = searched_dirs.begin();
595 iterator const end = searched_dirs.end();
596 for (iterator it = begin; it != end; ++it) {
598 searched_dirs_str += "\n\t";
599 searched_dirs_str += it->absFileName();
603 throw ExceptionMessage(ErrorException, _("No system directory"),
604 bformat(_("Unable to determine the system directory "
607 "Use the '-sysdir' command line parameter or "
608 "set the environment variable\n%2$s "
609 "to the LyX system directory containing the "
610 "file `chkconfig.ltx'."),
611 from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
613 // Keep the compiler happy.
618 // Returns the absolute path to the user lyxdir, together with a flag
619 // indicating whether this directory was specified explicitly (as -userdir
620 // or through an environment variable) or whether it was deduced.
621 bool userSupportDir(FileName const & default_user_support_dir,
622 string const & command_line_user_support_dir, FileName & result)
624 // 1. Use the -userdir command line parameter.
625 result = abs_path_from_command_line(command_line_user_support_dir);
629 // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
630 result = extract_env_var_dir(LYX_USERDIR_VER);
634 // 3. Use the OS-dependent default_user_support_dir
635 result = default_user_support_dir;
640 // $HOME/.lyx on POSIX but on Win32 it will be something like
641 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
642 FileName const get_default_user_support_dir(FileName const & home_dir)
644 #if defined (USE_WINDOWS_PACKAGING)
645 (void)home_dir; // Silence warning about unused variable.
647 os::GetFolderPath win32_folder_path;
648 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
650 #elif defined (USE_MACOSX_PACKAGING)
651 (void)home_dir; // Silence warning about unused variable.
652 return FileName(addPath(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), PACKAGE));
654 #else // USE_POSIX_PACKAGING
655 return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
660 // Check that directory @c dir contains @c file.
661 // Else emit an error message about an invalid @c command_line_switch.
662 bool check_command_line_dir(string const & dir,
664 string const & command_line_switch)
666 FileName const abs_path = fileSearch(dir, file);
667 if (abs_path.empty()) {
669 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
670 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
671 from_utf8(command_line_switch), from_utf8(dir),
675 return !abs_path.empty();
679 // The environment variable @c env_var expands to a (single) file path.
680 FileName const extract_env_var_dir(string const & env_var)
682 string const dir = fix_dir_name(getEnv(env_var));
683 return dir.empty() ? FileName() : makeAbsPath(dir);
687 // Check that directory @c dir contains @c file.
688 // Else emit a warning about an invalid @c env_var.
689 bool check_env_var_dir(FileName const & dir,
691 string const & env_var)
693 FileName const abs_path = fileSearch(dir.absFileName(), file);
694 if (abs_path.empty()) {
696 throw ExceptionMessage(WarningException, _("File not found"), bformat(
697 _("Invalid %1$s environment variable.\n"
698 "Directory %2$s does not contain %3$s."),
699 from_utf8(env_var), from_utf8(dir.absFileName()),
703 return !abs_path.empty();
707 // Check that directory @c dir is indeed a directory.
708 // Else emit a warning about an invalid @c env_var.
709 bool check_env_var_dir(FileName const & dir,
710 string const & env_var)
712 bool const success = dir.exists() && dir.isDirectory();
715 // Put this string on a single line so that the gettext
716 // search mechanism in po/Makefile.in.in will register
717 // Package.cpp.in as a file containing strings that need
720 docstring const fmt =
721 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
723 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
724 fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
731 // The locale directory relative to the LyX system directory.
732 string const relative_locale_dir()
734 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
742 // The system lyxdir is relative to the directory containing the LyX binary.
743 string const relative_system_support_dir()
747 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
748 result = "../Resources/";
750 result = addPath("../share/", PACKAGE);
758 } // namespace support