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
  • Loading branch information
allevato authored and swiple-rules-gardener committed Aug 30, 2022
1 parent 14b5fb0 commit 016604d
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions swift/internal/swift_protoc_gen_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ load(
"proto_import_path",
"register_module_mapping_write_action",
)
load(":toolchain_utils.bzl", "use_swift_toolchain")
load(":utils.bzl", "get_compilation_contexts")

# The paths of proto files bundled with the runtime. This is mainly the well
Expand Down Expand Up @@ -552,4 +553,5 @@ detail of the `swift_proto_library` rule.
""",
fragments = ["cpp"],
implementation = _swift_protoc_gen_aspect_impl,
toolchains = use_swift_toolchain(),
)
3 changes: 2 additions & 1 deletion swift/internal/swift_symbol_graph_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ file in the parent directory.
load(":attrs.bzl", "swift_toolchain_attrs")
load(":features.bzl", "configure_features")
load(":symbol_graph_extracting.bzl", "extract_symbol_graph")
load(":toolchain_utils.bzl", "get_swift_toolchain")
load(":toolchain_utils.bzl", "get_swift_toolchain", "use_swift_toolchain")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"@build_bazel_rules_swift//swift:providers.bzl",
Expand Down Expand Up @@ -162,4 +162,5 @@ default value is {default_value}.
fragments = ["cpp"],
implementation = aspect_impl,
provides = [SwiftSymbolGraphInfo],
toolchains = use_swift_toolchain(),
)
25 changes: 23 additions & 2 deletions swift/internal/toolchain_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Helpers used to depend on and access the Swift toolchain."""

SWIFT_TOOLCHAIN_TYPE = "@build_bazel_rules_swift//toolchains:toolchain_type"
SWIFT_TOOLCHAIN_TYPE = "@build_bazel_rules_swift//toolchains:toolchain-type"

def get_swift_toolchain(ctx, attr = "_toolchain"):
"""Gets the Swift toolchain associated with the rule or aspect.
Expand All @@ -41,4 +41,25 @@ def get_swift_toolchain(ctx, attr = "_toolchain"):

fail("To use `swift_common.get_toolchain`, you must declare the " +
"toolchain in your rule using " +
"`toolchains = [swift_common.toolchain_type()]`.")
"`toolchains = swift_common.use_toolchain()`.")

def use_swift_toolchain():
"""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()`.
"""

# TODO(b/205018581): Intentionally empty for now so that rule definitions
# can reference the function while still being a no-op. A future change will
# add the toolchain type to this list to enable toolchain resolution.
return []
5 changes: 5 additions & 0 deletions swift/swift_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ load(
"malloc_linking_context",
"register_link_binary_action",
)
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"use_swift_toolchain",
)
load(
"@build_bazel_rules_swift//swift/internal:utils.bzl",
"expand_locations",
Expand Down Expand Up @@ -133,4 +137,5 @@ please use one of the platform-specific application rules in
executable = True,
fragments = ["cpp"],
implementation = _swift_binary_impl,
toolchains = use_swift_toolchain(),
)
2 changes: 2 additions & 0 deletions swift/swift_clang_module_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ load(
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"get_swift_toolchain",
"use_swift_toolchain",
)
load(
"@build_bazel_rules_swift//swift/internal:utils.bzl",
Expand Down Expand Up @@ -782,4 +783,5 @@ it propagates for its targets.
[apple_common.Objc],
[CcInfo],
],
toolchains = use_swift_toolchain(),
)
2 changes: 2 additions & 0 deletions swift/swift_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ load(
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"get_swift_toolchain",
"use_swift_toolchain",
)

# The exported `swift_common` module, which defines the public API for directly
Expand All @@ -84,4 +85,5 @@ swift_common = struct(
library_rule_attrs = swift_library_rule_attrs,
precompile_clang_module = precompile_clang_module,
toolchain_attrs = swift_toolchain_attrs,
use_toolchain = use_swift_toolchain,
)
5 changes: 5 additions & 0 deletions swift/swift_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ load(
"proto_import_path",
"register_module_mapping_write_action",
)
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"use_swift_toolchain",
)
load(
"@build_bazel_rules_swift//swift/internal:utils.bzl",
"compact",
Expand Down Expand Up @@ -490,4 +494,5 @@ swift_grpc_library(
""",
fragments = ["cpp"],
implementation = _swift_grpc_library_impl,
toolchains = use_swift_toolchain(),
)
5 changes: 5 additions & 0 deletions swift/swift_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ load(
)
load(":providers.bzl", "SwiftInfo")
load(":swift_common.bzl", "swift_common")
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"use_swift_toolchain",
)

def _swift_import_impl(ctx):
archives = ctx.files.archives
Expand Down Expand Up @@ -180,4 +184,5 @@ Allows for the use of Swift textual module interfaces and/or precompiled Swift m
""",
fragments = ["cpp"],
implementation = _swift_import_impl,
toolchains = use_swift_toolchain(),
)
5 changes: 5 additions & 0 deletions swift/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ load(
"@build_bazel_rules_swift//swift/internal:linking.bzl",
"new_objc_provider",
)
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"use_swift_toolchain",
)
load(
"@build_bazel_rules_swift//swift/internal:utils.bzl",
"compact",
Expand Down Expand Up @@ -310,4 +314,5 @@ Compiles and links Swift code into a static library and Swift module.
outputs = swift_library_output_map,
implementation = _swift_library_impl,
fragments = ["cpp"],
toolchains = use_swift_toolchain(),
)
5 changes: 5 additions & 0 deletions swift/swift_module_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ load(
"@build_bazel_rules_swift//swift/internal:linking.bzl",
"new_objc_provider",
)
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"use_swift_toolchain",
)
load(
"@build_bazel_rules_swift//swift/internal:utils.bzl",
"compact",
Expand Down Expand Up @@ -185,4 +189,5 @@ symbol is defined; it is not repeated by the alias module.)
""",
fragments = ["cpp"],
implementation = _swift_module_alias_impl,
toolchains = use_swift_toolchain(),
)
5 changes: 5 additions & 0 deletions swift/swift_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ load(
"@build_bazel_rules_swift//swift/internal:swift_symbol_graph_aspect.bzl",
"make_swift_symbol_graph_aspect",
)
load(
"@build_bazel_rules_swift//swift/internal:toolchain_utils.bzl",
"use_swift_toolchain",
)
load(
"@build_bazel_rules_swift//swift/internal:utils.bzl",
"expand_locations",
Expand Down Expand Up @@ -407,4 +411,5 @@ to your BUILD file's `package()` declaration instead of the individual targets.
fragments = ["cpp"],
test = True,
implementation = _swift_test_impl,
toolchains = use_swift_toolchain(),
)

1 comment on commit 016604d

@brentleyjones
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please sign in to comment.