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()) +
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 FileName res = FileName(lyx_dir().absFileName() + "/../po/" + c + ".gmo");
173 if (!res.isReadableFile())
174 res = FileName(top_srcdir().absFileName() + "/po/" + c + ".gmo");
177 return FileName(locale_dir_.absFileName() + "/" + c
178 + "/LC_MESSAGES/" PACKAGE ".mo");
182 // The specification of home_dir_ is fixed for a given OS.
183 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
184 // and on a Posix-like machine: "/home/USERNAME".
185 FileName const & Package::get_home_dir()
187 #if defined (USE_WINDOWS_PACKAGING)
188 static FileName const home_dir(getEnv("USERPROFILE"));
189 #elif defined (USE_MACOSX_PACKAGING)
190 static FileName const home_dir(fromqstr(QDir::homePath()));
192 static FileName const home_dir(getEnv("HOME"));
200 // These next functions contain the stuff that is substituted at
201 // configuration-time.
202 FileName const hardcoded_localedir()
205 // The build system needs to make sure that this is in utf8 encoding.
206 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
210 FileName const hardcoded_system_support_dir()
213 // The build system needs to make sure that this is in utf8 encoding.
214 return FileName(LYX_ABS_INSTALLED_DATADIR);
218 string const & with_version_suffix()
220 static string const program_suffix = PROGRAM_SUFFIX;
221 static string const with_version_suffix =
222 " --with-version-suffix=" PROGRAM_SUFFIX;
223 return program_suffix.empty() ? program_suffix : with_version_suffix;
229 FileName const & Package::top_srcdir()
232 // The build system needs to make sure that this is in utf8 encoding.
233 static FileName const dir(LYX_ABS_TOP_SRCDIR);
240 bool check_command_line_dir(string const & dir,
242 string const & command_line_switch);
244 FileName const extract_env_var_dir(string const & env_var);
246 bool check_env_var_dir(FileName const & dir,
247 string const & env_var);
249 bool check_env_var_dir(FileName const & dir,
251 string const & env_var);
253 string const relative_locale_dir();
255 string const relative_system_support_dir();
259 * Convert \p name to internal path and strip a trailing slash, since it
260 * comes from user input (commandline or environment).
261 * \p name is encoded in utf8.
263 string const fix_dir_name(string const & name)
265 return rtrim(os::internal_path(name), "/");
270 bool isBuildDir(FileName const & abs_binary, string const & dir_location,
271 FileName & build_support_dir)
273 string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
275 // Makefile by automake
276 build_support_dir = FileName(addPath(search_dir, "lib"));
277 if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
280 // cmake file, no Makefile in lib
281 FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
282 if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
289 bool inBuildDir(FileName const & abs_binary,
290 FileName & build_support_dir, FileName & system_support_dir)
292 string const check_text = "Checking whether LyX is run in place...";
294 // We're looking for "Makefile" in a directory
296 // We're also looking for "chkconfig.ltx" in a directory
298 // If both are found, then we're running LyX in-place.
300 // Note that the name of the lyx binary may be a symbolic link.
301 // If that is the case, then we follow the links too.
302 FileName binary = abs_binary;
304 // Try and find "lyxrc.defaults".
305 if( isBuildDir(binary, "../", build_support_dir) ||
306 isBuildDir(binary, "../../", build_support_dir))
308 // Try and find "chkconfig.ltx".
310 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
312 if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
313 LYXERR(Debug::INIT, check_text << " yes");
318 // Check whether binary is a symbolic link.
319 // If so, resolve it and repeat the exercise.
320 if (!binary.isSymLink())
324 if (readLink(binary, link)) {
327 // Unable to resolve the link.
332 LYXERR(Debug::INIT, check_text << " no");
333 system_support_dir = FileName();
334 build_support_dir = FileName();
340 bool doesFileExist(FileName & result, string const & search_dir, string const & name)
342 result = fileSearch(search_dir, name);
343 if (!result.empty()) {
350 bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
352 lyx_binary = FileName();
354 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
355 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
356 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
357 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
359 return !lyx_binary.empty() ? true : false;
363 FileName findLyxBinary(FileName const & abs_binary)
366 string checkname = abs_binary.toFilesystemEncoding();
367 int check_len = checkname.length();
368 int prgsuffixlen = string(PROGRAM_SUFFIX).length();
369 if ((prgsuffixlen > 0) && (check_len > prgsuffixlen) &&
370 (checkname.substr(check_len-prgsuffixlen) == string(PROGRAM_SUFFIX))) {
373 else if (!abs_binary.extension().empty()) {
374 ext = "." + abs_binary.extension();
377 string binary_dir = onlyPath(abs_binary.absFileName());
380 if (lyxBinaryPath(lyx_binary, binary_dir, ext))
383 string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
384 if (lyxBinaryPath(lyx_binary, search_dir, ext))
391 // Specification of document_dir_ may be reset by LyXRC,
392 // but the default is fixed for a given OS.
393 FileName const get_document_dir(FileName const & home_dir)
395 #if defined (USE_WINDOWS_PACKAGING)
396 (void)home_dir; // Silence warning about unused variable.
397 os::GetFolderPath win32_folder_path;
398 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
399 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
400 (void)home_dir; // Silence warning about unused variable.
401 return FileName(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
402 #elif defined (USE_MACOSX_PACKAGING)
403 (void)home_dir; // Silence warning about unused variable.
404 return FileName(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)));
412 // Several sources are probed to ascertain the locale directory.
413 // The only requirement is that the result is indeed a directory.
414 FileName const get_locale_dir(FileName const & system_support_dir)
416 // 1. Use the "LYX_LOCALEDIR" environment variable.
417 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
418 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
421 // 2. Search for system_support_dir / <relative locale dir>
422 // The <relative locale dir> is OS-dependent. (On Unix, it will
424 FileName path(addPath(system_support_dir.absFileName(),
425 relative_locale_dir()));
427 if (path.exists() && path.isDirectory())
430 // 3. Fall back to the hard-coded LOCALEDIR.
431 path = hardcoded_localedir();
432 if (path.exists() && path.isDirectory())
439 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
440 FileName const abs_path_from_command_line(string const & command_line)
442 if (command_line.empty())
445 string const str_path = fix_dir_name(command_line);
446 return makeAbsPath(str_path);
450 // Does the grunt work for abs_path_from_binary_name()
451 FileName const get_binary_path(string const & exe)
453 #if defined (USE_WINDOWS_PACKAGING)
454 // The executable may have been invoked either with or
455 // without the .exe extension.
456 // Ensure that it is present.
457 string const as_internal_path = os::internal_path(exe);
458 string const exe_path = suffixIs(as_internal_path, ".exe") ?
459 as_internal_path : as_internal_path + ".exe";
461 string const exe_path = os::internal_path(exe);
463 if (FileName::isAbsolute(exe_path))
464 return FileName(exe_path);
466 // Two possibilities present themselves.
467 // 1. The binary is relative to the CWD.
468 FileName const abs_exe_path = makeAbsPath(exe_path);
469 if (abs_exe_path.exists())
472 // 2. exe must be the name of the binary only and it
473 // can be found on the PATH.
474 string const exe_name = onlyFileName(exe_path);
475 if (exe_name != exe_path)
478 vector<string> const path = getEnvPath("PATH");
479 vector<string>::const_iterator it = path.begin();
480 vector<string>::const_iterator const end = path.end();
481 for (; it != end; ++it) {
482 // This will do nothing if *it is already absolute.
483 string const exe_dir = makeAbsPath(*it).absFileName();
485 FileName const exe_path(addName(exe_dir, exe_name));
486 if (exe_path.exists())
490 // Didn't find anything.
495 // Extracts the absolute path to the binary name received as argv[0].
496 FileName const abs_path_from_binary_name(string const & exe)
498 FileName const abs_binary = get_binary_path(exe);
499 if (abs_binary.empty()) {
501 throw ExceptionMessage(ErrorException,
502 _("LyX binary not found"),
503 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
510 // A plethora of directories is searched to ascertain the system
511 // lyxdir which is defined as the first directory to contain
514 get_system_support_dir(FileName const & abs_binary,
515 string const & command_line_system_support_dir)
517 string const chkconfig_ltx = "chkconfig.ltx";
519 // searched_dirs is used for diagnostic purposes only in the case
520 // that "chkconfig.ltx" is not found.
521 list<FileName> searched_dirs;
523 // 1. Use the -sysdir command line parameter.
524 FileName path = abs_path_from_command_line(command_line_system_support_dir);
526 searched_dirs.push_back(path);
527 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
531 // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
532 path = extract_env_var_dir(LYX_DIR_VER);
534 searched_dirs.push_back(path);
535 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
539 // 3. Search relative to the lyx binary.
540 // We're looking for "chkconfig.ltx" in a directory
541 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
542 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
543 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
544 string const relative_lyxdir = relative_system_support_dir();
546 // One subtlety to be aware of. The name of the lyx binary may be
547 // a symbolic link. If that is the case, then we follow the links too.
548 FileName binary = abs_binary;
550 // Try and find "chkconfig.ltx".
551 string const binary_dir = onlyPath(binary.absFileName());
553 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
554 searched_dirs.push_back(lyxdir);
556 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
557 // Success! "chkconfig.ltx" has been found.
561 // Check whether binary is a symbolic link.
562 // If so, resolve it and repeat the exercise.
563 if (!binary.isSymLink())
567 if (readLink(binary, link)) {
570 // Unable to resolve the link.
575 // 4. Repeat the exercise on the directory itself.
576 FileName binary_dir(onlyPath(abs_binary.absFileName()));
578 // This time test whether the directory is a symbolic link
579 // *before* looking for "chkconfig.ltx".
580 // (We've looked relative to the original already.)
581 if (!binary.isSymLink())
585 if (readLink(binary_dir, link)) {
588 // Unable to resolve the link.
592 // Try and find "chkconfig.ltx".
593 FileName const lyxdir(addPath(binary_dir.absFileName(),
595 searched_dirs.push_back(lyxdir);
597 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
598 // Success! "chkconfig.ltx" has been found.
603 // 5. In desparation, try the hard-coded system support dir.
604 path = hardcoded_system_support_dir();
605 if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
608 // Everything has failed :-(
609 // So inform the user and exit.
610 string searched_dirs_str;
611 typedef list<FileName>::const_iterator iterator;
612 iterator const begin = searched_dirs.begin();
613 iterator const end = searched_dirs.end();
614 for (iterator it = begin; it != end; ++it) {
616 searched_dirs_str += "\n\t";
617 searched_dirs_str += it->absFileName();
621 throw ExceptionMessage(ErrorException, _("No system directory"),
622 bformat(_("Unable to determine the system directory "
625 "Use the '-sysdir' command line parameter or "
626 "set the environment variable\n%2$s "
627 "to the LyX system directory containing the "
628 "file `chkconfig.ltx'."),
629 from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
631 // Keep the compiler happy.
636 // Returns the absolute path to the user lyxdir, together with a flag
637 // indicating whether this directory was specified explicitly (as -userdir
638 // or through an environment variable) or whether it was deduced.
639 bool userSupportDir(FileName const & default_user_support_dir,
640 string const & command_line_user_support_dir, FileName & result)
642 // 1. Use the -userdir command line parameter.
643 result = abs_path_from_command_line(command_line_user_support_dir);
647 // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
648 result = extract_env_var_dir(LYX_USERDIR_VER);
652 // 3. Use the OS-dependent default_user_support_dir
653 result = default_user_support_dir;
658 // $HOME/.lyx on POSIX but on Win32 it will be something like
659 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
660 FileName const get_default_user_support_dir(FileName const & home_dir)
662 #if defined (USE_WINDOWS_PACKAGING)
663 (void)home_dir; // Silence warning about unused variable.
665 os::GetFolderPath win32_folder_path;
666 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
668 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
669 (void)home_dir; // Silence warning about unused variable.
670 return FileName(addPath(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DataLocation)), PACKAGE));
672 #elif defined (USE_MACOSX_PACKAGING)
673 (void)home_dir; // Silence warning about unused variable.
674 return FileName(addPath(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), PACKAGE));
676 #else // USE_POSIX_PACKAGING
677 return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
682 // Check that directory @c dir contains @c file.
683 // Else emit an error message about an invalid @c command_line_switch.
684 bool check_command_line_dir(string const & dir,
686 string const & command_line_switch)
688 FileName const abs_path = fileSearch(dir, file);
689 if (abs_path.empty()) {
691 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
692 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
693 from_utf8(command_line_switch), from_utf8(dir),
697 return !abs_path.empty();
701 // The environment variable @c env_var expands to a (single) file path.
702 FileName const extract_env_var_dir(string const & env_var)
704 string const dir = fix_dir_name(getEnv(env_var));
705 return dir.empty() ? FileName() : makeAbsPath(dir);
709 // Check that directory @c dir contains @c file.
710 // Else emit a warning about an invalid @c env_var.
711 bool check_env_var_dir(FileName const & dir,
713 string const & env_var)
715 FileName const abs_path = fileSearch(dir.absFileName(), file);
716 if (abs_path.empty()) {
718 throw ExceptionMessage(WarningException, _("File not found"), bformat(
719 _("Invalid %1$s environment variable.\n"
720 "Directory %2$s does not contain %3$s."),
721 from_utf8(env_var), from_utf8(dir.absFileName()),
725 return !abs_path.empty();
729 // Check that directory @c dir is indeed a directory.
730 // Else emit a warning about an invalid @c env_var.
731 bool check_env_var_dir(FileName const & dir,
732 string const & env_var)
734 bool const success = dir.exists() && dir.isDirectory();
737 // Put this string on a single line so that the gettext
738 // search mechanism in po/Makefile.in.in will register
739 // Package.cpp.in as a file containing strings that need
742 docstring const fmt =
743 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
745 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
746 fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
753 // The locale directory relative to the LyX system directory.
754 string const relative_locale_dir()
756 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
764 // The system lyxdir is relative to the directory containing the LyX binary.
765 string const relative_system_support_dir()
769 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
770 result = "../Resources/";
772 result = addPath("../share/", PACKAGE);
780 } // namespace support