From 2831d7177340bc5db0fcde421ca31277767d9290 Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Tue, 30 Aug 2022 13:30:18 -0700 Subject: [PATCH] Add the `swift_common.use_toolchain()` helper 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 016604d34a0b3896d5298b49d539bfaef31c4d4d) Signed-off-by: Brentley Jones --- doc/api.md | 26 +++++++++++++++ .../rules/custom_swift_proto_compiler.bzl | 2 +- .../swift_import/get_swiftmodule.bzl | 2 +- proto/BUILD | 10 ++++-- proto/swift_proto_compiler.bzl | 2 +- proto/swift_proto_library.bzl | 19 +++++------ proto/swift_proto_library_group.bzl | 9 ++--- swift/BUILD | 33 +++++-------------- swift/internal/BUILD | 5 ++- swift/internal/swift_symbol_graph_aspect.bzl | 7 +++- swift/internal/toolchain_utils.bzl | 25 ++++++++++++-- swift/swift_binary.bzl | 2 ++ swift/swift_clang_module_aspect.bzl | 7 +++- swift/swift_common.bzl | 2 ++ swift/swift_compiler_plugin.bzl | 8 +++-- swift/swift_import.bzl | 3 +- swift/swift_library.bzl | 6 ++-- swift/swift_library_group.bzl | 2 ++ swift/swift_module_alias.bzl | 2 ++ swift/swift_test.bzl | 2 ++ 20 files changed, 118 insertions(+), 56 deletions(-) diff --git a/doc/api.md b/doc/api.md index 26247a3b5..6ca045dfa 100644 --- a/doc/api.md +++ b/doc/api.md @@ -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. + + +## swift_common.use_toolchain + +
+swift_common.use_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()`. + + diff --git a/examples/xplatform/custom_swift_proto_compiler/rules/custom_swift_proto_compiler.bzl b/examples/xplatform/custom_swift_proto_compiler/rules/custom_swift_proto_compiler.bzl index 0ec7f42c0..1ddbf5494 100644 --- a/examples/xplatform/custom_swift_proto_compiler/rules/custom_swift_proto_compiler.bzl +++ b/examples/xplatform/custom_swift_proto_compiler/rules/custom_swift_proto_compiler.bzl @@ -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 = [], @@ -125,4 +124,5 @@ custom_swift_proto_compiler = rule( cfg = "exec", ), }, + implementation = _custom_swift_proto_compiler_impl, ) diff --git a/examples/xplatform/swift_import/get_swiftmodule.bzl b/examples/xplatform/swift_import/get_swiftmodule.bzl index dc37497ae..bbfd733c4 100644 --- a/examples/xplatform/swift_import/get_swiftmodule.bzl +++ b/examples/xplatform/swift_import/get_swiftmodule.bzl @@ -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, ) diff --git a/proto/BUILD b/proto/BUILD index 36a83aa1b..2d02c7326 100644 --- a/proto/BUILD +++ b/proto/BUILD @@ -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", @@ -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", ], diff --git a/proto/swift_proto_compiler.bzl b/proto/swift_proto_compiler.bzl index fa0dcb39a..917a5e083 100644 --- a/proto/swift_proto_compiler.bzl +++ b/proto/swift_proto_compiler.bzl @@ -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 = """\ @@ -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, ) diff --git a/proto/swift_proto_library.bzl b/proto/swift_proto_library.bzl index 719866f3a..dbb7fef55 100644 --- a/proto/swift_proto_library.bzl +++ b/proto/swift_proto_library.bzl @@ -24,24 +24,22 @@ 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 @@ -190,4 +188,5 @@ swift_proto_library( """, fragments = ["cpp"], implementation = _swift_proto_library_impl, + toolchains = use_swift_toolchain(), ) diff --git a/proto/swift_proto_library_group.bzl b/proto/swift_proto_library_group.bzl index 6937b70d1..18ee0bc5d 100644 --- a/proto/swift_proto_library_group.bzl +++ b/proto/swift_proto_library_group.bzl @@ -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 @@ -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 diff --git a/swift/BUILD b/swift/BUILD index 046ebd29d..5a332792f 100644 --- a/swift/BUILD +++ b/swift/BUILD @@ -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", @@ -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", ], @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", ], @@ -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", @@ -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", @@ -213,13 +201,13 @@ 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:utils", + "//swift/internal:toolchain_utils", "@bazel_skylib//lib:dicts", ], ) @@ -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", ], @@ -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", @@ -251,9 +238,9 @@ 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", ], ) @@ -261,7 +248,6 @@ bzl_library( bzl_library( name = "swift_test", srcs = ["swift_test.bzl"], - visibility = ["//swift:__subpackages__"], deps = [ ":providers", ":swift_common", @@ -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", ], @@ -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` @@ -310,7 +296,6 @@ 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 @@ -318,14 +303,12 @@ swift_interop_hint( 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. diff --git a/swift/internal/BUILD b/swift/internal/BUILD index 3153598a3..5bdb932c0 100644 --- a/swift/internal/BUILD +++ b/swift/internal/BUILD @@ -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( diff --git a/swift/internal/swift_symbol_graph_aspect.bzl b/swift/internal/swift_symbol_graph_aspect.bzl index 006b1d37f..79734ba44 100644 --- a/swift/internal/swift_symbol_graph_aspect.bzl +++ b/swift/internal/swift_symbol_graph_aspect.bzl @@ -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): @@ -186,4 +190,5 @@ default value is {default_value}. fragments = ["cpp"], implementation = aspect_impl, provides = [SwiftSymbolGraphInfo], + toolchains = use_swift_toolchain(), ) diff --git a/swift/internal/toolchain_utils.bzl b/swift/internal/toolchain_utils.bzl index 7af57df73..d99ab72d1 100644 --- a/swift/internal/toolchain_utils.bzl +++ b/swift/internal/toolchain_utils.bzl @@ -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. @@ -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 [] diff --git a/swift/swift_binary.bzl b/swift/swift_binary.bzl index 5b2aa09e8..5a682a170 100644 --- a/swift/swift_binary.bzl +++ b/swift/swift_binary.bzl @@ -30,6 +30,7 @@ load( "//swift/internal:output_groups.bzl", "supplemental_compilation_output_groups", ) +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load( "//swift/internal:utils.bzl", "expand_locations", @@ -212,4 +213,5 @@ please use one of the platform-specific application rules in executable = True, fragments = ["cpp"], implementation = _swift_binary_impl, + toolchains = use_swift_toolchain(), ) diff --git a/swift/swift_clang_module_aspect.bzl b/swift/swift_clang_module_aspect.bzl index 4cb9f7b24..e8830bb81 100644 --- a/swift/swift_clang_module_aspect.bzl +++ b/swift/swift_clang_module_aspect.bzl @@ -45,7 +45,11 @@ load( "//swift/internal:swift_interop_info.bzl", "SwiftInteropInfo", ) -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", "compilation_context_for_explicit_module_compilation", @@ -782,4 +786,5 @@ it propagates for its targets. [apple_common.Objc], [CcInfo], ], + toolchains = use_swift_toolchain(), ) diff --git a/swift/swift_common.bzl b/swift/swift_common.bzl index 1bc1bd806..f958ae9cc 100644 --- a/swift/swift_common.bzl +++ b/swift/swift_common.bzl @@ -59,6 +59,7 @@ load( load( "//swift/internal:toolchain_utils.bzl", "get_swift_toolchain", + "use_swift_toolchain", ) # The exported `swift_common` module, which defines the public API for directly @@ -83,4 +84,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, ) diff --git a/swift/swift_compiler_plugin.bzl b/swift/swift_compiler_plugin.bzl index 4e8ba6b8a..70074c3a2 100644 --- a/swift/swift_compiler_plugin.bzl +++ b/swift/swift_compiler_plugin.bzl @@ -35,6 +35,7 @@ load( "//swift/internal:output_groups.bzl", "supplemental_compilation_output_groups", ) +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load( "//swift/internal:utils.bzl", "expand_locations", @@ -250,6 +251,7 @@ swift_library( executable = True, fragments = ["cpp"], implementation = _swift_compiler_plugin_impl, + toolchains = use_swift_toolchain(), ) def _universal_swift_compiler_plugin_impl(ctx): @@ -330,9 +332,6 @@ universal_swift_compiler_plugin = rule( ), }, ), - executable = True, - fragments = ["cpp", "apple"], - implementation = _universal_swift_compiler_plugin_impl, doc = """\ Wraps an existing `swift_compiler_plugin` target to produce a universal binary. @@ -368,4 +367,7 @@ swift_library( ) ``` """, + executable = True, + fragments = ["cpp", "apple"], + implementation = _universal_swift_compiler_plugin_impl, ) diff --git a/swift/swift_import.bzl b/swift/swift_import.bzl index 8d3f9272f..d1b8d18d2 100644 --- a/swift/swift_import.bzl +++ b/swift/swift_import.bzl @@ -21,6 +21,7 @@ load( "swift_toolchain_attrs", ) load("//swift/internal:linking.bzl", "new_objc_provider") +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load( "//swift/internal:utils.bzl", "compact", @@ -148,7 +149,6 @@ def _swift_import_impl(ctx): swift_import = rule( attrs = dicts.add( - swift_toolchain_attrs(), swift_common_rule_attrs(), swift_toolchain_attrs(), { @@ -207,4 +207,5 @@ the `.private.swiftinterface` files are required in order to build any code that """, fragments = ["cpp"], implementation = _swift_import_impl, + toolchains = use_swift_toolchain(), ) diff --git a/swift/swift_library.bzl b/swift/swift_library.bzl index 79102b83d..40a28ae63 100644 --- a/swift/swift_library.bzl +++ b/swift/swift_library.bzl @@ -36,6 +36,7 @@ load( "//swift/internal:output_groups.bzl", "supplemental_compilation_output_groups", ) +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load( "//swift/internal:utils.bzl", "compact", @@ -319,7 +320,8 @@ dependent for linking, but artifacts/flags required for compilation (such as doc = """\ Compiles and links Swift code into a static library and Swift module. """, - outputs = swift_library_output_map, - implementation = _swift_library_impl, fragments = ["cpp"], + implementation = _swift_library_impl, + outputs = swift_library_output_map, + toolchains = use_swift_toolchain(), ) diff --git a/swift/swift_library_group.bzl b/swift/swift_library_group.bzl index bc8e0096f..719f837a9 100644 --- a/swift/swift_library_group.bzl +++ b/swift/swift_library_group.bzl @@ -16,6 +16,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts") load("//swift/internal:attrs.bzl", "swift_deps_attr", "swift_toolchain_attrs") +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load("//swift/internal:utils.bzl", "get_providers") load(":providers.bzl", "SwiftInfo") load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect") @@ -97,4 +98,5 @@ Unlike `swift_module_alias`, a new module isn't created for this target, you need to import the grouped libraries directly. """, implementation = _swift_library_group_impl, + toolchains = use_swift_toolchain(), ) diff --git a/swift/swift_module_alias.bzl b/swift/swift_module_alias.bzl index d72c6430a..b41c15a62 100644 --- a/swift/swift_module_alias.bzl +++ b/swift/swift_module_alias.bzl @@ -20,6 +20,7 @@ load( "//swift/internal:output_groups.bzl", "supplemental_compilation_output_groups", ) +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load("//swift/internal:utils.bzl", "compact", "get_providers") load(":providers.bzl", "SwiftInfo") load(":swift_common.bzl", "swift_common") @@ -185,4 +186,5 @@ symbol is defined; it is not repeated by the alias module.) """, fragments = ["cpp"], implementation = _swift_module_alias_impl, + toolchains = use_swift_toolchain(), ) diff --git a/swift/swift_test.bzl b/swift/swift_test.bzl index 21ca4ef11..7573dacfc 100644 --- a/swift/swift_test.bzl +++ b/swift/swift_test.bzl @@ -35,6 +35,7 @@ load( "//swift/internal:swift_symbol_graph_aspect.bzl", "make_swift_symbol_graph_aspect", ) +load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load( "//swift/internal:utils.bzl", "expand_locations", @@ -519,4 +520,5 @@ bazel test //:Tests --test_filter=TestModuleName.TestClassName/testMethodName fragments = ["cpp"], test = True, implementation = _swift_test_impl, + toolchains = use_swift_toolchain(), )