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

@ -506,10 +506,10 @@ namespace {
struct ProjectFileDependencies {
std::vector<Configuration*> 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);