Author: Kousik Kumar Date: 2019-11-22T07:43:54-08:00 New Revision: 26fa9e31f58a7ff693f3a5d8c2eb1b6a5a13b133
URL: https://github.com/llvm/llvm-project/commit/26fa9e31f58a7ff693f3a5d8c2eb1b6a5a13b133 DIFF: https://github.com/llvm/llvm-project/commit/26fa9e31f58a7ff693f3a5d8c2eb1b6a5a13b133.diff LOG: Add support to find out resource dir and add it as compilation args Summary: If -resource-dir is not specified as part of the compilation command, then by default clang-scan-deps picks up a directory relative to its own path as resource-directory. This is probably not the right behavior - since resource directory should be picked relative to the path of the clang-compiler in the compilation command. This patch adds support for it along with a cache to store the resource-dir paths based on compiler paths. Notes: 1. "-resource-dir" is a behavior that's specific to clang, gcc does not have that flag. That's why if I'm not able to find a resource-dir, I quietly ignore it. 2. Should I also use the mtime of the compiler in the cache? I think its not strictly necessary since we assume the filesystem is immutable. 3. From my testing, this does not regress performance. 4. Will try to get this tested on Windows. But basically the problem that this patch is trying to solve is, clients might not always want to specify "-resource-dir" in their compile commands, so scan-deps must auto-infer it correctly. Reviewers: arphaman, Bigcheese, jkorous, dexonsmith, klimek Reviewed By: Bigcheese Subscribers: MaskRay, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69122 Added: Modified: clang/tools/clang-scan-deps/ClangScanDeps.cpp Removed: ################################################################################ diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp index bd463cbdeee9..1294e6682841 100644 --- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp @@ -13,6 +13,7 @@ #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h" #include "clang/Tooling/JSONCompilationDatabase.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/Program.h" #include "llvm/Support/Signals.h" @@ -39,6 +40,64 @@ class SharedStream { raw_ostream &OS; }; +class ResourceDirectoryCache { +public: + /// findResourceDir finds the resource directory relative to the clang + /// compiler being used in Args, by running it with "-print-resource-dir" + /// option and cache the results for reuse. \returns resource directory path + /// associated with the given invocation command or empty string if the + /// compiler path is NOT an absolute path. + StringRef findResourceDir(const tooling::CommandLineArguments &Args) { + if (Args.size() < 1) + return ""; + + const std::string &ClangBinaryPath = Args[0]; + if (!llvm::sys::path::is_absolute(ClangBinaryPath)) + return ""; + + const std::string &ClangBinaryName = + llvm::sys::path::filename(ClangBinaryPath); + + std::unique_lock<std::mutex> LockGuard(CacheLock); + const auto &CachedResourceDir = Cache.find(ClangBinaryPath); + if (CachedResourceDir != Cache.end()) + return CachedResourceDir->second; + + std::vector<StringRef> PrintResourceDirArgs{ClangBinaryName, + "-print-resource-dir"}; + llvm::SmallString<64> OutputFile, ErrorFile; + llvm::sys::fs::createTemporaryFile("print-resource-dir-output", + "" /*no-suffix*/, OutputFile); + llvm::sys::fs::createTemporaryFile("print-resource-dir-error", + "" /*no-suffix*/, ErrorFile); + llvm::FileRemover OutputRemover(OutputFile.c_str()); + llvm::FileRemover ErrorRemover(ErrorFile.c_str()); + llvm::Optional<StringRef> Redirects[] = { + {""}, // Stdin + StringRef(OutputFile), + StringRef(ErrorFile), + }; + if (const int RC = llvm::sys::ExecuteAndWait( + ClangBinaryPath, PrintResourceDirArgs, {}, Redirects)) { + auto ErrorBuf = llvm::MemoryBuffer::getFile(ErrorFile.c_str()); + llvm::errs() << ErrorBuf.get()->getBuffer(); + return ""; + } + + auto OutputBuf = llvm::MemoryBuffer::getFile(OutputFile.c_str()); + if (!OutputBuf) + return ""; + StringRef Output = OutputBuf.get()->getBuffer().rtrim('\n'); + + Cache[ClangBinaryPath] = Output.str(); + return Cache[ClangBinaryPath]; + } + +private: + std::map<std::string, std::string> Cache; + std::mutex CacheLock; +}; + llvm::cl::opt<bool> Help("h", llvm::cl::desc("Alias for -help"), llvm::cl::Hidden); @@ -169,12 +228,15 @@ int main(int argc, const char **argv) { auto AdjustingCompilations = std::make_unique<tooling::ArgumentsAdjustingCompilations>( std::move(Compilations)); + ResourceDirectoryCache ResourceDirCache; AdjustingCompilations->appendArgumentsAdjuster( - [](const tooling::CommandLineArguments &Args, StringRef FileName) { + [&ResourceDirCache](const tooling::CommandLineArguments &Args, + StringRef FileName) { std::string LastO = ""; bool HasMT = false; bool HasMQ = false; bool HasMD = false; + bool HasResourceDir = false; // We need to find the last -o value. if (!Args.empty()) { std::size_t Idx = Args.size() - 1; @@ -188,6 +250,8 @@ int main(int argc, const char **argv) { HasMQ = true; if (Args[Idx] == "-MD") HasMD = true; + if (Args[Idx] == "-resource-dir") + HasResourceDir = true; } --Idx; } @@ -215,6 +279,15 @@ int main(int argc, const char **argv) { AdjustedArgs.push_back("-Xclang"); AdjustedArgs.push_back("-sys-header-deps"); AdjustedArgs.push_back("-Wno-error"); + + if (!HasResourceDir) { + StringRef ResourceDir = + ResourceDirCache.findResourceDir(Args); + if (!ResourceDir.empty()) { + AdjustedArgs.push_back("-resource-dir"); + AdjustedArgs.push_back(ResourceDir); + } + } return AdjustedArgs; }); AdjustingCompilations->appendArgumentsAdjuster( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits