mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-06 05:40:54 -06:00
Add options to run ldd -u -r as a "link-what-you-use" tool
Create a LINK_WHAT_YOU_USE target property and corresponding CMAKE_LINK_WHAT_YOU_USE variable to enable this behavior. Extend link commands by running `ldd -u -r` to detect shared libraries that are linked but not needed.
This commit is contained in:
@@ -163,6 +163,9 @@ void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
|
||||
extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
|
||||
this->AddModuleDefinitionFlag(extraFlags);
|
||||
|
||||
if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
|
||||
this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
|
||||
}
|
||||
this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
|
||||
}
|
||||
|
||||
@@ -682,6 +685,15 @@ void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
|
||||
// Get the set of commands.
|
||||
std::string linkRule = this->GetLinkRule(linkRuleVar);
|
||||
cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
|
||||
if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE") &&
|
||||
(this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY)) {
|
||||
std::string cmakeCommand =
|
||||
this->Convert(cmSystemTools::GetCMakeCommand(),
|
||||
cmLocalGenerator::NONE, cmLocalGenerator::SHELL);
|
||||
cmakeCommand += " -E __run_iwyu --lwyu=";
|
||||
cmakeCommand += targetOutPathReal;
|
||||
real_link_commands.push_back(cmakeCommand);
|
||||
}
|
||||
|
||||
// Expand placeholders.
|
||||
for (std::vector<std::string>::iterator i = real_link_commands.begin();
|
||||
@@ -728,6 +740,7 @@ void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
|
||||
commands.insert(commands.end(), commands1.begin(), commands1.end());
|
||||
commands1.clear();
|
||||
}
|
||||
|
||||
// Add the post-build rules when building but not when relinking.
|
||||
if (!relink) {
|
||||
this->LocalGenerator->AppendCustomCommands(
|
||||
|
||||
Reference in New Issue
Block a user