2024-02-13 02:54:22 +00:00
|
|
|
# Copyright 2024 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.
|
|
|
|
"""Rules to turn action types into bazel labels."""
|
|
|
|
|
2024-02-21 01:58:00 +00:00
|
|
|
load("//cc/toolchains/impl:collect.bzl", "collect_action_types")
|
2024-02-13 02:54:22 +00:00
|
|
|
load(":cc_toolchain_info.bzl", "ActionTypeInfo", "ActionTypeSetInfo")
|
|
|
|
|
|
|
|
visibility("public")
|
|
|
|
|
|
|
|
def _cc_action_type_impl(ctx):
|
|
|
|
action_type = ActionTypeInfo(label = ctx.label, name = ctx.attr.action_name)
|
|
|
|
return [
|
|
|
|
action_type,
|
|
|
|
ActionTypeSetInfo(
|
|
|
|
label = ctx.label,
|
|
|
|
actions = depset([action_type]),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
cc_action_type = rule(
|
|
|
|
implementation = _cc_action_type_impl,
|
|
|
|
attrs = {
|
|
|
|
"action_name": attr.string(
|
|
|
|
mandatory = True,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
doc = """A type of action (eg. c_compile, assemble, strip).
|
|
|
|
|
2024-09-30 16:14:24 +00:00
|
|
|
`cc_action_type` rules are used to associate arguments and tools together to
|
|
|
|
perform a specific action. Bazel prescribes a set of known action types that are used to drive
|
|
|
|
typical C/C++/ObjC actions like compiling, linking, and archiving. The set of well-known action
|
2024-10-09 15:25:32 +00:00
|
|
|
types can be found in [@rules_cc//cc/toolchains/actions:BUILD](https://github.com/bazelbuild/rules_cc/tree/main/cc/toolchains/actions/BUILD).
|
2024-09-30 16:14:24 +00:00
|
|
|
|
|
|
|
It's possible to create project-specific action types for use in toolchains. Be careful when
|
|
|
|
doing this, because every toolchain that encounters the action will need to be configured to
|
|
|
|
support the custom action type. If your project is a library, avoid creating new action types as
|
|
|
|
it will reduce compatibility with existing toolchains and increase setup complexity for users.
|
2024-02-13 02:54:22 +00:00
|
|
|
|
2024-09-30 16:14:24 +00:00
|
|
|
Example:
|
|
|
|
```
|
|
|
|
load("//cc:action_names.bzl", "ACTION_NAMES")
|
|
|
|
load("//cc/toolchains:actions.bzl", "cc_action_type")
|
2024-02-13 02:54:22 +00:00
|
|
|
|
|
|
|
cc_action_type(
|
2024-09-30 16:14:24 +00:00
|
|
|
name = "cpp_compile",
|
|
|
|
action_name = = ACTION_NAMES.cpp_compile,
|
2024-02-13 02:54:22 +00:00
|
|
|
)
|
2024-09-30 16:14:24 +00:00
|
|
|
```
|
2024-02-13 02:54:22 +00:00
|
|
|
""",
|
|
|
|
provides = [ActionTypeInfo, ActionTypeSetInfo],
|
|
|
|
)
|
|
|
|
|
|
|
|
def _cc_action_type_set_impl(ctx):
|
2024-03-27 05:49:54 +00:00
|
|
|
if not ctx.attr.actions and not ctx.attr.allow_empty:
|
2024-02-26 21:58:02 +00:00
|
|
|
fail("Each cc_action_type_set must contain at least one action type.")
|
2024-02-13 02:54:22 +00:00
|
|
|
return [ActionTypeSetInfo(
|
|
|
|
label = ctx.label,
|
2024-02-21 01:58:00 +00:00
|
|
|
actions = collect_action_types(ctx.attr.actions),
|
2024-02-13 02:54:22 +00:00
|
|
|
)]
|
|
|
|
|
|
|
|
cc_action_type_set = rule(
|
|
|
|
doc = """Represents a set of actions.
|
|
|
|
|
2024-09-30 16:14:24 +00:00
|
|
|
This is a convenience rule to allow for more compact representation of a group of action types.
|
|
|
|
Use this anywhere a `cc_action_type` is accepted.
|
|
|
|
|
2024-02-13 02:54:22 +00:00
|
|
|
Example:
|
2024-09-30 16:14:24 +00:00
|
|
|
```
|
|
|
|
load("//cc/toolchains:actions.bzl", "cc_action_type_set")
|
2024-02-13 02:54:22 +00:00
|
|
|
|
|
|
|
cc_action_type_set(
|
2024-03-21 22:44:09 +00:00
|
|
|
name = "link_executable_actions",
|
2024-02-13 02:54:22 +00:00
|
|
|
actions = [
|
2024-09-30 16:14:24 +00:00
|
|
|
"//cc/toolchains/actions:cpp_link_executable",
|
|
|
|
"//cc/toolchains/actions:lto_index_for_executable",
|
2024-02-13 02:54:22 +00:00
|
|
|
],
|
|
|
|
)
|
2024-09-30 16:14:24 +00:00
|
|
|
```
|
2024-02-13 02:54:22 +00:00
|
|
|
""",
|
|
|
|
implementation = _cc_action_type_set_impl,
|
|
|
|
attrs = {
|
|
|
|
"actions": attr.label_list(
|
|
|
|
providers = [ActionTypeSetInfo],
|
|
|
|
mandatory = True,
|
|
|
|
doc = "A list of cc_action_type or cc_action_type_set",
|
|
|
|
),
|
2024-03-27 05:49:54 +00:00
|
|
|
"allow_empty": attr.bool(default = False),
|
2024-02-13 02:54:22 +00:00
|
|
|
},
|
|
|
|
provides = [ActionTypeSetInfo],
|
|
|
|
)
|