Skip to content

Commit

Permalink
Separate toolchain configuration logic from the rest of the rules' bu…
Browse files Browse the repository at this point in the history
…siness logic

The tool/action config logic had previously been exported as a single module. Since I wanted to separate that out, it required updating all the call sites, so I took the opportunity to remove the tool/action config function wrappers and just expose the actual "providers" that back them, using new provider initializers to massage/validate the arguments before they are set as fields. I also removed the `swift_action_names` struct and just made those names top-level constants.

PiperOrigin-RevId: 451429760
(cherry picked from commit 699ffd5)
Signed-off-by: Brentley Jones <[email protected]>
  • Loading branch information
allevato authored and brentleyjones committed Jun 21, 2024
1 parent 25bc63e commit 407beca
Show file tree
Hide file tree
Showing 21 changed files with 2,996 additions and 2,777 deletions.
66 changes: 37 additions & 29 deletions swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ cc_library(

# Starlark libraries

bzl_library(
name = "action_names",
srcs = ["action_names.bzl"],
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "actions",
srcs = ["actions.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":features",
":toolchain_config",
"//swift/toolchains/config:action_config",
"@bazel_skylib//lib:types",
],
)
Expand All @@ -49,8 +55,9 @@ bzl_library(
srcs = ["autolinking.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":action_names",
":actions",
":toolchain_config",
"//swift/toolchains/config:action_config",
],
)

Expand All @@ -69,20 +76,19 @@ bzl_library(
"//swift:__subpackages__",
],
deps = [
":action_names",
":actions",
":autolinking",
":debugging",
":developer_dirs",
":explicit_module_map_file",
":feature_names",
":features",
":module_maps",
":providers",
":toolchain_config",
":utils",
":vfsoverlay",
"//swift/toolchains/config:compile_module_interface_config",
"@bazel_skylib//lib:collections",
":wmo",
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:sets",
"@bazel_skylib//lib:types",
],
)
Expand All @@ -92,9 +98,10 @@ bzl_library(
srcs = ["debugging.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":action_names",
":actions",
":feature_names",
":toolchain_config",
":features",
],
)

Expand All @@ -111,6 +118,12 @@ bzl_library(
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "explicit_module_map_file",
srcs = ["explicit_module_map_file.bzl"],
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "feature_names",
srcs = ["feature_names.bzl"],
Expand All @@ -137,6 +150,7 @@ bzl_library(
"//swift:__subpackages__",
],
deps = [
":action_names",
":actions",
":attrs",
":autolinking",
Expand All @@ -147,6 +161,7 @@ bzl_library(
":utils",
"//swift:swift_clang_module_aspect",
"@bazel_skylib//lib:collections",
"@bazel_skylib//lib:dicts",
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
],
)
Expand All @@ -167,6 +182,12 @@ bzl_library(
],
)

