Skip to content

Commit 9ea5b9b

Browse files
committed
Modification to support binary relocation when the FILESDIR macro is a relative path.
A second macro BINDIR can be used in combination to indicate an optional binary folder where the program is installed (e.g. the bin part from /usr/bin). When FILESDIR is a relative path, the computed files dir will be the concatenation of the executable parent path [- BINDIR>] + <FILESDIR>. If FILESDIR is an absolute path, it is used unmodified. This is a possible solution to danmar#4424.
1 parent bfa9594 commit 9ea5b9b

16 files changed

+112
-27
lines changed

cli/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ install(TARGETS cppcheck
7373
COMPONENT applications)
7474

7575
install(FILES ${addons}
76-
DESTINATION ${FILESDIR}/addons
76+
DESTINATION ${FULL_FILESDIR}/addons
7777
COMPONENT headers)
7878

7979
install(FILES ${cfgs}
80-
DESTINATION ${FILESDIR}/cfg
80+
DESTINATION ${FULL_FILESDIR}/cfg
8181
COMPONENT headers)
8282

8383
install(FILES ${platforms}
84-
DESTINATION ${FILESDIR}/platforms
84+
DESTINATION ${FULL_FILESDIR}/platforms
8585
COMPONENT headers)
8686

cli/cmdlineparser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,9 +1743,11 @@ bool CmdLineParser::loadLibraries(Settings& settings)
17431743
if (!tryLoadLibrary(settings.library, settings.exename, "std.cfg")) {
17441744
const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install.");
17451745
#ifdef FILESDIR
1746+
const std::string filesdir = Path::getOptionalFilesDirPath(settings.exename);
1747+
const std::string location = filesdir.empty() ? Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) : filesdir;
17461748
const std::string details("The Cppcheck binary was compiled with FILESDIR set to \""
17471749
FILESDIR "\" and will therefore search for "
1748-
"std.cfg in " FILESDIR "/cfg.");
1750+
"std.cfg in " + location + "/cfg.");
17491751
#else
17501752
const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) + "cfg");
17511753
const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the "

cmake/compilerDefinitions.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ endif()
5555

5656
file(TO_CMAKE_PATH ${FILESDIR} _filesdir)
5757
add_definitions(-DFILESDIR="${_filesdir}")
58+
59+
cmake_path(NORMAL_PATH CMAKE_INSTALL_FULL_BINDIR OUTPUT_VARIABLE _bindir)
60+
cmake_path(NORMAL_PATH CMAKE_INSTALL_PREFIX OUTPUT_VARIABLE _prefix)
61+
if (NOT "${_bindir}" EQUAL "${_prefix}")
62+
cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_BINDIR BASE_DIRECTORY "${CMAKE_INSTALL_PREFIX}" OUTPUT_VARIABLE _bindir)
63+
add_definitions(-DBINDIR="${_bindir}")
64+
endif ()

cmake/options.cmake

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ option(ANALYZE_DATAFLOW "Clang dynamic analyzer: general dynamic dataflow an
2020
option(WARNINGS_ARE_ERRORS "Treat warnings as errors" OFF)
2121

2222
set(USE_MATCHCOMPILER "Auto" CACHE STRING "Usage of match compiler")
23-
set_property(CACHE USE_MATCHCOMPILER PROPERTY STRINGS Auto Off On Verify)
23+
set_property(CACHE USE_MATCHCOMPILER PROPERTY STRINGS Auto Off On Verify)
2424
if (USE_MATCHCOMPILER STREQUAL "Auto")
2525
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
2626
set(USE_MATCHCOMPILER_OPT "On")
@@ -77,5 +77,10 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
7777
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
7878
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
7979

80-
set(FILESDIR ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME} CACHE STRING "Cppcheck files directory")
80+
set(FILESDIR share/${PROJECT_NAME} CACHE STRING "Cppcheck files directory")
8181

