Skip to content

Commit

Permalink
Move const_extract_protocol.json to static file in toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
BalestraPatrick committed Mar 6, 2024
1 parent fed35cc commit 5e2a87b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 60 deletions.
11 changes: 6 additions & 5 deletions doc/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ Propagates Swift-specific information about a `proto_library`.
## SwiftToolchainInfo

<pre>
SwiftToolchainInfo(<a href="#SwiftToolchainInfo-action_configs">action_configs</a>, <a href="#SwiftToolchainInfo-cc_toolchain_info">cc_toolchain_info</a>, <a href="#SwiftToolchainInfo-clang_implicit_deps_providers">clang_implicit_deps_providers</a>, <a href="#SwiftToolchainInfo-developer_dirs">developer_dirs</a>,
<a href="#SwiftToolchainInfo-entry_point_linkopts_provider">entry_point_linkopts_provider</a>, <a href="#SwiftToolchainInfo-feature_allowlists">feature_allowlists</a>,
<a href="#SwiftToolchainInfo-generated_header_module_implicit_deps_providers">generated_header_module_implicit_deps_providers</a>, <a href="#SwiftToolchainInfo-implicit_deps_providers">implicit_deps_providers</a>,
<a href="#SwiftToolchainInfo-package_configurations">package_configurations</a>, <a href="#SwiftToolchainInfo-requested_features">requested_features</a>, <a href="#SwiftToolchainInfo-root_dir">root_dir</a>, <a href="#SwiftToolchainInfo-swift_worker">swift_worker</a>,
<a href="#SwiftToolchainInfo-test_configuration">test_configuration</a>, <a href="#SwiftToolchainInfo-tool_configs">tool_configs</a>, <a href="#SwiftToolchainInfo-unsupported_features">unsupported_features</a>)
SwiftToolchainInfo(<a href="#SwiftToolchainInfo-action_configs">action_configs</a>, <a href="#SwiftToolchainInfo-cc_toolchain_info">cc_toolchain_info</a>, <a href="#SwiftToolchainInfo-clang_implicit_deps_providers">clang_implicit_deps_providers</a>,
<a href="#SwiftToolchainInfo-const_gather_protocols">const_gather_protocols</a>, <a href="#SwiftToolchainInfo-developer_dirs">developer_dirs</a>, <a href="#SwiftToolchainInfo-entry_point_linkopts_provider">entry_point_linkopts_provider</a>,
<a href="#SwiftToolchainInfo-feature_allowlists">feature_allowlists</a>, <a href="#SwiftToolchainInfo-generated_header_module_implicit_deps_providers">generated_header_module_implicit_deps_providers</a>,
<a href="#SwiftToolchainInfo-implicit_deps_providers">implicit_deps_providers</a>, <a href="#SwiftToolchainInfo-package_configurations">package_configurations</a>, <a href="#SwiftToolchainInfo-requested_features">requested_features</a>, <a href="#SwiftToolchainInfo-root_dir">root_dir</a>,
<a href="#SwiftToolchainInfo-swift_worker">swift_worker</a>, <a href="#SwiftToolchainInfo-test_configuration">test_configuration</a>, <a href="#SwiftToolchainInfo-tool_configs">tool_configs</a>, <a href="#SwiftToolchainInfo-unsupported_features">unsupported_features</a>)
</pre>

Propagates information about a Swift toolchain to compilation and linking rules
Expand All @@ -121,6 +121,7 @@ that use the toolchain.
| <a id="SwiftToolchainInfo-action_configs"></a>action_configs | This field is an internal implementation detail of the build rules. |
| <a id="SwiftToolchainInfo-cc_toolchain_info"></a>cc_toolchain_info | The `cc_common.CcToolchainInfo` provider from the Bazel C++ toolchain that this Swift toolchain depends on. |
| <a id="SwiftToolchainInfo-clang_implicit_deps_providers"></a>clang_implicit_deps_providers | A `struct` with the following fields, which represent providers from targets that should be added as implicit dependencies of any precompiled explicit C/Objective-C modules:<br><br>* `cc_infos`: A list of `CcInfo` providers from targets specified as the toolchain's implicit dependencies. * `objc_infos`: A list of `apple_common.Objc` providers from targets specified as the toolchain's implicit dependencies. * `swift_infos`: A list of `SwiftInfo` providers from targets specified as the toolchain's implicit dependencies.<br><br>For ease of use, this field is never `None`; it will always be a valid `struct` containing the fields described above, even if those lists are empty. |
| <a id="SwiftToolchainInfo-const_gather_protocols"></a>const_gather_protocols | `File`. A JSON file specifying a list of protocols for extraction of conformances' const values. |
| <a id="SwiftToolchainInfo-developer_dirs"></a>developer_dirs | A list of `structs` containing the following fields:* `developer_path_label`: A `string` representing the type of developer path. * `path`: A `string` representing the path to the developer framework. |
| <a id="SwiftToolchainInfo-entry_point_linkopts_provider"></a>entry_point_linkopts_provider | A function that returns flags that should be passed to the linker to control the name of the entry point of a linked binary for rules that customize their entry point. This function must take the following keyword arguments: * `entry_point_name`: The name of the entry point function, as was passed to the Swift compiler using the `-entry-point-function-name` flag. It must return a `struct` with the following fields: * `linkopts`: A list of strings that will be passed as additional linker flags when linking a binary with a custom entry point. |
| <a id="SwiftToolchainInfo-feature_allowlists"></a>feature_allowlists | A list of `SwiftFeatureAllowlistInfo` providers that allow or prohibit packages from requesting or disabling features. |
Expand Down
56 changes: 1 addition & 55 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2388,11 +2388,7 @@ def compile(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE__SUPPORTS_CONST_VALUE_EXTRACTION,
):
const_gather_protocols_file = _maybe_create_const_protocols_file(
actions = actions,
swift_infos = generated_module_deps_swift_infos,
target_name = target_name,
)
const_gather_protocols_file = swift_toolchain.const_gather_protocols
else:
const_gather_protocols_file = []

