Skip to content

Commit 1a7352d

Browse files
[ClangImporter] Swift needs to pass -Xclang -fbuiltin-headers-in-system-modules for its module maps that group cstd headers
Swift has some module maps it overlays on Linux and Windows that groups all of the C standard library headers into a single module. This doesn’t allow clang and C++ headers to layer properly with the OS/SDK modules. clang will set -fbuiltin-headers-in-system-modules as necessary for Apple SDKs, but Swift will need to pass that flag itself when required by its module maps.
1 parent 0eded5f commit 1a7352d

File tree

10 files changed

+78
-17
lines changed

10 files changed

+78
-17
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ bool isCxxConstReferenceType(const clang::Type *type);
647647
struct ClangInvocationFileMapping {
648648
SmallVector<std::pair<std::string, std::string>, 2> redirectedFiles;
649649
SmallVector<std::pair<std::string, std::string>, 1> overridenFiles;
650+
bool requiresBuiltinHeadersInSystemModules;
650651
};
651652

652653
/// On Linux, some platform libraries (glibc, libstdc++) are not modularized.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,11 @@ ClangImporter::create(ASTContext &ctx,
12041204
// Create a new Clang compiler invocation.
12051205
{
12061206
importer->Impl.ClangArgs = getClangArguments(ctx);
1207+
if (fileMapping.requiresBuiltinHeadersInSystemModules) {
1208+
if (!importerOpts.DirectClangCC1ModuleBuild)
1209+
importer->Impl.ClangArgs.push_back("-Xclang");
1210+
importer->Impl.ClangArgs.push_back("-fbuiltin-headers-in-system-modules");
1211+
}
12071212
ArrayRef<std::string> invocationArgStrs = importer->Impl.ClangArgs;
12081213
if (importerOpts.DumpClangDiagnostics) {
12091214
llvm::errs() << "'";

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ GetWindowsAuxiliaryFile(StringRef modulemap, const SearchPathOptions &Options) {
420420

421421
SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
422422
ASTContext &Context,
423-
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS) {
423+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
424+
bool &requiresBuiltinHeadersInSystemModules) {
424425
const llvm::Triple &Triple = Context.LangOpts.Target;
425426
const SearchPathOptions &SearchPathOpts = Context.SearchPathOpts;
426427
SmallVector<std::pair<std::string, std::string>, 2> Mappings;
@@ -468,8 +469,21 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
468469
llvm::sys::path::append(UCRTInjection, "module.modulemap");
469470

470471
AuxiliaryFile = GetWindowsAuxiliaryFile("ucrt.modulemap", SearchPathOpts);
471-
if (!AuxiliaryFile.empty())
472+
if (!AuxiliaryFile.empty()) {
473+
// The ucrt module map has the C standard library headers all together.
474+
// That leads to module cycles with the clang _Builtin_ modules. e.g.
475+
// <fenv.h> on ucrt includes <float.h>. The clang builtin <float.h>
476+
// include-nexts <float.h>. When both of those UCRT headers are in the
477+
// ucrt module, there's a module cycle ucrt -> _Builtin_float -> ucrt
478+
// (i.e. fenv.h (ucrt) -> float.h (builtin) -> float.h (ucrt)). Until the
479+
// ucrt module map is updated, the builtin headers need to join the system
480+
// modules. i.e. when the builtin float.h is in the ucrt module too, the
481+
// cycle goes away. Note that -fbuiltin-headers-in-system-modules does
482+
// nothing to fix the same problem with C++ headers, and is generally
483+
// fragile.
472484
Mappings.emplace_back(std::string(UCRTInjection), AuxiliaryFile);
485+
requiresBuiltinHeadersInSystemModules = true;
486+
}
473487
}
474488

475489
struct {
@@ -515,20 +529,36 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
515529

516530
const llvm::Triple &triple = ctx.LangOpts.Target;
517531

532+
SmallVector<std::pair<std::string, std::string>, 2> libcFileMapping;
518533
if (triple.isOSWASI()) {
519534
// WASI Mappings
520-
result.redirectedFiles.append(
521-
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs));
535+
libcFileMapping =
536+
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
522537
} else {
523538
// Android/BSD/Linux Mappings
524-
result.redirectedFiles.append(getLibcFileMapping(
525-
ctx, "glibc.modulemap", StringRef("SwiftGlibc.h"), vfs));
539+
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
540+
StringRef("SwiftGlibc.h"), vfs);
526541
}
542+
result.redirectedFiles.append(libcFileMapping);
543+
// Both libc module maps have the C standard library headers all together in a
544+
// SwiftLibc module. That leads to module cycles with the clang _Builtin_
545+
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
546+
// clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
547+
// platform headers are in the SwiftLibc module, there's a module cycle
548+
// SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
549+
// stdint.h (builtin) -> stdint.h (platform)). Until this can be fixed in
550+
// these module maps, the clang builtin headers need to join the "system"
551+
// modules (SwiftLibc). i.e. when the clang builtin stdint.h is in the
552+
// SwiftLibc module too, the cycle goes away. Note that
553+
// -fbuiltin-headers-in-system-modules does nothing to fix the same problem
554+
// with C++ headers, and is generally fragile.
555+
result.requiresBuiltinHeadersInSystemModules = !libcFileMapping.empty();
527556

528557
if (ctx.LangOpts.EnableCXXInterop)
529558
getLibStdCxxFileMapping(result, ctx, vfs);
530559

531-
result.redirectedFiles.append(GetWindowsFileMappings(ctx, vfs));
560+
result.redirectedFiles.append(GetWindowsFileMappings(
561+
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));
532562

533563
return result;
534564
}

