fix: let a program find the shared libraries it is built on

The link of an executable built on a LibraryDynamic dependency took
lib<name>.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.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 19:24:02 +01:00
commit bc98c87f01
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 26 additions and 10 deletions

View file

@ -1204,9 +1204,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// The program loads its shared dependencies from where they were built, // The program loads its shared dependencies from where they were built,
// the external cache among them. ExternalOutputs leaves the mingw target // the external cache among them. ExternalOutputs leaves the mingw target
// without any: PE resolves DLLs another way. // without any: PE resolves DLLs another way.
for (const std::string& dir : buildResult.runPaths) { for (const std::string& dir : buildResult.runPaths) linkExtras += RunPathFlags(dir);
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.
@ -1406,6 +1404,13 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
buildResult.libs.insert(std::format("-L{}", outputDir.string())); buildResult.libs.insert(std::format("-L{}", outputDir.string()));
buildResult.libs.insert(std::format("-l{}", config.outputName)); buildResult.libs.insert(std::format("-l{}", config.outputName));
} }
// A program built on a shared library loads it from where it was linked
// from: its run path names this directory. Only for the consumers: the
// library's own link is done. On Windows a DLL is found beside the
// program instead, where the mingw link copies it.
if (config.type == ConfigurationType::LibraryDynamic && config.target != "x86_64-w64-mingw32") {
buildResult.runPaths.insert(fs::absolute(outputDir).string());
}
// Browser-wasm feature-detected variants. The baseline outputName.wasm is // Browser-wasm feature-detected variants. The baseline outputName.wasm is
// now built (above). For each declared variant, recompile the entire build // now built (above). For each declared variant, recompile the entire build

View file

@ -506,10 +506,10 @@ 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
// Every dependency's BuildResult.libs, verbatim, then an -Wl,-rpath // Every dependency's BuildResult.libs, verbatim, then a run path per
// per directory of its runPaths, as Build() links a program: the // directory of its runPaths (RunPathFlags), as Build() links a
// shared libraries a pragma dependency is built on are found there // program: the shared libraries a pragma dependency is built on are
// when LoadProject dlopens project.so. // found there when LoadProject dlopens project.so.
std::string linkFlags; std::string linkFlags;
fs::file_time_type newestArtifact = fs::file_time_type::min(); fs::file_time_type newestArtifact = fs::file_time_type::min();
}; };
@ -589,7 +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); for (const std::string& dir : runPaths) out.linkFlags += RunPathFlags(dir);
return out; 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 Crafter::ShellQuote(std::string_view s) {
std::string out; std::string out;
out.reserve(s.size() + 2); out.reserve(s.size() + 2);

View file

@ -54,8 +54,10 @@ export namespace Crafter {
// 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 // Absolute directories the shared libraries this links against are
// loaded from at run time, its dependencies' included. An executable // loaded from at run time, its dependencies' included, and, once a
// or shared library linked from them records each one as a run path. // 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<std::string> runPaths; std::set<std::string> runPaths;
}; };

View file

@ -53,6 +53,11 @@ namespace Crafter {
// and not to crafter-build, which then sees it as exit code 130. // and not to crafter-build, which then sees it as exit code 130.
// Project scripts reach this through ScriptContext::Run. // Project scripts reach this through ScriptContext::Run.
std::int32_t RunCommandAttached(std::string_view command); 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 // `s` as one single-quoted sh word, safe to splice into a command line
// whatever it contains: paths with spaces, arguments a user typed. // whatever it contains: paths with spaces, arguments a user typed.
export std::string ShellQuote(std::string_view s); export std::string ShellQuote(std::string_view s);