Skip to content

Commit 388ab82

Browse files
committed
[ClangImporter] Look for platform modulemaps in the resource dir first
This applies #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.
1 parent 93f9a17 commit 388ab82

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,64 @@ static std::optional<Path> getActualModuleMapPath(
3333
StringRef platform = swift::getPlatformNameForTriple(triple);
3434
StringRef arch = swift::getMajorArchitectureName(triple);
3535

36-
Path result;
37-
38-
StringRef SDKPath = Opts.getSDKPath();
39-
if (!SDKPath.empty()) {
40-
result.append(SDKPath.begin(), SDKPath.end());
41-
llvm::sys::path::append(result, "usr", "lib", "swift");
42-
llvm::sys::path::append(result, platform);
43-
if (isArchSpecific) {
44-
llvm::sys::path::append(result, arch);
36+
auto checkRuntimeResourcesPath = [&]() -> std::optional<Path> {
37+
Path result;
38+
if (!Opts.RuntimeResourcePath.empty()) {
39+
result.append(Opts.RuntimeResourcePath.begin(),
40+
Opts.RuntimeResourcePath.end());
41+
llvm::sys::path::append(result, platform);
42+
if (isArchSpecific) {
43+
llvm::sys::path::append(result, arch);
44+
}
45+
llvm::sys::path::append(result, name);
46+
47+
// Only specify the module map if that file actually exists. It may not;
48+
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
49+
// -emit-ir` is invoked using a Swift compiler not built for Linux
50+
// targets.
51+
if (vfs->exists(result))
52+
return result;
4553
}
46-
llvm::sys::path::append(result, name);
47-
48-
// Only specify the module map if that file actually exists. It may not;
49-
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
50-
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
51-
if (vfs->exists(result))
52-
return result;
53-
}
54+
return std::nullopt;
55+
};
5456

55-
if (!Opts.RuntimeResourcePath.empty()) {
56-
result.clear();
57-
result.append(Opts.RuntimeResourcePath.begin(),
58-
Opts.RuntimeResourcePath.end());
59-
llvm::sys::path::append(result, platform);
60-
if (isArchSpecific) {
61-
llvm::sys::path::append(result, arch);
57+
auto checkSDKPath = [&]() -> std::optional<Path> {
58+
Path result;
59+
StringRef SDKPath = Opts.getSDKPath();
60+
if (!SDKPath.empty()) {
61+
result.append(SDKPath.begin(), SDKPath.end());
62+
llvm::sys::path::append(result, "usr", "lib", "swift");
63+
llvm::sys::path::append(result, platform);
64+
if (isArchSpecific) {
65+
llvm::sys::path::append(result, arch);
66+
}
67+
llvm::sys::path::append(result, name);
68+
69+
// Only specify the module map if that file actually exists. It may not;
70+
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
71+
// -emit-ir` is invoked using a Swift compiler not built for Linux
72+
// targets.
73+
if (vfs->exists(result))
74+
return result;
6275
}
63-
llvm::sys::path::append(result, name);
76+
return std::nullopt;
77+
};
6478

65-
// Only specify the module map if that file actually exists. It may not;
66-
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
67-
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
68-
if (vfs->exists(result))
69-
return result;
79+
// FIXME: This is a workaround to keep the Android SDK build working.
80+
// See https://github.com/swiftlang/swift/pull/74814.
81+
if (triple.isAndroid()) {
82+
if (auto path = checkSDKPath())
83+
return path;
84+
if (auto path = checkRuntimeResourcesPath())
85+
return path;
86+
return std::nullopt;
7087
}
7188

89+
if (auto path = checkRuntimeResourcesPath())
90+
return path;
91+
if (auto path = checkSDKPath())
92+
return path;
93+
7294
return std::nullopt;
7395
}
7496

0 commit comments

Comments
 (0)