Skip to content
Open
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
13 changes: 13 additions & 0 deletions examples/apple/objc_interop_modulemap/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@
load("@rules_cc//cc:objc_library.bzl", "objc_library")
load("//swift:swift_binary.bzl", "swift_binary")
load("//swift:swift_library.bzl", "swift_library")
load("//swift:swift_interop_hint.bzl", "swift_interop_hint")

licenses(["notice"])

swift_interop_hint(
name = "PrintStream_swift_interop",
module_name = "examples_apple_objc_interop_modulemap_PrintStream",
system_pcms = select({
# this lets objc library to depend on the same system_sdks as the swift library
"//conditions:default": [
"@build_bazel_rules_swift//system_sdks:system_sdks",
],
}),
)

objc_library(
name = "PrintStream",
srcs = ["OIPrintStream.m"],
hdrs = ["OIPrintStream.h"],
aspect_hints = [":PrintStream_swift_interop"],
target_compatible_with = ["@platforms//os:macos"],
)

Expand Down
1 change: 1 addition & 0 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ to use swift_common.compile(include_dev_srch_paths = ...) instead.\
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_NO_GENERATED_MODULE_MAP,
):

compilation_context_to_compile = (
compilation_context_for_explicit_module_compilation(
compilation_contexts = [
Expand Down
4 changes: 4 additions & 0 deletions swift/internal/swift_interop_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ be disabled when the aspect processes that target; for example, a rule that
processes frameworks with headers that do not follow strict layering can request
that `swift.strict_module` always be disabled for its targets even if it is
enabled by default in the toolchain.
""",
"system_pcms": """\
A list of precompiled clang modules with `SwiftInfo` providers for system modules.
This is used to pass `system_pcm` dependencies originating from `swift_interop_hint`.
""",
},
)
7 changes: 6 additions & 1 deletion swift/swift_clang_module_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def _handle_module(
)

output_groups = {}

pcm_outputs = precompile_clang_module(
actions = aspect_ctx.actions,
cc_compilation_context = compilation_context_to_compile,
Expand All @@ -420,6 +420,7 @@ def _handle_module(
target_name = target.label.name,
toolchain_type = toolchain_type,
)

precompiled_module = getattr(pcm_outputs, "pcm_file", None)
pcm_indexstore = getattr(pcm_outputs, "indexstore_directory", None)

Expand Down Expand Up @@ -734,6 +735,10 @@ def _swift_clang_module_aspect_impl(target, aspect_ctx, toolchain_type):
if interop_info.suppressed:
return providers

# Add system_pcms passed via `swift_interop_hint` to swift_infos
for system_pcm in interop_info.system_pcms:
swift_infos.append(system_pcm[SwiftInfo])

exclude_headers = interop_info.exclude_headers
module_map_file = interop_info.module_map
module_name = (
Expand Down
9 changes: 9 additions & 0 deletions swift/swift_interop_hint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def _swift_interop_hint_impl(ctx):
module_map = ctx.file.module_map,
module_name = ctx.attr.module_name,
suppressed = ctx.attr.suppressed,
system_pcms = ctx.attr.system_pcms,
)

swift_interop_hint = rule(
Expand Down Expand Up @@ -73,6 +74,14 @@ other non-identifier characters with underscores.
doc = """\
If `True`, the hinted target should suppress any module that it would otherwise
generate.
""",
mandatory = False,
),
"system_pcms": attr.label_list(
doc = """\
A list of precompiled clang modules with `SwiftInfo` providers for system modules.
This is used to define system pcm dependencies and eventually propagated to swift_interop_info.
swift_interop_info will pass those to compile actions.
""",
mandatory = False,
),
Expand Down
7 changes: 6 additions & 1 deletion swift/swift_interop_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def create_swift_interop_info(
requested_features = [],
suppressed = False,
swift_infos = [],
unsupported_features = []):
unsupported_features = [],
system_pcms = []):
"""Returns a provider that lets a target expose C/Objective-C APIs to Swift.

The provider returned by this function allows custom build rules written in
Expand Down Expand Up @@ -107,6 +108,9 @@ def create_swift_interop_info(
strict layering can request that `swift.strict_module` always be
disabled for its targets even if it is enabled by default in the
toolchain.
system_pcms: A list of precompiled clang modules with `SwiftInfo`
providers for system modules. This is used to define system pcm
dependencies passed from `swift_interop_hint`.

Returns:
A provider whose type/layout is an implementation detail and should not
Expand All @@ -128,4 +132,5 @@ def create_swift_interop_info(
suppressed = suppressed,
swift_infos = swift_infos,
unsupported_features = unsupported_features,
system_pcms = system_pcms,
)
30 changes: 29 additions & 1 deletion swift/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ load(
"SWIFT_FEATURE_EMIT_PRIVATE_SWIFTINTERFACE",
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_USE_C_MODULES",
"SWIFT_FEATURE_EMIT_C_MODULE",
)
load("//swift/internal:features.bzl", "configure_features")
load("//swift/internal:features.bzl", "configure_features", "is_feature_enabled")
load(
"//swift/internal:linking.bzl",
"create_linking_context_from_compilation_outputs",
Expand Down Expand Up @@ -159,6 +161,18 @@ def _swift_library_impl(ctx):
swift_infos = get_providers(deps, SwiftInfo)
private_swift_infos = get_providers(private_deps, SwiftInfo)

if is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_USE_C_MODULES,
) and is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_EMIT_C_MODULE,
):
# only add system_deps if both features are enabled
system_deps = ctx.attr.system_deps
system_swift_infos = get_providers(system_deps, SwiftInfo)
swift_infos.extend(system_swift_infos)

if ctx.attr.generates_header:
generated_header_name = (
ctx.attr.generated_header_name or
Expand Down Expand Up @@ -288,6 +302,20 @@ dependent for linking, but artifacts/flags required for compilation (such as
# TODO(b/301253335): Once AEGs are enabled in Bazel, set the swift toolchain type in the
# exec configuration of `plugins` attribute and enable AEGs in swift_library.
"_use_auto_exec_groups": attr.bool(default = False),
# TODO : system deps
"system_deps": swift_deps_attr(
aspects = [swift_clang_module_aspect],
doc = """\
Default and universal system modules necessary for Swift compilation with explicit C modules.
It's desired that each swift_library rule target defines its own system module dependencies,
however, we are defining a long list of commonly used system modules here just to ease the burden
of the developers during the migration to explicit C modules. This attribute should be removed once
the explicit c modules are fully adopted and enabled by default.
""",
default = [
"//system_sdks:system_sdks",
],
)
},
),
doc = """\
Expand Down
1 change: 1 addition & 0 deletions swift/toolchains/config/compile_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ def compile_action_configs(
SWIFT_ACTION_COMPILE_MODULE_INTERFACE,
SWIFT_ACTION_DERIVE_FILES,
SWIFT_ACTION_DUMP_AST,
SWIFT_ACTION_PRECOMPILE_C_MODULE,
],
configurators = [
lambda _, args: args.add_all(
Expand Down
104 changes: 104 additions & 0 deletions system_sdks/16.3.0.16E140/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library_group")
package(default_visibility = ["//visibility:public"])

config_setting(
name = "macos_x86_64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
)

config_setting(
name = "macos_arm64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:arm64",
],
)

config_setting(
name = "ios_device_arm64",
constraint_values = [
"@platforms//os:ios",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:device",
],
)

config_setting(
name = "ios_sim_x86_64",
constraint_values = [
"@platforms//os:ios",
"@platforms//cpu:x86_64",
"@build_bazel_apple_support//constraints:simulator",
],
)

config_setting(
name = "ios_sim_arm64",
constraint_values = [
"@platforms//os:ios",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:simulator",
],
)

config_setting(
name = "watchos_sim_arm64",
constraint_values = [
"@platforms//os:watchos",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:simulator",
],
)

config_setting(
name = "watchos_sim_x86_64",
constraint_values = [
"@platforms//os:watchos",
"@platforms//cpu:x86_64",
"@build_bazel_apple_support//constraints:simulator",
],
)

config_setting(
name = "watchos_device_arm64_32",
constraint_values = [
"@platforms//os:watchos",
"@platforms//cpu:arm64_32",
"@build_bazel_apple_support//constraints:device",
],
)

swift_library_group(
name = "system_sdks",
deps = select({
"ios_device_arm64": ["//system_sdks/16.3.0.16E140/iPhoneOS/arm64:all_generated_targets"],
"watchos_device_arm64_32": ["//system_sdks/16.3.0.16E140/WatchOS/arm64_32:all_generated_targets"],
"watchos_sim_arm64": ["//system_sdks/16.3.0.16E140/WatchSimulator/arm64:all_generated_targets"],
"watchos_sim_x86_64": ["//system_sdks/16.3.0.16E140/WatchSimulator/x86_64:all_generated_targets"],
"ios_sim_arm64": ["//system_sdks/16.3.0.16E140/iPhoneSimulator/arm64:all_generated_targets"],
"ios_sim_x86_64": ["//system_sdks/16.3.0.16E140/iPhoneSimulator/x86_64:all_generated_targets"],
"macos_arm64": ["//system_sdks/16.3.0.16E140/MacOSX/arm64:all_generated_targets"],
"macos_x86_64": ["//system_sdks/16.3.0.16E140/MacOSX/x86_64:all_generated_targets"],
"//conditions:default": [],
}),
)


swift_library_group(
name = "testonly_system_sdks",
testonly = True,
deps = select({
"ios_device_arm64": ["//system_sdks/16.3.0.16E140/iPhoneOS/arm64:all_generated_testonly_targets"],
"watchos_device_arm64_32": ["//system_sdks/16.3.0.16E140/WatchOS/arm64_32:all_generated_testonly_targets"],
"watchos_sim_arm64": ["//system_sdks/16.3.0.16E140/WatchSimulator/arm64:all_generated_testonly_targets"],
"watchos_sim_x86_64": ["//system_sdks/16.3.0.16E140/WatchSimulator/x86_64:all_generated_testonly_targets"],
"ios_sim_arm64": ["//system_sdks/16.3.0.16E140/iPhoneSimulator/arm64:all_generated_testonly_targets"],
"ios_sim_x86_64": ["//system_sdks/16.3.0.16E140/iPhoneSimulator/x86_64:all_generated_testonly_targets"],
"macos_arm64": ["//system_sdks/16.3.0.16E140/MacOSX/arm64:all_generated_testonly_targets"],
"macos_x86_64": ["//system_sdks/16.3.0.16E140/MacOSX/x86_64:all_generated_testonly_targets"],
"//conditions:default": [],
}),
)
Loading