Skip to content

Commit

Permalink
linker: Add RPaths after LDFLAGS_BEFORE
Browse files Browse the repository at this point in the history
Otherwise, the calling environment has no way to override RPaths set
automatically to in the linker stage. This is needed to allow program
install RPaths to superscede the detected dependency ones.
  • Loading branch information
wkennington committed Dec 11, 2019
1 parent 6d4a232 commit 5a9759e
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,23 @@ constexpr bool remove_dynamic_linker = false;
constexpr nonstd::string_view dynamic_linker = "";
#endif

void maybeFilterLinking(std::vector<nonstd::string_view> &new_args,
nonstd::span<const nonstd::string_view> old_args, bool is_linking) {
if (!is_linking)
args::filterLinking(new_args, old_args);
else
for (auto arg : old_args)
new_args.push_back(arg);
}

int main(const bins::Info &info, nonstd::span<const nonstd::string_view> args) {
std::vector<nonstd::string_view> combined_args;
std::vector<nonstd::string_view> combined_args, combined_args_begin;
if (!dynamic_linker.empty() && args::hasDynamicLinker(args)) {
combined_args.push_back("-dynamic-linker");
combined_args.push_back(dynamic_linker);
combined_args_begin.push_back("-dynamic-linker");
combined_args_begin.push_back(dynamic_linker);
}
flags::appendFromString(combined_args, WRAPPER_LDFLAGS_BEFORE);
flags::appendFromVar(combined_args, VAR_PREFIX "_LDFLAGS_BEFORE");
flags::appendFromString(combined_args_begin, WRAPPER_LDFLAGS_BEFORE);
flags::appendFromVar(combined_args_begin, VAR_PREFIX "_LDFLAGS_BEFORE");

std::vector<nonstd::string_view> initial_args;
compiler::filterFlags(initial_args, args, remove_dynamic_linker);
Expand All @@ -50,14 +59,18 @@ int main(const bins::Info &info, nonstd::span<const nonstd::string_view> args) {
flags::appendFromVar(combined_args, VAR_PREFIX "_LDFLAGS_DYNAMIC");
}

std::vector<nonstd::string_view> filtered_args;
std::vector<nonstd::string_view> filtered_args, filtered_args_begin;
path::appendGood(filtered_args_begin, combined_args_begin, env::purePrefixes());
path::appendGood(filtered_args, combined_args, env::purePrefixes());

const bool is_linking = args::isLinking(args);
std::vector<nonstd::string_view> final_args;
maybeFilterLinking(final_args, filtered_args_begin, is_linking);
state::Libs libs;
const bool add_rpath =
util::getenv(VAR_PREFIX "_LD_ADD_RPATH").value_or("1") == "1";
if (dynamic && add_rpath) {
args::parseLibs(libs, filtered_args_begin);
args::parseLibs(libs, filtered_args);
#ifdef BUILD_DIR_ENV_VAR
auto build_dir = util::getenv(BUILD_DIR_ENV_VAR);
Expand All @@ -72,11 +85,7 @@ int main(const bins::Info &info, nonstd::span<const nonstd::string_view> args) {
final_args.push_back(rpath);
}
}
if (!args::isLinking(args))
args::filterLinking(final_args, filtered_args);
else
for (auto arg : filtered_args)
final_args.push_back(arg);
maybeFilterLinking(final_args, filtered_args, is_linking);
return generic::main(info, final_args);
}

Expand Down

0 comments on commit 5a9759e

Please sign in to comment.