xref: /aosp_15_r20/external/pigweed/pw_toolchain_bazel/actions/defs.bzl (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Rules to turn action names into bazel labels."""
15
16load(":providers.bzl", "ActionNameInfo", "ActionNameSetInfo")
17
18visibility("//cc_toolchain")
19
20def _pw_cc_action_name_impl(ctx):
21    return [
22        ActionNameInfo(name = ctx.attr.action_name),
23        ActionNameSetInfo(actions = depset([ctx.attr.action_name])),
24    ]
25
26pw_cc_action_name = rule(
27    implementation = _pw_cc_action_name_impl,
28    attrs = {
29        "action_name": attr.string(
30            mandatory = True,
31        ),
32    },
33    doc = """The name of a single type of action.
34
35This rule represents a type-safe definition for an action name.
36Listing this in a pw_cc_flag_set or pw_cc_action tells a toolchain which actions
37apply to the attached flags or tools.
38
39Example:
40
41load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
42
43pw_cc_action_name(
44  name = "cpp_compile",
45  action_name =  = ACTION_NAMES.cpp_compile,
46)
47
48pw_cc_flag_set(
49    name = "c++17",
50    actions = [":cpp_compile"],
51    flags = ["-std=c++17"],
52)
53""",
54    provides = [ActionNameInfo, ActionNameSetInfo],
55)
56
57def _pw_cc_action_name_set_impl(ctx):
58    return [ActionNameSetInfo(actions = depset(transitive = [
59        attr[ActionNameSetInfo].actions
60        for attr in ctx.attr.actions
61    ]))]
62
63pw_cc_action_name_set = rule(
64    doc = """A set of action names.
65
66This rule represents a group of one or more pw_cc_action_name rules.
67This can be used in place of a pw_cc_action_name for rule attributes that
68accept multiple action names.
69
70Example:
71
72pw_cc_action_name_set(
73  name = "all_cpp_compiler_actions",
74  actions = [":cpp_compile", ":cpp_header_parsing"],
75)
76
77pw_cc_flag_set(
78    name = "c++17",
79    actions = [":all_cpp_compiler_actions"],
80    flags = ["-std=c++17"],
81)
82""",
83    implementation = _pw_cc_action_name_set_impl,
84    attrs = {
85        "actions": attr.label_list(
86            providers = [ActionNameSetInfo],
87            mandatory = True,
88            doc = "A list of pw_cc_action_name or pw_cc_action_name_set to be combined.",
89        ),
90    },
91    provides = [ActionNameSetInfo],
92)
93