@@ -66,6 +66,11 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
6666 Path, FrontendOpts.UseSharedResourceFolder , LibPath);
6767 setRuntimeResourcePath (LibPath.str ());
6868
69+ llvm::SmallString<128 > clangPath (Path);
70+ llvm::sys::path::remove_filename (clangPath);
71+ llvm::sys::path::append (clangPath, " clang" );
72+ ClangImporterOpts.clangPath = std::string (clangPath);
73+
6974 llvm::SmallString<128 > DiagnosticDocsPath (Path);
7075 llvm::sys::path::remove_filename (DiagnosticDocsPath); // Remove /swift
7176 llvm::sys::path::remove_filename (DiagnosticDocsPath); // Remove /bin
@@ -936,6 +941,18 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
936941 StringRef workingDirectory) {
937942 using namespace options ;
938943
944+ if (const Arg *a = Args.getLastArg (OPT_tools_directory)) {
945+ // If a custom tools directory is specified, try to find Clang there.
946+ // This is useful when the Swift executable is located in a different
947+ // directory than the Clang/LLVM executables, for example, when building
948+ // the Swift project itself.
949+ llvm::SmallString<128 > clangPath (a->getValue ());
950+ llvm::sys::path::append (clangPath, " clang" );
951+ if (llvm::sys::fs::exists (clangPath)) {
952+ Opts.clangPath = std::string (clangPath);
953+ }
954+ }
955+
939956 if (const Arg *A = Args.getLastArg (OPT_module_cache_path)) {
940957 Opts.ModuleCachePath = A->getValue ();
941958 }
0 commit comments