From 388ab821827ae6ce00853918184722da223b94ea Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Thu, 22 Aug 2024 15:15:32 +0100 Subject: [PATCH] [ClangImporter] Look for platform modulemaps in the resource dir first This applies https://github.com/swiftlang/swift/pull/74814 for all targets except Android. This fixes a common source of build failures on Linux, where shim functions from CxxStdlibShim would not be found by the hosttools Swift compiler when building the CxxStdlib overlay. This change is keeping Android intact to keep the existing Android SDK working. --- lib/ClangImporter/ClangIncludePaths.cpp | 82 ++++++++++++++++--------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/lib/ClangImporter/ClangIncludePaths.cpp b/lib/ClangImporter/ClangIncludePaths.cpp index 77173166ba494..0612c26e08123 100644 --- a/lib/ClangImporter/ClangIncludePaths.cpp +++ b/lib/ClangImporter/ClangIncludePaths.cpp @@ -33,42 +33,64 @@ static std::optional getActualModuleMapPath( StringRef platform = swift::getPlatformNameForTriple(triple); StringRef arch = swift::getMajorArchitectureName(triple); - Path result; - - StringRef SDKPath = Opts.getSDKPath(); - if (!SDKPath.empty()) { - result.append(SDKPath.begin(), SDKPath.end()); - llvm::sys::path::append(result, "usr", "lib", "swift"); - llvm::sys::path::append(result, platform); - if (isArchSpecific) { - llvm::sys::path::append(result, arch); + auto checkRuntimeResourcesPath = [&]() -> std::optional { + Path result; + if (!Opts.RuntimeResourcePath.empty()) { + result.append(Opts.RuntimeResourcePath.begin(), + Opts.RuntimeResourcePath.end()); + llvm::sys::path::append(result, platform); + if (isArchSpecific) { + llvm::sys::path::append(result, arch); + } + llvm::sys::path::append(result, name); + + // Only specify the module map if that file actually exists. It may not; + // for example in the case that `swiftc -target x86_64-unknown-linux-gnu + // -emit-ir` is invoked using a Swift compiler not built for Linux + // targets. + if (vfs->exists(result)) + return result; } - llvm::sys::path::append(result, name); - - // Only specify the module map if that file actually exists. It may not; - // for example in the case that `swiftc -target x86_64-unknown-linux-gnu - // -emit-ir` is invoked using a Swift compiler not built for Linux targets. - if (vfs->exists(result)) - return result; - } + return std::nullopt; + }; - if (!Opts.RuntimeResourcePath.empty()) { - result.clear(); - result.append(Opts.RuntimeResourcePath.begin(), - Opts.RuntimeResourcePath.end()); - llvm::sys::path::append(result, platform); - if (isArchSpecific) { - llvm::sys::path::append(result, arch); + auto checkSDKPath = [&]() -> std::optional { + Path result; + StringRef SDKPath = Opts.getSDKPath(); + if (!SDKPath.empty()) { + result.append(SDKPath.begin(), SDKPath.end()); + llvm::sys::path::append(result, "usr", "lib", "swift"); + llvm::sys::path::append(result, platform); + if (isArchSpecific) { + llvm::sys::path::append(result, arch); + } + llvm::sys::path::append(result, name); + + // Only specify the module map if that file actually exists. It may not; + // for example in the case that `swiftc -target x86_64-unknown-linux-gnu + // -emit-ir` is invoked using a Swift compiler not built for Linux + // targets. + if (vfs->exists(result)) + return result; } - llvm::sys::path::append(result, name); + return std::nullopt; + }; - // Only specify the module map if that file actually exists. It may not; - // for example in the case that `swiftc -target x86_64-unknown-linux-gnu - // -emit-ir` is invoked using a Swift compiler not built for Linux targets. - if (vfs->exists(result)) - return result; + // FIXME: This is a workaround to keep the Android SDK build working. + // See https://github.com/swiftlang/swift/pull/74814. + if (triple.isAndroid()) { + if (auto path = checkSDKPath()) + return path; + if (auto path = checkRuntimeResourcesPath()) + return path; + return std::nullopt; } + if (auto path = checkRuntimeResourcesPath()) + return path; + if (auto path = checkSDKPath()) + return path; + return std::nullopt; }