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.
11 * Warning! This file is autogenerated from package.C.in.
12 * All changes to this file will be lost.
17 #include "support/package.h"
22 #include "support/environment.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
27 #include <boost/assert.hpp>
28 #include <boost/filesystem/operations.hpp>
29 #include <boost/tuple/tuple.hpp>
34 #if !defined (USE_WINDOWS_PACKAGING) && \
35 !defined (USE_MACOSX_PACKAGING) && \
36 !defined (USE_POSIX_PACKAGING)
37 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
40 #if defined (USE_WINDOWS_PACKAGING)
43 * MinGW's version of winver.h contains this comment:
45 * If you need Win32 API features newer the Win95 and WinNT then you must
46 * define WINVER before including windows.h or any other method of including
47 * the windef.h header.
49 * GetLongPathNameA requires WINVER == 0x0500.
51 * It doesn't matter if the Windows version is older than this because the
52 * function will compile but will fail at run time. See
53 * http://msdn.microsoft.com/library/en-us/mslu/winprog/microsoft_layer_for_unicode_apis_with_limited_support.asp
55 # if defined(__MINGW32__)
56 # define WINVER 0x0500
60 # include <shlobj.h> // SHGetFolderPath
63 # ifndef SHGFP_TYPE_CURRENT
64 # define SHGFP_TYPE_CURRENT 0
67 #elif defined (USE_MACOSX_PACKAGING)
68 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
73 namespace fs = boost::filesystem;
81 bool initialised_ = false;
86 void init_package(string const & command_line_arg0,
87 string const & command_line_system_support_dir,
88 string const & command_line_user_support_dir,
89 exe_build_dir_to_top_build_dir top_build_dir_location)
91 // Can do so only once.
95 package_ = Package(command_line_arg0,
96 command_line_system_support_dir,
97 command_line_user_support_dir,
98 top_build_dir_location);
103 Package const & package()
105 // Commented out because package().locale_dir() can be called
106 // from the message translation code in messages.C before
107 // init_package() is called. Lars is on the case...
108 // BOOST_ASSERT(initialised_);
115 string const abs_path_from_binary_name(string const & exe);
117 std::pair<string, string> const
118 get_build_dirs(string const & abs_binary,
119 exe_build_dir_to_top_build_dir top_build_dir_location);
121 string const get_document_dir(string const & home_dir);
123 string const get_home_dir();
125 string const get_locale_dir(string const & system_support_dir);
127 string const get_system_support_dir(string const & abs_binary,
128 string const & command_line_system_support_dir);
130 string const get_temp_dir();
132 string const get_default_user_support_dir(string const & home_dir);
134 std::pair<string, bool> const
135 get_user_support_dir(string const & default_user_support_dir,
136 string const & command_line_user_support_dir);
141 Package::Package(string const & command_line_arg0,
142 string const & command_line_system_support_dir,
143 string const & command_line_user_support_dir,
144 exe_build_dir_to_top_build_dir top_build_dir_location)
145 : explicit_user_support_dir_(false)
147 home_dir_ = get_home_dir();
148 temp_dir_ = get_temp_dir();
149 document_dir_ = get_document_dir(home_dir_);
151 string const abs_binary = abs_path_from_binary_name(command_line_arg0);
152 binary_dir_ = OnlyPath(abs_binary);
154 // Is LyX being run in-place from the build tree?
155 boost::tie(build_support_dir_, system_support_dir_) =
156 get_build_dirs(abs_binary, top_build_dir_location);
158 if (build_support_dir_.empty())
159 system_support_dir_ =
160 get_system_support_dir(abs_binary,
161 command_line_system_support_dir);
163 locale_dir_ = get_locale_dir(system_support_dir_);
165 string const default_user_support_dir =
166 get_default_user_support_dir(home_dir_);
167 boost::tie(user_support_dir_, explicit_user_support_dir_) =
168 get_user_support_dir(default_user_support_dir,
169 command_line_user_support_dir);
173 << "\tbinary_dir " << binary_dir() << '\n'
174 << "\tsystem_support " << system_support() << '\n'
175 << "\tbuild_support " << build_support() << '\n'
176 << "\tuser_support " << user_support() << '\n'
177 << "\tlocale_dir " << locale_dir() << '\n'
178 << "\tdocument_dir " << document_dir() << '\n'
179 << "\ttemp_dir " << temp_dir() << '\n'
180 << "\thome_dir " << home_dir() << '\n'
181 << "</package>\n" << std::endl;
187 // These next three functions contain the stuff that is substituted at
188 // configuration-time.
189 string const top_srcdir()
191 static string const dir("%TOP_SRCDIR%");
196 string const hardcoded_localedir()
198 return string("%LOCALEDIR%");
202 string const hardcoded_system_support_dir()
204 return string("%LYX_DIR%");
210 string const & Package::top_srcdir() const
212 static string const dir("%TOP_SRCDIR%");
219 bool check_command_line_dir(string const & dir,
221 string const & command_line_switch);
223 string const extract_env_var_dir(string const & env_var);
225 bool check_env_var_dir(string const & dir,
226 string const & env_var);
228 bool check_env_var_dir(string const & dir,
230 string const & env_var);
232 string const relative_locale_dir();
234 string const relative_system_support_dir();
237 #if defined (USE_WINDOWS_PACKAGING)
238 // Given a folder ID, returns the folder name (in unix-style format).
239 // Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
240 string const win32_folder_path(int folder_id)
242 char folder_path[PATH_MAX + 1];
243 if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
244 SHGFP_TYPE_CURRENT, folder_path)))
245 return os::internal_path(folder_path);
252 get_build_support_dir(std::string const & binary_dir,
253 exe_build_dir_to_top_build_dir top_build_dir_location)
256 switch (top_build_dir_location) {
257 case top_build_dir_is_one_level_up:
258 indirection = "../lib";
260 case top_build_dir_is_two_levels_up:
261 indirection = "../../lib";
265 return NormalizePath(AddPath(binary_dir, indirection));
269 std::pair<string, string> const
270 get_build_dirs(string const & abs_binary,
271 exe_build_dir_to_top_build_dir top_build_dir_location)
273 string const check_text = "Checking whether LyX is run in place...";
275 // We're looking for "lyxrc.defaults" in a directory
277 // We're also looking for "chkconfig.ltx" in a directory
278 // binary_dir/top_srcdir()/lib
279 // If both are found, then we're running LyX in-place.
281 // Note that the name of the lyx binary may be a symbolic link.
282 // If that is the case, then we follow the links too.
283 string binary = abs_binary;
285 // Try and find "lyxrc.defaults".
286 string const binary_dir = OnlyPath(binary);
287 string const build_support_dir =
288 get_build_support_dir(binary_dir, top_build_dir_location);
290 if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) {
291 // Try and find "chkconfig.ltx".
292 string const system_support_dir =
293 MakeAbsPath(AddPath(top_srcdir(), "lib"),
294 AddPath(binary_dir, "support"));
296 if (!FileSearch(system_support_dir, "chkconfig.ltx").empty()) {
297 lyxerr[Debug::INIT] << check_text << " yes"
299 return std::make_pair(build_support_dir, system_support_dir);
303 // Check whether binary is a symbolic link.
304 // If so, resolve it and repeat the exercise.
305 if (!fs::symbolic_link_exists(binary))
309 if (LyXReadLink(binary, link, true)) {
312 // Unable to resolve the link.
317 lyxerr[Debug::INIT] << check_text << " no" << std::endl;
318 return std::make_pair(string(), string());
322 // Specification of document_dir_ may be reset by LyXRC,
323 // but the default is fixed for a given OS.
324 string const get_document_dir(string const & home_dir)
326 #if defined (USE_WINDOWS_PACKAGING)
327 (void)home_dir; // Silence warning about unused variable.
328 return win32_folder_path(CSIDL_PERSONAL);
335 // The specification of home_dir_ is fixed for a given OS.
336 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
337 // and on a Posix-like machine: "/home/USERNAME".
338 string const get_home_dir()
340 #if defined (USE_WINDOWS_PACKAGING)
341 string const home_dir = getEnv("USERPROFILE");
343 string const home_dir = getEnv("HOME");
346 return os::internal_path(home_dir);
350 // Several sources are probed to ascertain the locale directory.
351 // The only requirement is that the result is indeed a directory.
352 string const get_locale_dir(string const & system_support_dir)
354 // 1. Use the "LYX_LOCALEDIR" environment variable.
355 string path = extract_env_var_dir("LYX_LOCALEDIR");
356 if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR"))
359 // 2. Search for system_support_dir / <relative locale dir>
360 // The <relative locale dir> is OS-dependent. (On Unix, it will
362 path = NormalizePath(AddPath(system_support_dir, relative_locale_dir()));
364 if (fs::exists(path) && fs::is_directory(path))
367 // 3. Fall back to the hard-coded LOCALEDIR.
368 path = hardcoded_localedir();
369 if (fs::exists(path) && fs::is_directory(path))
376 // Specification of temp_dir_ may be reset by LyXRC,
377 // but the default is fixed for a given OS.
378 string const get_temp_dir()
380 #if defined (USE_WINDOWS_PACKAGING)
381 // Typical example: C:/TEMP/.
382 char path[PATH_MAX + 1];
383 GetTempPath(PATH_MAX, path);
384 GetLongPathName(path, path, PATH_MAX + 1);
385 return os::internal_path(path);
392 // If we use a 'lazy' lyxerr in the hope of setting the locale before
393 // printing any messages, then we should ensure that it is flushed first.
396 #ifndef CXX_GLOBAL_CSTD
403 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
404 string const abs_path_from_command_line(string const & command_line)
406 if (command_line.empty())
409 string const path = os::internal_path(command_line);
410 return os::is_absolute_path(path) ? path : MakeAbsPath(path);
414 // Does the grunt work for abs_path_from_binary_name()
415 string const get_binary_path(string const & exe)
417 #if defined (USE_WINDOWS_PACKAGING)
418 // The executable may have been invoked either with or
419 // without the .exe extension.
420 // Ensure that it is present.
421 string const as_internal_path = os::internal_path(exe);
422 string const exe_path = suffixIs(as_internal_path, ".exe") ?
423 as_internal_path : as_internal_path + ".exe";
425 string const exe_path = os::internal_path(exe);
427 if (os::is_absolute_path(exe_path))
430 // Two possibilities present themselves.
431 // 1. The binary is relative to the CWD.
432 string const abs_exe_path = MakeAbsPath(exe_path);
433 if (fs::exists(abs_exe_path))
436 // 2. exe must be the name of the binary only and it
437 // can be found on the PATH.
438 string const exe_name = OnlyFilename(exe_path);
439 if (exe_name != exe_path)
442 std::vector<string> const path = getEnvPath("PATH");
443 std::vector<string>::const_iterator it = path.begin();
444 std::vector<string>::const_iterator const end = path.end();
445 for (; it != end; ++it) {
446 // This will do nothing if *it is already absolute.
447 string const exe_dir = MakeAbsPath(*it);
449 string const exe_path = AddName(exe_dir, exe_name);
450 if (fs::exists(exe_path))
454 // Didn't find anything.
459 // Extracts the absolute path to the binary name received as argv[0].
460 string const abs_path_from_binary_name(string const & exe)
462 string const abs_binary = get_binary_path(exe);
463 if (abs_binary.empty()) {
464 lyxerr << bformat(_("Unable to determine the path to the "
465 "LyX binary from the command line %1$s"),
474 // A plethora of directories is searched to ascertain the system
475 // lyxdir which is defined as the first directory to contain
478 get_system_support_dir(string const & abs_binary,
479 string const & command_line_system_support_dir)
481 string const chkconfig_ltx = "chkconfig.ltx";
483 // searched_dirs is used for diagnostic purposes only in the case
484 // that "chkconfig.ltx" is not found.
485 std::list<string> searched_dirs;
487 // 1. Use the -sysdir command line parameter.
488 string path = abs_path_from_command_line(command_line_system_support_dir);
490 searched_dirs.push_back(path);
491 if (check_command_line_dir(path, chkconfig_ltx, "-sysdir"))
495 // 2. Use the "LYX_DIR_13x" environment variable.
496 path = extract_env_var_dir("LYX_DIR_13x");
498 searched_dirs.push_back(path);
499 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_13x"))
503 // 3. Search relative to the lyx binary.
504 // We're looking for "chkconfig.ltx" in a directory
505 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
506 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
507 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
508 string const relative_lyxdir = relative_system_support_dir();
510 // One subtlety to be aware of. The name of the lyx binary may be
511 // a symbolic link. If that is the case, then we follow the links too.
512 string binary = abs_binary;
514 // Try and find "chkconfig.ltx".
515 string const binary_dir = OnlyPath(binary);
517 string const lyxdir =
518 NormalizePath(AddPath(binary_dir, relative_lyxdir));
519 searched_dirs.push_back(lyxdir);
521 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
522 // Success! "chkconfig.ltx" has been found.
526 // Check whether binary is a symbolic link.
527 // If so, resolve it and repeat the exercise.
528 if (!fs::symbolic_link_exists(binary))
532 if (LyXReadLink(binary, link, true)) {
535 // Unable to resolve the link.
540 // 4. Repeat the exercise on the directory itself.
541 string binary_dir = OnlyPath(abs_binary);
543 // This time test whether the directory is a symbolic link
544 // *before* looking for "chkconfig.ltx".
545 // (We've looked relative to the original already.)
546 if (!fs::symbolic_link_exists(binary))
550 if (LyXReadLink(binary_dir, link, true)) {
553 // Unable to resolve the link.
557 // Try and find "chkconfig.ltx".
558 string const lyxdir =
559 NormalizePath(AddPath(binary_dir, relative_lyxdir));
560 searched_dirs.push_back(lyxdir);
562 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
563 // Success! "chkconfig.ltx" has been found.
568 // 5. In desparation, try the hard-coded system support dir.
569 path = hardcoded_system_support_dir();
570 if (!FileSearch(path, chkconfig_ltx).empty())
573 // Everything has failed :-(
574 // So inform the user and exit.
575 string searched_dirs_str;
576 typedef std::list<string>::const_iterator iterator;
577 iterator const begin = searched_dirs.begin();
578 iterator const end = searched_dirs.end();
579 for (iterator it = begin; it != end; ++it) {
581 searched_dirs_str += "\n\t";
582 searched_dirs_str += *it;
585 lyxerr << bformat(_("Unable to determine the system directory "
588 "Try the '-sysdir' command line parameter or "
589 "set the environment variable LYX_DIR_13x to "
590 "the LyX system directory containing the file "
596 // Keep the compiler happy.
601 // Returns the absolute path to the user lyxdir, together with a flag
602 // indicating whether this directory was specified explicitly (as -userdir
603 // or through an environment variable) or whether it was deduced.
604 std::pair<string, bool> const
605 get_user_support_dir(string const & default_user_support_dir,
606 string const & command_line_user_support_dir)
608 bool explicit_userdir = true;
610 // 1. Use the -userdir command line parameter.
612 abs_path_from_command_line(command_line_user_support_dir);
614 return std::make_pair(path, explicit_userdir);
616 // 2. Use the LYX_USERDIR_13x environment variable.
617 path = extract_env_var_dir("LYX_USERDIR_13x");
619 return std::make_pair(path, explicit_userdir);
621 // 3. Use the OS-dependent default_user_support_dir
622 explicit_userdir = false;
623 return std::make_pair(default_user_support_dir, explicit_userdir);
627 // $HOME/.lyx on all platforms but Win32 where it will be something like
628 // "C:/Documents and Settings/USERNAME/Application Data/lyx"
629 string const get_default_user_support_dir(string const & home_dir)
631 #if defined (USE_WINDOWS_PACKAGING)
632 (void)home_dir; // Silence warning about unused variable.
634 return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
636 #elif defined (USE_MACOSX_PACKAGING)
637 (void)home_dir; // Silence warning about unused variable.
640 OSErr const error_code =
641 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
642 kDontCreateFolder, &fsref);
646 char store[PATH_MAX + 1];
647 OSStatus const status_code =
648 FSRefMakePath(&fsref,
649 reinterpret_cast<UInt8*>(store), PATH_MAX);
650 if (status_code != 0)
653 return AddPath(reinterpret_cast<char const *>(store), PACKAGE);
655 #else // USE_POSIX_PACKAGING
656 return AddPath(home_dir, string(".") + PACKAGE);
661 // Check that directory @c dir contains @c file.
662 // Else emit a warning about an invalid @c command_line_switch.
663 bool check_command_line_dir(string const & dir,
665 string const & command_line_switch)
667 string const abs_path = FileSearch(dir, file);
668 if (abs_path.empty()) {
669 lyxerr << bformat(_("Invalid %1$s switch.\n"
670 "Directory %2$s does not contain %3$s."),
671 command_line_switch, dir, file)
675 return !abs_path.empty();
679 // The environment variable @c env_var expands to a (single) file path.
680 string const extract_env_var_dir(string const & env_var)
682 string const dir = os::internal_path(getEnv(env_var));
683 return dir.empty() ? dir : 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(string const & dir,
691 string const & env_var)
693 string const abs_path = FileSearch(dir, file);
694 if (abs_path.empty()) {
695 lyxerr << bformat(_("Invalid %1$s environment variable.\n"
696 "Directory %2$s does not contain %3$s."),
701 return !abs_path.empty();
705 // Check that directory @c dir is indeed a directory.
706 // Else emit a warning about an invalid @c env_var.
707 bool check_env_var_dir(string const & dir,
708 string const & env_var)
710 bool const success = (fs::exists(dir) && fs::is_directory(dir));
713 // Put this string on a single line so that the gettext
714 // search mechanism in po/Makefile.in.in will register
715 // package.C.in as a file containing strings that need
718 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
720 lyxerr << bformat(fmt, env_var, dir)
728 // The locale directory relative to the LyX system directory.
729 string const relative_locale_dir()
735 // The system lyxdir is relative to the directory containing the LyX binary.
736 string const relative_system_support_dir()
740 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
741 result = AddPath("../Resources/", PACKAGE);
743 result = AddPath("../share/", PACKAGE);
751 } // namespace support