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 <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
50 bool initialised_ = false;
55 void init_package(string const & command_line_arg0,
56 string const & command_line_system_support_dir,
57 string const & command_line_user_support_dir,
58 exe_build_dir_to_top_build_dir top_build_dir_location)
60 package_ = Package(command_line_arg0,
61 command_line_system_support_dir,
62 command_line_user_support_dir,
63 top_build_dir_location);
68 bool packageInitialized()
74 Package const & package()
76 LASSERT(initialised_, /**/);
83 FileName const abs_path_from_binary_name(string const & exe);
85 void buildDirs(FileName const & abs_binary,
86 exe_build_dir_to_top_build_dir top_build_dir_location,
87 FileName &, FileName &);
89 FileName const get_document_dir(FileName const & home_dir);
91 FileName const get_home_dir();
93 FileName const get_locale_dir(FileName const & system_support_dir);
95 FileName const get_system_support_dir(FileName const & abs_binary,
96 string const & command_line_system_support_dir);
98 FileName const get_default_user_support_dir(FileName const & home_dir);
100 bool userSupportDir(FileName const & default_user_support_dir,
101 string const & command_line_user_support_dir, FileName & result);
104 string const & with_version_suffix();
109 Package::Package(string const & command_line_arg0,
110 string const & command_line_system_support_dir,
111 string const & command_line_user_support_dir,
112 exe_build_dir_to_top_build_dir top_build_dir_location)
113 : explicit_user_support_dir_(false)
115 home_dir_ = get_home_dir();
116 // Specification of temp_dir_ may be reset by LyXRC,
117 // but the default is fixed for a given OS.
118 system_temp_dir_ = FileName::tempPath();
119 temp_dir_ = system_temp_dir_;
120 document_dir_ = get_document_dir(home_dir_);
122 FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
123 binary_dir_ = FileName(onlyPath(abs_binary.absFileName()));
125 // the LyX package directory
126 lyx_dir_ = FileName(addPath(binary_dir_.absFileName(), "../"));
127 lyx_dir_ = FileName(lyx_dir_.realPath());
129 // Is LyX being run in-place from the build tree?
130 buildDirs(abs_binary, top_build_dir_location,
131 build_support_dir_, system_support_dir_);
133 if (build_support_dir_.empty())
134 system_support_dir_ =
135 get_system_support_dir(abs_binary,
136 command_line_system_support_dir);
138 // The LyX executable is one level above binary_dir_ if we are running
139 // tex2lyx in place. Otherwise it is in binary_dir_.
141 if (build_support_dir_.empty() ||
142 top_build_dir_location == top_build_dir_is_one_level_up)
143 abs_lyx_dir = binary_dir_.absFileName();
145 FileName fn(addPath(binary_dir_.absFileName(), "../"));
146 abs_lyx_dir = fn.realPath();
148 // The LyX executable may have a package suffix if we are not running
150 if (build_support_dir_.empty())
151 lyx_binary_ = FileName(addName(abs_lyx_dir,
152 "lyx" + string(PROGRAM_SUFFIX)));
154 lyx_binary_ = FileName(addName(abs_lyx_dir, "lyx"));
156 locale_dir_ = get_locale_dir(system_support_dir_);
158 FileName const default_user_support_dir =
159 get_default_user_support_dir(home_dir_);
161 explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
162 command_line_user_support_dir, user_support_dir_);
164 FileName const configure_script(addName(system_support().absFileName(), "configure.py"));
165 configure_command_ = os::python() + ' ' +
166 quoteName(configure_script.toFilesystemEncoding(), quote_python) +
167 with_version_suffix();
169 LYXERR(Debug::INIT, "<package>\n"
170 << "\tbinary_dir " << binary_dir().absFileName() << '\n'
171 << "\tsystem_support " << system_support().absFileName() << '\n'
172 << "\tbuild_support " << build_support().absFileName() << '\n'
173 << "\tuser_support " << user_support().absFileName() << '\n'
174 << "\tlocale_dir " << locale_dir().absFileName() << '\n'
175 << "\tdocument_dir " << document_dir().absFileName() << '\n'
176 << "\ttemp_dir " << temp_dir().absFileName() << '\n'
177 << "\thome_dir " << home_dir().absFileName() << '\n'
182 void Package::set_temp_dir(FileName const & temp_dir) const
184 if (temp_dir.empty())
185 temp_dir_ = system_temp_dir_;
187 temp_dir_ = temp_dir;
193 // These next functions contain the stuff that is substituted at
194 // configuration-time.
195 FileName const hardcoded_localedir()
198 // The build system needs to make sure that this is in utf8 encoding.
199 return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
203 FileName const hardcoded_system_support_dir()
206 // The build system needs to make sure that this is in utf8 encoding.
207 return FileName(LYX_ABS_INSTALLED_DATADIR);
211 string const & with_version_suffix()
213 static string const program_suffix = PROGRAM_SUFFIX;
214 static string const with_version_suffix =
215 " --with-version-suffix=" PROGRAM_SUFFIX;
216 return program_suffix.empty() ? program_suffix : with_version_suffix;
222 FileName const & Package::top_srcdir()
225 // The build system needs to make sure that this is in utf8 encoding.
226 static FileName const dir(LYX_ABS_TOP_SRCDIR);
233 bool check_command_line_dir(string const & dir,
235 string const & command_line_switch);
237 FileName const extract_env_var_dir(string const & env_var);
239 bool check_env_var_dir(FileName const & dir,
240 string const & env_var);
242 bool check_env_var_dir(FileName const & dir,
244 string const & env_var);
246 string const relative_locale_dir();
248 string const relative_system_support_dir();
252 * Convert \p name to internal path and strip a trailing slash, since it
253 * comes from user input (commandline or environment).
254 * \p name is encoded in utf8.
256 string const fix_dir_name(string const & name)
258 return rtrim(os::internal_path(name), "/");
262 FileName buildSupportDir(string const & binary_dir,
263 exe_build_dir_to_top_build_dir top_build_dir_location)
266 switch (top_build_dir_location) {
267 case top_build_dir_is_one_level_up:
268 indirection = "../lib";
270 case top_build_dir_is_two_levels_up:
271 indirection = "../../lib";
274 return FileName(addPath(binary_dir, indirection));
278 void buildDirs(FileName const & abs_binary,
279 exe_build_dir_to_top_build_dir top_build_dir_location,
280 FileName & build_support_dir, FileName & system_support_dir)
282 string const check_text = "Checking whether LyX is run in place...";
284 // We're looking for "Makefile" in a directory
286 // We're also looking for "chkconfig.ltx" in a directory
288 // If both are found, then we're running LyX in-place.
290 // Note that the name of the lyx binary may be a symbolic link.
291 // If that is the case, then we follow the links too.
292 FileName binary = abs_binary;
294 // Try and find "lyxrc.defaults".
295 string binary_dir = onlyPath(binary.absFileName());
296 build_support_dir = buildSupportDir(binary_dir, top_build_dir_location);
297 if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
298 // Try and find "chkconfig.ltx".
300 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
302 if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
303 LYXERR(Debug::INIT, check_text << " yes");
308 // Check whether binary is a symbolic link.
309 // If so, resolve it and repeat the exercise.
310 if (!binary.isSymLink())
314 if (readLink(binary, link)) {
317 // Unable to resolve the link.
322 LYXERR(Debug::INIT, check_text << " no");
323 system_support_dir = FileName();
324 build_support_dir = FileName();
328 // Specification of document_dir_ may be reset by LyXRC,
329 // but the default is fixed for a given OS.
330 FileName const get_document_dir(FileName const & home_dir)
332 #if defined (USE_WINDOWS_PACKAGING)
333 (void)home_dir; // Silence warning about unused variable.
334 os::GetFolderPath win32_folder_path;
335 return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
342 // The specification of home_dir_ is fixed for a given OS.
343 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
344 // and on a Posix-like machine: "/home/USERNAME".
345 FileName const get_home_dir()
347 #if defined (USE_WINDOWS_PACKAGING)
348 string const home_dir = getEnv("USERPROFILE");
350 string const home_dir = getEnv("HOME");
353 return FileName(fix_dir_name(home_dir));
357 // Several sources are probed to ascertain the locale directory.
358 // The only requirement is that the result is indeed a directory.
359 FileName const get_locale_dir(FileName const & system_support_dir)
361 // 1. Use the "LYX_LOCALEDIR" environment variable.
362 FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
363 if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
366 // 2. Search for system_support_dir / <relative locale dir>
367 // The <relative locale dir> is OS-dependent. (On Unix, it will
369 FileName path(addPath(system_support_dir.absFileName(),
370 relative_locale_dir()));
372 if (path.exists() && path.isDirectory())
375 // 3. Fall back to the hard-coded LOCALEDIR.
376 path = hardcoded_localedir();
377 if (path.exists() && path.isDirectory())
384 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
385 FileName const abs_path_from_command_line(string const & command_line)
387 if (command_line.empty())
390 string const str_path = fix_dir_name(command_line);
391 return makeAbsPath(str_path);
395 // Does the grunt work for abs_path_from_binary_name()
396 FileName const get_binary_path(string const & exe)
398 #if defined (USE_WINDOWS_PACKAGING)
399 // The executable may have been invoked either with or
400 // without the .exe extension.
401 // Ensure that it is present.
402 string const as_internal_path = os::internal_path(exe);
403 string const exe_path = suffixIs(as_internal_path, ".exe") ?
404 as_internal_path : as_internal_path + ".exe";
406 string const exe_path = os::internal_path(exe);
408 if (FileName::isAbsolute(exe_path))
409 return FileName(exe_path);
411 // Two possibilities present themselves.
412 // 1. The binary is relative to the CWD.
413 FileName const abs_exe_path = makeAbsPath(exe_path);
414 if (abs_exe_path.exists())
417 // 2. exe must be the name of the binary only and it
418 // can be found on the PATH.
419 string const exe_name = onlyFileName(exe_path);
420 if (exe_name != exe_path)
423 vector<string> const path = getEnvPath("PATH");
424 vector<string>::const_iterator it = path.begin();
425 vector<string>::const_iterator const end = path.end();
426 for (; it != end; ++it) {
427 // This will do nothing if *it is already absolute.
428 string const exe_dir = makeAbsPath(*it).absFileName();
430 FileName const exe_path(addName(exe_dir, exe_name));
431 if (exe_path.exists())
435 // Didn't find anything.
440 // Extracts the absolute path to the binary name received as argv[0].
441 FileName const abs_path_from_binary_name(string const & exe)
443 FileName const abs_binary = get_binary_path(exe);
444 if (abs_binary.empty()) {
446 throw ExceptionMessage(ErrorException,
447 _("LyX binary not found"),
448 bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
455 // A plethora of directories is searched to ascertain the system
456 // lyxdir which is defined as the first directory to contain
459 get_system_support_dir(FileName const & abs_binary,
460 string const & command_line_system_support_dir)
462 string const chkconfig_ltx = "chkconfig.ltx";
464 // searched_dirs is used for diagnostic purposes only in the case
465 // that "chkconfig.ltx" is not found.
466 list<FileName> searched_dirs;
468 // 1. Use the -sysdir command line parameter.
469 FileName path = abs_path_from_command_line(command_line_system_support_dir);
471 searched_dirs.push_back(path);
472 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
476 // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
477 path = extract_env_var_dir(LYX_DIR_VER);
479 searched_dirs.push_back(path);
480 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
484 // 3. Search relative to the lyx binary.
485 // We're looking for "chkconfig.ltx" in a directory
486 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
487 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
488 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
489 string const relative_lyxdir = relative_system_support_dir();
491 // One subtlety to be aware of. The name of the lyx binary may be
492 // a symbolic link. If that is the case, then we follow the links too.
493 FileName binary = abs_binary;
495 // Try and find "chkconfig.ltx".
496 string const binary_dir = onlyPath(binary.absFileName());
498 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
499 searched_dirs.push_back(lyxdir);
501 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
502 // Success! "chkconfig.ltx" has been found.
506 // Check whether binary is a symbolic link.
507 // If so, resolve it and repeat the exercise.
508 if (!binary.isSymLink())
512 if (readLink(binary, link)) {
515 // Unable to resolve the link.
520 // 4. Repeat the exercise on the directory itself.
521 FileName binary_dir(onlyPath(abs_binary.absFileName()));
523 // This time test whether the directory is a symbolic link
524 // *before* looking for "chkconfig.ltx".
525 // (We've looked relative to the original already.)
526 if (!binary.isSymLink())
530 if (readLink(binary_dir, link)) {
533 // Unable to resolve the link.
537 // Try and find "chkconfig.ltx".
538 FileName const lyxdir(addPath(binary_dir.absFileName(),
540 searched_dirs.push_back(lyxdir);
542 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
543 // Success! "chkconfig.ltx" has been found.
548 // 5. In desparation, try the hard-coded system support dir.
549 path = hardcoded_system_support_dir();
550 if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
553 // Everything has failed :-(
554 // So inform the user and exit.
555 string searched_dirs_str;
556 typedef list<FileName>::const_iterator iterator;
557 iterator const begin = searched_dirs.begin();
558 iterator const end = searched_dirs.end();
559 for (iterator it = begin; it != end; ++it) {
561 searched_dirs_str += "\n\t";
562 searched_dirs_str += it->absFileName();
566 throw ExceptionMessage(ErrorException, _("No system directory"),
567 bformat(_("Unable to determine the system directory "
570 "Use the '-sysdir' command line parameter or "
571 "set the environment variable\n%2$s "
572 "to the LyX system directory containing the "
573 "file `chkconfig.ltx'."),
574 from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
576 // Keep the compiler happy.
581 // Returns the absolute path to the user lyxdir, together with a flag
582 // indicating whether this directory was specified explicitly (as -userdir
583 // or through an environment variable) or whether it was deduced.
584 bool userSupportDir(FileName const & default_user_support_dir,
585 string const & command_line_user_support_dir, FileName & result)
587 // 1. Use the -userdir command line parameter.
588 result = abs_path_from_command_line(command_line_user_support_dir);
592 // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
593 result = extract_env_var_dir(LYX_USERDIR_VER);
597 // 3. Use the OS-dependent default_user_support_dir
598 result = default_user_support_dir;
603 // $HOME/.lyx on POSIX but on Win32 it will be something like
604 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
605 FileName const get_default_user_support_dir(FileName const & home_dir)
607 #if defined (USE_WINDOWS_PACKAGING)
608 (void)home_dir; // Silence warning about unused variable.
610 os::GetFolderPath win32_folder_path;
611 return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
613 #elif defined (USE_MACOSX_PACKAGING)
614 (void)home_dir; // Silence warning about unused variable.
617 OSErr const error_code =
618 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
619 kDontCreateFolder, &fsref);
623 // FSRefMakePath returns the result in utf8
624 char store[PATH_MAX + 1];
625 OSStatus const status_code =
626 FSRefMakePath(&fsref,
627 reinterpret_cast<UInt8*>(store), PATH_MAX);
628 if (status_code != 0)
631 return FileName(addPath(reinterpret_cast<char const *>(store), PACKAGE));
633 #else // USE_POSIX_PACKAGING
634 return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
639 // Check that directory @c dir contains @c file.
640 // Else emit an error message about an invalid @c command_line_switch.
641 bool check_command_line_dir(string const & dir,
643 string const & command_line_switch)
645 FileName const abs_path = fileSearch(dir, file);
646 if (abs_path.empty()) {
648 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
649 _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
650 from_utf8(command_line_switch), from_utf8(dir),
654 return !abs_path.empty();
658 // The environment variable @c env_var expands to a (single) file path.
659 FileName const extract_env_var_dir(string const & env_var)
661 string const dir = fix_dir_name(getEnv(env_var));
662 return dir.empty() ? FileName() : makeAbsPath(dir);
666 // Check that directory @c dir contains @c file.
667 // Else emit a warning about an invalid @c env_var.
668 bool check_env_var_dir(FileName const & dir,
670 string const & env_var)
672 FileName const abs_path = fileSearch(dir.absFileName(), file);
673 if (abs_path.empty()) {
675 throw ExceptionMessage(WarningException, _("File not found"), bformat(
676 _("Invalid %1$s environment variable.\n"
677 "Directory %2$s does not contain %3$s."),
678 from_utf8(env_var), from_utf8(dir.absFileName()),
682 return !abs_path.empty();
686 // Check that directory @c dir is indeed a directory.
687 // Else emit a warning about an invalid @c env_var.
688 bool check_env_var_dir(FileName const & dir,
689 string const & env_var)
691 bool const success = dir.exists() && dir.isDirectory();
694 // Put this string on a single line so that the gettext
695 // search mechanism in po/Makefile.in.in will register
696 // Package.cpp.in as a file containing strings that need
699 docstring const fmt =
700 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
702 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
703 fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
710 // The locale directory relative to the LyX system directory.
711 string const relative_locale_dir()
713 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
721 // The system lyxdir is relative to the directory containing the LyX binary.
722 string const relative_system_support_dir()
726 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
727 result = "../Resources/";
729 result = addPath("../share/", PACKAGE);
737 } // namespace support