lib/DriverTool/sil_opt_main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ struct SILOptOptions {
518518
swift::UnavailableDeclOptimization::Complete, "complete",
519519
"Eliminate unavailable decls from lowered SIL/IR")),
520520
llvm::cl::init(swift::UnavailableDeclOptimization::None));
521+
522+
llvm::cl::list<std::string> ClangXCC = llvm::cl::list<std::string>("Xcc", llvm::cl::desc("option to pass to clang"));
521523
};
522524

523525
/// Regular expression corresponding to the value given in one of the
@@ -671,6 +673,11 @@ int sil_opt_main(ArrayRef<const char *> argv, void *MainAddr) {
671673
Invocation.getDiagnosticOptions().VerifyMode =
672674
options.VerifyMode ? DiagnosticOptions::Verify : DiagnosticOptions::NoVerify;
673675

676+
ClangImporterOptions &clangImporterOptions = Invocation.getClangImporterOptions();
677+
for (const auto &xcc : options.ClangXCC) {
678+
clangImporterOptions.ExtraArgs.push_back(xcc);
679+
}
680+
674681
// Setup the SIL Options.
675682
SILOptions &SILOpts = Invocation.getSILOptions();
676683
SILOpts.InlineThreshold = options.SILInlineThreshold;

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ function(add_swift_target_library_single target name)
929929
-Xcc;-Xclang;-Xcc;-ivfsoverlay;-Xcc;-Xclang;-Xcc;${SWIFTLIB_SINGLE_VFS_OVERLAY})
930930
endif()
931931
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS
932-
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}")
932+
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}";-Xcc;-Xclang;-Xcc;-fbuiltin-headers-in-system-modules)
933933
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
934934
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
935935
foreach(directory ${SWIFTLIB_INCLUDE})
@@ -2659,7 +2659,7 @@ function(_add_swift_target_executable_single name)
26592659

26602660
if(SWIFTEXE_SINGLE_SDK STREQUAL "WINDOWS")
26612661
list(APPEND SWIFTEXE_SINGLE_COMPILE_FLAGS
2662-
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}")
2662+
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}";-Xcc;-Xclang;-Xcc;-fbuiltin-headers-in-system-modules)
26632663
endif()
26642664

26652665
if ("${SWIFTEXE_SINGLE_SDK}" STREQUAL "LINUX")

stdlib/public/Backtracing/SymbolicatedBacktrace.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import Swift
2323

2424
@_implementationOnly import OS.Libc
2525
@_implementationOnly import Runtime
26+
// Because we've turned off the OS/SDK modules, and we don't have a module for
27+
// stddef.h, and we sometimes build with -fbuiltin-headers-in-system-modules for
28+
// vfs reasons, stddef.h can be absorbed into a random module. Sometimes it's
29+
// SwiftOverlayShims.
30+
@_implementationOnly import SwiftOverlayShims
2631

