Skip to content

Commit 370aa9d

Browse files
committed
ClangImporter: search for glbc.modulemap in the SDK
When cross-compiling with a swift built for another target or when building with a pure toolchain without the resource directory containing host content, the modulemap required for Linux may not be found. In such a case, we should also look for the content in the SDK if one is provided. This improves the behaviour of `swiftc` for Linux with a standalone SDK.
1 parent 61261fc commit 370aa9d

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -413,27 +413,38 @@ void ClangImporter::clearTypeResolver() {
413413
/// compiling for, and is not included in the resource directory with the other
414414
/// implicit module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
415415
static Optional<StringRef>
416-
getGlibcModuleMapPath(StringRef resourceDir, llvm::Triple triple,
417-
SmallVectorImpl<char> &scratch) {
418-
if (resourceDir.empty())
419-
return None;
416+
getGlibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
417+
SmallVectorImpl<char> &buffer) {
418+
StringRef platform = swift::getPlatformNameForTriple(triple);
419+
StringRef arch = swift::getMajorArchitectureName(triple);
420420

421-
scratch.append(resourceDir.begin(), resourceDir.end());
422-
llvm::sys::path::append(
423-
scratch,
424-
swift::getPlatformNameForTriple(triple),
425-
swift::getMajorArchitectureName(triple),
426-
"glibc.modulemap");
427-
428-
// Only specify the module map if that file actually exists.
429-
// It may not--for example in the case that
430-
// `swiftc -target x86_64-unknown-linux-gnu -emit-ir` is invoked using
431-
// a Swift compiler not built for Linux targets.
432-
if (llvm::sys::fs::exists(scratch)) {
433-
return StringRef(scratch.data(), scratch.size());
434-
} else {
435-
return None;
421+
if (!Opts.SDKPath.empty()) {
422+
buffer.clear();
423+
buffer.append(Opts.SDKPath.begin(), Opts.SDKPath.end());
424+
llvm::sys::path::append(buffer, "usr", "lib", "swift");
425+
llvm::sys::path::append(buffer, platform, arch, "glibc.modulemap");
426+
427+
// Only specify the module map if that file actually exists. It may not;
428+
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
429+
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
430+
if (llvm::sys::fs::exists(buffer))
431+
return StringRef(buffer.data(), buffer.size());
436432
}
433+
434+
if (!Opts.RuntimeResourcePath.empty()) {
435+
buffer.clear();
436+
buffer.append(Opts.RuntimeResourcePath.begin(),
437+
Opts.RuntimeResourcePath.end());
438+
llvm::sys::path::append(buffer, platform, arch, "glibc.modulemap");
439+
440+
// Only specify the module map if that file actually exists. It may not;
441+
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
442+
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
443+
if (llvm::sys::fs::exists(buffer))
444+
return StringRef(buffer.data(), buffer.size());
445+
}
446+
447+
return None;
437448
}
438449

439450
static void
@@ -606,9 +617,8 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
606617
}
607618
}
608619

609-
SmallString<128> GlibcModuleMapPath;
610-
if (auto path = getGlibcModuleMapPath(searchPathOpts.RuntimeResourcePath,
611-
triple, GlibcModuleMapPath)) {
620+
SmallString<128> buffer;
621+
if (auto path = getGlibcModuleMapPath(searchPathOpts, triple, buffer)) {
612622
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
613623
} else {
614624
// FIXME: Emit a warning of some kind.

0 commit comments

Comments
 (0)