]> git.lyx.org Git - lyx.git/blobdiff - src/support/Package.cpp
Add quote style information to languages
[lyx.git] / src / support / Package.cpp
index 6939dc77c7aae9c592c9d2c2b9f98ab556cc9410..ec08a55b7be91ff6058a6185a83ecea416f662f5 100644 (file)
@@ -18,6 +18,7 @@
 #include "support/ExceptionMessage.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/lassert.h"
 #include "support/lstrings.h"
 #include "support/os.h"
 
@@ -53,27 +54,18 @@ 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,
-                 exe_build_dir_to_top_build_dir top_build_dir_location)
+                 string const & command_line_user_support_dir)
 {
-       // Can do so only once.
-       if (initialised_)
-               return;
-
        package_ = Package(command_line_arg0,
                           command_line_system_support_dir,
-                          command_line_user_support_dir,
-                          top_build_dir_location);
+                          command_line_user_support_dir);
        initialised_ = true;
 }
 
 
 Package const & package()
 {
-       // Commented out because package().locale_dir() can be called
-       // from the message translation code in Messages.cpp before
-       // init_package() is called. Lars is on the case...
-       // LASSERT(initialised_, /**/);
+       LASSERT(initialised_, /**/);
        return package_;
 }
 
@@ -82,13 +74,12 @@ namespace {
 
 FileName const abs_path_from_binary_name(string const & exe);
 
-void buildDirs(FileName const & abs_binary,
-       exe_build_dir_to_top_build_dir top_build_dir_location,
-       FileName &, FileName &);
 
-FileName const get_document_dir(FileName const & home_dir);
+bool inBuildDir(FileName const & abs_binary, FileName &, FileName &);
+
+FileName findLyxBinary(FileName const & abs_binary);
 
-FileName const get_home_dir();
+FileName const get_document_dir(FileName const & home_dir);
 
 FileName const get_locale_dir(FileName const & system_support_dir);
 
@@ -100,41 +91,47 @@ FileName const get_default_user_support_dir(FileName const & home_dir);
 bool userSupportDir(FileName const & default_user_support_dir,
                     string const & command_line_user_support_dir, FileName & result);
 
-
 string const & with_version_suffix();
 
+string const fix_dir_name(string const & name);
+
 } // namespace anon
 
 
 Package::Package(string const & command_line_arg0,
                 string const & command_line_system_support_dir,
-                string const & command_line_user_support_dir,
-                exe_build_dir_to_top_build_dir top_build_dir_location)
+                string const & command_line_user_support_dir)
        : explicit_user_support_dir_(false)
 {
-       home_dir_ = get_home_dir();
        // Specification of temp_dir_ may be reset by LyXRC,
        // but the default is fixed for a given OS.
        system_temp_dir_ = FileName::tempPath();
        temp_dir_ = system_temp_dir_;
-       document_dir_ = get_document_dir(home_dir_);
+       document_dir_ = get_document_dir(get_home_dir());
 
        FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
        binary_dir_ = FileName(onlyPath(abs_binary.absFileName()));
 
+       // the LyX package directory
+       lyx_dir_ = FileName(addPath(binary_dir_.absFileName(), "../"));
+       lyx_dir_ = FileName(lyx_dir_.realPath());
+
        // Is LyX being run in-place from the build tree?
-       buildDirs(abs_binary, top_build_dir_location,
-               build_support_dir_, system_support_dir_);
+       bool in_build_dir = inBuildDir(abs_binary, build_support_dir_, system_support_dir_);
 
-       if (build_support_dir_.empty())
+       if (!in_build_dir) {
                system_support_dir_ =
                        get_system_support_dir(abs_binary,
                                               command_line_system_support_dir);
+    }
+
+       // Find the LyX executable
+       lyx_binary_ = findLyxBinary(abs_binary);
 
        locale_dir_ = get_locale_dir(system_support_dir_);
 
        FileName const default_user_support_dir =
-               get_default_user_support_dir(home_dir_);
+               get_default_user_support_dir(get_home_dir());
 
        explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
                                     command_line_user_support_dir, user_support_dir_);
@@ -152,7 +149,7 @@ Package::Package(string const & command_line_arg0,
                << "\tlocale_dir " << locale_dir().absFileName() << '\n'
                << "\tdocument_dir " << document_dir().absFileName() << '\n'
                << "\ttemp_dir " << temp_dir().absFileName() << '\n'
-               << "\thome_dir " << home_dir().absFileName() << '\n'
+               << "\thome_dir " << get_home_dir().absFileName() << '\n'
                << "</package>\n");
 }
 
@@ -165,6 +162,19 @@ void Package::set_temp_dir(FileName const & temp_dir) const
                temp_dir_ = temp_dir;
 }
 
