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

Commit

Permalink
Use -fsystem-module if the compiler is new enough to support it (Xc…
Browse files Browse the repository at this point in the history
…ode 12.5/Swift 5.4 or higher).

PiperOrigin-RevId: 373046121
  • Loading branch information
allevato authored and swiple-rules-gardener committed May 11, 2021
1 parent cc15b6e commit 4cd9b2c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
28 changes: 21 additions & 7 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ load(
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_REWRITE_GENERATED_HEADER",
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
"SWIFT_FEATURE_SYSTEM_MODULE",
"SWIFT_FEATURE_USE_C_MODULES",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
Expand Down Expand Up @@ -485,20 +486,33 @@ def compile_action_configs(
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
# TODO(b/165649949): ClangImporter doesn't currently handle the
# IsSystem bit correctly for the input file, which causes the
# module map to be treated as a user input. To work around this
# for now, we disable all diagnostics when compiling the
# explicit module for system modules, since they're not useful
# anyway; this is "close enough" to compiling it as a system
# module for the purposes of avoiding noise in the build logs.
# Before Swift 5.4, ClangImporter doesn't currently handle the
# IsSystem bit correctly for the input file and ignores the
# `-fsystem-module` flag, which causes the module map to be
# treated as a user input. We can work around this by disabling
# diagnostics for system modules. However, this also disables
# behavior in ClangImporter that causes system APIs that use
# `UInt` to be imported to use `Int` instead. The only solution
# here is to use Xcode 12.5 or higher.
swift_toolchain_config.add_arg("-Xcc", "-w"),
swift_toolchain_config.add_arg(
"-Xcc",
"-Wno-nullability-declspec",
),
],
features = [SWIFT_FEATURE_SYSTEM_MODULE],
not_features = [SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
swift_toolchain_config.add_arg("-Xcc", "-Xclang"),
swift_toolchain_config.add_arg("-Xcc", "-fsystem-module"),
],
features = [
SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG,
SWIFT_FEATURE_SYSTEM_MODULE,
],
),
]

Expand Down
8 changes: 8 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ SWIFT_FEATURE_LAYERING_CHECK = "swift.layering_check"
# If enabled, the C or Objective-C target should be compiled as a system module.
SWIFT_FEATURE_SYSTEM_MODULE = "swift.system_module"

# If enabled, the `-Xcc -fsystem-module` flag will be passed when compiling a
# system C/Objective-C module (with feature `swift.system_module`) because the
# compiler is new enough to honor it correctly. If disabled, we attempt to mimic
# this by disabling certain warnings; however, this unfortunately causes `UInt`
# APIs to be imported by ClangImporter as `UInt` instead of `Int` because
# ClangImporter doesn't recognize them as true system modules.
SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG = "swift.supports_system_module_flag"

# If enabled, Swift compilation actions will use batch mode by passing
# `-enable-batch-mode` to `swiftc`. This is a new compilation mode as of
# Swift 4.2 that is intended to speed up non-incremental non-WMO builds by
Expand Down
5 changes: 5 additions & 0 deletions swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ load(
"SWIFT_FEATURE_MODULE_MAP_NO_PRIVATE_HEADERS",
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS",
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
"SWIFT_FEATURE_USE_RESPONSE_FILES",
)
load(":features.bzl", "features_for_build_modes")
Expand Down Expand Up @@ -681,6 +682,10 @@ def _xcode_swift_toolchain_impl(ctx):
requested_features.append(SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION)
requested_features.append(SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS)

# Xcode 12.5 implies Swift 5.4.
if _is_xcode_at_least_version(xcode_config, "12.5"):
requested_features.append(SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG)

env = _xcode_env(platform = platform, xcode_config = xcode_config)
execution_requirements = xcode_config.execution_info()
generated_header_rewriter = ctx.executable.generated_header_rewriter
Expand Down

0 comments on commit 4cd9b2c

Please sign in to comment.