Skip to content

Commit b13aef1

Browse files
committed
Adds sdk_clang_module to represent sdk clang modules in explicit module builds
Also add a basic example for explicit module builds. Run 'bazel build --config=explicit_modules //example/explicit_module:hello_world'.
1 parent 0a5d081 commit b13aef1

6 files changed

Lines changed: 70 additions & 2 deletions

File tree

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ build:remote_cache --noslim_profile
6262
build:remote_cache --remote_cache=grpcs://remote.buildbuddy.io
6363
build:remote_cache --remote_timeout=3600
6464

65+
# Configure for explicit module compilation
66+
build:explicit_modules --features=swift.use_c_modules
67+
build:explicit_modules --features=swift.emit_c_module
68+
6569
# By default don't upload local results to remote cache, only CI does this.
6670
build --noremote_upload_local_results
6771
build:ci --remote_upload_local_results
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
2+
load("//rules/explicit_module:sdk_clang_module.bzl", "sdk_clang_module")
3+
4+
sdk_clang_module(
5+
name = "swift_concurrency_shims",
6+
module_map = "__BAZEL_XCODE_SDKROOT__/usr/lib/swift/shims/module.modulemap",
7+
module_name = "_SwiftConcurrencyShims",
8+
)
9+
10+
sdk_clang_module(
11+
name = "shims",
12+
module_map = "__BAZEL_XCODE_SDKROOT__/usr/lib/swift/shims/module.modulemap",
13+
module_name = "SwiftShims",
14+
)
15+
16+
swift_binary(
17+
name = "hello_world",
18+
srcs = ["main.swift"],
19+
deps = [
20+
":shims",
21+
":swift_concurrency_shims",
22+
],
23+
)

example/explicit_module/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello world")

rules/explicit_module/BUILD.bazel

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
load("@build_bazel_rules_swift//swift/internal:feature_names.bzl", "SWIFT_FEATURE_SYSTEM_MODULE")
2+
load("@build_bazel_rules_swift//swift/internal:swift_common.bzl", "swift_common")
3+
4+
def _sdk_clang_module_impl(ctx):
5+
return [
6+
swift_common.create_swift_interop_info(
7+
module_map = ctx.attr.module_map,
8+
module_name = ctx.attr.module_name,
9+
requested_features = [SWIFT_FEATURE_SYSTEM_MODULE],
10+
),
11+
# We need to return CcInfo and its compilation_context. We may also consider to update swift_clang_module_aspect.
12+
# See https://github.com/bazelbuild/rules_swift/blob/d68b21471e4e9d922b75e2b0621082b8ce017d11/swift/internal/swift_clang_module_aspect.bzl#L548
13+
CcInfo(compilation_context = cc_common.create_compilation_context()),
14+
]
15+
16+
sdk_clang_module = rule(
17+
attrs = {
18+
"module_map": attr.string(
19+
doc = """\
20+
The path to a SDK framework module map.
21+
Variables `__BAZEL_XCODE_SDKROOT__` and `__BAZEL_XCODE_DEVELOPER_DIR__` will be substitued
22+
appropriately for, i.e.
23+
`/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk`
24+
and
25+
`/Applications/Xcode.app/Contents/Developer` respectively.
26+
""",
27+
mandatory = True,
28+
),
29+
"module_name": attr.string(
30+
doc = """\
31+
The name of the top-level module in the module map that this target represents.
32+
""",
33+
mandatory = True,
34+
),
35+
},
36+
doc = """\
37+
A rule representing a SDK clang module. It's required for explicit module builds.
38+
""",
39+
implementation = _sdk_clang_module_impl,
40+
)

rules/repositories.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def _rules_ios_bzlmod_dependencies():
8585
_maybe(
8686
http_archive,
8787
name = "build_bazel_rules_swift",
88-
sha256 = "bb01097c7c7a1407f8ad49a1a0b1960655cf823c26ad2782d0b7d15b323838e2",
89-
url = "https://github.com/bazelbuild/rules_swift/releases/download/1.18.0/rules_swift.1.18.0.tar.gz",
88+
sha256 = "a91877169614a1fd24afc7e2a7c5e77f8e05caa8332dde115ba4ca4a5b6d14ec",
89+
url = "https://github.com/bazelbuild/rules_swift/archive/8c144a2c28af3af7b78b5fd679550e6783a19194.zip",
9090
)
9191

9292
_maybe(

0 commit comments

Comments
 (0)