+// The specification of home_dir_ is fixed for a given OS.
+// A typical example on Windows: "C:/Documents and Settings/USERNAME"
+// and on a Posix-like machine: "/home/USERNAME".
+FileName const & Package::get_home_dir()
+{
+#if defined (USE_WINDOWS_PACKAGING)
+       static FileName const home_dir(getEnv("USERPROFILE"));
+#else // Posix-like.
+       static FileName const home_dir(getEnv("HOME"));
+#endif
+       return home_dir;
+}
+
 
 namespace {
 
@@ -237,24 +247,27 @@ string const fix_dir_name(string const & name)
 }
 
 
-FileName buildSupportDir(string const & binary_dir,
-                     exe_build_dir_to_top_build_dir top_build_dir_location)
+
+bool isBuildDir(FileName const & abs_binary, string const & dir_location,
+       FileName & build_support_dir)
 {
-       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 FileName(addPath(binary_dir, indirection));
+    string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
+
+    // Makefile by automake
+    build_support_dir = FileName(addPath(search_dir, "lib"));
+    if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
+        return true;
+    }
+    //  cmake file, no Makefile in lib
+    FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
+    if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
+        return true;
+    }
+
+    return false;
 }
 
-
-void buildDirs(FileName const & abs_binary,
-  exe_build_dir_to_top_build_dir top_build_dir_location,
+bool inBuildDir(FileName const & abs_binary,
        FileName & build_support_dir, FileName & system_support_dir)
 {
        string const check_text = "Checking whether LyX is run in place...";
@@ -267,19 +280,19 @@ void buildDirs(FileName const & abs_binary,
 
        // Note that the name of the lyx binary may be a symbolic link.
        // If that is the case, then we follow the links too.
-       FileName binary = abs_binary;
+    FileName binary = abs_binary;
        while (true) {
                // Try and find "lyxrc.defaults".
-               string binary_dir = onlyPath(binary.absFileName());
-               build_support_dir = buildSupportDir(binary_dir, top_build_dir_location);
-               if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
+               if( isBuildDir(binary, "../", build_support_dir) ||
+            isBuildDir(binary, "../../", build_support_dir))
+        {
                        // Try and find "chkconfig.ltx".
                        system_support_dir =
                                FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
 
                        if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
                                LYXERR(Debug::INIT, check_text << " yes");
-                               return;
+                               return true;
                        }
                }
 
@@ -300,6 +313,52 @@ void buildDirs(FileName const & abs_binary,
        LYXERR(Debug::INIT, check_text << " no");
        system_support_dir = FileName();
        build_support_dir = FileName();
+
+    return false;
+}
+
+
+bool doesFileExist(FileName & result, string const & search_dir, string const & name)
+{
+    result = fileSearch(search_dir, name);
+    if (!result.empty()) {
+        return true;
+    }
+    return false;
+}
+
+
+bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
+{
+    lyx_binary = FileName();
+    if(false) {   
+    } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
+    } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
+    } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
+    } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
+    }
+    return !lyx_binary.empty() ? true : false;
+}
+
+
+FileName findLyxBinary(FileName const & abs_binary)
+{
+    string ext;
+    if (!abs_binary.extension().empty()) {
+        ext = "." + abs_binary.extension();
+    }
+    
+    string binary_dir = onlyPath(abs_binary.absFileName());
+      
+    FileName lyx_binary;
+    if (lyxBinaryPath(lyx_binary, binary_dir, ext))
+        return lyx_binary;
+
+    string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
+    if (lyxBinaryPath(lyx_binary, search_dir, ext))
+        return lyx_binary;
+    
+    return FileName();
 }
 
 
@@ -317,20 +376,6 @@ FileName const get_document_dir(FileName const & home_dir)
 }
 
 
-// The specification of home_dir_ is fixed for a given OS.
-// A typical example on Windows: "C:/Documents and Settings/USERNAME"
-// and on a Posix-like machine: "/home/USERNAME".
-FileName const get_home_dir()
-{
-#if defined (USE_WINDOWS_PACKAGING)
-       string const home_dir = getEnv("USERPROFILE");
-#else // Posix-like.
-       string const home_dir = getEnv("HOME");
-#endif
-
-       return FileName(fix_dir_name(home_dir));
-}
-
 
 // Several sources are probed to ascertain the locale directory.
 // The only requirement is that the result is indeed a directory.
@@ -394,7 +439,7 @@ FileName const get_binary_path(string const & exe)
 
        // 2. exe must be the name of the binary only and it
        // can be found on the PATH.
-       string const exe_name = onlyFilename(exe_path);
+       string const exe_name = onlyFileName(exe_path);
        if (exe_name != exe_path)
                return FileName();