@@ -407,6 +407,35 @@ void ClangImporter::clearTypeResolver() {
407
407
408
408
#pragma mark Module loading
409
409
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
+
410
439
static void
411
440
getNormalInvocationArguments (std::vector<std::string> &invocationArgStrs,
412
441
ASTContext &ctx,
@@ -571,28 +600,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
571
600
}
572
601
}
573
602
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.
577
603
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 ());
596
607
} else {
597
608
// FIXME: Emit a warning of some kind.
598
609
}
0 commit comments