Skip to content

Commit 5a29cdc

Browse files
author
Harlan Haskins
committed
[ClangImporter] Allow importing without a resource dir on non-Darwin
Previously, we would assert that there's a runtime resource dir available, but then accept the possibility of no glibc.modulemap. We should just do the same thing for 'no resource dir' as 'no module map'.
1 parent 9ef4b5f commit 5a29cdc

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,35 @@ void ClangImporter::clearTypeResolver() {
407407

408408
#pragma mark Module loading
409409

410+
/// Finds the glibc.modulemap file relative to the provided resource dir.
411+
///
412+
/// Note that the module map used for Glibc depends on the target we're
413+
/// compiling for, and is not included in the resource directory with the other
414+
/// implicit module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
415+
static Optional<StringRef>
416+
getGlibcModuleMapPath(StringRef resourceDir, llvm::Triple triple,
417+
SmallVectorImpl<char> &scratch) {
418+
if (resourceDir.empty())
419+
return None;
420+
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;
436+
}
437+
}
438+
410439
static void
411440
getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
412441
ASTContext &ctx,
@@ -571,28 +600,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
571600
}
572601
}
573602

574-
// The module map used for Glibc depends on the target we're compiling for,
575-
// and is not included in the resource directory with the other implicit
576-
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
577603
SmallString<128> GlibcModuleMapPath;
578-
GlibcModuleMapPath = searchPathOpts.RuntimeResourcePath;
579-
580-
// Running without a resource directory is not a supported configuration.
581-
assert(!GlibcModuleMapPath.empty());
582-
583-
llvm::sys::path::append(
584-
GlibcModuleMapPath,
585-
swift::getPlatformNameForTriple(triple),
586-
swift::getMajorArchitectureName(triple),
587-
"glibc.modulemap");
588-
589-
// Only specify the module map if that file actually exists.
590-
// It may not--for example in the case that
591-
// `swiftc -target x86_64-unknown-linux-gnu -emit-ir` is invoked using
592-
// a Swift compiler not built for Linux targets.
593-
if (llvm::sys::fs::exists(GlibcModuleMapPath)) {
594-
invocationArgStrs.push_back(
595-
(Twine("-fmodule-map-file=") + GlibcModuleMapPath).str());
604+
if (auto path = getGlibcModuleMapPath(searchPathOpts.RuntimeResourcePath,
605+
triple, GlibcModuleMapPath)) {
606+
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
596607
} else {
597608
// FIXME: Emit a warning of some kind.
598609
}

0 commit comments

Comments
 (0)