Skip to content

Commit 87e1104

Browse files
authored
[clang-scan-deps] Infer the target from the executable name (#108189)
This allows clang-scan-deps to work correctly when using cross compilers with names like <triple>-clang.
1 parent e0cd11e commit 87e1104

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Check that we can detect an implicit target when clang is invoked as
2+
// <triple->clang. Using an implicit triple requires that the target actually
3+
// is available, too.
4+
// REQUIRES: x86-registered-target
5+
6+
// RUN: rm -rf %t
7+
// RUN: split-file %s %t
8+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
9+
10+
// Check that we can deduce this both when using a compilation database, and when using
11+
// a literal command line.
12+
13+
// RUN: clang-scan-deps -format experimental-full -compilation-database %t/cdb.json | FileCheck %s
14+
15+
// RUN: clang-scan-deps -format experimental-full -- x86_64-w64-mingw32-clang %t/source.c -o %t/source.o | FileCheck %s
16+
17+
// CHECK: "-triple",
18+
// CHECK-NEXT: "x86_64-w64-windows-gnu",
19+
20+
21+
//--- cdb.json.in
22+
[
23+
{
24+
"directory": "DIR"
25+
"command": "x86_64-w64-mingw32-clang -c DIR/source.c -o DIR/source.o"
26+
"file": "DIR/source.c"
27+
},
28+
]
29+
30+
//--- source.c
31+
void func(void) {}

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
1616
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
1717
#include "clang/Tooling/JSONCompilationDatabase.h"
18+
#include "clang/Tooling/Tooling.h"
1819
#include "llvm/ADT/STLExtras.h"
1920
#include "llvm/ADT/Twine.h"
2021
#include "llvm/Support/CommandLine.h"
@@ -24,6 +25,7 @@
2425
#include "llvm/Support/LLVMDriver.h"
2526
#include "llvm/Support/Program.h"
2627
#include "llvm/Support/Signals.h"
28+
#include "llvm/Support/TargetSelect.h"
2729
#include "llvm/Support/ThreadPool.h"
2830
#include "llvm/Support/Threading.h"
2931
#include "llvm/Support/Timer.h"
@@ -795,6 +797,7 @@ getCompilationDatabase(int argc, char **argv, std::string &ErrorMessage) {
795797
}
796798

797799
int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
800+
llvm::InitializeAllTargetInfos();
798801
std::string ErrorMessage;
799802
std::unique_ptr<tooling::CompilationDatabase> Compilations =
800803
getCompilationDatabase(argc, argv, ErrorMessage);
@@ -810,6 +813,8 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
810813
Compilations = expandResponseFiles(std::move(Compilations),
811814
llvm::vfs::getRealFileSystem());
812815

816+
Compilations = inferTargetAndDriverMode(std::move(Compilations));
817+
813818
// The command options are rewritten to run Clang in preprocessor only mode.
814819
auto AdjustingCompilations =
815820
std::make_unique<tooling::ArgumentsAdjustingCompilations>(

0 commit comments

Comments
 (0)