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