aboutsummaryrefslogtreecommitdiff
path: root/pw_toolchain_bazel/cc_toolchain/private/utils.bzl
blob: 7030f24987f23de6af8c870110f9072741c7ea86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2023 The Pigweed Authors
#
# 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
#
#     https://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.
"""Private utilities and global variables."""

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
    "FlagSetInfo",
)
load(
    "//cc_toolchain/private:providers.bzl",
    "ToolchainFeatureInfo",
)

# A potentially more complete set of these constants are available at
# @rules_cc//cc:action_names.bzl, but it's not clear if they should be depended
# on.
ALL_ASM_ACTIONS = [
    ACTION_NAMES.assemble,
    ACTION_NAMES.preprocess_assemble,
]
ALL_C_COMPILER_ACTIONS = [
    ACTION_NAMES.c_compile,
    ACTION_NAMES.cc_flags_make_variable,
]
ALL_CPP_COMPILER_ACTIONS = [
    ACTION_NAMES.cpp_compile,
    ACTION_NAMES.cpp_header_parsing,
]
ALL_LINK_ACTIONS = [
    ACTION_NAMES.cpp_link_executable,
    ACTION_NAMES.cpp_link_dynamic_library,
    ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]
ALL_AR_ACTIONS = [
    ACTION_NAMES.cpp_link_static_library,
]

ACTION_MAP = {
    "aropts": ALL_AR_ACTIONS,
    "asmopts": ALL_ASM_ACTIONS,
    "copts": ALL_C_COMPILER_ACTIONS + ALL_CPP_COMPILER_ACTIONS,
    "conlyopts": ALL_C_COMPILER_ACTIONS,
    "cxxopts": ALL_CPP_COMPILER_ACTIONS,
    "linkopts": ALL_LINK_ACTIONS,
}

def _check_dep_provides(ctx_label, dep, provider, what_provides):
    if provider not in dep:
        fail(
            "{} listed as a dependency of {}, but it's not a {}".format(
                dep.label,
                ctx_label,
                what_provides,
            ),
        )

def check_deps(ctx):
    for dep in ctx.attr.feature_deps:
        _check_dep_provides(ctx.label, dep, ToolchainFeatureInfo, "pw_cc_toolchain_feature")
    for dep in ctx.attr.action_config_flag_sets:
        _check_dep_provides(ctx.label, dep, FlagSetInfo, "pw_cc_flag_set")