Expand Down Expand Up @@ -3577,56 +3573,6 @@ def _emitted_output_nature(feature_configuration, user_compile_flags):
is_wmo = is_wmo,
)

def _maybe_create_const_protocols_file(actions, swift_infos, target_name):
"""Create the const extraction protocols file, if necessary.
Args:
actions: The object used to register actions.
swift_infos: A list of `SwiftInfo` providers describing the dependencies
of the code being compiled.
target_name: The name of the build target, which is used to generate
output file names.
Returns:
A file passed as an input to the compiler that lists the protocols whose
conforming types should have values extracted.
"""
const_gather_protocols = []
for swift_info in swift_infos:
for module_context in swift_info.direct_modules:
const_gather_protocols.extend(
module_context.const_gather_protocols,
)

# TODO: remove hack, I picked these from Xcode.
# Not sure how rules_apple should pass these.
const_gather_protocols = [
"AppIntent",
"EntityQuery",
"AppEntity",
"TransientEntity",
"AppEnum",
"AppShortcutProviding",
"AppShortcutsProvider",
"AnyResolverProviding",
"AppIntentsPackage",
"DynamicOptionsProvider",
]

# If there are no protocols to extract, return early.
if not const_gather_protocols:
return None

# Create the input file to the compiler, which contains a JSON array of
# protocol names.
const_gather_protocols_file = actions.declare_file(
"{}_const_extract_protocols.json".format(target_name),
)
actions.write(
content = json.encode(const_gather_protocols),
output = const_gather_protocols_file,
)
return const_gather_protocols_file

def _exclude_swift_incompatible_define(define):
"""A `map_each` helper that excludes a define if it is not Swift-compatible.
Expand Down
4 changes: 4 additions & 0 deletions swift/internal/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ C/Objective-C modules:
For ease of use, this field is never `None`; it will always be a valid `struct`
containing the fields described above, even if those lists are empty.
""",
"const_gather_protocols": """\
`File`. A JSON file specifying a list of protocols for extraction of
conformances' const values.
""",
"developer_dirs": """
A list of `structs` containing the following fields:\
Expand Down
9 changes: 9 additions & 0 deletions swift/internal/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ def _swift_toolchain_impl(ctx):
requested_features = requested_features,
root_dir = toolchain_root,
swift_worker = ctx.attr._worker[DefaultInfo].files_to_run,
const_gather_protocols = ctx.file.const_gather_protocols,
test_configuration = struct(
env = env,
execution_requirements = {},
Expand Down Expand Up @@ -480,6 +481,14 @@ for incremental compilation using a persistent mode.
""",
executable = True,
),
"const_gather_protocols": attr.label(
default = Label("@build_bazel_rules_swift//swift/toolchains/config:const_extract_protocols.json"),
allow_single_file = True,
doc = """\
The label of the file specifying a list of protocols for extraction of conformances'
const values.
""",
),
"env": attr.string_dict(
doc = """\
The preserved environment variables required for the toolchain to operate
Expand Down
9 changes: 9 additions & 0 deletions swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ def _xcode_swift_toolchain_impl(ctx):
],
requested_features = requested_features,
swift_worker = ctx.attr._worker[DefaultInfo].files_to_run,
const_gather_protocols = ctx.file.const_gather_protocols,
test_configuration = struct(
env = env,
execution_requirements = execution_requirements,
Expand Down Expand Up @@ -798,6 +799,14 @@ configuration options that are applied to targets on a per-package basis.
""",
providers = [[SwiftPackageConfigurationInfo]],
),
"const_gather_protocols": attr.label(
default = Label("@build_bazel_rules_swift//swift/toolchains/config:const_extract_protocols.json"),
allow_single_file = True,
doc = """\
The label of the file specifying a list of protocols for extraction of conformances'
const values.
""",
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
doc = """\
Expand Down
2 changes: 2 additions & 0 deletions swift/toolchains/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package(
],
)

exports_files(["const_extract_protocols.json"])

bzl_library(
name = "compile_module_interface_config",
srcs = ["compile_module_interface_config.bzl"],
Expand Down
12 changes: 12 additions & 0 deletions swift/toolchains/config/const_extract_protocols.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
"AppIntent",
"EntityQuery",
"AppEntity",
"TransientEntity",
"AppEnum",
"AppShortcutProviding",
"AppShortcutsProvider",
"AnyResolverProviding",
"AppIntentsPackage",
"DynamicOptionsProvider"
]

0 comments on commit 5e2a87b

Please sign in to comment.