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 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);
}
for (const std::string& dir : buildResult.runPaths) linkExtras += RunPathFlags(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.
@ -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{}", 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
// now built (above). For each declared variant, recompile the entire build