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=" + binary_dir().absFileName();
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;
168 FileName Package::messages_file(string const & c) const
171 return FileName(top_srcdir().absFileName() + "/po/"
174 return FileName(locale_dir_.absFileName() + "/" + c
175 + "/LC_MESSAGES/" PACKAGE ".mo");
179 // The specification of home_dir_ is fixed for a given OS.
180 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
181 // and on a Posix-like machine: "/home/USERNAME".
182 FileName const & Package::get_home_dir()
184 #if defined (USE_WINDOWS_PACKAGING)
185 static FileName const home_dir(getEnv("USERPROFILE"));
186 #elif defined (USE_MACOSX_PACKAGING)
187 static FileName const home_dir(fromqstr(QDir::homePath()));
189 static FileName const home_dir(getEnv("HOME"));
197 // These next functions contain the stuff that is substituted at
198 // configuration-time.
199 FileName const hardcoded_localedir()
202 // The build system needs to make sure that this is in utf8 encoding.
203 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
207 FileName const hardcoded_system_support_dir()
210 // The build system needs to make sure that this is in utf8 encoding.
211 return FileName(LYX_ABS_INSTALLED_DATADIR);
215 string const & with_version_suffix()
217 static string const program_suffix = PROGRAM_SUFFIX;
218 static string const with_version_suffix =
219 " --with-version-suffix=" PROGRAM_SUFFIX;
220 return program_suffix.empty() ? program_suffix : with_version_suffix;
226 FileName const & Package::top_srcdir()
229 // The build system needs to make sure that this is in utf8 encoding.
230 static FileName const dir(LYX_ABS_TOP_SRCDIR);
237 bool check_command_line_dir(string const & dir,
239 string const & command_line_switch);
241 FileName const extract_env_var_dir(string const & env_var);
243 bool check_env_var_dir(FileName const & dir,
244 string const & env_var);
246 bool check_env_var_dir(FileName const & dir,
248 string const & env_var);
250 string const relative_locale_dir();
252 string const relative_system_support_dir();
256 * Convert \p name to internal path and strip a trailing slash, since it
257 * comes from user input (commandline or environment).
258 * \p name is encoded in utf8.
260 string const fix_dir_name(string const & name)
262 return rtrim(os::internal_path(name), "/");
267 bool isBuildDir(FileName const & abs_binary, string const & dir_location,
268 FileName & build_support_dir)
270 string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
272 // Makefile by automake
273 build_support_dir = FileName(addPath(search_dir, "lib"));
274 if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
277 // cmake file, no Makefile in lib
278 FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
279 if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
286 bool inBuildDir(FileName const & abs_binary,
287 FileName & build_support_dir, FileName & system_support_dir)
289 string const check_text = "Checking whether LyX is run in place...";
291 // We're looking for "Makefile" in a directory
293 // We're also looking for "chkconfig.ltx" in a directory
295 // If both are found, then we're running LyX in-place.
297 // Note that the name of the lyx binary may be a symbolic link.
298 // If that is the case, then we follow the links too.
299 FileName binary = abs_binary;
301 // Try and find "lyxrc.defaults".
302 if( isBuildDir(binary, "../", build_support_dir) ||
303 isBuildDir(binary, "../../", build_support_dir))
305 // Try and find "chkconfig.ltx".
307 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
309 if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
310 LYXERR(Debug::INIT, check_text << " yes");
315 // Check whether binary is a symbolic link.
316 // If so, resolve it and repeat the exercise.
317 if (!binary.isSymLink())
321 if (readLink(binary, link)) {
324 // Unable to resolve the link.
329 LYXERR(Debug::INIT, check_text << " no");
330 system_support_dir = FileName();
331 build_support_dir = FileName();
337 bool doesFileExist(FileName & result, string const & search_dir, string const & name)
339 result = fileSearch(search_dir, name);
340 if (!result.empty()) {
347 bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
349 lyx_binary = FileName();
351 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
352 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
353 } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
354 } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
356 return !lyx_binary.empty() ? true : false;
360 FileName findLyxBinary(FileName const & abs_binary)
363 string checkname = abs_binary.toFilesystemEncoding();
364 int check_len = checkname.length();
365 int prgsuffixlen = string(PROGRAM_SUFFIX).length();
366 if ((prgsuffixlen > 0) && (check_len > prgsuffixlen) &&
367 (checkname.substr(check_len-prgsuffixlen) == string(PROGRAM_SUFFIX))) {
370 else if (!abs_binary.extension().empty()) {
371 ext = "." + abs_binary.extension();
374 string binary_dir = onlyPath(abs_binary.absFileName());
377 if (lyxBinaryPath(lyx_binary, binary_dir, ext))
380 string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
381 if (lyxBinaryPath(lyx_binary, search_dir, ext))
388 // Specification of document_dir_ may be reset by LyXRC,
389 // but the default is fixed for a given OS.
390 FileName const get_document_dir(FileName const & home_dir)
392 #if defined (USE_WINDOWS_PACKAGING)
393 (void)home_dir; // Silence warning about unused variable.
394 os::GetFolderPath win32_folder_path;
395 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
396 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
397 (void)home_dir; // Silence warning about unused variable.
398 return FileName(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
399 #elif defined (USE_MACOSX_PACKAGING)
400 (void)home_dir; // Silence warning about unused variable.
401 return FileName(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)));
409 // Several sources are probed to ascertain the locale directory.
410 // The only requirement is that the result is indeed a directory.
411 FileName const get_locale_dir(FileName const & system_support_dir)
413 // 1. Use the "LYX_LOCALEDIR" environment variable.
414 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
415 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
418 // 2. Search for system_support_dir / <relative locale dir>
419 // The <relative locale dir> is OS-dependent. (On Unix, it will
421 FileName path(addPath(system_support_dir.absFileName(),
422 relative_locale_dir()));
424 if (path.exists() && path.isDirectory())
427 // 3. Fall back to the hard-coded LOCALEDIR.
428 path = hardcoded_localedir();
429 if (path.exists() && path.isDirectory())
436 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
437 FileName const abs_path_from_command_line(string const & command_line)
439 if (command_line.empty())
442 string const str_path = fix_dir_name(command_line);
443 return makeAbsPath(str_path);
447 // Does the grunt work for abs_path_from_binary_name()
448 FileName const get_binary_path(string const & exe)
450 #if defined (USE_WINDOWS_PACKAGING)
451 // The executable may have been invoked either with or
452 // without the .exe extension.
453 // Ensure that it is present.
454 string const as_internal_path = os::internal_path(exe);
455 string const exe_path = suffixIs(as_internal_path, ".exe") ?
456 as_internal_path : as_internal_path + ".exe";
458 string const exe_path = os::internal_path(exe);
460 if (FileName::isAbsolute(exe_path))
461 return FileName(exe_path);
463 // Two possibilities present themselves.
464 // 1. The binary is relative to the CWD.
465 FileName const abs_exe_path = makeAbsPath(exe_path);
466 if (abs_exe_path.exists())
469 // 2. exe must be the name of the binary only and it
470 // can be found on the PATH.
471 string const exe_name = onlyFileName(exe_path);
472 if (exe_name != exe_path)
475 vector<string> const path = getEnvPath("PATH");
476 vector<string>::const_iterator it = path.begin();
477 vector<string>::const_iterator const end = path.end();
478 for (; it != end; ++it) {
479 // This will do nothing if *it is already absolute.
480 string const exe_dir = makeAbsPath(*it).absFileName();
482 FileName const exe_path(addName(exe_dir, exe_name));
483 if (exe_path.exists())
487 // Didn't find anything.
492 // Extracts the absolute path to the binary name received as argv[0].
493 FileName const abs_path_from_binary_name(string const & exe)
495 FileName const abs_binary = get_binary_path(exe);
496 if (abs_binary.empty()) {
498 throw ExceptionMessage(ErrorException,
499 _("LyX binary not found"),
500 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
507 // A plethora of directories is searched to ascertain the system
508 // lyxdir which is defined as the first directory to contain
511 get_system_support_dir(FileName const & abs_binary,
512 string const & command_line_system_support_dir)
514 string const chkconfig_ltx = "chkconfig.ltx";
516 // searched_dirs is used for diagnostic purposes only in the case
517 // that "chkconfig.ltx" is not found.
518 list<FileName> searched_dirs;
520 // 1. Use the -sysdir command line parameter.
521 FileName path = abs_path_from_command_line(command_line_system_support_dir);
523 searched_dirs.push_back(path);
524 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
528 // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
529 path = extract_env_var_dir(LYX_DIR_VER);
531 searched_dirs.push_back(path);
532 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
536 // 3. Search relative to the lyx binary.
537 // We're looking for "chkconfig.ltx" in a directory
538 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
539 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
540 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
541 string const relative_lyxdir = relative_system_support_dir();
543 // One subtlety to be aware of. The name of the lyx binary may be
544 // a symbolic link. If that is the case, then we follow the links too.
545 FileName binary = abs_binary;
547 // Try and find "chkconfig.ltx".
548 string const binary_dir = onlyPath(binary.absFileName());
550 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
551 searched_dirs.push_back(lyxdir);
553 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
554 // Success! "chkconfig.ltx" has been found.
558 // Check whether binary is a symbolic link.
559 // If so, resolve it and repeat the exercise.
560 if (!binary.isSymLink())
564 if (readLink(binary, link)) {
567 // Unable to resolve the link.
572 // 4. Repeat the exercise on the directory itself.
573 FileName binary_dir(onlyPath(abs_binary.absFileName()));
575 // This time test whether the directory is a symbolic link
576 // *before* looking for "chkconfig.ltx".
577 // (We've looked relative to the original already.)
578 if (!binary.isSymLink())
582 if (readLink(binary_dir, link)) {
585 // Unable to resolve the link.
589 // Try and find "chkconfig.ltx".
590 FileName const lyxdir(addPath(binary_dir.absFileName(),
592 searched_dirs.push_back(lyxdir);
594 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
595 // Success! "chkconfig.ltx" has been found.
600 // 5. In desparation, try the hard-coded system support dir.
601 path = hardcoded_system_support_dir();
602 if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
605 // Everything has failed :-(
606 // So inform the user and exit.
607 string searched_dirs_str;
608 typedef list<FileName>::const_iterator iterator;
609 iterator const begin = searched_dirs.begin();
610 iterator const end = searched_dirs.end();
611 for (iterator it = begin; it != end; ++it) {
613 searched_dirs_str += "\n\t";
614 searched_dirs_str += it->absFileName();
618 throw ExceptionMessage(ErrorException, _("No system directory"),
619 bformat(_("Unable to determine the system directory "
622 "Use the '-sysdir' command line parameter or "
623 "set the environment variable\n%2$s "
624 "to the LyX system directory containing the "
625 "file `chkconfig.ltx'."),
626 from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
628 // Keep the compiler happy.
633 // Returns the absolute path to the user lyxdir, together with a flag
634 // indicating whether this directory was specified explicitly (as -userdir
635 // or through an environment variable) or whether it was deduced.
636 bool userSupportDir(FileName const & default_user_support_dir,
637 string const & command_line_user_support_dir, FileName & result)
639 // 1. Use the -userdir command line parameter.
640 result = abs_path_from_command_line(command_line_user_support_dir);
644 // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
645 result = extract_env_var_dir(LYX_USERDIR_VER);
649 // 3. Use the OS-dependent default_user_support_dir
650 result = default_user_support_dir;
655 // $HOME/.lyx on POSIX but on Win32 it will be something like
656 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
657 FileName const get_default_user_support_dir(FileName const & home_dir)
659 #if defined (USE_WINDOWS_PACKAGING)
660 (void)home_dir; // Silence warning about unused variable.
662 os::GetFolderPath win32_folder_path;
663 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
665 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
666 (void)home_dir; // Silence warning about unused variable.
667 return FileName(addPath(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DataLocation)), PACKAGE));
669 #elif defined (USE_MACOSX_PACKAGING)
670 (void)home_dir; // Silence warning about unused variable.
671 return FileName(addPath(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), PACKAGE));
673 #else // USE_POSIX_PACKAGING
674 return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
679 // Check that directory @c dir contains @c file.
680 // Else emit an error message about an invalid @c command_line_switch.
681 bool check_command_line_dir(string const & dir,
683 string const & command_line_switch)
685 FileName const abs_path = fileSearch(dir, file);
686 if (abs_path.empty()) {
688 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
689 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
690 from_utf8(command_line_switch), from_utf8(dir),
694 return !abs_path.empty();
698 // The environment variable @c env_var expands to a (single) file path.
699 FileName const extract_env_var_dir(string const & env_var)
701 string const dir = fix_dir_name(getEnv(env_var));
702 return dir.empty() ? FileName() : makeAbsPath(dir);
706 // Check that directory @c dir contains @c file.
707 // Else emit a warning about an invalid @c env_var.
708 bool check_env_var_dir(FileName const & dir,
710 string const & env_var)
712 FileName const abs_path = fileSearch(dir.absFileName(), file);
713 if (abs_path.empty()) {
715 throw ExceptionMessage(WarningException, _("File not found"), bformat(
716 _("Invalid %1$s environment variable.\n"
717 "Directory %2$s does not contain %3$s."),
718 from_utf8(env_var), from_utf8(dir.absFileName()),
722 return !abs_path.empty();
726 // Check that directory @c dir is indeed a directory.
727 // Else emit a warning about an invalid @c env_var.
728 bool check_env_var_dir(FileName const & dir,
729 string const & env_var)
731 bool const success = dir.exists() && dir.isDirectory();
734 // Put this string on a single line so that the gettext
735 // search mechanism in po/Makefile.in.in will register
736 // Package.cpp.in as a file containing strings that need
739 docstring const fmt =
740 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
742 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
743 fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
750 // The locale directory relative to the LyX system directory.
751 string const relative_locale_dir()
753 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
761 // The system lyxdir is relative to the directory containing the LyX binary.
762 string const relative_system_support_dir()
766 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
767 result = "../Resources/";
769 result = addPath("../share/", PACKAGE);
777 } // namespace support