From aa6b555395002d8a005003635f5037743bfc3690 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 1 Nov 2026 18:56:41 +0100 Subject: [PATCH] refactor: carry run paths as directories, not -Wl,-rpath flags ExternalOutputs put an -Wl,-rpath, 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. --- implementations/Crafter.Build-Clang.cpp | 10 ++++++++++ implementations/Crafter.Build-External.cpp | 16 ++++++++-------- implementations/Crafter.Build-Platform.cpp | 9 ++++++++- interfaces/Crafter.Build-Clang.cppm | 4 ++++ interfaces/Crafter.Build-External.cppm | 9 +++++++-- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index 1105f74..34bb11e 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -906,6 +906,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map libSet; std::set publicFlagSet; + std::set runPathSet; std::mutex fileMutex; std::vector depThreads; depThreads.reserve(config.dependencies.size()); @@ -921,6 +922,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map externalFloor) externalFloor = ext.latestArtifact; } @@ -1197,6 +1201,12 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map 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 forced; std::unordered_set walked; std::set libs; + std::set 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; } diff --git a/interfaces/Crafter.Build-Clang.cppm b/interfaces/Crafter.Build-Clang.cppm index 38f99b3..7818a44 100644 --- a/interfaces/Crafter.Build-Clang.cppm +++ b/interfaces/Crafter.Build-Clang.cppm @@ -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 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 runPaths; }; struct Define { diff --git a/interfaces/Crafter.Build-External.cppm b/interfaces/Crafter.Build-External.cppm index 054771e..f21dc47 100644 --- a/interfaces/Crafter.Build-External.cppm +++ b/interfaces/Crafter.Build-External.cppm @@ -38,6 +38,11 @@ export namespace Crafter { struct ExternalBuildResult { std::vector compileFlags; std::vector 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 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);