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