diff --git a/clang/test/ClangScanDeps/Inputs/resource_directory/cdb.json.template b/clang/test/ClangScanDeps/Inputs/resource_directory/cdb.json.template new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/resource_directory/cdb.json.template @@ -0,0 +1,7 @@ +[ + { + "directory": "DIR", + "command": "CLANG -fmodules -gmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -c DIR/tu.c -o DIR/tu.o", + "file": "DIR/tu.c" + } +] diff --git a/clang/test/ClangScanDeps/Inputs/resource_directory/compiler b/clang/test/ClangScanDeps/Inputs/resource_directory/compiler new file mode 100755 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/resource_directory/compiler @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +print("/custom/compiler/resources") diff --git a/clang/test/ClangScanDeps/Inputs/resource_directory/mod.h b/clang/test/ClangScanDeps/Inputs/resource_directory/mod.h new file mode 100644 diff --git a/clang/test/ClangScanDeps/Inputs/resource_directory/module.modulemap b/clang/test/ClangScanDeps/Inputs/resource_directory/module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/resource_directory/module.modulemap @@ -0,0 +1 @@ +module mod { header "mod.h" } diff --git a/clang/test/ClangScanDeps/Inputs/resource_directory/tu.c b/clang/test/ClangScanDeps/Inputs/resource_directory/tu.c new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/resource_directory/tu.c @@ -0,0 +1 @@ +#include "mod.h" diff --git a/clang/test/ClangScanDeps/resource_directory.c b/clang/test/ClangScanDeps/resource_directory.c new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/resource_directory.c @@ -0,0 +1,23 @@ +// RUN: rm -rf %t && mkdir %t +// RUN: cp %S/Inputs/resource_directory/* %t + +// Deduce the resource directory from the compiler path. +// +// RUN: sed -e "s|CLANG|/our/custom/bin/clang|g" -e "s|DIR|%/t|g" \ +// RUN: %S/Inputs/resource_directory/cdb.json.template > %t/cdb_path.json +// RUN: clang-scan-deps -compilation-database %t/cdb_path.json --format experimental-full \ +// RUN: --resource-dir-recipe modify-compiler-path > %t/result_path.json +// RUN: cat %t/result_path.json | sed 's:\\\\\?:/:g' | FileCheck %s --check-prefix=CHECK-PATH +// CHECK-PATH: "-resource-dir" +// CHECK-PATH-NEXT: "/our/custom/lib{{.*}}" + +// Run the compiler and ask it for the resource directory. +// +// RUN: chmod +x %t/compiler +// RUN: sed -e "s|CLANG|%/t/compiler|g" -e "s|DIR|%/t|g" \ +// RUN: %S/Inputs/resource_directory/cdb.json.template > %t/cdb_invocation.json +// RUN: clang-scan-deps -compilation-database %t/cdb_invocation.json --format experimental-full \ +// RUN: --resource-dir-recipe invoke-compiler > %t/result_invocation.json +// RUN: cat %t/result_invocation.json | sed 's:\\\\\?:/:g' | FileCheck %s --check-prefix=CHECK-INVOCATION +// CHECK-INVOCATION: "-resource-dir" +// CHECK-INVOCATION-NEXT: "/custom/compiler/resources" diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp --- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp @@ -194,6 +194,25 @@ "until reaching the end directive."), llvm::cl::init(true), llvm::cl::cat(DependencyScannerCategory)); +enum ResourceDirRecipeKind { + RDRK_ModifyCompilerPath, + RDRK_InvokeCompiler, +}; + +static llvm::cl::opt ResourceDirRecipe( + "resource-dir-recipe", + llvm::cl::desc("How to produce missing '-resource-dir' argument"), + llvm::cl::values( + clEnumValN(RDRK_ModifyCompilerPath, "modify-compiler-path", + "Construct the resource directory from the compiler path in " + "the compilation database. This assumes it's part of the " + "same toolchain as this clang-scan-deps. (default)"), + clEnumValN(RDRK_InvokeCompiler, "invoke-compiler", + "Invoke the compiler with '-print-resource-dir' and use the " + "reported path as the resource directory. (deprecated)")), + llvm::cl::init(RDRK_ModifyCompilerPath), + llvm::cl::cat(DependencyScannerCategory)); + llvm::cl::opt Verbose("v", llvm::cl::Optional, llvm::cl::desc("Use verbose output."), llvm::cl::init(false), @@ -485,7 +504,7 @@ AdjustedArgs.push_back("/clang:" + LastO); } - if (!HasResourceDir) { + if (!HasResourceDir && ResourceDirRecipe == RDRK_InvokeCompiler) { StringRef ResourceDir = ResourceDirCache.findResourceDir(Args, ClangCLMode); if (!ResourceDir.empty()) {