xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/toolchains/impl/toolchain_config.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
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("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
17load(
18    "//cc/toolchains:cc_toolchain_info.bzl",
19    "ActionTypeConfigSetInfo",
20    "ActionTypeSetInfo",
21    "ArgsListInfo",
22    "FeatureSetInfo",
23    "ToolchainConfigInfo",
24)
25load(":collect.bzl", "collect_action_types")
26load(":legacy_converter.bzl", "convert_toolchain")
27load(":toolchain_config_info.bzl", "toolchain_config_info")
28
29visibility([
30    "//cc/toolchains/...",
31    "//tests/rule_based_toolchain/...",
32])
33
34def _cc_legacy_file_group_impl(ctx):
35    files = ctx.attr.config[ToolchainConfigInfo].files
36
37    return [DefaultInfo(files = depset(transitive = [
38        files[action]
39        for action in collect_action_types(ctx.attr.actions).to_list()
40        if action in files
41    ]))]
42
43cc_legacy_file_group = rule(
44    implementation = _cc_legacy_file_group_impl,
45    attrs = {
46        "actions": attr.label_list(providers = [ActionTypeSetInfo], mandatory = True),
47        "config": attr.label(providers = [ToolchainConfigInfo], mandatory = True),
48    },
49)
50
51def _cc_toolchain_config_impl(ctx):
52    if ctx.attr.features:
53        fail("Features is a reserved attribute in bazel. Did you mean 'toolchain_features'")
54
55    if not ctx.attr._enabled[BuildSettingInfo].value and not ctx.attr.skip_experimental_flag_validation_for_test:
56        fail("Rule based toolchains are experimental. To use it, please add --@rules_cc//cc/toolchains:experimental_enable_rule_based_toolchains to your bazelrc")
57
58    toolchain_config = toolchain_config_info(
59        label = ctx.label,
60        features = ctx.attr.toolchain_features + [ctx.attr._builtin_features],
61        action_type_configs = ctx.attr.action_type_configs,
62        args = ctx.attr.args,
63    )
64
65    legacy = convert_toolchain(toolchain_config)
66
67    return [
68        toolchain_config,
69        cc_common.create_cc_toolchain_config_info(
70            ctx = ctx,
71            action_configs = legacy.action_configs,
72            features = legacy.features,
73            cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
74            # toolchain_identifier is deprecated, but setting it to None results
75            # in an error that it expected a string, and for safety's sake, I'd
76            # prefer to provide something unique.
77            toolchain_identifier = str(ctx.label),
78            target_system_name = ctx.attr.target_system_name,
79            target_cpu = ctx.attr.target_cpu,
80            target_libc = ctx.attr.target_libc,
81            compiler = ctx.attr.compiler,
82            abi_version = ctx.attr.abi_version,
83            abi_libc_version = ctx.attr.abi_libc_version,
84            builtin_sysroot = ctx.attr.sysroot or None,
85        ),
86        # This allows us to support all_files.
87        # If all_files was simply an alias to
88        # ///cc/toolchains/actions:all_actions,
89        # then if a toolchain introduced a new type of action, it wouldn't get
90        # put in all_files.
91        DefaultInfo(files = depset(transitive = toolchain_config.files.values())),
92    ]
93
94cc_toolchain_config = rule(
95    implementation = _cc_toolchain_config_impl,
96    # @unsorted-dict-items
97    attrs = {
98        # Attributes new to this rule.
99        "action_type_configs": attr.label_list(providers = [ActionTypeConfigSetInfo]),
100        "args": attr.label_list(providers = [ArgsListInfo]),
101        "toolchain_features": attr.label_list(providers = [FeatureSetInfo]),
102        "skip_experimental_flag_validation_for_test": attr.bool(default = False),
103        "_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"),
104        "_enabled": attr.label(default = "//cc/toolchains:experimental_enable_rule_based_toolchains"),
105
106        # Attributes from create_cc_toolchain_config_info.
107        # artifact_name_patterns is currently unused. Consider adding it later.
108        # TODO: Consider making this into a label_list that takes a
109        #  cc_directory_marker rule as input.
110        "cxx_builtin_include_directories": attr.string_list(),
111        "target_system_name": attr.string(mandatory = True),
112        "target_cpu": attr.string(mandatory = True),
113        "target_libc": attr.string(mandatory = True),
114        "compiler": attr.string(mandatory = True),
115        "abi_version": attr.string(),
116        "abi_libc_version": attr.string(),
117        # tool_paths currently unused.
118        # TODO: Consider making this into a label that takes a
119        #  cc_directory_marker rule as an input.
120        "sysroot": attr.string(),
121    },
122    provides = [ToolchainConfigInfo],
123)
124