swift explicit modules examples #1590
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why should we adopt explicit modules?
Example for explicit clang module usage
This pull request introduces partial support for explicit system C module dependencies in Swift builds using Bazel (feature request can be found here #1475). The changes add new rules and attributes to propagate and manage system C modules, making it easier to handle Swift interop with C/Objective-C code, particularly when using explicit modules. It also introduces new build targets to represent system SDKs for different platforms and Xcode versions.
Key changes include:
System SDKs and C Module Support:
swift_c_module
rule to wrap C targets with custom module maps for Swift interop, supporting both user-supplied and system module maps. This enables explicit C module usage in Swift targets.system_sdks
andtestonly_system_sdks
targets insystem_sdks/BUILD
andsystem_sdks/16.3.0.16E140/BUILD
to represent platform-specific system SDKs, selected by Xcode version and platform constraints.Swift Rule and Attribute Changes:
system_deps
attribute toswift_library
, allowing explicit specification of system module dependencies. These are automatically set to include//system_sdks:system_sdks
for migration convenience.swift_library
implementation to propagate system module dependencies'SwiftInfo
when the relevant features are enabled, integrating system modules into the build graph.SWIFT_FEATURE_USE_C_MODULES
,SWIFT_FEATURE_EMIT_C_MODULE
).Interop Hint and Info Propagation:
swift_interop_hint
andswift_interop_info
to accept and propagatesystem_pcms
, a list of precompiled Clang modules for system modules, ensuring these dependencies are correctly wired into Swift compilation.system_pcms
to the set of propagatedSwiftInfo
providers, ensuring system modules are available where needed.swift_interop_hint
in example targets to demonstrate system PCM wiring.Build and Toolchain Integration:
These changes lay the groundwork for robust, explicit Swift-C interop in Bazel builds, particularly as the ecosystem moves toward explicit C module adoption.
Couple things to note
swift_c_module
rules which was deleted in this commit to expose system sdks to swift libraries. This rule does extra stuff and a new rule should be written for providing only necessary information to the provider.swift_interop_hint
andswift_library
to add all found system sdks as dependencies because a) make the transition to explicit module incur less friction b) in swift objc interop, both swift library and objc library needs to depend on the same system sdk targets (otherwise, compilation fails due to duplicate definition)What needs to be done
swift_c_module