2732
/// A symbolicated backtrace
2833
public struct SymbolicatedBacktrace: CustomStringConvertible {

test/DebugInfo/C-typedef-Darwin.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ let blah = size_t(1024)
99
use(blah)
1010
// CHECK: !DIDerivedType(tag: DW_TAG_typedef,
1111
// CHECK-SAME: scope: ![[DARWIN_MODULE:[0-9]+]],
12-
// CHECK: ![[DARWIN_MODULE]] = !DIModule({{.*}}, name: "stddef"
12+
13+
// size_t is defined in clang, originally by <stddef.h>, and later split out to
14+
// <__stddef_size_t.h>. Depending on the state of clang's builtin headers and
15+
// modules, size_t can be in either Darwin.C.stddef or _Builtin_stddef.size_t.
16+
// size_t is also defined in macOS by <sys/_types/_size_t.h> which is in the
17+
// Darwin.POSIX._types._size_t module. Ideally Apple will remove the duplicate
18+
// declaration and clang will settle to _Builtin_stddef.size_t, but while it's
19+
// all in flux allow all three of those modules.
20+
// Darwin.C.stddef|_Builtin_stddef.size_t|Darwin.POSIX._types._size_t
21+
// CHECK: ![[DARWIN_MODULE]] = !DIModule({{.*}}, name: "{{stddef|size_t|_size_t}}"

test/Interop/lit.local.cfg

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ if get_target_os() in ['windows-msvc']:
3333
# Clang should build object files with link settings equivalent to -libc MD
3434
# when building for the MSVC target.
3535
clang_opt = clang_compile_opt + '-D_MT -D_DLL -Xclang --dependent-lib=msvcrt -Xclang --dependent-lib=oldnames '
36-
config.substitutions.insert(0, ('%target-swift-flags', '-vfsoverlay {}'.format(os.path.join(config.swift_obj_root,
37-
'stdlib',
38-
'windows-vfs-overlay.yaml'))))
36+
# ucrt.modulemap currently requires -fbuiltin-headers-in-system-modules
37+
config.substitutions.insert(0, ('%target-swift-flags', '-vfsoverlay {} -Xcc -Xclang -Xcc -fbuiltin-headers-in-system-modules'.format(
38+
os.path.join(config.swift_obj_root,
39+
'stdlib',
40+
'windows-vfs-overlay.yaml'))))
3941
else:
4042
# FIXME(compnerd) do all the targets we currently support use SysV ABI?
4143
config.substitutions.insert(0, ('%target-abi', 'SYSV'))

test/lit.cfg

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ else:
374374
config.swift_system_overlay_opt = ""
375375
config.clang_system_overlay_opt = ""
376376
if kIsWindows:
377-
config.swift_system_overlay_opt = "-vfsoverlay {}".format(
377+
config.swift_system_overlay_opt = "-vfsoverlay {} -Xcc -Xclang -Xcc -fbuiltin-headers-in-system-modules".format(
378378
os.path.join(config.swift_obj_root, "stdlib", "windows-vfs-overlay.yaml")
379379
)
380-
config.clang_system_overlay_opt = "-Xcc -ivfsoverlay -Xcc {}".format(
380+
config.clang_system_overlay_opt = "-Xcc -ivfsoverlay -Xcc {} -Xcc -Xclang -Xcc -fbuiltin-headers-in-system-modules".format(
381381
os.path.join(config.swift_obj_root, "stdlib", "windows-vfs-overlay.yaml")
382382
)
383383
stdlib_resource_dir_opt = config.resource_dir_opt
@@ -1592,7 +1592,9 @@ elif run_os in ['windows-msvc']:
15921592
config.target_swift_modulewrap = \
15931593
('%r -modulewrap -target %s' % (config.swiftc, config.variant_triple))
15941594
config.target_swift_emit_pcm = \
1595-
('%r -emit-pcm -target %s' % (config.swiftc, config.variant_triple))
1595+
('%r -emit-pcm -target %s %s' % (config.swiftc, \
1596+
config.variant_triple, \
1597+
config.swift_system_overlay_opt))
15961598

15971599

15981600
elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-cygnus', 'windows-gnu'] or

utils/build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ function Build-CMakeProject {
608608

609609
$SwiftArgs += @("-resource-dir", "$SwiftResourceDir")
610610
$SwiftArgs += @("-L", "$SwiftResourceDir\windows")
611-
$SwiftArgs += @("-vfsoverlay", "$RuntimeBinaryCache\stdlib\windows-vfs-overlay.yaml")
611+
$SwiftArgs += @("-vfsoverlay", "$RuntimeBinaryCache\stdlib\windows-vfs-overlay.yaml", "-Xcc", "-Xclang", "-Xcc", "-fbuiltin-headers-in-system-modules")
612612
}
613613
} else {
614614
$SwiftArgs += @("-sdk", "$BinaryCache\toolchains\$PinnedToolchain\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk")

0 commit comments

Comments
 (0)