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("//cc:defs.bzl", _cc_toolchain = "cc_toolchain") 17load( 18 "//cc/toolchains/impl:toolchain_config.bzl", 19 "cc_legacy_file_group", 20 "cc_toolchain_config", 21) 22 23visibility("public") 24 25# Taken from https://bazel.build/docs/cc-toolchain-config-reference#actions 26# TODO: This is best-effort. Update this with the correct file groups once we 27# work out what actions correspond to what file groups. 28_LEGACY_FILE_GROUPS = { 29 "ar_files": [ 30 "@rules_cc//cc/toolchains/actions:ar_actions", # copybara-use-repo-external-label 31 ], 32 "as_files": [ 33 "@rules_cc//cc/toolchains/actions:assembly_actions", # copybara-use-repo-external-label 34 ], 35 "compiler_files": [ 36 "@rules_cc//cc/toolchains/actions:cc_flags_make_variable", # copybara-use-repo-external-label 37 "@rules_cc//cc/toolchains/actions:c_compile", # copybara-use-repo-external-label 38 "@rules_cc//cc/toolchains/actions:cpp_compile", # copybara-use-repo-external-label 39 "@rules_cc//cc/toolchains/actions:cpp_header_parsing", # copybara-use-repo-external-label 40 ], 41 # There are no actions listed for coverage, dwp, and objcopy in action_names.bzl. 42 "coverage_files": [], 43 "dwp_files": [], 44 "linker_files": [ 45 "@rules_cc//cc/toolchains/actions:cpp_link_dynamic_library", # copybara-use-repo-external-label 46 "@rules_cc//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", # copybara-use-repo-external-label 47 "@rules_cc//cc/toolchains/actions:cpp_link_executable", # copybara-use-repo-external-label 48 ], 49 "objcopy_files": [], 50 "strip_files": [ 51 "@rules_cc//cc/toolchains/actions:strip", # copybara-use-repo-external-label 52 ], 53} 54 55def cc_toolchain( 56 name, 57 dynamic_runtime_lib = None, 58 libc_top = None, 59 module_map = None, 60 output_licenses = [], 61 static_runtime_lib = None, 62 supports_header_parsing = False, 63 supports_param_files = True, 64 target_compatible_with = None, 65 exec_compatible_with = None, 66 compatible_with = None, 67 tags = [], 68 visibility = None, 69 **kwargs): 70 """A macro that invokes native.cc_toolchain under the hood. 71 72 Generated rules: 73 {name}: A `cc_toolchain` for this toolchain. 74 _{name}_config: A `cc_toolchain_config` for this toolchain. 75 _{name}_*_files: Generated rules that group together files for 76 "ar_files", "as_files", "compiler_files", "coverage_files", 77 "dwp_files", "linker_files", "objcopy_files", and "strip_files" 78 normally enumerated as part of the `cc_toolchain` rule. 79 80 Args: 81 name: str: The name of the label for the toolchain. 82 dynamic_runtime_lib: See cc_toolchain.dynamic_runtime_lib 83 libc_top: See cc_toolchain.libc_top 84 module_map: See cc_toolchain.module_map 85 output_licenses: See cc_toolchain.output_licenses 86 static_runtime_lib: See cc_toolchain.static_runtime_lib 87 supports_header_parsing: See cc_toolchain.supports_header_parsing 88 supports_param_files: See cc_toolchain.supports_param_files 89 target_compatible_with: target_compatible_with to apply to all generated 90 rules 91 exec_compatible_with: exec_compatible_with to apply to all generated 92 rules 93 compatible_with: compatible_with to apply to all generated rules 94 tags: Tags to apply to all generated rules 95 visibility: Visibility of toolchain rule 96 **kwargs: Args to be passed through to cc_toolchain_config. 97 """ 98 all_kwargs = { 99 "compatible_with": compatible_with, 100 "exec_compatible_with": exec_compatible_with, 101 "tags": tags, 102 "target_compatible_with": target_compatible_with, 103 } 104 for group in _LEGACY_FILE_GROUPS: 105 if group in kwargs: 106 fail("Don't use legacy file groups such as %s. Instead, associate files with tools, actions, and args." % group) 107 108 config_name = "_{}_config".format(name) 109 cc_toolchain_config( 110 name = config_name, 111 visibility = ["//visibility:private"], 112 **(all_kwargs | kwargs) 113 ) 114 115 # Provides ar_files, compiler_files, linker_files, ... 116 legacy_file_groups = {} 117 for group, actions in _LEGACY_FILE_GROUPS.items(): 118 group_name = "_{}_{}".format(name, group) 119 cc_legacy_file_group( 120 name = group_name, 121 config = config_name, 122 actions = actions, 123 visibility = ["//visibility:private"], 124 **all_kwargs 125 ) 126 legacy_file_groups[group] = group_name 127 128 if visibility != None: 129 all_kwargs["visibility"] = visibility 130 131 _cc_toolchain( 132 name = name, 133 toolchain_config = config_name, 134 all_files = config_name, 135 dynamic_runtime_lib = dynamic_runtime_lib, 136 libc_top = libc_top, 137 module_map = module_map, 138 output_licenses = output_licenses, 139 static_runtime_lib = static_runtime_lib, 140 supports_header_parsing = supports_header_parsing, 141 supports_param_files = supports_param_files, 142 **(all_kwargs | legacy_file_groups) 143 ) 144