Skip to content

Commit

Permalink
Add the swift_common.use_toolchain() helper
Browse files Browse the repository at this point in the history
This function currently returns an empty list so that adding it to the rules/aspects is a no-op. A future change will have it return a list containing the Swift toolchain type, enabling new toolchain resolution.

PiperOrigin-RevId: 471075965
(cherry picked from commit 016604d)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Jun 21, 2024
1 parent a218312 commit bcfa554
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 56 deletions.
26 changes: 26 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,29 @@ A new attribute dictionary that can be added to the attributes of a
custom build rule to provide access to the Swift toolchain.


<a id="swift_common.use_toolchain"></a>

## swift_common.use_toolchain

<pre>
swift_common.use_toolchain()
</pre>

Returns a list of toolchain types needed to use the Swift toolchain.

This function returns a list so that it can be easily composed with other
toolchains if necessary. For example, a rule with multiple toolchain
dependencies could write:

```
toolchains = swift_common.use_toolchain() + [other toolchains...]
```



**RETURNS**

A list of toolchain types that should be passed to `rule()` or
`aspect()`.


Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def _custom_swift_proto_compiler_impl(ctx):
]

custom_swift_proto_compiler = rule(
implementation = _custom_swift_proto_compiler_impl,
attrs = {
"deps": attr.label_list(
default = [],
Expand All @@ -125,4 +124,5 @@ custom_swift_proto_compiler = rule(
cfg = "exec",
),
},
implementation = _custom_swift_proto_compiler_impl,
)
2 changes: 1 addition & 1 deletion examples/xplatform/swift_import/get_swiftmodule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def _impl(ctx):
return [DefaultInfo(files = depset([modules[0].swift.swiftmodule]))]

get_swiftmodule = rule(
implementation = _impl,
attrs = {
"lib": attr.label(providers = [SwiftInfo]),
},
implementation = _impl,
)
10 changes: 7 additions & 3 deletions proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ bzl_library(
name = "swift_proto_library",
srcs = ["swift_proto_library.bzl"],
deps = [
":swift_proto_utils",
"//swift:swift_clang_module_aspect",
"//swift:swift_common",
"//swift/internal:attrs",
"//swift/internal:compiling",
"//swift/internal:linking",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
"@rules_proto//proto:defs",
Expand All @@ -53,7 +54,10 @@ bzl_library(
srcs = ["swift_proto_library_group.bzl"],
deps = [
":swift_proto_utils",
"//swift",
"//swift:providers",
"//swift:swift_common",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
"@rules_proto//proto:defs",
],
Expand Down
2 changes: 1 addition & 1 deletion proto/swift_proto_compiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ def _swift_proto_compiler_impl(ctx):
]

swift_proto_compiler = rule(
implementation = _swift_proto_compiler_impl,
attrs = {
"bundled_proto_paths": attr.string_list(
doc = """\
Expand Down Expand Up @@ -386,4 +385,5 @@ Each compiler target should configure this based on the suffix applied to the ge
allow_single_file = True,
),
},
implementation = _swift_proto_compiler_impl,
)
18 changes: 8 additions & 10 deletions proto/swift_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ load(
"@rules_proto//proto:defs.bzl",
"ProtoInfo",
)
load(
"//proto:swift_proto_utils.bzl",
"compile_swift_protos_for_target",
)
load("//swift:providers.bzl", "SwiftProtoCompilerInfo")
load("//swift:swift_clang_module_aspect.bzl", "swift_clang_module_aspect")
load("//swift:swift_common.bzl", "swift_common")

# buildifier: disable=bzl-visibility
load(
"//swift/internal:attrs.bzl",
"swift_deps_attr",
)
load("//swift/internal:attrs.bzl", "swift_deps_attr")

# buildifier: disable=bzl-visibility
load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain")

# buildifier: disable=bzl-visibility
load("//swift/internal:utils.bzl", "compact")
load(
"//swift/internal:utils.bzl",
"compact",
":swift_proto_utils.bzl",
"compile_swift_protos_for_target",
)

# Private
Expand Down Expand Up @@ -190,4 +187,5 @@ swift_proto_library(
""",
fragments = ["cpp"],
implementation = _swift_proto_library_impl,
toolchains = use_swift_toolchain(),
)
9 changes: 5 additions & 4 deletions proto/swift_proto_library_group.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ load(
load("//swift:swift_common.bzl", "swift_common")

# buildifier: disable=bzl-visibility
load(
"//swift/internal:utils.bzl",
"compact",
)
load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain")

# buildifier: disable=bzl-visibility
load("//swift/internal:utils.bzl", "compact")

# _swift_proto_library_group_aspect

Expand Down Expand Up @@ -89,6 +89,7 @@ from which the Swift protos will be generated.
""",
fragments = ["cpp"],
implementation = _swift_proto_library_group_aspect_impl,
toolchains = use_swift_toolchain(),
)

# _swift_proto_compiler_transition
Expand Down
33 changes: 8 additions & 25 deletions swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ bzl_library(
bzl_library(
name = "providers",
srcs = ["providers.bzl"],
visibility = [
"//swift:__subpackages__",
"//test:__subpackages__",
],
deps = [
"@bazel_skylib//lib:sets",
"@bazel_skylib//lib:types",
Expand Down Expand Up @@ -81,13 +77,13 @@ bzl_library(
bzl_library(
name = "swift_binary",
srcs = ["swift_binary.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_common",
"//swift/internal:compiling",
"//swift/internal:feature_names",
"//swift/internal:linking",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:paths",
],
Expand All @@ -96,11 +92,6 @@ bzl_library(
bzl_library(
name = "swift_clang_module_aspect",
srcs = ["swift_clang_module_aspect.bzl"],
visibility = [
"//proto:__subpackages__",
"//swift:__subpackages__",
"//test:__subpackages__",
],
deps = [
":providers",
"//swift/internal:attrs",
Expand All @@ -117,7 +108,6 @@ bzl_library(
bzl_library(
name = "swift_common",
srcs = ["swift_common.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
"//swift/internal:attrs",
"//swift/internal:compiling",
Expand All @@ -140,6 +130,7 @@ bzl_library(
"//swift/internal:compiling",
"//swift/internal:linking",
"//swift/internal:output_groups",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
"@build_bazel_apple_support//lib:apple_support",
Expand All @@ -151,7 +142,6 @@ bzl_library(
bzl_library(
name = "swift_extract_symbol_graph",
srcs = ["swift_extract_symbol_graph.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_symbol_graph_aspect",
Expand All @@ -161,7 +151,6 @@ bzl_library(
bzl_library(
name = "swift_feature_allowlist",
srcs = ["swift_feature_allowlist.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
"//swift/internal:package_specs",
Expand All @@ -171,12 +160,12 @@ bzl_library(
bzl_library(
name = "swift_import",
srcs = ["swift_import.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_common",
"//swift/internal:attrs",
"//swift/internal:linking",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
],
Expand All @@ -185,14 +174,12 @@ bzl_library(
bzl_library(
name = "swift_interop_hint",
srcs = ["swift_interop_hint.bzl"],
visibility = ["//swift:__subpackages__"],
deps = ["//swift/internal:swift_interop_info"],
)

bzl_library(
name = "swift_library",
srcs = ["swift_library.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_clang_module_aspect",
Expand All @@ -203,6 +190,7 @@ bzl_library(
"//swift/internal:feature_names",
"//swift/internal:linking",
"//swift/internal:output_groups",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
"@bazel_skylib//lib:sets",
Expand All @@ -213,12 +201,12 @@ bzl_library(
bzl_library(
name = "swift_library_group",
srcs = ["swift_library_group.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_clang_module_aspect",
":swift_common",
"//swift/internal:attrs",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
],
Expand All @@ -227,12 +215,12 @@ bzl_library(
bzl_library(
name = "swift_module_alias",
srcs = ["swift_module_alias.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_common",
"//swift/internal:linking",
"//swift/internal:output_groups",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
],
Expand All @@ -241,7 +229,6 @@ bzl_library(
bzl_library(
name = "swift_package_configuration",
srcs = ["swift_package_configuration.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
"//swift/internal:package_specs",
Expand All @@ -251,17 +238,16 @@ bzl_library(
bzl_library(
name = "swift_symbol_graph_aspect",
srcs = ["swift_symbol_graph_aspect.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
"//swift/internal:swift_symbol_graph_aspect",
"//swift/internal:toolchain_utils",
"@bazel_skylib//lib:dicts",
],
)

bzl_library(
name = "swift_test",
srcs = ["swift_test.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":providers",
":swift_common",
Expand All @@ -270,6 +256,7 @@ bzl_library(
"//swift/internal:linking",
"//swift/internal:output_groups",
"//swift/internal:swift_symbol_graph_aspect",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
],
Expand All @@ -294,7 +281,6 @@ filegroup(
repeatable_string_flag(
name = "copt",
build_setting_default = [],
visibility = ["//visibility:public"],
)

# Additional Swift compiler flags that will be applied to all `SwiftCompile`
Expand All @@ -310,22 +296,19 @@ repeatable_string_flag(
# hinted target's label.
swift_interop_hint(
name = "auto_module",
visibility = ["//visibility:public"],
)

# An aspect hint that suppresses generation of a module for a non-Swift target
# that would otherwise generate one by default, like an `objc_library`.
swift_interop_hint(
name = "no_module",
suppressed = True,
visibility = ["//visibility:public"],
)

# User settable flag that specifies additional Swift copts on a per-swiftmodule basis.
per_module_swiftcopt_flag(
name = "per_module_swiftcopt",
build_setting_default = "",
visibility = ["//visibility:public"],
)

# Configuration setting for enabling the generation of swiftinterface files.
Expand Down
5 changes: 4 additions & 1 deletion swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ bzl_library(
bzl_library(
name = "toolchain_utils",
srcs = ["toolchain_utils.bzl"],
visibility = ["//swift:__subpackages__"],
visibility = [
"//proto:__subpackages__",
"//swift:__subpackages__",
],
)

bzl_library(
Expand Down
7 changes: 6 additions & 1 deletion swift/internal/swift_symbol_graph_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ load("//swift:providers.bzl", "SwiftInfo", "SwiftSymbolGraphInfo")
load("//swift/internal:attrs.bzl", "swift_toolchain_attrs")
load("//swift/internal:features.bzl", "configure_features")
load("//swift/internal:symbol_graph_extracting.bzl", "extract_symbol_graph")
load("//swift/internal:toolchain_utils.bzl", "get_swift_toolchain")
load(
"//swift/internal:toolchain_utils.bzl",
"get_swift_toolchain",
"use_swift_toolchain",
)
load("//swift/internal:utils.bzl", "include_developer_search_paths")

def _swift_symbol_graph_aspect_impl(target, aspect_ctx):
Expand Down Expand Up @@ -186,4 +190,5 @@ default value is {default_value}.
fragments = ["cpp"],
implementation = aspect_impl,
provides = [SwiftSymbolGraphInfo],
toolchains = use_swift_toolchain(),
)
Loading

0 comments on commit bcfa554

Please sign in to comment.