mirror of
https://github.com/fwbuilder/fwbuilder
synced 2026-03-22 03:07:20 +01:00
streamlined code that detects location of the binary in init.cpp
This commit is contained in:
parent
6ea2fca59b
commit
e44bd1cc72
@ -1,24 +1,21 @@
|
||||
|
||||
#include "../../config.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#if defined(Q_OS_MACX) || defined(Q_OS_WIN32)
|
||||
# include <qsettings.h>
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <QDir>
|
||||
# include <QApplication>
|
||||
# include <QCoreApplication>
|
||||
# include <QString>
|
||||
//# include <CoreFoundation/CFURL.h>
|
||||
//# include <CoreFoundation/CFBundle.h>
|
||||
#else
|
||||
# include <limits.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_MACX
|
||||
# include <QDir>
|
||||
# include <QApplication>
|
||||
//# include <CoreFoundation/CFURL.h>
|
||||
//# include <CoreFoundation/CFBundle.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include <QString>
|
||||
|
||||
#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 <wherever>/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";
|
||||
|
||||
|
||||
@ -26,6 +26,7 @@
|
||||
#include "../../config.h"
|
||||
#include "global.h"
|
||||
#include "utils.h"
|
||||
#include "utils_no_qt.h"
|
||||
|
||||
#include "RCS.h"
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user