Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit 4cd9b2c

Browse files
allevatoswiple-rules-gardener
authored andcommitted
Use -fsystem-module if the compiler is new enough to support it (Xcode 12.5/Swift 5.4 or higher).
PiperOrigin-RevId: 373046121
1 parent cc15b6e commit 4cd9b2c

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

swift/internal/compiling.bzl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ load(
5151
"SWIFT_FEATURE_OPT_USES_WMO",
5252
"SWIFT_FEATURE_REWRITE_GENERATED_HEADER",
5353
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
54+
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
5455
"SWIFT_FEATURE_SYSTEM_MODULE",
5556
"SWIFT_FEATURE_USE_C_MODULES",
5657
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
@@ -485,20 +486,33 @@ def compile_action_configs(
485486
swift_toolchain_config.action_config(
486487
actions = [swift_action_names.PRECOMPILE_C_MODULE],
487488
configurators = [
488-
# TODO(b/165649949): ClangImporter doesn't currently handle the
489-
# IsSystem bit correctly for the input file, which causes the
490-
# module map to be treated as a user input. To work around this
491-
# for now, we disable all diagnostics when compiling the
492-
# explicit module for system modules, since they're not useful
493-
# anyway; this is "close enough" to compiling it as a system
494-
# module for the purposes of avoiding noise in the build logs.
489+
# Before Swift 5.4, ClangImporter doesn't currently handle the
490+
# IsSystem bit correctly for the input file and ignores the
491+
# `-fsystem-module` flag, which causes the module map to be
492+
# treated as a user input. We can work around this by disabling
493+
# diagnostics for system modules. However, this also disables
494+
# behavior in ClangImporter that causes system APIs that use
495+
# `UInt` to be imported to use `Int` instead. The only solution
496+
# here is to use Xcode 12.5 or higher.
495497
swift_toolchain_config.add_arg("-Xcc", "-w"),
496498
swift_toolchain_config.add_arg(
497499
"-Xcc",
498500
"-Wno-nullability-declspec",
499501
),
500502
],
501503
features = [SWIFT_FEATURE_SYSTEM_MODULE],
504+
not_features = [SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG],
505+
),
506+
swift_toolchain_config.action_config(
507+
actions = [swift_action_names.PRECOMPILE_C_MODULE],
508+
configurators = [
509+
swift_toolchain_config.add_arg("-Xcc", "-Xclang"),
510+
swift_toolchain_config.add_arg("-Xcc", "-fsystem-module"),
511+
],
512+
features = [
513+
SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG,
514+
SWIFT_FEATURE_SYSTEM_MODULE,
515+
],
502516
),
503517
]
504518

swift/internal/feature_names.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ SWIFT_FEATURE_LAYERING_CHECK = "swift.layering_check"
6666
# If enabled, the C or Objective-C target should be compiled as a system module.
6767
SWIFT_FEATURE_SYSTEM_MODULE = "swift.system_module"
6868

69+
# If enabled, the `-Xcc -fsystem-module` flag will be passed when compiling a
70+
# system C/Objective-C module (with feature `swift.system_module`) because the
71+
# compiler is new enough to honor it correctly. If disabled, we attempt to mimic
72+
# this by disabling certain warnings; however, this unfortunately causes `UInt`
73+
# APIs to be imported by ClangImporter as `UInt` instead of `Int` because
74+
# ClangImporter doesn't recognize them as true system modules.
75+
SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG = "swift.supports_system_module_flag"
76+
6977
# If enabled, Swift compilation actions will use batch mode by passing
7078
# `-enable-batch-mode` to `swiftc`. This is a new compilation mode as of
7179
# Swift 4.2 that is intended to speed up non-incremental non-WMO builds by

swift/internal/xcode_swift_toolchain.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ load(
3737
"SWIFT_FEATURE_MODULE_MAP_NO_PRIVATE_HEADERS",
3838
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
3939
"SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS",
40+
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
4041
"SWIFT_FEATURE_USE_RESPONSE_FILES",
4142
)
4243
load(":features.bzl", "features_for_build_modes")
@@ -681,6 +682,10 @@ def _xcode_swift_toolchain_impl(ctx):
681682
requested_features.append(SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION)
682683
requested_features.append(SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS)
683684

685+
# Xcode 12.5 implies Swift 5.4.
686+
if _is_xcode_at_least_version(xcode_config, "12.5"):
687+
requested_features.append(SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG)
688+
684689
env = _xcode_env(platform = platform, xcode_config = xcode_config)
685690
execution_requirements = xcode_config.execution_info()
686691
generated_header_rewriter = ctx.executable.generated_header_rewriter

0 commit comments

Comments
 (0)