]> git.lyx.org Git - lyx.git/blobdiff - src/support/package.C.in
make "make distcheck" work
[lyx.git] / src / support / package.C.in
index 7ffb387519356dd660ce713a2c3405a7c431491d..9a9a2afc7b6fe0a6468fcab8dd24b6b9aca281cb 100644 (file)
 #include "debug.h"
 #include "gettext.h"
 
-#include "support/FileInfo.h"
+#include "support/environment.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
 #include "support/os.h"
 
 #include <boost/assert.hpp>
+#include <boost/filesystem/operations.hpp>
 #include <boost/tuple/tuple.hpp>
 
 #include <list>
@@ -51,6 +52,7 @@
 
 using std::string;
 
+namespace fs = boost::filesystem;
 
 namespace lyx {
 namespace support {
@@ -65,7 +67,8 @@ bool initialised_ = false;
 
 void init_package(string const & command_line_arg0,
                  string const & command_line_system_support_dir,
-                 string const & command_line_user_support_dir)
+                 string const & command_line_user_support_dir,
+                 exe_build_dir_to_top_build_dir top_build_dir_location)
 {
        // Can do so only once.
        if (initialised_)
@@ -73,13 +76,17 @@ void init_package(string const & command_line_arg0,
 
        package_ = Package(command_line_arg0,
                           command_line_system_support_dir,
-                          command_line_user_support_dir);
+                          command_line_user_support_dir,
+                          top_build_dir_location);
        initialised_ = true;
 }
 
 
 Package const & package()
 {
+       // Commented out because package().locale_dir() can be called
+       // from the message translation code in messages.C before
+       // init_package() is called. Lars is on the case...
        // BOOST_ASSERT(initialised_);
        return package_;
 }
@@ -89,7 +96,9 @@ namespace {
 
 string const abs_path_from_binary_name(string const & exe);
 
-std::pair<string, string> const get_build_dirs(string const & abs_binary);
+std::pair<string, string> const
+get_build_dirs(string const & abs_binary,
+              exe_build_dir_to_top_build_dir top_build_dir_location);
 
 string const get_document_dir(string const & home_dir);
 
@@ -113,7 +122,8 @@ get_user_support_dir(string const & default_user_support_dir,
 
 Package::Package(string const & command_line_arg0,
                 string const & command_line_system_support_dir,
-                string const & command_line_user_support_dir)
+                string const & command_line_user_support_dir,
+                exe_build_dir_to_top_build_dir top_build_dir_location)
        : explicit_user_support_dir_(false)
 {
        home_dir_ = get_home_dir();
@@ -125,7 +135,7 @@ Package::Package(string const & command_line_arg0,
 
        // Is LyX being run in-place from the build tree?
        boost::tie(build_support_dir_, system_support_dir_) =
-               get_build_dirs(abs_binary);
+               get_build_dirs(abs_binary, top_build_dir_location);
 
        if (build_support_dir_.empty())
                system_support_dir_ =
@@ -150,7 +160,7 @@ Package::Package(string const & command_line_arg0,
                << "\tdocument_dir " << document_dir() << '\n'
                << "\ttemp_dir " << temp_dir() << '\n'
                << "\thome_dir " << home_dir() << '\n'
-               << "<\\package>\n" << std::endl;
+               << "</package>\n" << std::endl;
 }
 
 
@@ -220,7 +230,27 @@ string const win32_folder_path(int folder_id)
 #endif
 
 
-std::pair<string, string> const get_build_dirs(string const & abs_binary)
+std::string const
+get_build_support_dir(std::string const & binary_dir,
+                     exe_build_dir_to_top_build_dir top_build_dir_location)
+{
+       string indirection;
+       switch (top_build_dir_location) {
+       case top_build_dir_is_one_level_up:
+               indirection = "../lib";
+               break;
+       case top_build_dir_is_two_levels_up:
+               indirection = "../../lib";
+               break;
+       }
+
+       return NormalizePath(AddPath(binary_dir, indirection));
+}
+
+
+std::pair<string, string> const
+get_build_dirs(string const & abs_binary,
+              exe_build_dir_to_top_build_dir top_build_dir_location)
 {
        string const check_text = "Checking whether LyX is run in place...";
 
@@ -237,7 +267,7 @@ std::pair<string, string> const get_build_dirs(string const & abs_binary)
                // Try and find "lyxrc.defaults".
                string const binary_dir = OnlyPath(binary);
                string const build_support_dir =
-                       NormalizePath(AddPath(binary_dir, "../lib"));
+                       get_build_support_dir(binary_dir, top_build_dir_location);
 
                if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) {
                        // Try and find "chkconfig.ltx".
@@ -254,8 +284,7 @@ std::pair<string, string> const get_build_dirs(string const & abs_binary)
 
                // Check whether binary is a symbolic link.
                // If so, resolve it and repeat the exercise.
-               FileInfo const file(binary, true);
-               if (!file.isOK() || !file.isLink())
+               if (!fs::symbolic_link_exists(binary))
                        break;
 
                string link;
@@ -291,9 +320,9 @@ string const get_document_dir(string const & home_dir)
 string const get_home_dir()
 {
 #if defined (USE_WINDOWS_PACKAGING)
-       string const home_dir = GetEnv("USERPROFILE");
+       string const home_dir = getEnv("USERPROFILE");
 #else // Posix-like.
-       string const home_dir = GetEnv("HOME");
+       string const home_dir = getEnv("HOME");
 #endif
 
        return os::internal_path(home_dir);
@@ -314,14 +343,12 @@ string const get_locale_dir(string const & system_support_dir)
        // be "../locale/".)
        path = NormalizePath(AddPath(system_support_dir, relative_locale_dir()));
 
-       FileInfo fi(path);
-       if (fi.isOK() && fi.isDir())
+       if (fs::exists(path) && fs::is_directory(path))
                return path;
 
        // 3. Fall back to the hard-coded LOCALEDIR.
        path = hardcoded_localedir();
-       FileInfo fi2(path);
-       if (fi2.isOK() && fi2.isDir())
+       if (fs::exists(path) && fs::is_directory(path))
                return path;
 
        return string();
@@ -375,7 +402,7 @@ string const get_binary_path(string const & exe)
        // Two possibilities present themselves.
        // 1. The binary is relative to the CWD.
        string const abs_exe_path = MakeAbsPath(exe_path);
-       if (FileInfo(abs_exe_path, true).isOK())
+       if (fs::exists(abs_exe_path))
                return abs_exe_path;
 
        // 2. exe must be the name of the binary only and it
@@ -388,12 +415,11 @@ string const get_binary_path(string const & exe)
        std::vector<string>::const_iterator it = path.begin();
        std::vector<string>::const_iterator const end = path.end();
        for (; it != end; ++it) {
-               if (!os::is_absolute_path(*it))
-                       // Someone is playing silly buggers.
-                       continue;
+               // This will do nothing if *it is already absolute.
+               string const exe_dir = MakeAbsPath(*it);
 
-               string const exe_path = AddName(*it, exe_name);
-               if (FileInfo(exe_path, true).isOK())
+               string const exe_path = AddName(exe_dir, exe_name);
+               if (fs::exists(exe_path))
                        return exe_path;
        }
 
@@ -408,7 +434,7 @@ string const abs_path_from_binary_name(string const & exe)
        string const abs_binary = get_binary_path(exe);
        if (abs_binary.empty()) {
                lyxerr << bformat(_("Unable to determine the path to the "
-                                   "LyX binary from the command line %1%"),
+                                   "LyX binary from the command line %1$s"),
                                  exe)
                       << std::endl;
                bail_out();
@@ -471,8 +497,7 @@ get_system_support_dir(string const & abs_binary,
 
                // Check whether binary is a symbolic link.
                // If so, resolve it and repeat the exercise.
-               FileInfo const file(binary, true);
-               if (!file.isOK() || !file.isLink())
+               if (!fs::symbolic_link_exists(binary))
                        break;
 
                string link;
@@ -490,8 +515,7 @@ get_system_support_dir(string const & abs_binary,
                // This time test whether the directory is a symbolic link
                // *before* looking for "chkconfig.ltx".
                // (We've looked relative to the original already.)
-               FileInfo const file(binary_dir, true);
-               if (!file.isOK() || !file.isLink())
+               if (!fs::symbolic_link_exists(binary))
                        break;
 
                string link;
@@ -532,7 +556,7 @@ get_system_support_dir(string const & abs_binary,
 
        lyxerr << bformat(_("Unable to determine the system directory "
                            "having searched\n"
-                           "\t%1%\n"
+                           "\t%1$s\n"
                            "Try the '-sysdir' command line parameter or "
                            "set the environment variable LYX_DIR_13x to "
                            "the LyX system directory containing the file "
@@ -614,8 +638,8 @@ bool check_command_line_dir(string const & dir,
 {
        string const abs_path = FileSearch(dir, file);
        if (abs_path.empty()) {
-               lyxerr << bformat(_("Invalid %1% switch.\n"
-                                   "Directory %2% does not contain %3%."),
+               lyxerr << bformat(_("Invalid %1$s switch.\n"
+                                   "Directory %2$s does not contain %3$s."),
                                  command_line_switch, dir, file)
                       << std::endl;
        }
@@ -627,7 +651,7 @@ bool check_command_line_dir(string const & dir,
 // The environment variable @c env_var expands to a (single) file path.
 string const extract_env_var_dir(string const & env_var)
 {
-       string const dir = os::internal_path(GetEnv(env_var));
+       string const dir = os::internal_path(getEnv(env_var));
        return dir.empty() ? dir : MakeAbsPath(dir);
 }
 
@@ -640,8 +664,8 @@ bool check_env_var_dir(string const & dir,
 {
        string const abs_path = FileSearch(dir, file);
        if (abs_path.empty()) {
-               lyxerr << bformat(_("Invalid %1% environment variable.\n"
-                                   "Directory %2% does not contain %3%."),
+               lyxerr << bformat(_("Invalid %1$s environment variable.\n"
+                                   "Directory %2$s does not contain %3$s."),
                                  env_var, dir, file)
                       << std::endl;
        }
@@ -655,8 +679,7 @@ bool check_env_var_dir(string const & dir,
 bool check_env_var_dir(string const & dir,
                       string const & env_var)
 {
-       FileInfo fi(dir);
-       bool const success = (fi.isOK() && fi.isDir());
+       bool const success = (fs::exists(dir) && fs::is_directory(dir));
 
        if (!success) {
                // Put this string on a single line so that the gettext
@@ -664,8 +687,8 @@ bool check_env_var_dir(string const & dir,
                // package.C.in as a file containing strings that need
                // translation.
                string const fmt =
-               _("Invalid %1% environment variable.\n%2% is not a directory.");
-               
+               _("Invalid %1$s environment variable.\n%2$s is not a directory.");
+
                lyxerr << bformat(fmt, env_var, dir)
                       << std::endl;
        }