Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use -fsystem-module if the compiler is new enough to support it (Xcode 12.5/Swift 5.4 or higher). #626

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ load(
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_SPLIT_DERIVED_FILES_GENERATION",
"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 @@ -603,20 +604,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 @@ -71,6 +71,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.
Comment on lines +74 to +79
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That took me so long to figure out 😅

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 @@ -41,6 +41,7 @@ load(
"SWIFT_FEATURE_REMAP_XCODE_PATH",
"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 @@ -732,6 +733,10 @@ def _xcode_swift_toolchain_impl(ctx):
if _is_xcode_at_least_version(xcode_config, "11.4"):
requested_features.append(SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES)

# 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