• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Implementation of the cc_toolchain rule."""
15
16load(
17    "//cc/toolchains:cc_toolchain_info.bzl",
18    "ActionTypeSetInfo",
19    "ArgsListInfo",
20    "FeatureSetInfo",
21    "ToolConfigInfo",
22    "ToolchainConfigInfo",
23)
24load(":collect.bzl", "collect_action_types")
25load(":legacy_converter.bzl", "convert_toolchain")
26load(":toolchain_config_info.bzl", "toolchain_config_info")
27
28visibility([
29    "//cc/toolchains/...",
30    "//tests/rule_based_toolchain/...",
31])
32
33def _cc_legacy_file_group_impl(ctx):
34    files = ctx.attr.config[ToolchainConfigInfo].files
35
36    return [DefaultInfo(files = depset(transitive = [
37        files[action]
38        for action in collect_action_types(ctx.attr.actions).to_list()
39        if action in files
40    ]))]
41
42cc_legacy_file_group = rule(
43    implementation = _cc_legacy_file_group_impl,
44    attrs = {
45        "actions": attr.label_list(providers = [ActionTypeSetInfo], mandatory = True),
46        "config": attr.label(providers = [ToolchainConfigInfo], mandatory = True),
47    },
48)
49
50def _cc_toolchain_config_impl(ctx):
51    if ctx.attr.features:
52        fail("Features is a reserved attribute in bazel. Did you mean 'known_features' or 'enabled_features'?")
53
54    toolchain_config = toolchain_config_info(
55        label = ctx.label,
56        known_features = ctx.attr.known_features + [ctx.attr._builtin_features],
57        enabled_features = ctx.attr.enabled_features,
58        tool_map = ctx.attr.tool_map,
59        args = ctx.attr.args,
60    )
61
62    legacy = convert_toolchain(toolchain_config)
63
64    return [
65        toolchain_config,
66        cc_common.create_cc_toolchain_config_info(
67            ctx = ctx,
68            action_configs = legacy.action_configs,
69            features = legacy.features,
70            cxx_builtin_include_directories = legacy.cxx_builtin_include_directories,
71            # toolchain_identifier is deprecated, but setting it to None results
72            # in an error that it expected a string, and for safety's sake, I'd
73            # prefer to provide something unique.
74            toolchain_identifier = str(ctx.label),
75            # These fields are only relevant for legacy toolchain resolution.
76            target_system_name = "",
77            target_cpu = "",
78            target_libc = "",
79            compiler = "",
80            abi_version = "",
81            abi_libc_version = "",
82        ),
83        # This allows us to support all_files.
84        # If all_files was simply an alias to
85        # //cc/toolchains/actions:all_actions,
86        # then if a toolchain introduced a new type of action, it wouldn't get
87        # put in all_files.
88        DefaultInfo(files = depset(transitive = toolchain_config.files.values())),
89    ]
90
91cc_toolchain_config = rule(
92    implementation = _cc_toolchain_config_impl,
93    # @unsorted-dict-items
94    attrs = {
95        # Attributes new to this rule.
96        "tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True),
97        "args": attr.label_list(providers = [ArgsListInfo]),
98        "known_features": attr.label_list(providers = [FeatureSetInfo]),
99        "enabled_features": attr.label_list(providers = [FeatureSetInfo]),
100        "_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"),
101    },
102    provides = [ToolchainConfigInfo],
103)
104