Skip to content

Commit

Permalink
Apply swift.add_target_name_to_output only to outputs that need it
Browse files Browse the repository at this point in the history
In particular, only outputs that need to have the module name as part of the filename.

Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
brentleyjones committed Jun 17, 2024
1 parent d60ff7f commit acdb5e7
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 292 deletions.
4 changes: 3 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ A tuple containing three elements:

<pre>
swift_common.compile_module_interface(<a href="#swift_common.compile_module_interface-actions">actions</a>, <a href="#swift_common.compile_module_interface-compilation_contexts">compilation_contexts</a>, <a href="#swift_common.compile_module_interface-feature_configuration">feature_configuration</a>,
<a href="#swift_common.compile_module_interface-module_name">module_name</a>, <a href="#swift_common.compile_module_interface-swiftinterface_file">swiftinterface_file</a>, <a href="#swift_common.compile_module_interface-swift_infos">swift_infos</a>, <a href="#swift_common.compile_module_interface-swift_toolchain">swift_toolchain</a>)
<a href="#swift_common.compile_module_interface-module_name">module_name</a>, <a href="#swift_common.compile_module_interface-swiftinterface_file">swiftinterface_file</a>, <a href="#swift_common.compile_module_interface-swift_infos">swift_infos</a>, <a href="#swift_common.compile_module_interface-swift_toolchain">swift_toolchain</a>,
<a href="#swift_common.compile_module_interface-target_name">target_name</a>)
</pre>

Compiles a Swift module interface.
Expand All @@ -168,6 +169,7 @@ Compiles a Swift module interface.
| <a id="swift_common.compile_module_interface-swiftinterface_file"></a>swiftinterface_file | The Swift module interface file to compile. | none |
| <a id="swift_common.compile_module_interface-swift_infos"></a>swift_infos | A list of `SwiftInfo` providers from dependencies of the target being compiled. | none |
| <a id="swift_common.compile_module_interface-swift_toolchain"></a>swift_toolchain | The `SwiftToolchainInfo` provider of the toolchain. | none |
| <a id="swift_common.compile_module_interface-target_name"></a>target_name | The name of the target for which the code is being compiled, which is used to determine unique file paths for the outputs. | none |

**RETURNS**

Expand Down
4 changes: 2 additions & 2 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ swift_proto_library(
| <a id="swift_proto_library-additional_compiler_info"></a>additional_compiler_info | Dictionary of additional information passed to the compiler targets. See the documentation of the respective compiler rules for more information on which fields are accepted and how they are used. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
| <a id="swift_proto_library-always_include_developer_search_paths"></a>always_include_developer_search_paths | If `True`, the developer framework search paths will be added to the compilation command. This enables a Swift module to access `XCTest` without having to mark the target as `testonly = True`. | Boolean | optional | `False` |
| <a id="swift_proto_library-alwayslink"></a>alwayslink | If true, any binary that depends (directly or indirectly) on this Swift module will link in all the object files for the files listed in `srcs`, even if some contain no symbols referenced by the binary. This is useful if your code isn't explicitly called by code in the binary; for example, if you rely on runtime checks for protocol conformances added in extensions in the library but do not directly reference any other symbols in the object file that adds that conformance. | Boolean | optional | `False` |
| <a id="swift_proto_library-compilers"></a>compilers | One or more `swift_proto_compiler` targets (or targets producing `SwiftProtoCompilerInfo`), from which the Swift protos will be generated. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `["@build_bazel_rules_swift//proto/compilers:swift_proto"]` |
| <a id="swift_proto_library-compilers"></a>compilers | One or more `swift_proto_compiler` targets (or targets producing `SwiftProtoCompilerInfo`), from which the Swift protos will be generated. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `["@rules_swift//proto/compilers:swift_proto"]` |
| <a id="swift_proto_library-copts"></a>copts | Additional compiler options that should be passed to `swiftc`. These strings are subject to `$(location ...)` and ["Make" variable](https://docs.bazel.build/versions/master/be/make-variables.html) expansion. | List of strings | optional | `[]` |
| <a id="swift_proto_library-defines"></a>defines | A list of defines to add to the compilation command line.<br><br>Note that unlike C-family languages, Swift defines do not have values; they are simply identifiers that are either defined or undefined. So strings in this list should be simple identifiers, **not** `name=value` pairs.<br><br>Each string is prepended with `-D` and added to the command line. Unlike `copts`, these flags are added for the target and every target that depends on it, so use this attribute with caution. It is preferred that you add defines directly to `copts`, only using this feature in the rare case that a library needs to propagate a symbol up to those that depend on it. | List of strings | optional | `[]` |
| <a id="swift_proto_library-generated_header_name"></a>generated_header_name | The name of the generated Objective-C interface header. This name must end with a `.h` extension and cannot contain any path separators.<br><br>If this attribute is not specified, then the default behavior is to name the header `${target_name}-Swift.h`.<br><br>This attribute is ignored if the toolchain does not support generating headers. | String | optional | `""` |
Expand Down Expand Up @@ -674,7 +674,7 @@ swift_binary(
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="swift_proto_library_group-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="swift_proto_library_group-compiler"></a>compiler | A `swift_proto_compiler` target (or target producing `SwiftProtoCompilerInfo`), from which the Swift protos will be generated. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@build_bazel_rules_swift//proto/compilers:swift_proto"` |
| <a id="swift_proto_library_group-compiler"></a>compiler | A `swift_proto_compiler` target (or target producing `SwiftProtoCompilerInfo`), from which the Swift protos will be generated. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@rules_swift//proto/compilers:swift_proto"` |
| <a id="swift_proto_library_group-proto"></a>proto | Exactly one `proto_library` target (or target producing `ProtoInfo`), from which the Swift source files should be generated. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |


Expand Down
40 changes: 7 additions & 33 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,8 @@ def compile_module_interface(
module_name,
swiftinterface_file,
swift_infos,
swift_toolchain):
swift_toolchain,
target_name):
"""Compiles a Swift module interface.
Args:
Expand All @@ -2198,6 +2199,9 @@ def compile_module_interface(
swift_infos: A list of `SwiftInfo` providers from dependencies of the
target being compiled.
swift_toolchain: The `SwiftToolchainInfo` provider of the toolchain.
target_name: The name of the target for which the code is being
compiled, which is used to determine unique file paths for the
outputs.
Returns:
A Swift module context (as returned by `swift_common.create_module`)
Expand Down Expand Up @@ -2235,11 +2239,6 @@ def compile_module_interface(
continue
transitive_swiftmodules.append(swift_module.swiftmodule)

add_target_name_to_output_path = is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_ADD_TARGET_NAME_TO_OUTPUT,
)

# We need this when generating the VFS overlay file and also when
# configuring inputs for the compile action, so it's best to precompute it
# here.
Expand All @@ -2249,8 +2248,7 @@ def compile_module_interface(
):
vfsoverlay_file = derived_files.vfsoverlay(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = module_name,
target_name = target_name,
)
write_vfsoverlay(
actions = actions,
Expand All @@ -2269,7 +2267,7 @@ def compile_module_interface(
fail("Cannot use both `swift.vfsoverlay` and `swift.use_explicit_swift_module_map` features at the same time.")

explicit_swift_module_map_file = actions.declare_file(
"{}.swift-explicit-module-map.json".format(module_name),
"{}.swift-explicit-module-map.json".format(target_name),
)
write_explicit_swift_module_map_file(
actions = actions,
Expand Down Expand Up @@ -2545,11 +2543,6 @@ def compile(
sets.make(swift_module.defines),
)

add_target_name_to_output_path = is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_ADD_TARGET_NAME_TO_OUTPUT,
)

# We need this when generating the VFS overlay file and also when
# configuring inputs for the compile action, so it's best to precompute it
# here.
Expand All @@ -2559,7 +2552,6 @@ def compile(
):
vfsoverlay_file = derived_files.vfsoverlay(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)
write_vfsoverlay(
Expand Down Expand Up @@ -2887,14 +2879,8 @@ def _precompile_clang_module(
):
return None

add_target_name_to_output_path = is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_ADD_TARGET_NAME_TO_OUTPUT,
)

precompiled_module = derived_files.precompiled_module(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)

Expand Down Expand Up @@ -3216,12 +3202,10 @@ def _declare_compile_outputs(
# no other partial outputs.
object_files = [derived_files.whole_module_object_file(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)]
ast_files = [derived_files.ast(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
src = srcs[0],
)]
Expand All @@ -3242,7 +3226,6 @@ def _declare_compile_outputs(
# object files so that we can pass them all to the archive action.
output_info = _declare_multiple_outputs_and_write_output_file_map(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
extract_const_values = extract_const_values,
is_wmo = output_nature.is_wmo,
emits_bc = emits_bc,
Expand Down Expand Up @@ -3270,7 +3253,6 @@ def _declare_compile_outputs(
):
indexstore_directory = derived_files.indexstore_directory(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)
else:
Expand All @@ -3283,7 +3265,6 @@ def _declare_compile_outputs(
if (emit_symbol_graph):
symbol_graph_directory = derived_files.symbol_graph_directory(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)
else:
Expand Down Expand Up @@ -3323,7 +3304,6 @@ def _declare_compile_outputs(

def _declare_multiple_outputs_and_write_output_file_map(
actions,
add_target_name_to_output_path,
extract_const_values,
is_wmo,
emits_bc,
Expand All @@ -3334,7 +3314,6 @@ def _declare_multiple_outputs_and_write_output_file_map(
Args:
actions: The object used to register actions.
add_target_name_to_output_path: Add target_name in output path. More info at SWIFT_FEATURE_ADD_TARGET_NAME_TO_OUTPUT description.
extract_const_values: A Boolean value indicating whether constant values
should be extracted during this compilation.
is_wmo: A Boolean value indicating whether whole-module-optimization was
Expand Down Expand Up @@ -3366,14 +3345,12 @@ def _declare_multiple_outputs_and_write_output_file_map(
"""
output_map_file = derived_files.swiftc_output_file_map(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)

if split_derived_file_generation:
derived_files_output_map_file = derived_files.swiftc_derived_output_file_map(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
)
else:
Expand Down Expand Up @@ -3420,7 +3397,6 @@ def _declare_multiple_outputs_and_write_output_file_map(
# Declare the llvm bc file (there is one per source file).
obj = derived_files.intermediate_bc_file(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
src = src,
)
Expand All @@ -3430,7 +3406,6 @@ def _declare_multiple_outputs_and_write_output_file_map(
# Declare the object file (there is one per source file).
obj = derived_files.intermediate_object_file(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
src = src,
)
Expand All @@ -3439,7 +3414,6 @@ def _declare_multiple_outputs_and_write_output_file_map(

ast = derived_files.ast(
actions = actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = target_name,
src = src,
)
Expand Down
6 changes: 0 additions & 6 deletions swift/internal/debugging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,11 @@ def ensure_swiftmodule_is_embedded(
action_name = swift_action_names.MODULEWRAP,
swift_toolchain = swift_toolchain,
):
add_target_name_to_output_path = is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_ADD_TARGET_NAME_TO_OUTPUT,
)

# For ELF-format binaries, we need to invoke a Swift modulewrap action
# to wrap the .swiftmodule file in a .o file that gets propagated to the
# linker.
modulewrap_obj = derived_files.modulewrap_object(
actions,
add_target_name_to_output_path = add_target_name_to_output_path,
target_name = label.name,
)
_register_modulewrap_action(
Expand Down
Loading

0 comments on commit acdb5e7

Please sign in to comment.