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