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
  • Loading branch information
allevato authored and swiple-rules-gardener committed May 27, 2022
1 parent 7797034 commit 699ffd5
Show file tree
Hide file tree
Showing 17 changed files with 2,058 additions and 1,884 deletions.
32 changes: 18 additions & 14 deletions swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ package(
],
)

bzl_library(
name = "action_names",
srcs = ["action_names.bzl"],
)

bzl_library(
name = "actions",
srcs = ["actions.bzl"],
deps = [
":features",
":toolchain_config",
":utils",
"//swift/toolchains/config:action_config",
"@bazel_skylib//lib:types",
],
)
Expand All @@ -37,14 +41,15 @@ bzl_library(
name = "compiling",
srcs = ["compiling.bzl"],
deps = [
":action_names",
":actions",
":feature_names",
":features",
":module_maps",
":providers",
":toolchain_config",
":utils",
":vfsoverlay",
":wmo",
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:sets",
"@bazel_skylib//lib:types",
Expand All @@ -55,10 +60,10 @@ bzl_library(
name = "debugging",
srcs = ["debugging.bzl"],
deps = [
":action_names",
":actions",
":feature_names",
":features",
":toolchain_config",
],
)

Expand Down Expand Up @@ -155,9 +160,9 @@ bzl_library(
name = "symbol_graph_extracting",
srcs = ["symbol_graph_extracting.bzl"],
deps = [
":action_names",
":actions",
":providers",
":toolchain_config",
":utils",
],
)
Expand All @@ -167,15 +172,6 @@ bzl_library(
srcs = ["target_triples.bzl"],
)

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

bzl_library(
name = "toolchain_utils",
srcs = ["toolchain_utils.bzl"],
Expand All @@ -195,6 +191,14 @@ bzl_library(
srcs = ["vfsoverlay.bzl"],
)

bzl_library(
name = "wmo",
srcs = ["wmo.bzl"],
deps = [
":feature_names",
],
)

# Consumed by Bazel integration tests.
filegroup(
name = "for_bazel_tests",
Expand Down
41 changes: 41 additions & 0 deletions swift/internal/action_names.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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."""

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

# 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_COMPILE,
SWIFT_ACTION_MODULEWRAP,
SWIFT_ACTION_PRECOMPILE_C_MODULE,
SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT,
)
57 changes: 9 additions & 48 deletions swift/internal/actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,12 @@

"""Functions for registering actions that invoke Swift tools."""

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

# The names of actions currently supported by the Swift build rules.
swift_action_names = struct(
# Compiles one or more `.swift` source files into a `.swiftmodule` and
# object files.
COMPILE = "SwiftCompile",

# 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",
)

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,
Expand All @@ -62,9 +40,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 @@ -116,15 +93,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 @@ -240,18 +216,3 @@ def run_toolchain_action(
resource_set = tool_config.resource_set,
**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],
),
]
Loading

1 comment on commit 699ffd5

@brentleyjones
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.