forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-scan-deps] Infer the tool locations from PATH (llvm#108539)
This allows the clang driver to know which tool is meant to be executed, which allows the clang driver to load the right clang config files, and allows clang to find colocated sysroots. This makes sure that doing `clang-scan-deps -- <tool> ...` looks up things in the same way as if one just would execute `<tool> ...`, when `<tool>` isn't an absolute or relative path. (cherry picked from commit a26ec54)
- Loading branch information
Showing
12 changed files
with
120 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//===- GuessTargetAndModeCompilationDatabase.cpp --------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/Tooling/CompilationDatabase.h" | ||
#include "clang/Tooling/Tooling.h" | ||
#include "llvm/Support/Path.h" | ||
#include "llvm/Support/Program.h" | ||
#include <memory> | ||
|
||
namespace clang { | ||
namespace tooling { | ||
|
||
namespace { | ||
class LocationAdderDatabase : public CompilationDatabase { | ||
public: | ||
LocationAdderDatabase(std::unique_ptr<CompilationDatabase> Base) | ||
: Base(std::move(Base)) { | ||
assert(this->Base != nullptr); | ||
} | ||
|
||
std::vector<std::string> getAllFiles() const override { | ||
return Base->getAllFiles(); | ||
} | ||
|
||
std::vector<CompileCommand> getAllCompileCommands() const override { | ||
return addLocation(Base->getAllCompileCommands()); | ||
} | ||
|
||
std::vector<CompileCommand> | ||
getCompileCommands(StringRef FilePath) const override { | ||
return addLocation(Base->getCompileCommands(FilePath)); | ||
} | ||
|
||
private: | ||
std::vector<CompileCommand> | ||
addLocation(std::vector<CompileCommand> Cmds) const { | ||
for (auto &Cmd : Cmds) { | ||
if (Cmd.CommandLine.empty()) | ||
continue; | ||
std::string &Driver = Cmd.CommandLine.front(); | ||
// If the driver name already is absolute, we don't need to do anything. | ||
if (llvm::sys::path::is_absolute(Driver)) | ||
continue; | ||
// If the name is a relative path, like bin/clang, we assume it's | ||
// possible to resolve it and don't do anything about it either. | ||
if (llvm::any_of(Driver, | ||
[](char C) { return llvm::sys::path::is_separator(C); })) | ||
continue; | ||
auto Absolute = llvm::sys::findProgramByName(Driver); | ||
// If we found it in path, update the entry in Cmd.CommandLine | ||
if (Absolute && llvm::sys::path::is_absolute(*Absolute)) | ||
Driver = std::move(*Absolute); | ||
} | ||
return Cmds; | ||
} | ||
std::unique_ptr<CompilationDatabase> Base; | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<CompilationDatabase> | ||
inferToolLocation(std::unique_ptr<CompilationDatabase> Base) { | ||
return std::make_unique<LocationAdderDatabase>(std::move(Base)); | ||
} | ||
|
||
} // namespace tooling | ||
} // namespace clang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// UNSUPPORTED: system-windows | ||
|
||
// Check that we expand the executable name to an absolute path, when invoked | ||
// with a plain executable name, which is implied to be found in PATH. | ||
// REQUIRES: x86-registered-target | ||
|
||
// RUN: rm -rf %t | ||
// RUN: mkdir -p %t/bin | ||
// RUN: ln -s %clang %t/bin/x86_64-w64-mingw32-clang | ||
// RUN: split-file %s %t | ||
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json | ||
|
||
// Check that we can deduce this both when using a compilation database, and when using | ||
// a literal command line. | ||
|
||
// RUN: env "PATH=%t/bin:%PATH%" clang-scan-deps -format experimental-full -compilation-database %t/cdb.json | FileCheck %s -DBASE=%/t | ||
|
||
// RUN: env "PATH=%t/bin:%PATH%" clang-scan-deps -format experimental-full -- x86_64-w64-mingw32-clang %t/source.c -o %t/source.o | FileCheck %s -DBASE=%/t | ||
|
||
// CHECK: "executable": "[[BASE]]/bin/x86_64-w64-mingw32-clang" | ||
|
||
//--- cdb.json.in | ||
[ | ||
{ | ||
"directory": "DIR" | ||
"command": "x86_64-w64-mingw32-clang -c DIR/source.c -o DIR/source.o" | ||
"file": "DIR/source.c" | ||
}, | ||
] | ||
|
||
//--- source.c | ||
void func(void) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters