From bc98c87f013d51709e89513ba64fb4c03378fd69 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 1 Nov 2026 19:24:02 +0100 Subject: [PATCH] fix: let a program find the shared libraries it is built on The link of an executable built on a LibraryDynamic dependency took lib.so from the library's bin dir and recorded nowhere to find it at run time: no run path pointed there and nothing copied it beside the program, which mingw does for a DLL. The program failed at start with "cannot open shared object file". A shared library now hands its bin dir up as a run path, as a CMake external does with its build directory, and a program linked against it records it. Run paths reach the linker through -Xlinker, quoted for the shell (RunPathFlags), in a program's link and in project.so's: unlike the external cache's directories, the bin dir is the project's own, and clang splits a -Wl, value at every comma its name may hold. --- implementations/Crafter.Build-Clang.cpp | 11 ++++++++--- implementations/Crafter.Build-Platform.cpp | 14 +++++++++----- interfaces/Crafter.Build-Clang.cppm | 6 ++++-- interfaces/Crafter.Build-Platform.cppm | 5 +++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index 34bb11e..5a0b343 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -1204,9 +1204,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map configs; std::string compileFlags; // " -fprebuilt-module-path=…" for each dependency and its transitive deps - // 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. + // Every dependency's BuildResult.libs, verbatim, then a run path per + // directory of its runPaths (RunPathFlags), 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(); }; @@ -589,7 +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); + for (const std::string& dir : runPaths) out.linkFlags += RunPathFlags(dir); return out; } @@ -666,6 +666,10 @@ namespace { } } +std::string Crafter::RunPathFlags(std::string_view dir) { + return std::format(" -Xlinker -rpath -Xlinker {}", ShellQuote(dir)); +} + std::string Crafter::ShellQuote(std::string_view s) { std::string out; out.reserve(s.size() + 2); diff --git a/interfaces/Crafter.Build-Clang.cppm b/interfaces/Crafter.Build-Clang.cppm index 7818a44..7499e63 100644 --- a/interfaces/Crafter.Build-Clang.cppm +++ b/interfaces/Crafter.Build-Clang.cppm @@ -54,8 +54,10 @@ export namespace Crafter { // 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. + // loaded from at run time, its dependencies' included, and, once a + // shared library is linked, its own bin dir, for its consumers. An + // executable or shared library linked from them records each one as + // a run path. std::set runPaths; }; diff --git a/interfaces/Crafter.Build-Platform.cppm b/interfaces/Crafter.Build-Platform.cppm index 32b230b..88207c4 100644 --- a/interfaces/Crafter.Build-Platform.cppm +++ b/interfaces/Crafter.Build-Platform.cppm @@ -53,6 +53,11 @@ namespace Crafter { // and not to crafter-build, which then sees it as exit code 130. // Project scripts reach this through ScriptContext::Run. std::int32_t RunCommandAttached(std::string_view command); + // The link flags that record `dir` as a run path, with -Xlinker rather + // than -Wl,: clang splits a -Wl, value at every comma, and `dir` may be a + // project's own directory, named whatever its owner chose. Quoted for + // the shell the link runs in. + std::string RunPathFlags(std::string_view dir); // `s` as one single-quoted sh word, safe to splice into a command line // whatever it contains: paths with spaces, arguments a user typed. export std::string ShellQuote(std::string_view s);