diff options
author | Bill Hoffman <bill.hoffman@kitware.com> | 2016-06-02 22:08:30 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2016-06-17 14:56:40 (GMT) |
commit | 96242f8022fa4bc718ef36cda43f5dfe6236c066 (patch) | |
tree | eb344591def11380b95fba62e71aa950e3671b87 /Source/cmcmd.cxx | |
parent | ba92e11f8b461513566d5cc3c0b53b2215bb85f7 (diff) | |
download | CMake-96242f8022fa4bc718ef36cda43f5dfe6236c066.zip CMake-96242f8022fa4bc718ef36cda43f5dfe6236c066.tar.gz CMake-96242f8022fa4bc718ef36cda43f5dfe6236c066.tar.bz2 |
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.
Diffstat (limited to 'Source/cmcmd.cxx')
-rw-r--r-- | Source/cmcmd.cxx | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 535dead..161256e 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -271,6 +271,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args) std::string iwyu; std::string tidy; std::string sourceFile; + std::string lwyu; for (std::string::size_type cc = 2; cc < args.size(); cc++) { std::string const& arg = args[cc]; if (arg == "--") { @@ -281,6 +282,8 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args) tidy = arg.substr(7); } else if (doing_options && cmHasLiteralPrefix(arg, "--source=")) { sourceFile = arg.substr(9); + } else if (doing_options && cmHasLiteralPrefix(arg, "--lwyu=")) { + lwyu = arg.substr(7); } else if (doing_options) { std::cerr << "__run_iwyu given unknown argument: " << arg << "\n"; return 1; @@ -288,7 +291,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args) orig_cmd.push_back(arg); } } - if (tidy.empty() && iwyu.empty()) { + if (tidy.empty() && iwyu.empty() && lwyu.empty()) { std::cerr << "__run_iwyu missing --tidy= or --iwyu=\n"; return 1; } @@ -296,7 +299,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args) std::cerr << "__run_iwyu --tidy= requires --source=\n"; return 1; } - if (orig_cmd.empty()) { + if (orig_cmd.empty() && lwyu.empty()) { std::cerr << "__run_iwyu missing compile command after --\n"; return 1; } @@ -345,13 +348,37 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args) std::cerr << "Error running '" << tidy_cmd[0] << "'\n"; return 1; } - // Output the stdout from clang-tidy to stderr std::cerr << stdOut; } + if (!lwyu.empty()) { + // Construct the ldd -r -u (link what you use lwyu) command line + // ldd -u -r lwuy target + std::vector<std::string> lwyu_cmd; + lwyu_cmd.push_back("ldd"); + lwyu_cmd.push_back("-u"); + lwyu_cmd.push_back("-r"); + lwyu_cmd.push_back(lwyu); + + // Run the ldd -u -r command line. + // Capture its stdout and hide its stderr. + std::string stdOut; + if (!cmSystemTools::RunSingleCommand(lwyu_cmd, &stdOut, 0, &ret, 0, + cmSystemTools::OUTPUT_NONE)) { + std::cerr << "Error running '" << lwyu_cmd[0] << "'\n"; + return 1; + } + // Output the stdout from ldd -r -u to stderr + // Warn if lwyu reported anything. + if (stdOut.find("Unused direct dependencies:") != stdOut.npos) { + std::cerr << "Warning: " << stdOut; + } + } + ret = 0; // Now run the real compiler command and return its result value. - if (!cmSystemTools::RunSingleCommand( + if (lwyu.empty() && + !cmSystemTools::RunSingleCommand( orig_cmd, 0, 0, &ret, 0, cmSystemTools::OUTPUT_PASSTHROUGH)) { std::cerr << "Error running '" << orig_cmd[0] << "'\n"; return 1; |