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

@ -906,6 +906,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
std::string files;
std::set<std::string> libSet;
std::set<std::string> publicFlagSet;
std::set<std::string> runPathSet;
std::mutex fileMutex;
std::vector<std::thread> depThreads;
depThreads.reserve(config.dependencies.size());
@ -921,6 +922,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
fileMutex.lock();
for (const std::string& lib : result.libs) libSet.insert(lib);
for (const std::string& f : result.publicCompileFlags) publicFlagSet.insert(f);
for (const std::string& dir : result.runPaths) runPathSet.insert(dir);
fileMutex.unlock();
if (!result.result.empty()) {
bool expected = false;
@ -1112,6 +1114,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
buildResult.repack = true;
}
buildResult.libs = std::move(libSet);
buildResult.runPaths = std::move(runPathSet);
for(const std::string& flag : config.linkFlags) {
buildResult.libs.insert(flag);
}
@ -1132,6 +1135,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
for(const std::string& flag : ext.linkFlags) {
buildResult.libs.insert(flag);
}
for (const std::string& dir : ext.runPaths) buildResult.runPaths.insert(dir);
if (ext.latestArtifact > externalFloor) externalFloor = ext.latestArtifact;
}
@ -1197,6 +1201,12 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
for(const std::string& flag : buildResult.libs) {
linkExtras += std::format(" {}", flag);
}
// The program loads its shared dependencies from where they were built,
// the external cache among them. ExternalOutputs leaves the mingw target
// without any: PE resolves DLLs another way.
for (const std::string& dir : buildResult.runPaths) {
linkExtras += std::format(" -Wl,-rpath,{}", dir);
}
// Link-only LTO/section flags (empty in Debug and for wasm). Every exe/lib
// link below ends with linkExtras, so this reaches them all; the static-lib
// archive path uses ar instead and is handled separately.