refactor: carry run paths as directories, not -Wl,-rpath flags

ExternalOutputs put an -Wl,-rpath,<dir> per library directory of a CMake
external into its link flags, and the flags travelled up the dependency
graph in BuildResult::libs to every link that consumed them: an
executable's or a shared library's, and that of the project.so
LoadProject links against a project file's pragma dependencies. Whether
a program should record those directories depends on how it is packaged:
a Flatpak has to load its libraries from its own files/lib instead, and
has to know the directories to copy them from. ExternalBuildResult and
BuildResult now carry them as runPaths, merged up the graph beside libs,
and all three links turn each into an -Wl,-rpath as before, so the link
commands differ only in the order of their flags. PragmaSharedExternal
holds project.so's link to them.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:56:41 +01:00
commit aa6b555395
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 37 additions and 11 deletions

View file

@ -293,21 +293,21 @@ ExternalBuildResult Crafter::ExternalOutputs(const ExternalDependency& dep, std:
const fs::path cmakeBuildDir = ExternalBuildDir(dep, target);
if (dep.builder != ExternalBuilder::CMake || cmakeBuildDir.empty()) return result;
// Each search path gets both a -L (link-time) and a -Wl,-rpath
// (runtime-loader). The rpath stays embedded in the produced
// binary so it picks up shared deps from the cache without any
// LD_LIBRARY_PATH gymnastics. Static deps (.a) ignore the rpath
// harmlessly. The PE/COFF target (mingw) resolves DLLs via PATH or
// adjacency rather than rpath, so lld warns if we pass it.
// Each search path is both a -L (link time) and a run path (the
// loader's), so the program picks its shared dependencies up from the
// cache without any LD_LIBRARY_PATH gymnastics; static ones (.a) need
// the run path not at all and are not hurt by it. The PE/COFF target
// (mingw) resolves DLLs through PATH or the program's own directory
// rather than a run path, and lld warns if it is given one.
const bool isPe = target == "x86_64-w64-mingw32";
std::string buildDirAbs = fs::absolute(cmakeBuildDir).string();
result.linkFlags.push_back(std::format("-L{}", buildDirAbs));
if (!isPe) result.linkFlags.push_back(std::format("-Wl,-rpath,{}", buildDirAbs));
if (!isPe) result.runPaths.push_back(buildDirAbs);
for (const fs::path& libDir : dep.libDirs) {
fs::path full = libDir.is_absolute() ? libDir : cmakeBuildDir / libDir;
std::string fullAbs = fs::absolute(full).string();
result.linkFlags.push_back(std::format("-L{}", fullAbs));
if (!isPe) result.linkFlags.push_back(std::format("-Wl,-rpath,{}", fullAbs));
if (!isPe) result.runPaths.push_back(fullAbs);
}
for (const std::string& lib : dep.libs) {
result.linkFlags.push_back(std::format("-l{}", lib));