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 LAPPERR(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 in_build_dir_ = inBuildDir(abs_binary, build_support_dir_, system_support_dir_);
124 if (!in_build_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() + " --binary-dir=" +
145 quoteName(FileName(binary_dir().absFileName()).toFilesystemEncoding());
147 LYXERR(Debug::INIT, "<package>\n"
148 << "\tbinary_dir " << binary_dir().absFileName() << '\n'
149 << "\tsystem_support " << system_support().absFileName() << '\n'
150 << "\tbuild_support " << build_support().absFileName() << '\n'
151 << "\tuser_support " << user_support().absFileName() << '\n'
152 << "\tlocale_dir " << locale_dir().absFileName() << '\n'
153 << "\tdocument_dir " << document_dir().absFileName() << '\n'
154 << "\ttemp_dir " << temp_dir().absFileName() << '\n'
155 << "\thome_dir " << get_home_dir().absFileName() << '\n'
160 void Package::set_temp_dir(FileName const & temp_dir) const
162 if (temp_dir.empty())
163 temp_dir_ = system_temp_dir_;
165 temp_dir_ = temp_dir;
169 FileName Package::messages_file(string const & c) const
172 return FileName(top_srcdir().absFileName() + "/po/"
175 return FileName(locale_dir_.absFileName() + "/" + c
176 + "/LC_MESSAGES/" PACKAGE ".mo");
180 // The specification of home_dir_ is fixed for a given OS.
181 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
182 // and on a Posix-like machine: "/home/USERNAME".
183 FileName const & Package::get_home_dir()
185 #if defined (USE_WINDOWS_PACKAGING)
186 static FileName const home_dir(getEnv("USERPROFILE"));
187 #elif defined (USE_MACOSX_PACKAGING)
188 static FileName const home_dir(fromqstr(QDir::homePath()));
190 static FileName const home_dir(getEnv("HOME"));
198 // These next functions contain the stuff that is substituted at
199 // configuration-time.
200 FileName const hardcoded_localedir()
203 // The build system needs to make sure that this is in utf8 encoding.
204 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
208 FileName const hardcoded_system_support_dir()
211 // The build system needs to make sure that this is in utf8 encoding.
212 return FileName(LYX_ABS_INSTALLED_DATADIR);
216 string const & with_version_suffix()
218 static string const program_suffix = PROGRAM_SUFFIX;
219 static string const with_version_suffix =
220 " --with-version-suffix=" PROGRAM_SUFFIX;
221 return program_suffix.empty() ? program_suffix : with_version_suffix;
227 FileName const & Package::top_srcdir()
230 // The build system needs to make sure that this is in utf8 encoding.
231 static FileName const dir(LYX_ABS_TOP_SRCDIR);
238 bool check_command_line_dir(string const & dir,
240 string const & command_line_switch);
242 FileName const extract_env_var_dir(string const & env_var);
244 bool check_env_var_dir(FileName const & dir,
245 string const & env_var);
247 bool check_env_var_dir(FileName const & dir,
249 string const & env_var);
251 string const relative_locale_dir();
253 string const relative_system_support_dir();
257 * Convert \p name to internal path and strip a trailing slash, since it
258 * comes from user input (commandline or environment).
259 * \p name is encoded in utf8.
261 string const fix_dir_name(string const & name)
263 return rtrim(os::internal_path(name), "/");
268 bool isBuildDir(FileName const & abs_binary, string const & dir_location,
269 FileName & build_support_dir)
271 string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
273 // Makefile by automake
274 build_support_dir = FileName(addPath(search_dir, "lib"));
275 if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
278 // cmake file, no Makefile in lib
279 FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
280 if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
287 bool inBuildDir(FileName const & abs_binary,
288 FileName & build_support_dir, FileName & system_support_dir)
290 string const check_text = "Checking whether LyX is run in place...";
292 // We're looking for "Makefile" in a directory
294 // We're also looking for "chkconfig.ltx" in a directory
296 // If both are found, then we're running LyX in-place.
298 // Note that the name of the lyx binary may be a symbolic link.
299 // If that is the case, then we follow the links too.
300 FileName binary = abs_binary;
302 // Try and find "lyxrc.defaults".
303 if( isBuildDir(binary, "../", build_support_dir) ||
304 isBuildDir(binary, "../../", build_support_dir))
306 // Try and find "chkconfig.ltx".
308 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
310 if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
311 LYXERR(Debug::INIT, check_text << " yes");
316 // Check whether binary is a symbolic link.
317 // If so, resolve it and repeat the exercise.
318 if (!binary.isSymLink())
322 if (readLink(binary, link)) {
325 // Unable to resolve the link.
330 LYXERR(Debug::INIT, check_text << " no");
331 system_support_dir = FileName();
332 build_support_dir = FileName();
338 bool doesFileExist(FileName & result, string const & search_dir, string const & name)
340 result = fileSearch(search_dir, name);
341 if (!result.empty()) {
348 bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
350 lyx_binary = FileName();
352 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
353 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
354 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
355 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
357 return !lyx_binary.empty() ? true : false;
361 FileName findLyxBinary(FileName const & abs_binary)
364 string checkname = abs_binary.toFilesystemEncoding();
365 int check_len = checkname.length();
366 int prgsuffixlen = string(PROGRAM_SUFFIX).length();
367 if ((prgsuffixlen > 0) && (check_len > prgsuffixlen) &&
368 (checkname.substr(check_len-prgsuffixlen) == string(PROGRAM_SUFFIX))) {
371 else if (!abs_binary.extension().empty()) {
372 ext = "." + abs_binary.extension();
375 string binary_dir = onlyPath(abs_binary.absFileName());
378 if (lyxBinaryPath(lyx_binary, binary_dir, ext))
381 string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
382 if (lyxBinaryPath(lyx_binary, search_dir, ext))
389 // Specification of document_dir_ may be reset by LyXRC,
390 // but the default is fixed for a given OS.
391 FileName const get_document_dir(FileName const & home_dir)
393 #if defined (USE_WINDOWS_PACKAGING)
394 (void)home_dir; // Silence warning about unused variable.
395 os::GetFolderPath win32_folder_path;
396 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
397 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
398 (void)home_dir; // Silence warning about unused variable.
399 return FileName(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
400 #elif defined (USE_MACOSX_PACKAGING)
401 (void)home_dir; // Silence warning about unused variable.
402 return FileName(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)));
410 // Several sources are probed to ascertain the locale directory.
411 // The only requirement is that the result is indeed a directory.
412 FileName const get_locale_dir(FileName const & system_support_dir)
414 // 1. Use the "LYX_LOCALEDIR" environment variable.
415 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
416 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
419 // 2. Search for system_support_dir / <relative locale dir>
420 // The <relative locale dir> is OS-dependent. (On Unix, it will
422 FileName path(addPath(system_support_dir.absFileName(),
423 relative_locale_dir()));
425 if (path.exists() && path.isDirectory())
428 // 3. Fall back to the hard-coded LOCALEDIR.
429 path = hardcoded_localedir();
430 if (path.exists() && path.isDirectory())
437 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
438 FileName const abs_path_from_command_line(string const & command_line)
440 if (command_line.empty())
443 string const str_path = fix_dir_name(command_line);
444 return makeAbsPath(str_path);
448 // Does the grunt work for abs_path_from_binary_name()
449 FileName const get_binary_path(string const & exe)
451 #if defined (USE_WINDOWS_PACKAGING)
452 // The executable may have been invoked either with or
453 // without the .exe extension.
454 // Ensure that it is present.
455 string const as_internal_path = os::internal_path(exe);
456 string const exe_path = suffixIs(as_internal_path, ".exe") ?
457 as_internal_path : as_internal_path + ".exe";
459 string const exe_path = os::internal_path(exe);
461 if (FileName::isAbsolute(exe_path))
462 return FileName(exe_path);
464 // Two possibilities present themselves.
465 // 1. The binary is relative to the CWD.
466 FileName const abs_exe_path = makeAbsPath(exe_path);
467 if (abs_exe_path.exists())
470 // 2. exe must be the name of the binary only and it
471 // can be found on the PATH.
472 string const exe_name = onlyFileName(exe_path);
473 if (exe_name != exe_path)
476 vector<string> const path = getEnvPath("PATH");
477 vector<string>::const_iterator it = path.begin();
478 vector<string>::const_iterator const end = path.end();
479 for (; it != end; ++it) {
480 // This will do nothing if *it is already absolute.
481 string const exe_dir = makeAbsPath(*it).absFileName();
483 FileName const exe_path(addName(exe_dir, exe_name));
484 if (exe_path.exists())
488 // Didn't find anything.
493 // Extracts the absolute path to the binary name received as argv[0].
494 FileName const abs_path_from_binary_name(string const & exe)
496 FileName const abs_binary = get_binary_path(exe);
497 if (abs_binary.empty()) {
499 throw ExceptionMessage(ErrorException,
500 _("LyX binary not found"),
501 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
508 // A plethora of directories is searched to ascertain the system
509 // lyxdir which is defined as the first directory to contain
512 get_system_support_dir(FileName const & abs_binary,
513 string const & command_line_system_support_dir)
515 string const chkconfig_ltx = "chkconfig.ltx";
517 // searched_dirs is used for diagnostic purposes only in the case
518 // that "chkconfig.ltx" is not found.
519 list<FileName> searched_dirs;
521 // 1. Use the -sysdir command line parameter.
522 FileName path = abs_path_from_command_line(command_line_system_support_dir);
524 searched_dirs.push_back(path);
525 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
529 // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
530 path = extract_env_var_dir(LYX_DIR_VER);
532 searched_dirs.push_back(path);
533 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
537 // 3. Search relative to the lyx binary.
538 // We're looking for "chkconfig.ltx" in a directory
539 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
540 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
541 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
542 string const relative_lyxdir = relative_system_support_dir();
544 // One subtlety to be aware of. The name of the lyx binary may be
545 // a symbolic link. If that is the case, then we follow the links too.
546 FileName binary = abs_binary;
548 // Try and find "chkconfig.ltx".
549 string const binary_dir = onlyPath(binary.absFileName());
551 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
552 searched_dirs.push_back(lyxdir);
554 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
555 // Success! "chkconfig.ltx" has been found.
559 // Check whether binary is a symbolic link.
560 // If so, resolve it and repeat the exercise.
561 if (!binary.isSymLink())
565 if (readLink(binary, link)) {
568 // Unable to resolve the link.
573 // 4. Repeat the exercise on the directory itself.
574 FileName binary_dir(onlyPath(abs_binary.absFileName()));
576 // This time test whether the directory is a symbolic link
577 // *before* looking for "chkconfig.ltx".
578 // (We've looked relative to the original already.)
579 if (!binary.isSymLink())
583 if (readLink(binary_dir, link)) {
586 // Unable to resolve the link.
590 // Try and find "chkconfig.ltx".
591 FileName const lyxdir(addPath(binary_dir.absFileName(),
593 searched_dirs.push_back(lyxdir);
595 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
596 // Success! "chkconfig.ltx" has been found.
601 // 5. In desparation, try the hard-coded system support dir.
602 path = hardcoded_system_support_dir();
603 if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
606 // Everything has failed :-(
607 // So inform the user and exit.
608 string searched_dirs_str;
609 typedef list<FileName>::const_iterator iterator;
610 iterator const begin = searched_dirs.begin();
611 iterator const end = searched_dirs.end();
612 for (iterator it = begin; it != end; ++it) {
614 searched_dirs_str += "\n\t";
615 searched_dirs_str += it->absFileName();
619 throw ExceptionMessage(ErrorException, _("No system directory"),
620 bformat(_("Unable to determine the system directory "
623 "Use the '-sysdir' command line parameter or "
624 "set the environment variable\n%2$s "
625 "to the LyX system directory containing the "
626 "file `chkconfig.ltx'."),
627 from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
629 // Keep the compiler happy.
634 // Returns the absolute path to the user lyxdir, together with a flag
635 // indicating whether this directory was specified explicitly (as -userdir
636 // or through an environment variable) or whether it was deduced.
637 bool userSupportDir(FileName const & default_user_support_dir,
638 string const & command_line_user_support_dir, FileName & result)
640 // 1. Use the -userdir command line parameter.
641 result = abs_path_from_command_line(command_line_user_support_dir);
645 // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
646 result = extract_env_var_dir(LYX_USERDIR_VER);
650 // 3. Use the OS-dependent default_user_support_dir
651 result = default_user_support_dir;
656 // $HOME/.lyx on POSIX but on Win32 it will be something like
657 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
658 FileName const get_default_user_support_dir(FileName const & home_dir)
660 #if defined (USE_WINDOWS_PACKAGING)
661 (void)home_dir; // Silence warning about unused variable.
663 os::GetFolderPath win32_folder_path;
664 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
666 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
667 (void)home_dir; // Silence warning about unused variable.
668 return FileName(addPath(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DataLocation)), PACKAGE));
670 #elif defined (USE_MACOSX_PACKAGING)
671 (void)home_dir; // Silence warning about unused variable.
672 return FileName(addPath(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), PACKAGE));
674 #else // USE_POSIX_PACKAGING
675 return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
680 // Check that directory @c dir contains @c file.
681 // Else emit an error message about an invalid @c command_line_switch.
682 bool check_command_line_dir(string const & dir,
684 string const & command_line_switch)
686 FileName const abs_path = fileSearch(dir, file);
687 if (abs_path.empty()) {
689 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
690 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
691 from_utf8(command_line_switch), from_utf8(dir),
695 return !abs_path.empty();
699 // The environment variable @c env_var expands to a (single) file path.
700 FileName const extract_env_var_dir(string const & env_var)
702 string const dir = fix_dir_name(getEnv(env_var));
703 return dir.empty() ? FileName() : makeAbsPath(dir);
707 // Check that directory @c dir contains @c file.
708 // Else emit a warning about an invalid @c env_var.
709 bool check_env_var_dir(FileName const & dir,
711 string const & env_var)
713 FileName const abs_path = fileSearch(dir.absFileName(), file);
714 if (abs_path.empty()) {
716 throw ExceptionMessage(WarningException, _("File not found"), bformat(
717 _("Invalid %1$s environment variable.\n"
718 "Directory %2$s does not contain %3$s."),
719 from_utf8(env_var), from_utf8(dir.absFileName()),
723 return !abs_path.empty();
727 // Check that directory @c dir is indeed a directory.
728 // Else emit a warning about an invalid @c env_var.
729 bool check_env_var_dir(FileName const & dir,
730 string const & env_var)
732 bool const success = dir.exists() && dir.isDirectory();
735 // Put this string on a single line so that the gettext
736 // search mechanism in po/Makefile.in.in will register
737 // Package.cpp.in as a file containing strings that need
740 docstring const fmt =
741 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
743 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
744 fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
751 // The locale directory relative to the LyX system directory.
752 string const relative_locale_dir()
754 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
762 // The system lyxdir is relative to the directory containing the LyX binary.
763 string const relative_system_support_dir()
767 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
768 result = "../Resources/";
770 result = addPath("../share/", PACKAGE);
778 } // namespace support