@@ -530,33 +530,44 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
530530
531531 const llvm::Triple &triple = ctx.LangOpts .Target ;
532532
533+ // For modulemaps that have all the C standard library headers together in
534+ // a single module, we end up with module cycles with the clang _Builtin_
535+ // modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
536+ // clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
537+ // platform headers are in the SwiftLibc module, there's a module cycle
538+ // SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
539+ // stdint.h (builtin) -> stdint.h (platform)).
540+ //
541+ // Until these modulemaps can be fixed, the builtin headers need to join
542+ // the system modules to avoid the cycle.
543+ //
544+ // Note that this does nothing to fix the same problem with C++ headers,
545+ // and that this is generally a fragile solution.
546+ //
547+ // We start by assuming we do *not* need to do this, then enable it for
548+ // affected modulemaps.
549+ result.requiresBuiltinHeadersInSystemModules = false ;
550+
533551 SmallVector<std::pair<std::string, std::string>, 2 > libcFileMapping;
534552 if (triple.isOSWASI ()) {
535553 // WASI Mappings
536554 libcFileMapping =
537555 getLibcFileMapping (ctx, " wasi-libc.modulemap" , std::nullopt , vfs);
556+
557+ // WASI's module map needs fixing
558+ result.requiresBuiltinHeadersInSystemModules = true ;
538559 } else if (triple.isMusl ()) {
539560 libcFileMapping =
540561 getLibcFileMapping (ctx, " musl.modulemap" , StringRef (" SwiftMusl.h" ), vfs);
541562 } else {
542563 // Android/BSD/Linux Mappings
543564 libcFileMapping = getLibcFileMapping (ctx, " glibc.modulemap" ,
544565 StringRef (" SwiftGlibc.h" ), vfs);
566+
567+ // glibc.modulemap needs fixing
568+ result.requiresBuiltinHeadersInSystemModules = true ;
545569 }
546570 result.redirectedFiles .append (libcFileMapping);
547- // Both libc module maps have the C standard library headers all together in a
548- // SwiftLibc module. That leads to module cycles with the clang _Builtin_
549- // modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
550- // clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
551- // platform headers are in the SwiftLibc module, there's a module cycle
552- // SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
553- // stdint.h (builtin) -> stdint.h (platform)). Until this can be fixed in
554- // these module maps, the clang builtin headers need to join the "system"
555- // modules (SwiftLibc). i.e. when the clang builtin stdint.h is in the
556- // SwiftLibc module too, the cycle goes away. Note that
557- // -fbuiltin-headers-in-system-modules does nothing to fix the same problem
558- // with C++ headers, and is generally fragile.
559- result.requiresBuiltinHeadersInSystemModules = !libcFileMapping.empty ();
560571
561572 if (ctx.LangOpts .EnableCXXInterop )
562573 getLibStdCxxFileMapping (result, ctx, vfs);
0 commit comments