Skip to content

Commit

Permalink
Align macro support with upstream
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 540589258
(cherry picked from commit b114729)

Cherry-pick notes: We already added macro support in c9d1f5f. This is just aligning the changes more. Also, I accidentally made `SwiftCompilerPluginInfo` public in cf155fc. This corrects that change by making it internal again.

Signed-off-by: Brentley Jones <[email protected]>
  • Loading branch information
allevato authored and brentleyjones committed Oct 14, 2024
1 parent 7fafc9b commit ea09cd9
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 47 deletions.
6 changes: 3 additions & 3 deletions doc/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ that use the toolchain.
| <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.<br><br>* `objc_infos`: A list of `apple_common.Objc` providers from targets specified as the toolchain's implicit dependencies.<br><br>* `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_protocols_to_gather"></a>const_protocols_to_gather | `File`. A JSON file specifying a list of protocols for extraction of conformances' const values. |
| <a id="SwiftToolchainInfo-cross_import_overlays"></a>cross_import_overlays | A list of `SwiftCrossImportOverlayInfo` providers whose `SwiftInfo` providers will be automatically injected into the dependencies of Swift compilations if their declaring module and bystanding module are both already declared as dependencies. |
| <a id="SwiftToolchainInfo-debug_outputs_provider"></a>debug_outputs_provider | An optional function that provides toolchain-specific logic around the handling of additional debug outputs for `swift_binary` and `swift_test` targets. If specified, this function must take the following keyword arguments: * `ctx`: The rule context of the calling binary or test rule. It must return a `struct` with the following fields: * `additional_outputs`: Additional outputs expected from the linking action.<br><br>* `variables_extension`: A dictionary of additional crosstool variables to pass to the linking action. |
| <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-debug_outputs_provider"></a>debug_outputs_provider | An optional function that provides toolchain-specific logic around the handling of additional debug outputs for `swift_binary` and `swift_test` targets.<br><br>If specified, this function must take the following keyword arguments:<br><br>* `ctx`: The rule context of the calling binary or test rule.<br><br>It must return a `struct` with the following fields:<br><br>* `additional_outputs`: Additional outputs expected from the linking action.<br><br>* `variables_extension`: A dictionary of additional crosstool variables to pass to the linking action. |
| <a id="SwiftToolchainInfo-developer_dirs"></a>developer_dirs | A list of `structs` containing the following fields:<br><br>* `developer_path_label`: A `string` representing the type of developer path.<br><br>* `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.<br><br>This function must take the following keyword arguments:<br><br>* `entry_point_name`: The name of the entry point function, as was passed to the Swift compiler using the `-entry-point-function-name` flag.<br><br>It must return a `struct` with the following fields:<br><br>* `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. |
| <a id="SwiftToolchainInfo-generated_header_module_implicit_deps_providers"></a>generated_header_module_implicit_deps_providers | A `struct` with the following fields, which are providers from targets that should be treated as compile-time inputs to actions that precompile the explicit module for the generated Objective-C header of a Swift module:<br><br>* `cc_infos`: A list of `CcInfo` providers from targets specified as the toolchain's implicit dependencies.<br><br>* `objc_infos`: A list of `apple_common.Objc` providers from targets specified as the toolchain's implicit dependencies.<br><br>* `swift_infos`: A list of `SwiftInfo` providers from targets specified as the toolchain's implicit dependencies.<br><br>This is used to provide modular dependencies for the fixed inclusions (Darwin, Foundation) that are unconditionally emitted in those files.<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-implicit_deps_providers"></a>implicit_deps_providers | A `struct` with the following fields, which represent providers from targets that should be added as implicit dependencies of any Swift compilation or linking target (but not to precompiled explicit C/Objective-C modules):<br><br>* `cc_infos`: A list of `CcInfo` providers from targets specified as the toolchain's implicit dependencies.<br><br>* `objc_infos`: A list of `apple_common.Objc` providers from targets specified as the toolchain's implicit dependencies.<br><br>* `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. |
Expand Down
4 changes: 3 additions & 1 deletion swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ bzl_library(
"//swift/internal:compiling",
"//swift/internal:feature_names",
"//swift/internal:linking",
"//swift/internal:providers",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
Expand Down Expand Up @@ -189,6 +190,7 @@ bzl_library(
"//swift/internal:features",
"//swift/internal:linking",
"//swift/internal:output_groups",
"//swift/internal:providers",
"//swift/internal:toolchain_utils",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
Expand Down Expand Up @@ -270,9 +272,9 @@ bzl_library(
"//swift/internal:compiling",
"//swift/internal:env_expansion",
"//swift/internal:feature_names",
"//swift/internal:features",
"//swift/internal:linking",
"//swift/internal:output_groups",
"//swift/internal:providers",
"//swift/internal:swift_symbol_graph_aspect",
"//swift/internal:symbol_graph_extracting",
"//swift/internal:toolchain_utils",
Expand Down
3 changes: 2 additions & 1 deletion swift/internal/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"""Common attributes used by multiple Swift build rules."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("//swift:providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo")
load("//swift:providers.bzl", "SwiftInfo")
load(":providers.bzl", "SwiftCompilerPluginInfo")

def swift_common_rule_attrs(
additional_deps_aspects = [],
Expand Down
6 changes: 3 additions & 3 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def compile(
used_plugins = list(plugins)
for module_context in transitive_modules:
if module_context.swift and module_context.swift.plugins:
used_plugins.extend(module_context.swift.plugins.to_list())
used_plugins.extend(module_context.swift.plugins)

if include_dev_srch_paths != None and is_test != None:
fail("""\
Expand Down Expand Up @@ -659,7 +659,7 @@ to use swift_common.compile(include_dev_srch_paths = ...) instead.\
objc_info = merged_objc_info,
original_module_name = original_module_name,
package_name = package_name,
plugins = depset(used_plugins),
plugins = used_plugins,
source_files = srcs,
target_label = feature_configuration._label,
transitive_modules = transitive_modules,
Expand Down Expand Up @@ -793,7 +793,7 @@ to use swift_common.compile(include_dev_srch_paths = ...) instead.\
generated_header = compile_outputs.generated_header_file,
indexstore = compile_outputs.indexstore_directory,
original_module_name = original_module_name,
plugins = depset(plugins),
plugins = plugins,
private_swiftinterface = compile_outputs.private_swiftinterface_file,
swiftdoc = compile_outputs.swiftdoc_file,
swiftinterface = compile_outputs.swiftinterface_file,
Expand Down
23 changes: 23 additions & 0 deletions swift/internal/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@

"""Internal providers."""

SwiftCompilerPluginInfo = provider(
doc = "Information about compiler plugins, like macros.",
fields = {
"cc_info": """\
A `CcInfo` provider containing the `swift_compiler_plugin`'s code compiled as a
static library, which is suitable for linking into a `swift_test` so that unit
tests can be written against it.
""",
"executable": "A `File` representing the plugin's binary executable.",
"module_names": """\
A `depset` of strings denoting the names of the Swift modules that provide
plugin types looked up by the compiler. This currently contains a single
element, the name of the module created by the `swift_compiler_plugin` target.
""",
"swift_info": """\
A `SwiftInfo` provider representing the Swift module created by the
`swift_compiler_plugin` target. This is used specifically by `swift_test` to
allow test code to depend on the plugin's module without making it possible for
arbitrary libraries/binaries to depend on a plugin.
""",
},
)

SwiftCrossImportOverlayInfo = provider(
doc = "Information about a cross-import overlay module.",
fields = {
Expand Down
39 changes: 13 additions & 26 deletions swift/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,6 @@ to the rule implementations do not unnecessarily cause reanalysis impacting
users who just load these providers to inspect and/or repropagate them.
"""

SwiftCompilerPluginInfo = provider(
doc = "Information about compiler plugins, like macros.",
fields = {
"cc_info": """\
A `CcInfo` provider containing the `swift_compiler_plugin`'s code compiled as a
static library, which is suitable for linking into a `swift_test` so that unit
tests can be written against it.
""",
"executable": "A `File` representing the plugin's binary executable.",
"module_names": """\
A `depset` of strings denoting the names of the Swift modules that provide
plugin types looked up by the compiler. This currently contains a single
element, the name of the module created by the `swift_compiler_plugin` target.
""",
"swift_info": """\
A `SwiftInfo` provider representing the Swift module created by the
`swift_compiler_plugin` target. This is used specifically by `swift_test` to
allow test code to depend on the plugin's module without making it possible for
arbitrary libraries/binaries to depend on a plugin.
""",
},
)

SwiftFeatureAllowlistInfo = provider(
doc = """\
Describes a set of features and the packages and aspects that are allowed to
Expand Down Expand Up @@ -296,27 +273,37 @@ dependencies.
"debug_outputs_provider": """\
An optional function that provides toolchain-specific logic around the handling
of additional debug outputs for `swift_binary` and `swift_test` targets.
If specified, this function must take the following keyword arguments:
* `ctx`: The rule context of the calling binary or test rule.
It must return a `struct` with the following fields:
* `additional_outputs`: Additional outputs expected from the linking action.
* `variables_extension`: A dictionary of additional crosstool variables to
pass to the linking action.
""",
"developer_dirs": """
A list of `structs` containing the following fields:\
"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.
""",
"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.
""",
Expand Down Expand Up @@ -602,7 +589,7 @@ def create_swift_module_inputs(
defines = tuple(defines),
generated_header = generated_header,
indexstore = indexstore,
plugins = plugins,
plugins = tuple(plugins),
private_swiftinterface = private_swiftinterface,
original_module_name = original_module_name,
swiftdoc = swiftdoc,
Expand Down
10 changes: 3 additions & 7 deletions swift/swift_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implementation of the `swift_binary` and `swift_test` rules."""
"""Implementation of the `swift_binary` rule."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
Expand All @@ -33,6 +33,7 @@ load(
"//swift/internal:output_groups.bzl",
"supplemental_compilation_output_groups",
)
load("//swift/internal:providers.bzl", "SwiftCompilerPluginInfo")
load(
"//swift/internal:toolchain_utils.bzl",
"get_swift_toolchain",
Expand All @@ -45,12 +46,7 @@ load(
"include_developer_search_paths",
)
load(":module_name.bzl", "derive_swift_module_name")
load(
":providers.bzl",
"SwiftCompilerPluginInfo",
"SwiftInfo",
"create_swift_module_context",
)
load(":providers.bzl", "SwiftInfo", "create_swift_module_context")

def _maybe_parse_as_library_copts(srcs):
"""Returns a list of compiler flags depending on `main.swift`'s presence.
Expand Down
20 changes: 18 additions & 2 deletions swift/swift_compiler_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ load(
"//swift/internal:output_groups.bzl",
"supplemental_compilation_output_groups",
)
load("//swift/internal:providers.bzl", "SwiftCompilerPluginInfo")
load(
"//swift/internal:toolchain_utils.bzl",
"get_swift_toolchain",
Expand All @@ -50,7 +51,7 @@ load(
"get_providers",
)
load(":module_name.bzl", "derive_swift_module_name")
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo")
load(":providers.bzl", "SwiftInfo")

def _swift_compiler_plugin_impl(ctx):
swift_toolchain = get_swift_toolchain(ctx)
Expand Down Expand Up @@ -112,6 +113,17 @@ def _swift_compiler_plugin_impl(ctx):
compilation_outputs = compile_result.compilation_outputs
supplemental_outputs = compile_result.supplemental_outputs

# Apply the optional debugging outputs extension if the toolchain defines
# one.
debug_outputs_provider = swift_toolchain.debug_outputs_provider
if debug_outputs_provider:
debug_extension = debug_outputs_provider(ctx = ctx)
additional_debug_outputs = debug_extension.additional_outputs
variables_extension = debug_extension.variables_extension
else:
additional_debug_outputs = []
variables_extension = {}

if is_feature_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_ADD_TARGET_NAME_TO_OUTPUT,
Expand All @@ -125,6 +137,7 @@ def _swift_compiler_plugin_impl(ctx):
actions = ctx.actions,
additional_inputs = ctx.files.swiftc_inputs,
additional_linking_contexts = [malloc_linking_context(ctx)],
additional_outputs = additional_debug_outputs,
compilation_outputs = compilation_outputs,
deps = deps,
feature_configuration = feature_configuration,
Expand All @@ -145,6 +158,7 @@ def _swift_compiler_plugin_impl(ctx):
entry_point_name = entry_point_function_name,
).linkopts
),
variables_extension = variables_extension,
)

linking_context, _ = (
Expand All @@ -170,7 +184,9 @@ def _swift_compiler_plugin_impl(ctx):
return [
DefaultInfo(
executable = binary_linking_outputs.executable,
files = depset([binary_linking_outputs.executable]),
files = depset(
[binary_linking_outputs.executable] + additional_debug_outputs,
),
runfiles = ctx.runfiles(
collect_data = True,
collect_default = True,
Expand Down
3 changes: 2 additions & 1 deletion swift/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ load(
"//swift/internal:output_groups.bzl",
"supplemental_compilation_output_groups",
)
load("//swift/internal:providers.bzl", "SwiftCompilerPluginInfo")
load(
"//swift/internal:toolchain_utils.bzl",
"get_swift_toolchain",
Expand All @@ -58,7 +59,7 @@ load(
"include_developer_search_paths",
)
load(":module_name.bzl", "derive_swift_module_name")
load(":providers.bzl", "SwiftCompilerPluginInfo", "SwiftInfo")
load(":providers.bzl", "SwiftInfo")
load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect")

def _maybe_parse_as_library_copts(srcs):
Expand Down
9 changes: 7 additions & 2 deletions swift/swift_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implementation of the `swift_binary` and `swift_test` rules."""
"""Implementation of the `swift_test` rule."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
Expand All @@ -34,6 +34,7 @@ load(
"//swift/internal:output_groups.bzl",
"supplemental_compilation_output_groups",
)
load("//swift/internal:providers.bzl", "SwiftCompilerPluginInfo")
load(
"//swift/internal:swift_symbol_graph_aspect.bzl",
"make_swift_symbol_graph_aspect",
Expand All @@ -53,7 +54,6 @@ load(
load(":module_name.bzl", "derive_swift_module_name")
load(
":providers.bzl",
"SwiftCompilerPluginInfo",
"SwiftInfo",
"SwiftSymbolGraphInfo",
"create_swift_module_context",
Expand Down Expand Up @@ -269,6 +269,7 @@ def _do_compile(
objc_infos,
name,
package_name,
plugins = [],
srcs,
swift_infos,
swift_toolchain,
Expand All @@ -291,6 +292,8 @@ def _do_compile(
provided as inputs to the compilation action.
package_name: The semantic package of the name of the Swift module
being compiled.
plugins: A list of `SwiftCompilerPluginInfo` providers that need to be
loaded when compiling this module.
srcs: The sources to compile.
swift_infos: A list of `SwiftInfo` providers that should be used to
determine the module inputs for the action.
Expand All @@ -316,6 +319,7 @@ def _do_compile(
include_dev_srch_paths = include_dev_srch_paths,
module_name = module_name,
package_name = package_name,
plugins = plugins,
objc_infos = objc_infos,
srcs = srcs,
swift_infos = swift_infos,
Expand Down Expand Up @@ -424,6 +428,7 @@ def _swift_test_impl(ctx):
module_name = module_name,
objc_infos = deps_objc_infos,
package_name = ctx.attr.package_name,
plugins = get_providers(ctx.attr.plugins, SwiftCompilerPluginInfo),
name = ctx.label.name,
srcs = srcs,
swift_infos = deps_swift_infos,
Expand Down
Loading

0 comments on commit ea09cd9

Please sign in to comment.