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::string files;
std::set<std::string> libSet; std::set<std::string> libSet;
std::set<std::string> publicFlagSet; std::set<std::string> publicFlagSet;
std::set<std::string> runPathSet;
std::mutex fileMutex; std::mutex fileMutex;
std::vector<std::thread> depThreads; std::vector<std::thread> depThreads;
depThreads.reserve(config.dependencies.size()); depThreads.reserve(config.dependencies.size());
@ -921,6 +922,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
fileMutex.lock(); fileMutex.lock();
for (const std::string& lib : result.libs) libSet.insert(lib); for (const std::string& lib : result.libs) libSet.insert(lib);
for (const std::string& f : result.publicCompileFlags) publicFlagSet.insert(f); for (const std::string& f : result.publicCompileFlags) publicFlagSet.insert(f);
for (const std::string& dir : result.runPaths) runPathSet.insert(dir);
fileMutex.unlock(); fileMutex.unlock();
if (!result.result.empty()) { if (!result.result.empty()) {
bool expected = false; bool expected = false;
@ -1112,6 +1114,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
buildResult.repack = true; buildResult.repack = true;
} }
buildResult.libs = std::move(libSet); buildResult.libs = std::move(libSet);
buildResult.runPaths = std::move(runPathSet);
for(const std::string& flag : config.linkFlags) { for(const std::string& flag : config.linkFlags) {
buildResult.libs.insert(flag); 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) { for(const std::string& flag : ext.linkFlags) {
buildResult.libs.insert(flag); buildResult.libs.insert(flag);
} }
for (const std::string& dir : ext.runPaths) buildResult.runPaths.insert(dir);
if (ext.latestArtifact > externalFloor) externalFloor = ext.latestArtifact; 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) { for(const std::string& flag : buildResult.libs) {
linkExtras += std::format(" {}", flag); 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-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 // link below ends with linkExtras, so this reaches them all; the static-lib
// archive path uses ar instead and is handled separately. // archive path uses ar instead and is handled separately.

View file

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

View file

@ -506,7 +506,11 @@ namespace {
struct ProjectFileDependencies { struct ProjectFileDependencies {
std::vector<Configuration*> configs; std::vector<Configuration*> configs;
std::string compileFlags; // " -fprebuilt-module-path=…" for each dependency and its transitive deps std::string compileFlags; // " -fprebuilt-module-path=…" for each dependency and its transitive deps
std::string linkFlags; // every dependency's BuildResult.libs, verbatim // Every dependency's BuildResult.libs, verbatim, then an -Wl,-rpath
// per directory of its runPaths, as Build() links a program: the
// shared libraries a pragma dependency is built on are found there
// when LoadProject dlopens project.so.
std::string linkFlags;
fs::file_time_type newestArtifact = fs::file_time_type::min(); fs::file_time_type newestArtifact = fs::file_time_type::min();
}; };
@ -524,6 +528,7 @@ namespace {
std::unordered_set<Configuration*> forced; std::unordered_set<Configuration*> forced;
std::unordered_set<Configuration*> walked; std::unordered_set<Configuration*> walked;
std::set<std::string> libs; std::set<std::string> libs;
std::set<std::string> runPaths;
for (const PragmaDependency& pragma : pragmas) { for (const PragmaDependency& pragma : pragmas) {
// Neither spec sets args, and the tool still gets none of the // Neither spec sets args, and the tool still gets none of the
@ -568,6 +573,7 @@ namespace {
if (!built.result.empty()) PragmaError(absProject, pragma.line, std::format("project dependency '{}' failed to build: {}", cfg->name, built.result)); if (!built.result.empty()) PragmaError(absProject, pragma.line, std::format("project dependency '{}' failed to build: {}", cfg->name, built.result));
out.configs.push_back(cfg); out.configs.push_back(cfg);
libs.insert(built.libs.begin(), built.libs.end()); libs.insert(built.libs.begin(), built.libs.end());
runPaths.insert(built.runPaths.begin(), built.runPaths.end());
// The module path needs the dependency and everything it depends // The module path needs the dependency and everything it depends
// on, same as GetCompileCommand does for a consumer; the newest // on, same as GetCompileCommand does for a consumer; the newest
@ -583,6 +589,7 @@ namespace {
walk(cfg); walk(cfg);
} }
for (const std::string& lib : libs) out.linkFlags += std::format(" {}", lib); for (const std::string& lib : libs) out.linkFlags += std::format(" {}", lib);
for (const std::string& dir : runPaths) out.linkFlags += std::format(" -Wl,-rpath,{}", dir);
return out; return out;
} }

View file

@ -53,6 +53,10 @@ export namespace Crafter {
// dirs so headers a dep exposes in its public modules are reachable // dirs so headers a dep exposes in its public modules are reachable
// when a consumer #includes them directly. Propagates transitively. // when a consumer #includes them directly. Propagates transitively.
std::set<std::string> publicCompileFlags; std::set<std::string> publicCompileFlags;
// Absolute directories the shared libraries this links against are
// loaded from at run time, its dependencies' included. An executable
// or shared library linked from them records each one as a run path.
std::set<std::string> runPaths;
}; };
struct Define { struct Define {

View file

@ -38,6 +38,11 @@ export namespace Crafter {
struct ExternalBuildResult { struct ExternalBuildResult {
std::vector<std::string> compileFlags; std::vector<std::string> compileFlags;
std::vector<std::string> linkFlags; std::vector<std::string> linkFlags;
// Absolute directories the dependency's shared libraries are loaded
// from at run time: what a consumer's link records as its run path.
// Kept apart from linkFlags because what becomes of them depends on
// how the consumer is packaged (see BuildResult::runPaths).
std::vector<std::string> runPaths;
fs::file_time_type latestArtifact = fs::file_time_type::min(); fs::file_time_type latestArtifact = fs::file_time_type::min();
}; };
@ -66,8 +71,8 @@ export namespace Crafter {
// short would leave them a clone nobody fetched. // short would leave them a clone nobody fetched.
std::string BuildExternal(const ExternalDependency& dep, std::string_view target); std::string BuildExternal(const ExternalDependency& dep, std::string_view target);
// What a configuration that declares `dep` compiles and links with once // What a configuration that declares `dep` compiles and links with once
// BuildExternal has run: the -I flags and, for CMake, the -L, -rpath and -l // BuildExternal has run: the -I flags and, for CMake, the -L and -l flags,
// flags and the newest library. Per declaration, because two that share a // the run path directories and the newest library. Per declaration, because two that share a
// clone can still differ in includeDirs, libDirs and libs. // clone can still differ in includeDirs, libDirs and libs.
ExternalBuildResult ExternalOutputs(const ExternalDependency& dep, std::string_view target); ExternalBuildResult ExternalOutputs(const ExternalDependency& dep, std::string_view target);