diff --git a/doc/api.md b/doc/api.md index 9c8647139..225982df0 100644 --- a/doc/api.md +++ b/doc/api.md @@ -111,7 +111,7 @@ Compiles a Swift module. | generated_header_name | The name of the Objective-C generated header that should be generated for this module. If omitted, no header will be generated. | `None` | | is_test | Deprecated. This argument will be removed in the next major release. Use the `include_dev_srch_paths` attribute instead. Represents if the `testonly` value of the context. | `None` | | include_dev_srch_paths | A `bool` that indicates whether the developer framework search paths will be added to the compilation command. | `None` | -| module_name | The name of the Swift module being compiled. This must be present and valid; use `swift_common.derive_module_name` to generate a default from the target's label if needed. | none | +| module_name | The name of the Swift module being compiled. This must be present and valid; use `derive_swift_module_name` to generate a default from the target's label if needed. | none | | objc_infos | A list of `apple_common.ObjC` providers that represent C/Objective-C requirements of the target being compiled, such as Swift-compatible preprocessor defines, header search paths, and so forth. These are typically retrieved from a target's dependencies. | none | | package_name | The semantic package of the name of the Swift module being compiled. | none | | plugins | A list of `SwiftCompilerPluginInfo` providers that represent plugins that should be loaded by the compiler. | `[]` | @@ -180,7 +180,7 @@ Compiles a Swift module interface. | copts | A list of compiler flags that apply to the target being built. | `[]` | | exec_group | Runs the Swift compilation action under the given execution group's context. If `None`, the default execution group is used. | `None` | | feature_configuration | A feature configuration obtained from `swift_common.configure_features`. | none | -| module_name | The name of the Swift module being compiled. This must be present and valid; use `swift_common.derive_module_name` to generate a default from the target's label if needed. | none | +| module_name | The name of the Swift module being compiled. This must be present and valid; use `derive_swift_module_name` to generate a default from the target's label if needed. | none | | swiftinterface_file | The Swift module interface file to compile. | none | | swift_infos | A list of `SwiftInfo` providers from dependencies of the target being compiled. | none | | swift_toolchain | The `SwiftToolchainInfo` provider of the toolchain. | none | diff --git a/doc/doc.bzl b/doc/doc.bzl index 3e6d17595..9f94c591a 100644 --- a/doc/doc.bzl +++ b/doc/doc.bzl @@ -46,6 +46,10 @@ load( "//proto:swift_proto_library_group.bzl", _swift_proto_library_group = "swift_proto_library_group", ) +load( + "//swift:module_name.bzl", + _derive_swift_module_name = "derive_swift_module_name", +) load( "//swift:providers.bzl", _SwiftInfo = "SwiftInfo", @@ -103,6 +107,7 @@ swift_proto_library = _swift_proto_library swift_proto_library_group = _swift_proto_library_group # swift symbols +derive_swift_module_name = _derive_swift_module_name swift_common = _swift_common SwiftInfo = _SwiftInfo SwiftToolchainInfo = _SwiftToolchainInfo diff --git a/mixed_language/BUILD b/mixed_language/BUILD index 25995fe8a..b774a308d 100644 --- a/mixed_language/BUILD +++ b/mixed_language/BUILD @@ -18,6 +18,7 @@ bzl_library( "//mixed_language/internal:library", "//mixed_language/internal:module_map", "//mixed_language/internal:umbrella_header", + "//swift:module_name", "//swift:swift_interop_hint", "//swift:swift_library", "//swift/internal:compiling", diff --git a/mixed_language/mixed_language_library.bzl b/mixed_language/mixed_language_library.bzl index 84341b80c..9ffa54204 100644 --- a/mixed_language/mixed_language_library.bzl +++ b/mixed_language/mixed_language_library.bzl @@ -26,12 +26,10 @@ load( "//mixed_language/internal:umbrella_header.bzl", "mixed_language_umbrella_header", ) +load("//swift:module_name.bzl", "derive_swift_module_name") load("//swift:swift_interop_hint.bzl", "swift_interop_hint") load("//swift:swift_library.bzl", "swift_library") -# buildifier: disable=bzl-visibility -load("//swift/internal:compiling.bzl", "derive_module_name") - # `mixed_language_library` def mixed_language_library( @@ -225,7 +223,7 @@ a mixed language Swift library, use a clang only library rule like \ ) if not module_name: - module_name = derive_module_name(native.package_name(), name) + module_name = derive_swift_module_name(native.package_name(), name) if not module_map: internal_modulemap_name = name + "_modulemap" diff --git a/proto/BUILD b/proto/BUILD index 89175ed05..9576bf187 100644 --- a/proto/BUILD +++ b/proto/BUILD @@ -33,6 +33,7 @@ bzl_library( srcs = ["swift_proto_library.bzl"], deps = [ ":swift_proto_utils", + "//swift:module_name", "//swift:swift_clang_module_aspect", "//swift:swift_common", "//swift/internal:attrs", @@ -54,6 +55,7 @@ bzl_library( srcs = ["swift_proto_library_group.bzl"], deps = [ ":swift_proto_utils", + "//swift:module_name", "//swift:providers", "//swift:swift_common", "//swift/internal:toolchain_utils", diff --git a/proto/swift_proto_library.bzl b/proto/swift_proto_library.bzl index 473defc6d..f90fd600a 100644 --- a/proto/swift_proto_library.bzl +++ b/proto/swift_proto_library.bzl @@ -24,6 +24,7 @@ load( "@rules_proto//proto:defs.bzl", "ProtoInfo", ) +load("//swift:module_name.bzl", "derive_swift_module_name") load("//swift:providers.bzl", "SwiftProtoCompilerInfo") load("//swift:swift_clang_module_aspect.bzl", "swift_clang_module_aspect") load("//swift:swift_common.bzl", "swift_common") @@ -51,7 +52,7 @@ def _get_module_name(attr, target_label): """ module_name = attr.module_name if not module_name: - module_name = swift_common.derive_module_name(target_label) + module_name = derive_swift_module_name(target_label) return module_name # Rule diff --git a/proto/swift_proto_library_group.bzl b/proto/swift_proto_library_group.bzl index 18ee0bc5d..e2731014b 100644 --- a/proto/swift_proto_library_group.bzl +++ b/proto/swift_proto_library_group.bzl @@ -29,6 +29,7 @@ load( "SwiftProtoCcInfo", "compile_swift_protos_for_target", ) +load("//swift:module_name.bzl", "derive_swift_module_name") load( "//swift:providers.bzl", "SwiftInfo", @@ -47,7 +48,7 @@ load("//swift/internal:utils.bzl", "compact") def _swift_proto_library_group_aspect_impl(target, aspect_ctx): # Get the module name and generate the module mappings: - module_name = swift_common.derive_module_name(target.label) + module_name = derive_swift_module_name(target.label) # Compile the source files to a module: direct_providers = compile_swift_protos_for_target( diff --git a/swift/BUILD b/swift/BUILD index 0cf87f644..448c09e8b 100644 --- a/swift/BUILD +++ b/swift/BUILD @@ -57,6 +57,7 @@ bzl_library( name = "swift_binary", srcs = ["swift_binary.bzl"], deps = [ + ":module_name", ":providers", ":swift_common", "//swift/internal:compiling", @@ -72,6 +73,7 @@ bzl_library( name = "swift_clang_module_aspect", srcs = ["swift_clang_module_aspect.bzl"], deps = [ + ":module_name", ":providers", "//swift/internal:attrs", "//swift/internal:compiling", @@ -88,6 +90,7 @@ bzl_library( name = "swift_common", srcs = ["swift_common.bzl"], deps = [ + ":module_name", "//swift/internal:attrs", "//swift/internal:compiling", "//swift/internal:features", @@ -160,6 +163,7 @@ bzl_library( name = "swift_library", srcs = ["swift_library.bzl"], deps = [ + ":module_name", ":providers", ":swift_clang_module_aspect", ":swift_common", @@ -192,6 +196,7 @@ bzl_library( name = "swift_module_alias", srcs = ["swift_module_alias.bzl"], deps = [ + ":module_name", ":providers", ":swift_common", "//swift/internal:linking", @@ -214,6 +219,7 @@ bzl_library( name = "swift_module_mapping_test", srcs = ["swift_module_mapping_test.bzl"], deps = [ + ":providers", "//swift/internal:providers", ], ) @@ -241,6 +247,7 @@ bzl_library( name = "swift_test", srcs = ["swift_test.bzl"], deps = [ + ":module_name", ":providers", ":swift_common", "//swift/internal:env_expansion", diff --git a/swift/internal/BUILD b/swift/internal/BUILD index ec0c6a8f7..0865ed419 100644 --- a/swift/internal/BUILD +++ b/swift/internal/BUILD @@ -91,7 +91,6 @@ bzl_library( ":wmo", "@bazel_skylib//lib:paths", "@bazel_skylib//lib:sets", - "@bazel_skylib//lib:types", ], ) diff --git a/swift/internal/compiling.bzl b/swift/internal/compiling.bzl index b764bbd45..9f4f9e58e 100644 --- a/swift/internal/compiling.bzl +++ b/swift/internal/compiling.bzl @@ -16,7 +16,6 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_skylib//lib:sets.bzl", "sets") -load("@bazel_skylib//lib:types.bzl", "types") load( ":action_names.bzl", "SWIFT_ACTION_COMPILE", @@ -81,78 +80,6 @@ load(":wmo.bzl", "find_num_threads_flag_value", "is_wmo_manually_requested") # SWIFT_FEATURE_VFSOVERLAY is enabled. _SWIFTMODULES_VFS_ROOT = "/__build_bazel_rules_swift/swiftmodules" -def _module_name_safe(string): - """Returns a transformation of `string` that is safe for module names.""" - result = "" - saw_non_identifier_char = False - for ch in string.elems(): - if ch.isalnum() or ch == "_": - # If we're seeing an identifier character after a sequence of - # non-identifier characters, append an underscore and reset our - # tracking state before appending the identifier character. - if saw_non_identifier_char: - result += "_" - saw_non_identifier_char = False - result += ch - elif result: - # Only track this if `result` has content; this ensures that we - # (intentionally) drop leading non-identifier characters instead of - # adding a leading underscore. - saw_non_identifier_char = True - - return result - -def derive_module_name(*args): - """Returns a derived module name from the given build label. - - For targets whose module name is not explicitly specified, the module name - is computed using the following algorithm: - - * The package and name components of the label are considered separately. - All _interior_ sequences of non-identifier characters (anything other - than `a-z`, `A-Z`, `0-9`, and `_`) are replaced by a single underscore - (`_`). Any leading or trailing non-identifier characters are dropped. - * If the package component is non-empty after the above transformation, - it is joined with the transformed name component using an underscore. - Otherwise, the transformed name is used by itself. - * If this would result in a string that begins with a digit (`0-9`), an - underscore is prepended to make it identifier-safe. - - This mapping is intended to be fairly predictable, but not reversible. - - Args: - *args: Either a single argument of type `Label`, or two arguments of - type `str` where the first argument is the package name and the - second argument is the target name. - - Returns: - The module name derived from the label. - """ - if (len(args) == 1 and - hasattr(args[0], "package") and - hasattr(args[0], "name")): - label = args[0] - package = label.package - name = label.name - elif (len(args) == 2 and - types.is_string(args[0]) and - types.is_string(args[1])): - package = args[0] - name = args[1] - else: - fail("derive_module_name may only be called with a single argument " + - "of type 'Label' or two arguments of type 'str'.") - - package_part = _module_name_safe(package.lstrip("//")) - name_part = _module_name_safe(name) - if package_part: - module_name = package_part + "_" + name_part - else: - module_name = name_part - if module_name[0].isdigit(): - module_name = "_" + module_name - return module_name - def create_compilation_context(defines, srcs, transitive_modules): """Cretes a compilation context for a Swift target. @@ -228,8 +155,8 @@ def compile_module_interface( feature_configuration: A feature configuration obtained from `swift_common.configure_features`. module_name: The name of the Swift module being compiled. This must be - present and valid; use `swift_common.derive_module_name` to generate - a default from the target's label if needed. + present and valid; use `derive_swift_module_name` to generate a + default from the target's label if needed. swiftinterface_file: The Swift module interface file to compile. swift_infos: A list of `SwiftInfo` providers from dependencies of the target being compiled. @@ -416,8 +343,8 @@ def compile( should be generated for this module. If omitted, no header will be generated. module_name: The name of the Swift module being compiled. This must be - present and valid; use `swift_common.derive_module_name` to generate - a default from the target's label if needed. + present and valid; use `derive_swift_module_name` to generate a + default from the target's label if needed. objc_infos: A list of `apple_common.ObjC` providers that represent C/Objective-C requirements of the target being compiled, such as Swift-compatible preprocessor defines, header search paths, and so diff --git a/swift/module_name.bzl b/swift/module_name.bzl index 761ace000..bf6dda713 100644 --- a/swift/module_name.bzl +++ b/swift/module_name.bzl @@ -54,8 +54,8 @@ def derive_swift_module_name(*args): package = args[0] name = args[1] else: - fail("derive_module_name may only be called with a single argument " + - "of type 'Label' or two arguments of type 'str'.") + fail("derive_swift_module_name may only be called with a single " + + "argument of type 'Label' or two arguments of type 'str'.") package_part = _module_name_safe(package.lstrip("//")) name_part = _module_name_safe(name) diff --git a/swift/swift_binary.bzl b/swift/swift_binary.bzl index fcf6614bb..a6bd99d4b 100644 --- a/swift/swift_binary.bzl +++ b/swift/swift_binary.bzl @@ -37,6 +37,7 @@ load( "get_providers", "include_developer_search_paths", ) +load(":module_name.bzl", "derive_swift_module_name") load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo") load(":swift_common.bzl", "swift_common") @@ -79,7 +80,7 @@ def _swift_binary_impl(ctx): if srcs: module_name = ctx.attr.module_name if not module_name: - module_name = swift_common.derive_module_name(ctx.label) + module_name = derive_swift_module_name(ctx.label) include_dev_srch_paths = include_developer_search_paths(ctx.attr) diff --git a/swift/swift_clang_module_aspect.bzl b/swift/swift_clang_module_aspect.bzl index 8990860c5..c52c4ac60 100644 --- a/swift/swift_clang_module_aspect.bzl +++ b/swift/swift_clang_module_aspect.bzl @@ -16,11 +16,7 @@ load("@bazel_skylib//lib:sets.bzl", "sets") load("//swift/internal:attrs.bzl", "swift_toolchain_attrs") -load( - "//swift/internal:compiling.bzl", - "derive_module_name", - "precompile_clang_module", -) +load("//swift/internal:compiling.bzl", "precompile_clang_module") load( "//swift/internal:feature_names.bzl", "SWIFT_FEATURE_EMIT_C_MODULE", @@ -54,6 +50,7 @@ load( "//swift/internal:utils.bzl", "compilation_context_for_explicit_module_compilation", ) +load(":module_name.bzl", "derive_swift_module_name") load(":providers.bzl", "SwiftInfo") _MULTIPLE_TARGET_ASPECT_ATTRS = [ @@ -360,7 +357,7 @@ def _module_info_for_target( # was some other `Objc`-providing target, derive the module name # now. if not module_name: - module_name = derive_module_name(target.label) + module_name = derive_swift_module_name(target.label) # If we didn't get a module map above, generate it now. if not module_map_file: @@ -696,7 +693,7 @@ def _swift_clang_module_aspect_impl(target, aspect_ctx): exclude_headers = interop_info.exclude_headers module_map_file = interop_info.module_map module_name = ( - interop_info.module_name or derive_module_name(target.label) + interop_info.module_name or derive_swift_module_name(target.label) ) swift_infos.extend(interop_info.swift_infos) requested_features.extend(interop_info.requested_features) diff --git a/swift/swift_common.bzl b/swift/swift_common.bzl index f958ae9cc..f37d6d145 100644 --- a/swift/swift_common.bzl +++ b/swift/swift_common.bzl @@ -31,7 +31,6 @@ load( "compile", "compile_module_interface", "create_compilation_context", - "derive_module_name", "precompile_clang_module", ) load( @@ -61,6 +60,7 @@ load( "get_swift_toolchain", "use_swift_toolchain", ) +load(":module_name.bzl", "derive_swift_module_name") # The exported `swift_common` module, which defines the public API for directly # invoking actions that compile Swift code from other rules. @@ -77,7 +77,9 @@ swift_common = struct( create_swift_info = create_swift_info, create_swift_interop_info = create_swift_interop_info, create_swift_module = create_swift_module, - derive_module_name = derive_module_name, + # TODO(b/261444771): Remove this after everyone is migrated to the free + # function. + derive_module_name = derive_swift_module_name, extract_symbol_graph = extract_symbol_graph, get_toolchain = get_swift_toolchain, is_enabled = is_feature_enabled, diff --git a/swift/swift_library.bzl b/swift/swift_library.bzl index c4599e2db..fc9e67dae 100644 --- a/swift/swift_library.bzl +++ b/swift/swift_library.bzl @@ -43,6 +43,7 @@ load( "get_providers", "include_developer_search_paths", ) +load(":module_name.bzl", "derive_swift_module_name") load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo") load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect") load(":swift_common.bzl", "swift_common") @@ -139,7 +140,7 @@ def _swift_library_impl(ctx): module_name = ctx.attr.module_name if not module_name: - module_name = swift_common.derive_module_name(ctx.label) + module_name = derive_swift_module_name(ctx.label) swift_toolchain = swift_common.get_toolchain(ctx) feature_configuration = swift_common.configure_features( diff --git a/swift/swift_module_alias.bzl b/swift/swift_module_alias.bzl index b41c15a62..a83ec38a7 100644 --- a/swift/swift_module_alias.bzl +++ b/swift/swift_module_alias.bzl @@ -22,6 +22,7 @@ load( ) load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load("//swift/internal:utils.bzl", "compact", "get_providers") +load(":module_name.bzl", "derive_swift_module_name") load(":providers.bzl", "SwiftInfo") load(":swift_common.bzl", "swift_common") @@ -35,7 +36,7 @@ def _swift_module_alias_impl(ctx): module_name = ctx.attr.module_name if not module_name: - module_name = swift_common.derive_module_name(ctx.label) + module_name = derive_swift_module_name(ctx.label) # Generate a source file that imports each of the deps using `@_exported`. reexport_src = ctx.actions.declare_file( diff --git a/swift/swift_test.bzl b/swift/swift_test.bzl index ede3a350b..84f0ef4f2 100644 --- a/swift/swift_test.bzl +++ b/swift/swift_test.bzl @@ -42,6 +42,7 @@ load( "expand_locations", "include_developer_search_paths", ) +load(":module_name.bzl", "derive_swift_module_name") load( ":providers.bzl", "SwiftCompilerPluginInfo", @@ -382,7 +383,7 @@ def _swift_test_impl(ctx): module_name = ctx.attr.module_name if not module_name: - module_name = swift_common.derive_module_name(ctx.label) + module_name = derive_swift_module_name(ctx.label) include_dev_srch_paths = include_developer_search_paths(ctx.attr)