CMake: Don't add standard Linux paths to LD_LIBRARY_PATH

If a project specifically links to a library in a standard path (like
/usr/lib/...), we do not need to add that path to LD_LIBRARY_PATH.
Actually adding it can be harmful if the build needs to link against
some other library in a different version than is available in the system
path.

Common case is linking the application against a Qt version from the
online installer. If /usr/lib/... ends up in the LD_LIBRARY_PATH before
the path to the Qt from the online installer, the system Qt is picked up
at runtime instead of the Qt from the online installer.

Fixes: QTCREATORBUG-25292
Change-Id: Ib080e41f5893fb68e9d65cc9c9f11d1a9a60f485
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Eike Ziller
2021-03-05 13:25:32 +01:00
parent fab090907d
commit 5e1f40a3af

View File

@@ -179,6 +179,14 @@ QVector<FolderNode::LocationInfo> extractBacktraceInformation(const BacktraceInf
return info;
}
static bool isChildOf(const FilePath &path, const QStringList &prefixes)
{
for (const QString &prefix : prefixes)
if (path.isChildOf(FilePath::fromString(prefix)))
return true;
return false;
}
QList<CMakeBuildTarget> generateBuildTargets(const PreprocessedData &input,
const FilePath &sourceDirectory,
const FilePath &buildDirectory)
@@ -269,16 +277,28 @@ QList<CMakeBuildTarget> generateBuildTargets(const PreprocessedData &input,
if (f.role == "libraries")
tmp = tmp.parentDir();
if (!tmp.isEmpty()
&& tmp.isDir()) { // f.role is libraryPath or frameworkPath
librarySeachPaths.append(tmp);
// Libraries often have their import libs in ../lib and the
// actual dll files in ../bin on windows. Qt is one example of that.
if (tmp.fileName() == "lib" && HostOsInfo::isWindowsHost()) {
const FilePath path = tmp.parentDir().pathAppended("bin");
if (!tmp.isEmpty() && tmp.isDir()) {
// f.role is libraryPath or frameworkPath
// On Linux, exclude sub-paths from "/lib(64)", "/usr/lib(64)" and
// "/usr/local/lib" since these are usually in the standard search
// paths. There probably are more, but the naming schemes are arbitrary
// so we'd need to ask the linker ("ld --verbose | grep SEARCH_DIR").
if (!HostOsInfo::isLinuxHost()
|| !isChildOf(tmp,
{"/lib",
"/lib64",
"/usr/lib",
"/usr/lib64",
"/usr/local/lib"})) {
librarySeachPaths.append(tmp);
// Libraries often have their import libs in ../lib and the
// actual dll files in ../bin on windows. Qt is one example of that.
if (tmp.fileName() == "lib" && HostOsInfo::isWindowsHost()) {
const FilePath path = tmp.parentDir().pathAppended("bin");
if (path.isDir())
librarySeachPaths.append(path);
if (path.isDir())
librarySeachPaths.append(path);
}
}
}
}