bzl_library(
name = "package_specs",
srcs = ["package_specs.bzl"],
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "providers",
srcs = ["providers.bzl"],
Expand Down Expand Up @@ -209,21 +230,17 @@ bzl_library(
srcs = ["symbol_graph_extracting.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":action_names",
":actions",
":providers",
":toolchain_config",
":utils",
],
)

bzl_library(
name = "toolchain_config",
srcs = ["toolchain_config.bzl"],
name = "target_triples",
srcs = ["target_triples.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:types",
],
)

bzl_library(
Expand All @@ -243,21 +260,12 @@ bzl_library(
)

bzl_library(
name = "explicit_module_map_file",
srcs = ["explicit_module_map_file.bzl"],
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "package_specs",
srcs = ["package_specs.bzl"],
visibility = ["//swift:__subpackages__"],
)

bzl_library(
name = "target_triples",
srcs = ["target_triples.bzl"],
name = "wmo",
srcs = ["wmo.bzl"],
visibility = ["//swift:__subpackages__"],
deps = [
":feature_names",
],
)

bzl_library(
Expand Down
60 changes: 60 additions & 0 deletions swift/internal/action_names.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Constants representing the names of actions spawned by the Swift rules."""

# Extracts a linker input file containing libraries to link from a compiled
# object file to provide autolink functionality based on `import` directives on
# ELF platforms.
SWIFT_ACTION_AUTOLINK_EXTRACT = "SwiftAutolinkExtract"

# Compiles one or more `.swift` source files into a `.swiftmodule` and
# object files.
SWIFT_ACTION_COMPILE = "SwiftCompile"

# Compiles a `.swiftinterface` file into a `.swiftmodule` file.
SWIFT_ACTION_COMPILE_MODULE_INTERFACE = "SwiftCompileModuleInterface"

# Produces files that are usually fallout of the compilation such as
# .swiftmodule, -Swift.h and more.
SWIFT_ACTION_DERIVE_FILES = "SwiftDeriveFiles"

# Produces an AST file for each swift source file in a module.
SWIFT_ACTION_DUMP_AST = "SwiftDumpAST"

# Wraps a `.swiftmodule` in a `.o` file on ELF platforms so that it can be
# linked into a binary for debugging.
SWIFT_ACTION_MODULEWRAP = "SwiftModuleWrap"

# Precompiles an explicit module for a C/Objective-C module map and its
# headers, emitting a `.pcm` file.
SWIFT_ACTION_PRECOMPILE_C_MODULE = "SwiftPrecompileCModule"

# Extracts a JSON-formatted symbol graph from a module, which can be used as
# an input to documentation generating tools like `docc` or analyzed with
# other tooling.
SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT = "SwiftSymbolGraphExtract"

def all_action_names():
"""A convenience function to return all actions defined by this rule set."""
return (
SWIFT_ACTION_AUTOLINK_EXTRACT,
SWIFT_ACTION_COMPILE,
SWIFT_ACTION_COMPILE_MODULE_INTERFACE,
SWIFT_ACTION_DERIVE_FILES,
SWIFT_ACTION_DUMP_AST,
SWIFT_ACTION_MODULEWRAP,
SWIFT_ACTION_PRECOMPILE_C_MODULE,
SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT,
)
73 changes: 7 additions & 66 deletions swift/internal/actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,13 @@
"""Functions for registering actions that invoke Swift tools."""

load("@bazel_skylib//lib:types.bzl", "types")
load("//swift/toolchains/config:action_config.bzl", "ConfigResultInfo")
load(":features.bzl", "are_all_features_enabled")
load(":toolchain_config.bzl", "swift_toolchain_config")
load(":utils.bzl", "struct_fields")

# This is a proxy for being on bazel 7.x which has
# --incompatible_merge_fixed_and_default_shell_env enabled by default
USE_DEFAULT_SHELL_ENV = not hasattr(apple_common, "apple_crosstool_transition")

# The names of actions currently supported by the Swift build rules.
swift_action_names = struct(
# Extracts a linker input file containing libraries to link from a compiled
# object file to provide autolink functionality based on `import` directives
# on ELF platforms.
AUTOLINK_EXTRACT = "SwiftAutolinkExtract",

# Compiles one or more `.swift` source files into a `.swiftmodule` and
# object files.
COMPILE = "SwiftCompile",

# Compiles a `.swiftinterface` file into a `.swiftmodule` file.
COMPILE_MODULE_INTERFACE = "SwiftCompileModuleInterface",

# Wraps a `.swiftmodule` in a `.o` file on ELF platforms so that it can be
# linked into a binary for debugging.
MODULEWRAP = "SwiftModuleWrap",

# Precompiles an explicit module for a C/Objective-C module map and its
# headers, emitting a `.pcm` file.
PRECOMPILE_C_MODULE = "SwiftPrecompileCModule",

# Extracts a JSON-formatted symbol graph from a module, which can be used as
# an input to documentation generating tools like `docc` or analyzed with
# other tooling.
SYMBOL_GRAPH_EXTRACT = "SwiftSymbolGraphExtract",

# Produces files that are usually fallout of the compilation such as
# .swiftmodule, -Swift.h and more.
DERIVE_FILES = "SwiftDeriveFiles",

# Produces an AST file for each swift source file in a module.
DUMP_AST = "SwiftDumpAST",
)

def _all_action_names():
"""A convenience function to return all actions defined by this rule set."""
return struct_fields(swift_action_names).values()

def _apply_action_configs(
action_name,
args,
Expand All @@ -81,9 +41,8 @@ def _apply_action_configs(
swift_toolchain: The Swift toolchain being used to build.
Returns:
A `swift_toolchain_config.action_inputs` value that contains the files
that are required inputs of the action, as determined by the
configurators.
A `ConfigResultInfo` value that contains the files that are required
inputs of the action, as determined by the configurators.
"""
inputs = []
transitive_inputs = []
Expand Down Expand Up @@ -135,15 +94,14 @@ def _apply_action_configs(
# object for chaining. We can guard against this (and possibly
# other errors) by checking that the value is a struct. If it
# is, then it's not `None` and it probably came from the
# provider used by `swift_toolchain_config.config_result`. If
# it's some other kind of struct, then we'll error out trying to
# access the fields.
# provider used by `ConfigResultInfo`. If it's some other kind
# of struct, then we'll error out trying to access the fields.
if type(action_inputs) == "struct":
inputs.extend(action_inputs.inputs)
transitive_inputs.extend(action_inputs.transitive_inputs)

# Merge the action results into a single result that we return.
return swift_toolchain_config.config_result(
return ConfigResultInfo(
inputs = inputs,
transitive_inputs = transitive_inputs,
)
Expand Down Expand Up @@ -228,9 +186,7 @@ def run_toolchain_action(
tools.append(tool_config.executable)
else:
executable = tool_config.executable

if tool_config.tools:
tools.extend(tool_config.tools)
tools.extend(tool_config.additional_tools)

# If the tool configuration has any required arguments, add those first.
if tool_config.args:
Expand Down Expand Up @@ -262,18 +218,3 @@ def run_toolchain_action(
use_default_shell_env = USE_DEFAULT_SHELL_ENV,
**kwargs
)

def _target_label_configurator(prerequisites, args):
"""Adds the Bazel target label to the action command line."""
label = getattr(prerequisites, "target_label", None)
if label:
args.add(str(label), format = "-Xwrapped-swift=-bazel-target-label=%s")

def target_label_action_configs():
"""Returns action configs that add the target label to the command line."""
return [
swift_toolchain_config.action_config(
actions = _all_action_names(),
configurators = [_target_label_configurator],
),
]
17 changes: 11 additions & 6 deletions swift/internal/autolinking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@

"""Implementation of autolink logic for Swift."""

load(":actions.bzl", "run_toolchain_action", "swift_action_names")
load(":toolchain_config.bzl", "swift_toolchain_config")
load(
"//swift/toolchains/config:action_config.bzl",
"ActionConfigInfo",
"ConfigResultInfo",
)
load(":action_names.bzl", "SWIFT_ACTION_AUTOLINK_EXTRACT")
load(":actions.bzl", "run_toolchain_action")

def _autolink_extract_input_configurator(prerequisites, args):
"""Configures the inputs of the autolink-extract action."""
object_files = prerequisites.object_files

args.add_all(object_files)
return swift_toolchain_config.config_result(inputs = object_files)
return ConfigResultInfo(inputs = object_files)

def _autolink_extract_output_configurator(prerequisites, args):
"""Configures the outputs of the autolink-extract action."""
Expand All @@ -38,8 +43,8 @@ def autolink_extract_action_configs():
The list of action configs needed to perform autolink extraction.
"""
return [
swift_toolchain_config.action_config(
actions = [swift_action_names.AUTOLINK_EXTRACT],
ActionConfigInfo(
actions = [SWIFT_ACTION_AUTOLINK_EXTRACT],
configurators = [
_autolink_extract_input_configurator,
_autolink_extract_output_configurator,
Expand Down Expand Up @@ -77,7 +82,7 @@ def register_autolink_extract_action(
)
run_toolchain_action(
actions = actions,
action_name = swift_action_names.AUTOLINK_EXTRACT,
action_name = SWIFT_ACTION_AUTOLINK_EXTRACT,
feature_configuration = feature_configuration,
outputs = [autolink_file],
prerequisites = prerequisites,
Expand Down
Loading

0 comments on commit 407beca

Please sign in to comment.