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"""All providers for rule-based bazel toolchain config.""" 15*eed53cd4SHONG Yifan 16*eed53cd4SHONG Yifanload("//cc/toolchains/impl:args_utils.bzl", "validate_nested_args") 17*eed53cd4SHONG Yifanload( 18*eed53cd4SHONG Yifan "//cc/toolchains/impl:collect.bzl", 19*eed53cd4SHONG Yifan "collect_action_types", 20*eed53cd4SHONG Yifan "collect_files", 21*eed53cd4SHONG Yifan "collect_provider", 22*eed53cd4SHONG Yifan) 23*eed53cd4SHONG Yifanload( 24*eed53cd4SHONG Yifan "//cc/toolchains/impl:nested_args.bzl", 25*eed53cd4SHONG Yifan "NESTED_ARGS_ATTRS", 26*eed53cd4SHONG Yifan "args_wrapper_macro", 27*eed53cd4SHONG Yifan "nested_args_provider_from_ctx", 28*eed53cd4SHONG Yifan) 29*eed53cd4SHONG Yifanload( 30*eed53cd4SHONG Yifan ":cc_toolchain_info.bzl", 31*eed53cd4SHONG Yifan "ActionTypeSetInfo", 32*eed53cd4SHONG Yifan "ArgsInfo", 33*eed53cd4SHONG Yifan "ArgsListInfo", 34*eed53cd4SHONG Yifan "BuiltinVariablesInfo", 35*eed53cd4SHONG Yifan "FeatureConstraintInfo", 36*eed53cd4SHONG Yifan) 37*eed53cd4SHONG Yifan 38*eed53cd4SHONG Yifanvisibility("public") 39*eed53cd4SHONG Yifan 40*eed53cd4SHONG Yifandef _cc_args_impl(ctx): 41*eed53cd4SHONG Yifan actions = collect_action_types(ctx.attr.actions) 42*eed53cd4SHONG Yifan 43*eed53cd4SHONG Yifan if not ctx.attr.args and not ctx.attr.nested and not ctx.attr.env: 44*eed53cd4SHONG Yifan fail("cc_args requires at least one of args, nested, and env") 45*eed53cd4SHONG Yifan 46*eed53cd4SHONG Yifan nested = None 47*eed53cd4SHONG Yifan if ctx.attr.args or ctx.attr.nested: 48*eed53cd4SHONG Yifan nested = nested_args_provider_from_ctx(ctx) 49*eed53cd4SHONG Yifan validate_nested_args( 50*eed53cd4SHONG Yifan variables = ctx.attr._variables[BuiltinVariablesInfo].variables, 51*eed53cd4SHONG Yifan nested_args = nested, 52*eed53cd4SHONG Yifan actions = actions.to_list(), 53*eed53cd4SHONG Yifan label = ctx.label, 54*eed53cd4SHONG Yifan ) 55*eed53cd4SHONG Yifan files = nested.files 56*eed53cd4SHONG Yifan else: 57*eed53cd4SHONG Yifan files = collect_files(ctx.attr.data) 58*eed53cd4SHONG Yifan 59*eed53cd4SHONG Yifan requires = collect_provider(ctx.attr.requires_any_of, FeatureConstraintInfo) 60*eed53cd4SHONG Yifan 61*eed53cd4SHONG Yifan args = ArgsInfo( 62*eed53cd4SHONG Yifan label = ctx.label, 63*eed53cd4SHONG Yifan actions = actions, 64*eed53cd4SHONG Yifan requires_any_of = tuple(requires), 65*eed53cd4SHONG Yifan nested = nested, 66*eed53cd4SHONG Yifan env = ctx.attr.env, 67*eed53cd4SHONG Yifan files = files, 68*eed53cd4SHONG Yifan ) 69*eed53cd4SHONG Yifan return [ 70*eed53cd4SHONG Yifan args, 71*eed53cd4SHONG Yifan ArgsListInfo( 72*eed53cd4SHONG Yifan label = ctx.label, 73*eed53cd4SHONG Yifan args = tuple([args]), 74*eed53cd4SHONG Yifan files = files, 75*eed53cd4SHONG Yifan by_action = tuple([ 76*eed53cd4SHONG Yifan struct(action = action, args = tuple([args]), files = files) 77*eed53cd4SHONG Yifan for action in actions.to_list() 78*eed53cd4SHONG Yifan ]), 79*eed53cd4SHONG Yifan ), 80*eed53cd4SHONG Yifan ] 81*eed53cd4SHONG Yifan 82*eed53cd4SHONG Yifan_cc_args = rule( 83*eed53cd4SHONG Yifan implementation = _cc_args_impl, 84*eed53cd4SHONG Yifan attrs = { 85*eed53cd4SHONG Yifan "actions": attr.label_list( 86*eed53cd4SHONG Yifan providers = [ActionTypeSetInfo], 87*eed53cd4SHONG Yifan mandatory = True, 88*eed53cd4SHONG Yifan doc = """A list of action types that this flag set applies to. 89*eed53cd4SHONG Yifan 90*eed53cd4SHONG YifanSee @rules_cc//cc/toolchains/actions:all for valid options. 91*eed53cd4SHONG Yifan""", 92*eed53cd4SHONG Yifan ), 93*eed53cd4SHONG Yifan "env": attr.string_dict( 94*eed53cd4SHONG Yifan doc = "Environment variables to be added to the command-line.", 95*eed53cd4SHONG Yifan ), 96*eed53cd4SHONG Yifan "requires_any_of": attr.label_list( 97*eed53cd4SHONG Yifan providers = [FeatureConstraintInfo], 98*eed53cd4SHONG Yifan doc = """This will be enabled when any of the constraints are met. 99*eed53cd4SHONG Yifan 100*eed53cd4SHONG YifanIf omitted, this flag set will be enabled unconditionally. 101*eed53cd4SHONG Yifan""", 102*eed53cd4SHONG Yifan ), 103*eed53cd4SHONG Yifan "_variables": attr.label( 104*eed53cd4SHONG Yifan default = "//cc/toolchains/variables:variables", 105*eed53cd4SHONG Yifan ), 106*eed53cd4SHONG Yifan } | NESTED_ARGS_ATTRS, 107*eed53cd4SHONG Yifan provides = [ArgsInfo], 108*eed53cd4SHONG Yifan doc = """Declares a list of arguments bound to a set of actions. 109*eed53cd4SHONG Yifan 110*eed53cd4SHONG YifanRoughly equivalent to ctx.actions.args() 111*eed53cd4SHONG Yifan 112*eed53cd4SHONG YifanExamples: 113*eed53cd4SHONG Yifan cc_args( 114*eed53cd4SHONG Yifan name = "warnings_as_errors", 115*eed53cd4SHONG Yifan args = ["-Werror"], 116*eed53cd4SHONG Yifan ) 117*eed53cd4SHONG Yifan""", 118*eed53cd4SHONG Yifan) 119*eed53cd4SHONG Yifan 120*eed53cd4SHONG Yifancc_args = lambda **kwargs: args_wrapper_macro(rule = _cc_args, **kwargs) 121