run in-place: don't speculate where the lyx binary is, search for it at the common places

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38753 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Peter Kümmel 2011-05-14 12:26:11 +00:00
parent b3ca5850ee
commit 1af61e659c
4 changed files with 86 additions and 68 deletions

View File

@ -290,8 +290,7 @@ int LyX::exec(int & argc, char * argv[])
{
// Minimal setting of locale before parsing command line
try {
init_package(os::utf8_argv(0), string(), string(),
top_build_dir_is_one_level_up);
init_package(os::utf8_argv(0), string(), string());
// we do not get to this point when init_package throws an exception
locale_init();
} catch (ExceptionMessage const & message) {
@ -303,9 +302,7 @@ int LyX::exec(int & argc, char * argv[])
easyParse(argc, argv);
try {
init_package(os::utf8_argv(0),
cl_system_support, cl_user_support,
top_build_dir_is_one_level_up);
init_package(os::utf8_argv(0), cl_system_support, cl_user_support);
} catch (ExceptionMessage const & message) {
if (message.type_ == ErrorException) {
Alert::error(message.title_, message.details_);

View File

@ -54,13 +54,11 @@ 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)
{
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;
}
@ -76,9 +74,10 @@ 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 &);
bool inBuildDir(FileName const & abs_binary, FileName &, FileName &);
FileName findLyxBinaryDir(FileName const & abs_binary);
FileName const get_document_dir(FileName const & home_dir);
@ -101,8 +100,7 @@ string const fix_dir_name(string const & name);
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)
{
// Specification of temp_dir_ may be reset by LyXRC,
@ -119,31 +117,16 @@ Package::Package(string const & command_line_arg0,
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);
}
// The LyX executable is one level above binary_dir_ if we are running
// tex2lyx in place. Otherwise it is in binary_dir_.
string abs_lyx_dir;
if (build_support_dir_.empty() ||
top_build_dir_location == top_build_dir_is_one_level_up)
abs_lyx_dir = binary_dir_.absFileName();
else {
FileName fn(addPath(binary_dir_.absFileName(), "../"));
abs_lyx_dir = fn.realPath();
}
// The LyX executable may have a package suffix if we are not running
// in place.
if (build_support_dir_.empty())
lyx_binary_ = FileName(addName(abs_lyx_dir,
"lyx" + string(PROGRAM_SUFFIX)));
else
lyx_binary_ = FileName(addName(abs_lyx_dir, "lyx"));
// Find the LyX executable
lyx_binary_ = findLyxBinaryDir(abs_binary);
locale_dir_ = get_locale_dir(system_support_dir_);
@ -264,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...";
@ -294,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;
}
}
@ -327,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 findLyxBinaryDir(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();
}

View File

@ -25,14 +25,6 @@ namespace support {
class Package;
/** When run in-place <build-dir>/src/lyx is one level up from
* the <build-dir> whilst <build-dir>/src/tex2lyx/tex2lyx is
* two levels up.
*/
enum exe_build_dir_to_top_build_dir {
top_build_dir_is_one_level_up,
top_build_dir_is_two_levels_up
};
/** Initialise package() with the command line data.
@ -49,8 +41,7 @@ enum exe_build_dir_to_top_build_dir {
*/
void init_package(std::string const & command_line_arg0,
std::string const & command_line_system_support_dir,
std::string const & command_line_user_support_dir,
exe_build_dir_to_top_build_dir);
std::string const & command_line_user_support_dir);
/** Accessor to the global data.
* Asserts that init_package() has been called first.
@ -67,8 +58,7 @@ public:
*/
Package(std::string const & command_line_arg0,
std::string const & command_line_system_support_dir,
std::string const & command_line_user_support_dir,
exe_build_dir_to_top_build_dir);
std::string const & command_line_user_support_dir);
/** The directory containing the main executable (LyX or tex2lyx).
*/

View File

@ -773,8 +773,7 @@ int main(int argc, char * argv[])
try {
init_package(internal_path(os::utf8_argv(0)),
cl_system_support, cl_user_support,
top_build_dir_is_two_levels_up);
cl_system_support, cl_user_support);
} catch (ExceptionMessage const & message) {
cerr << to_utf8(message.title_) << ":\n"
<< to_utf8(message.details_) << endl;