ClangImporter: run Clang with a proper executable path (pt 2)

This change re-applies https://github.com/apple/swift/pull/36749 after it has been reverted in https://github.com/apple/swift/pull/37805 because of a broken standalone stdlib build.
This commit is contained in:
Egor Zhdan
2021-06-06 15:31:11 +03:00
parent 3368d9c597
commit 64113270df
12 changed files with 62 additions and 3 deletions

View File

@@ -66,6 +66,11 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
Path, FrontendOpts.UseSharedResourceFolder, LibPath);
setRuntimeResourcePath(LibPath.str());
llvm::SmallString<128> clangPath(Path);
llvm::sys::path::remove_filename(clangPath);
llvm::sys::path::append(clangPath, "clang");
ClangImporterOpts.clangPath = std::string(clangPath);
llvm::SmallString<128> DiagnosticDocsPath(Path);
llvm::sys::path::remove_filename(DiagnosticDocsPath); // Remove /swift
llvm::sys::path::remove_filename(DiagnosticDocsPath); // Remove /bin
@@ -952,6 +957,18 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
StringRef workingDirectory) {
using namespace options;
if (const Arg *a = Args.getLastArg(OPT_tools_directory)) {
// If a custom tools directory is specified, try to find Clang there.
// This is useful when the Swift executable is located in a different
// directory than the Clang/LLVM executables, for example, when building
// the Swift project itself.
llvm::SmallString<128> clangPath(a->getValue());
llvm::sys::path::append(clangPath, "clang");
if (llvm::sys::fs::exists(clangPath)) {
Opts.clangPath = std::string(clangPath);
}
}
if (const Arg *A = Args.getLastArg(OPT_module_cache_path)) {
Opts.ModuleCachePath = A->getValue();
}