@@ -396,25 +396,49 @@ static void getLibStdCxxFileMapping(
396
396
397
397
namespace {
398
398
std::string
399
- GetWindowsAuxiliaryFile (StringRef modulemap, const SearchPathOptions &Options) {
399
+ GetPlatformAuxiliaryFile (StringRef Platform, StringRef File,
400
+ const SearchPathOptions &Options) {
400
401
StringRef SDKPath = Options.getSDKPath ();
401
402
if (!SDKPath.empty ()) {
402
403
llvm::SmallString<261 > path{SDKPath};
403
- llvm::sys::path::append (path, " usr" , " share" , modulemap );
404
+ llvm::sys::path::append (path, " usr" , " share" , File );
404
405
if (llvm::sys::fs::exists (path))
405
406
return path.str ().str ();
406
407
}
407
408
408
409
if (!Options.RuntimeResourcePath .empty ()) {
409
410
llvm::SmallString<261 > path{Options.RuntimeResourcePath };
410
- llvm::sys::path::append (path, " windows " , modulemap );
411
+ llvm::sys::path::append (path, Platform, File );
411
412
if (llvm::sys::fs::exists (path))
412
413
return path.str ().str ();
413
414
}
414
415
415
416
return " " ;
416
417
}
417
418
419
+ SmallVector<std::pair<std::string, std::string>, 2 >
420
+ GetAndroidFileMappings (
421
+ ASTContext &Context, const std::string &sysroot,
422
+ const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS) {
423
+ const llvm::Triple &Triple = Context.LangOpts .Target ;
424
+ const SearchPathOptions &SearchPathOpts = Context.SearchPathOpts ;
425
+ SmallVector<std::pair<std::string, std::string>, 2 > Mappings;
426
+ std::string AuxiliaryFile;
427
+
428
+ if (!Triple.isAndroid ()) return Mappings;
429
+
430
+ llvm::SmallString<261 > NDKInjection{sysroot};
431
+ llvm::sys::path::append (NDKInjection, " posix_filesystem.apinotes" );
432
+
433
+ AuxiliaryFile =
434
+ GetPlatformAuxiliaryFile (" android" , " posix_filesystem.apinotes" ,
435
+ SearchPathOpts);
436
+ if (!AuxiliaryFile.empty ())
437
+ Mappings.emplace_back (std::string (NDKInjection), AuxiliaryFile);
438
+
439
+ return Mappings;
440
+ }
441
+
418
442
SmallVector<std::pair<std::string, std::string>, 2 > GetWindowsFileMappings (
419
443
ASTContext &Context,
420
444
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
@@ -449,7 +473,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
449
473
llvm::sys::path::append (WinSDKInjection, WindowsSDK.IncludeVersion , " um" );
450
474
llvm::sys::path::append (WinSDKInjection, " module.modulemap" );
451
475
452
- AuxiliaryFile = GetWindowsAuxiliaryFile (" winsdk.modulemap" , SearchPathOpts);
476
+ AuxiliaryFile =
477
+ GetPlatformAuxiliaryFile (" windows" , " winsdk.modulemap" , SearchPathOpts);
453
478
if (!AuxiliaryFile.empty ())
454
479
Mappings.emplace_back (std::string (WinSDKInjection), AuxiliaryFile);
455
480
}
@@ -465,7 +490,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
465
490
llvm::sys::path::append (UCRTInjection, " Include" , UCRTSDK.Version , " ucrt" );
466
491
llvm::sys::path::append (UCRTInjection, " module.modulemap" );
467
492
468
- AuxiliaryFile = GetWindowsAuxiliaryFile (" ucrt.modulemap" , SearchPathOpts);
493
+ AuxiliaryFile =
494
+ GetPlatformAuxiliaryFile (" windows" , " ucrt.modulemap" , SearchPathOpts);
469
495
if (!AuxiliaryFile.empty ()) {
470
496
// The ucrt module map has the C standard library headers all together.
471
497
// That leads to module cycles with the clang _Builtin_ modules. e.g.
@@ -502,14 +528,16 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
502
528
503
529
llvm::sys::path::append (VCToolsInjection, " module.modulemap" );
504
530
AuxiliaryFile =
505
- GetWindowsAuxiliaryFile (" vcruntime.modulemap" , SearchPathOpts);
531
+ GetPlatformAuxiliaryFile (" windows" , " vcruntime.modulemap" ,
532
+ SearchPathOpts);
506
533
if (!AuxiliaryFile.empty ())
507
534
Mappings.emplace_back (std::string (VCToolsInjection), AuxiliaryFile);
508
535
509
536
llvm::sys::path::remove_filename (VCToolsInjection);
510
537
llvm::sys::path::append (VCToolsInjection, " vcruntime.apinotes" );
511
538
AuxiliaryFile =
512
- GetWindowsAuxiliaryFile (" vcruntime.apinotes" , SearchPathOpts);
539
+ GetPlatformAuxiliaryFile (" windows" , " vcruntime.apinotes" ,
540
+ SearchPathOpts);
513
541
if (!AuxiliaryFile.empty ())
514
542
Mappings.emplace_back (std::string (VCToolsInjection), AuxiliaryFile);
515
543
}
@@ -525,6 +553,7 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
525
553
vfs = llvm::vfs::getRealFileSystem ();
526
554
527
555
const llvm::Triple &triple = ctx.LangOpts .Target ;
556
+ llvm::SmallString<256 > sysroot;
528
557
529
558
// For modulemaps that have all the C standard library headers together in
530
559
// a single module, we end up with module cycles with the clang _Builtin_
@@ -560,6 +589,11 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
560
589
StringRef headerFiles[] = {" SwiftAndroidNDK.h" , " SwiftBionic.h" };
561
590
libcFileMapping =
562
591
getLibcFileMapping (ctx, " android.modulemap" , headerFiles, vfs);
592
+
593
+ if (!libcFileMapping.empty ()) {
594
+ sysroot = libcFileMapping[0 ].first ;
595
+ llvm::sys::path::remove_filename (sysroot);
596
+ }
563
597
} else if (triple.isOSGlibc () || triple.isOSOpenBSD () ||
564
598
triple.isOSFreeBSD ()) {
565
599
// BSD/Linux Mappings
@@ -577,5 +611,8 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
577
611
result.redirectedFiles .append (GetWindowsFileMappings (
578
612
ctx, vfs, result.requiresBuiltinHeadersInSystemModules ));
579
613
614
+ result.redirectedFiles .append (GetAndroidFileMappings (ctx, sysroot.str ().str (),
615
+ vfs));
616
+
580
617
return result;
581
618
}
0 commit comments