82+
if (IS_ABSOLUTE "${FILESDIR}")
83+
set(FULL_FILESDIR "${FILESDIR}")
84+
else()
85+
set(FULL_FILESDIR "${CMAKE_INSTALL_PREFIX}/${FILESDIR}")
86+
endif()

gui/common.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,23 @@ QString getDataDir()
8585
return appPath.left(appPath.indexOf("/cppcheck/", 0, Qt::CaseInsensitive) + 9);
8686
return appPath;
8787
}
88+
89+
QString getFilesDir()
90+
{
91+
QString filesdir;
92+
#ifdef FILESDIR
93+
filesdir = FILESDIR;
94+
if (!filesdir.isEmpty()) {
95+
if (QFileInfo(filesdir).isRelative()) {
96+
filesdir = QApplication::applicationDirPath();
97+
#ifdef BINDIR
98+
if (filesdir.endsWith("/" BINDIR)) {
99+
filesdir = filesdir.left(filesdir.lastIndexOf("/" BINDIR));
100+
}
101+
#endif
102+
filesdir += "/" FILESDIR;
103+
}
104+
}
105+
#endif
106+
return filesdir;
107+
}

gui/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,10 @@ QString toFilterString(const QMap<QString,QString>& filters, bool addAllSupporte
151151
*/
152152
QString getDataDir();
153153

154+
/*
155+
* Get files dir. If FILESDIR is not defined then it will return empty.
156+
*/
157+
QString getFilesDir();
158+
154159
/// @}
155160
#endif

gui/helpdialog.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ static QString getHelpFile()
6060
<< (QApplication::applicationDirPath() + "/help")
6161
<< QApplication::applicationDirPath();
6262
#ifdef FILESDIR
63-
const QString filesdir = FILESDIR;
64-
paths << (filesdir + "/help")
65-
<< filesdir;
63+
const QString filesdir = getFilesDir();
64+
if (!filesdir.isEmpty()) {
65+
paths << (filesdir + "/help")
66+
<< filesdir;
67+
}
6668
#endif
6769
for (const QString &p: paths) {
6870
QString filename = p + "/online-help.qhc";

gui/mainwindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename
845845

846846
#ifdef FILESDIR
847847
// Try to load the library from FILESDIR/cfg..
848-
const QString filesdir = FILESDIR;
848+
const QString filesdir = getFilesDir();
849849
if (!filesdir.isEmpty()) {
850850
ret = library->load(nullptr, (filesdir+"/cfg/"+filename).toLatin1());
851851
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)

gui/projectfile.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,11 +1122,13 @@ QString ProjectFile::getAddonFilePath(QString filesDir, const QString &addon)
11221122
filesDir += "/";
11231123

11241124
QStringList searchPaths;
1125-
searchPaths << filesDir << (filesDir + "addons/") << (filesDir + "../addons/")
1125+
searchPaths << filesDir << (filesDir + "addons/") << (filesDir + "../addons/");
11261126
#ifdef FILESDIR
1127-
<< (QLatin1String(FILESDIR) + "/addons/")
1127+
const QString extra = getFilesDir();
1128+
if (!extra.isEmpty()) {
1129+
searchPaths << (QLatin1String(extra) + "/addons/");
1130+
}
11281131
#endif
1129-
;
11301132

11311133
for (const QString& path : searchPaths) {
11321134
QString f = path + addon + ".py";

gui/projectfiledialog.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi
124124
QStringList searchPaths;
125125
searchPaths << appPath << appPath + "/cfg" << inf.canonicalPath();
126126
#ifdef FILESDIR
127-
if (FILESDIR[0])
128-
searchPaths << FILESDIR << FILESDIR "/cfg";
127+
const QString filesDir = getFilesDir();
128+
if(!filesDir.isEmpty()) {
129+
searchPaths << filesDir << filesDir "/cfg";
130+
}
129131
#endif
130132
if (!datadir.isEmpty())
131133
searchPaths << datadir << datadir + "/cfg";

0 commit comments

Comments
 (0)