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