xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/toolchains/actions.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# Copyright 2024 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Rules to turn action types into bazel labels."""
15
16load("//cc/toolchains/impl:collect.bzl", "collect_action_types")
17load(":cc_toolchain_info.bzl", "ActionTypeInfo", "ActionTypeSetInfo")
18
19visibility("public")
20
21def _cc_action_type_impl(ctx):
22    action_type = ActionTypeInfo(label = ctx.label, name = ctx.attr.action_name)
23    return [
24        action_type,
25        ActionTypeSetInfo(
26            label = ctx.label,
27            actions = depset([action_type]),
28        ),
29    ]
30
31cc_action_type = rule(
32    implementation = _cc_action_type_impl,
33    attrs = {
34        "action_name": attr.string(
35            mandatory = True,
36        ),
37    },
38    doc = """A type of action (eg. c_compile, assemble, strip).
39
40Example:
41
42load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
43
44cc_action_type(
45  name = "cpp_compile",
46  action_name =  = ACTION_NAMES.cpp_compile,
47)
48""",
49    provides = [ActionTypeInfo, ActionTypeSetInfo],
50)
51
52def _cc_action_type_set_impl(ctx):
53    if not ctx.attr.actions and not ctx.attr.allow_empty:
54        fail("Each cc_action_type_set must contain at least one action type.")
55    return [ActionTypeSetInfo(
56        label = ctx.label,
57        actions = collect_action_types(ctx.attr.actions),
58    )]
59
60cc_action_type_set = rule(
61    doc = """Represents a set of actions.
62
63Example:
64
65cc_action_type_set(
66    name = "link_executable_actions",
67    actions = [
68        ":cpp_link_executable",
69        ":lto_index_for_executable",
70    ],
71)
72""",
73    implementation = _cc_action_type_set_impl,
74    attrs = {
75        "actions": attr.label_list(
76            providers = [ActionTypeSetInfo],
77            mandatory = True,
78            doc = "A list of cc_action_type or cc_action_type_set",
79        ),
80        "allow_empty": attr.bool(default = False),
81    },
82    provides = [ActionTypeSetInfo],
83)
84