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.

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));

View file

@ -506,7 +506,11 @@ namespace {
struct ProjectFileDependencies {
std::vector<Configuration*> configs;
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();
};
@ -524,6 +528,7 @@ namespace {
std::unordered_set<Configuration*> forced;
std::unordered_set<Configuration*> walked;
std::set<std::string> libs;
std::set<std::string> runPaths;
for (const PragmaDependency& pragma : pragmas) {
// 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));
out.configs.push_back(cfg);
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
// on, same as GetCompileCommand does for a consumer; the newest
@ -583,6 +589,7 @@ namespace {
walk(cfg);
}
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;
}

View file

@ -53,6 +53,10 @@ export namespace Crafter {
// dirs so headers a dep exposes in its public modules are reachable
// when a consumer #includes them directly. Propagates transitively.
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 {

View file

@ -38,6 +38,11 @@ export namespace Crafter {
struct ExternalBuildResult {
std::vector<std::string> compileFlags;
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();
};
@ -66,8 +71,8 @@ export namespace Crafter {
// short would leave them a clone nobody fetched.
std::string BuildExternal(const ExternalDependency& dep, std::string_view target);
// 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
// flags and the newest library. Per declaration, because two that share a
// BuildExternal has run: the -I flags and, for CMake, the -L and -l flags,
// the run path directories and the newest library. Per declaration, because two that share a
// clone can still differ in includeDirs, libDirs and libs.
ExternalBuildResult ExternalOutputs(const ExternalDependency& dep, std::string_view target);