Use path to executable when trying to load library (#5082)
* Use path to executable when trying to load library * Fix function call, add support for more OS, add test * Format * Handle MacOS * Amend * Argument as fallback * Use Path::getCurrentExecutablePath() * Move to cmdlineparser.cpp * Debug output * Use argv[0] in Cygwin * Revert "Debug output" This reverts commit 5a68d71f1d27549c7b4a46363f3cd5cd912018e7. * Use native python in Cygwin
This commit is contained in:
parent
bc03bed075
commit
fb850a844b
|
@ -23,6 +23,7 @@ jobs:
|
||||||
- platform: 'x86_64'
|
- platform: 'x86_64'
|
||||||
packages: |
|
packages: |
|
||||||
gcc-g++
|
gcc-g++
|
||||||
|
python3
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
|
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
|
@ -149,15 +149,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
|
||||||
bool def = false;
|
bool def = false;
|
||||||
bool maxconfigs = false;
|
bool maxconfigs = false;
|
||||||
|
|
||||||
mSettings.exename = argv[0];
|
mSettings.exename = Path::getCurrentExecutablePath(argv[0]);
|
||||||
#ifdef __linux__
|
|
||||||
// Executing cppcheck in PATH. argv[0] does not contain the path.
|
|
||||||
if (mSettings.exename.find_first_of("/\\") == std::string::npos) {
|
|
||||||
char buf[PATH_MAX] = {0};
|
|
||||||
if (FileLister::fileExists("/proc/self/exe") && readlink("/proc/self/exe", buf, sizeof(buf)-1) > 0)
|
|
||||||
mSettings.exename = buf;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (argv[i][0] == '-') {
|
if (argv[i][0] == '-') {
|
||||||
|
|
22
cli/main.cpp
22
cli/main.cpp
|
@ -68,18 +68,6 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
static char exename[1024] = {0};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
|
||||||
#include <mach-o/dyld.h>
|
|
||||||
|
|
||||||
static char exename[1024] = {0};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function of cppcheck
|
* Main function of cppcheck
|
||||||
*
|
*
|
||||||
|
@ -95,15 +83,7 @@ int main(int argc, char* argv[])
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
CppCheckExecutor exec;
|
CppCheckExecutor exec;
|
||||||
#ifdef _WIN32
|
|
||||||
GetModuleFileNameA(nullptr, exename, sizeof(exename)/sizeof(exename[0])-1);
|
|
||||||
argv[0] = exename;
|
|
||||||
#endif
|
|
||||||
#if defined(__APPLE__)
|
|
||||||
uint32_t size = sizeof(exename);
|
|
||||||
_NSGetExecutablePath(exename, &size);
|
|
||||||
argv[0] = exename;
|
|
||||||
#endif
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -107,8 +107,9 @@ Library::Error Library::load(const char exename[], const char path[])
|
||||||
cfgfolders.emplace_back(FILESDIR "/cfg");
|
cfgfolders.emplace_back(FILESDIR "/cfg");
|
||||||
#endif
|
#endif
|
||||||
if (exename) {
|
if (exename) {
|
||||||
const std::string exepath(Path::fromNativeSeparators(Path::getPathFromFilename(exename)));
|
const std::string exepath(Path::fromNativeSeparators(Path::getPathFromFilename(Path::getCurrentExecutablePath(exename))));
|
||||||
cfgfolders.push_back(exepath + "cfg");
|
cfgfolders.push_back(exepath + "cfg");
|
||||||
|
cfgfolders.push_back(exepath + "../cfg");
|
||||||
cfgfolders.push_back(exepath);
|
cfgfolders.push_back(exepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
lib/path.cpp
30
lib/path.cpp
|
@ -28,16 +28,21 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <simplecpp.h>
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#else
|
#else
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
#if defined(__CYGWIN__)
|
#if defined(__CYGWIN__)
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
#include <mach-o/dyld.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <simplecpp.h>
|
|
||||||
|
|
||||||
/** Is the filesystem case insensitive? */
|
/** Is the filesystem case insensitive? */
|
||||||
static bool caseInsensitiveFilesystem()
|
static bool caseInsensitiveFilesystem()
|
||||||
|
@ -131,6 +136,29 @@ std::string Path::getCurrentPath()
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Path::getCurrentExecutablePath(const char* fallback)
|
||||||
|
{
|
||||||
|
char buf[4096] = {};
|
||||||
|
bool success{};
|
||||||
|
#ifdef _WIN32
|
||||||
|
success = (GetModuleFileNameA(nullptr, buf, sizeof(buf)) < sizeof(buf));
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
uint32_t size = sizeof(buf);
|
||||||
|
success = (_NSGetExecutablePath(buf, &size) == 0);
|
||||||
|
#else
|
||||||
|
const char* procPath =
|
||||||
|
#ifdef __SVR4 // Solaris
|
||||||
|
"/proc/self/path/a.out";
|
||||||
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
||||||
|
"/proc/curproc/file";
|
||||||
|
#else // Linux
|
||||||
|
"/proc/self/exe";
|
||||||
|
#endif
|
||||||
|
success = (readlink(procPath, buf, sizeof(buf)) != -1);
|
||||||
|
#endif
|
||||||
|
return success ? std::string(buf) : std::string(fallback);
|
||||||
|
}
|
||||||
|
|
||||||
bool Path::isAbsolute(const std::string& path)
|
bool Path::isAbsolute(const std::string& path)
|
||||||
{
|
{
|
||||||
const std::string& nativePath = toNativeSeparators(path);
|
const std::string& nativePath = toNativeSeparators(path);
|
||||||
|
|
|
@ -104,6 +104,12 @@ public:
|
||||||
*/
|
*/
|
||||||
static std::string getCurrentPath();
|
static std::string getCurrentPath();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns the absolute path to the current executable
|
||||||
|
* @return absolute path to the current executable
|
||||||
|
*/
|
||||||
|
static std::string getCurrentExecutablePath(const char* fallback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if given path is absolute
|
* @brief Check if given path is absolute
|
||||||
* @param path Path to check
|
* @param path Path to check
|
||||||
|
|
|
@ -32,6 +32,7 @@ private:
|
||||||
TEST_CASE(removeQuotationMarks);
|
TEST_CASE(removeQuotationMarks);
|
||||||
TEST_CASE(acceptFile);
|
TEST_CASE(acceptFile);
|
||||||
TEST_CASE(getCurrentPath);
|
TEST_CASE(getCurrentPath);
|
||||||
|
TEST_CASE(getCurrentExecutablePath);
|
||||||
TEST_CASE(isAbsolute);
|
TEST_CASE(isAbsolute);
|
||||||
TEST_CASE(getRelative);
|
TEST_CASE(getRelative);
|
||||||
TEST_CASE(is_c);
|
TEST_CASE(is_c);
|
||||||
|
@ -72,6 +73,10 @@ private:
|
||||||
ASSERT_EQUALS(true, Path::isAbsolute(Path::getCurrentPath()));
|
ASSERT_EQUALS(true, Path::isAbsolute(Path::getCurrentPath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getCurrentExecutablePath() const {
|
||||||
|
ASSERT_EQUALS(false, Path::getCurrentExecutablePath("").empty());
|
||||||
|
}
|
||||||
|
|
||||||
void isAbsolute() const {
|
void isAbsolute() const {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ASSERT_EQUALS(true, Path::isAbsolute("C:\\foo\\bar"));
|
ASSERT_EQUALS(true, Path::isAbsolute("C:\\foo\\bar"));
|
||||||
|
|
Loading…
Reference in New Issue