From e44bd1cc72a674f4fb9989d0e48fccca610a0f74 Mon Sep 17 00:00:00 2001 From: SVN User Date: Fri, 25 Jul 2008 04:23:22 +0000 Subject: [PATCH] streamlined code that detects location of the binary in init.cpp --- build_num | 2 +- src/common/init.cpp | 138 ++++++++++++++++------------------------- src/gui/RCS.cpp | 1 + src/gui/instDialog.cpp | 3 +- 4 files changed, 55 insertions(+), 89 deletions(-) diff --git a/build_num b/build_num index fd69db1a2..11ebd66d0 100644 --- a/build_num +++ b/build_num @@ -1 +1 @@ -#define BUILD_NUM 407 +#define BUILD_NUM 408 diff --git a/src/common/init.cpp b/src/common/init.cpp index c1000de27..d11c8308d 100644 --- a/src/common/init.cpp +++ b/src/common/init.cpp @@ -1,24 +1,21 @@ #include "../../config.h" -#ifdef _WIN32 +#if defined(Q_OS_MACX) || defined(Q_OS_WIN32) # include -#endif - -#ifndef _WIN32 +# include +# include +# include +# include +//# include +//# include +#else # include # include #endif -#ifdef Q_OS_MACX -# include -# include -//# include -//# include -#endif - #include -#include + #include "fwbuilder/libfwbuilder-config.h" #include "fwbuilder/Tools.h" #include "fwbuilder/Resources.h" @@ -40,11 +37,43 @@ using namespace std; using namespace libfwbuilder; -string guessExecPath(const char *argv0) +/* + * We do all these different hacks on different OS in order to be able + * to avoid dependency on QT on Linux and BSD, so people can + * (theoretically) build and install compilers on the firewall machine + * where they do not have X11 and QT. It may not be easy but should be + * possible. (Unless I broke it in 3.0) + * + * Note findExecutable returns path to the directory where fwbuilder binary + * is installed (on Mac it is /fwbuilder3.app/Contents/MacOS , + * On Windows it is c:\FWBuilder30, on Linux it is something like /usr/bin + * or /usr/local/bin and so on. + */ +string findExecutable(const char *argv0) { - -#ifdef Q_OS_MACX - +#ifdef OS_LINUX +/* + * on modern Linux systems full path to the executable is available in + * /proc/self/exec. + */ + char buf[PATH_MAX]; + if ( readlink( "/proc/self/exec", buf, sizeof(buf) )<0 ) + { + // Can do better: use a macro defined in configure via PREFIX + return ""; + } else + { + // /proc/self/exec points at full path to the executable, including + // name of the program. Remove the latter and return only path + // to the directory. + string exe_path(buf); + string::size_type n0 = exe_path.find_last_of("/\\"); + if (n0!=string::npos) + return exe_path.substr(0,n0) + FS_SEPARATOR; + else + return exe_path; + } +#else if (QCoreApplication::instance()==NULL) { int ac = 0; @@ -53,60 +82,6 @@ string guessExecPath(const char *argv0) } QDir dir(QApplication::applicationDirPath()); return string(dir.absolutePath().toAscii().constData()); - -#else - - QString s = argv0; - - if (s[0]=='/') return argv0; - if (s.indexOf('/')!=-1) return s.toLatin1().constData(); - -// argv0 does not start with '/' and contains no '/' - use PATH - QString path = getenv("PATH"); - int i1=0; - - while ( !(s=path.section(':',i1,i1)).isEmpty() ) - { - s=s+"/"+argv0; - if (access(s.toLatin1().constData(),F_OK|X_OK)==0) - return s.toLatin1().constData(); - i1++; - } - return argv0; -#endif -} - -/* - * We do all these different hacks on different OS in order to be able - * to avoid dependency on QT on Linux and BSD, so people can - * (theoretically) build and install compilers on the firewall machine - * where they do not have X11 and QT. It may not be easy but should be - * possible. (Unless I broke it in 3.0) - */ -string findExecutable(const char *argv0) -{ -//#ifdef _WIN32 -///* see explanation about _pgmptr here: -//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getmodulefilename.asp -//*/ -// string res; -// -// res = _pgmptr; -// return res; -//#else - -#ifdef OS_LINUX -/* on modern Linux systems full path to the executable is available in - * /proc/self/exec. - */ - char buf[PATH_MAX]; - if ( readlink( "/proc/self/exec", buf, sizeof(buf) )<0 ) - { - return guessExecPath(argv0); - } else - return buf; -#else - return guessExecPath(argv0); #endif //#endif } @@ -115,38 +90,29 @@ string findExecutable(const char *argv0) void init(char * const *argv) { - argv0 = findExecutable(argv[0]); - - string::size_type n0 = argv0.find_last_of("/\\"); - if (n0!=string::npos) appRootDir = argv0.substr(0,n0) + FS_SEPARATOR; - else appRootDir = ""; + appRootDir = findExecutable(argv[0]); libfwbuilder::init(); #if defined(Q_OS_WIN32) || defined(Q_OS_MACX) - /* On windows and mac we install API resources (DTD etc) in the * dir right above the one where we install resources for the GUI and compilers */ - if (respath=="") respath = appRootDir + RES_DIR; - n0 = respath.find_last_of("/\\"); - librespath = respath.substr(0,n0); - - sysfname = respath + FS_SEPARATOR+"objects_init.xml"; - tempfname = respath + FS_SEPARATOR+"templates.xml"; + if (respath=="") respath = appRootDir + FS_SEPARATOR + RES_DIR; + string::size_type n0 = respath.find_last_of("/\\"); + librespath = respath.substr(0, n0); #else - /* On Unix RES_DIR and LIBFWBUILDER_TEMPLATE_DIR are absolute paths */ if (respath=="") respath = RES_DIR; librespath = LIBFWBUILDER_TEMPLATE_DIR; - sysfname = respath+ FS_SEPARATOR + "objects_init.xml"; - tempfname = respath+ FS_SEPARATOR + "templates.xml"; - #endif + sysfname = respath + FS_SEPARATOR + "objects_init.xml"; + tempfname = respath+ FS_SEPARATOR + "templates.xml"; + /* define localepath the same way as we define PKGLOCALEDIR in qmake.inc */ localepath = respath + "/locale"; diff --git a/src/gui/RCS.cpp b/src/gui/RCS.cpp index 655f1b327..ba11f5c31 100644 --- a/src/gui/RCS.cpp +++ b/src/gui/RCS.cpp @@ -26,6 +26,7 @@ #include "../../config.h" #include "global.h" #include "utils.h" +#include "utils_no_qt.h" #include "RCS.h" diff --git a/src/gui/instDialog.cpp b/src/gui/instDialog.cpp index 5f423db04..75bc67d80 100644 --- a/src/gui/instDialog.cpp +++ b/src/gui/instDialog.cpp @@ -1713,8 +1713,7 @@ Can't compile firewall policy."), /* try to find compiler in appRootDir. */ string ts = getPathToBinary(compiler); - if (fwbdebug) - qDebug("Checking compiler in %s", ts.c_str()); + if (fwbdebug) qDebug("Checking compiler in %s", ts.c_str()); if ( QFile::exists( ts.c_str() ) ) compiler = ts;