1""" 2THIS IS THE EXTERNAL-ONLY VERSION OF THIS FILE. G3 HAS ITS OWN. 3 4This file contains a way to set flags from BUILD.bazel instead of requiring users to set them from 5the CLI. 6 7It is based off of https://github.com/bazelbuild/examples/tree/main/rules/starlark_configurations/cc_binary_selectable_copts 8 9""" 10 11load("@skia_user_config//:copts.bzl", "DEFAULT_COPTS") 12 13_bool_flags = [ 14 "//bazel/common_config_settings:use_harfbuzz", 15 "//bazel/common_config_settings:use_icu", 16 "//src/gpu/ganesh/vk:enable_secondary_draw_context", 17 "//src/gpu:enable_gpu_test_utils", 18 "//src/lazy:enable_discardable_memory", 19 "//src/lazy:use_default_global_memory_pool", 20 "//src/pdf:enable_pdf_backend", 21 "//src/sksl:enable_sksl", 22 "//src/sksl:enable_sksl_tracing", 23 "//src/sksl:enable_skslc", 24 "//src/svg:enable_svg_canvas", 25] 26 27_string_flags = [ 28 "//bazel/common_config_settings:fontmgr_factory", 29 "//src/gpu:with_gl_standard", 30] 31 32_string_list_flags = [ 33 "//src/gpu:gpu_backend", 34 "//src/codec:include_decoder", 35 "//src/encode:include_encoder", 36 "//bazel/common_config_settings:include_fontmgr", 37] 38 39# These are the flags that we support setting via set_flags 40_flags = _bool_flags + _string_flags + _string_list_flags 41_short_flags = [long_flag.split(":")[1] for long_flag in _flags] 42 43def _flag_transition_impl(settings, attr): 44 rv = {} 45 for flag in attr.set_flags: 46 if flag not in _short_flags: 47 fail("unknown flag " + flag) 48 49 for key in settings: 50 # Get the short form of the name. This the short form used as the keys in the 51 # set_flags dictionary. 52 flag_name = key.split(":")[1] 53 54 # If there is an entry in set_flags for the short-version of a flag, use that 55 # value or values. If not, use whatever value is set via flags. 56 flag_setting = attr.set_flags.get(flag_name, settings[key]) 57 if key in _string_list_flags: 58 if type(flag_setting) == "list": 59 rv[key] = flag_setting 60 else: 61 rv[key] = [flag_setting] # This usually happens when the default value is used. 62 elif key in _string_flags: 63 if type(flag_setting) == "list": 64 rv[key] = flag_setting[0] 65 else: 66 rv[key] = flag_setting # we know flag_setting is a string (e.g. the default). 67 elif key in _bool_flags: 68 if type(flag_setting) == "list": 69 rv[key] = flag_setting[0].lower() == "true" 70 else: 71 rv[key] = flag_setting # flag_setting will be a boolean, the default 72 return rv 73 74# This defines a Starlark transition and which flags it reads and writes. 75with_flags_transition = transition( 76 implementation = _flag_transition_impl, 77 inputs = _flags, 78 outputs = _flags, 79) 80 81# The implementation of transition_rule: all this does is copy the cc_binary's output to 82# its own output and propagate its runfiles and executable to use for "$ bazel run". 83# 84# This makes transition_rule as close to a pure wrapper of cc_binary as possible. 85def _transition_rule_impl(ctx): 86 actual_binary = ctx.attr.actual_binary[0] 87 outfile = ctx.actions.declare_file(ctx.label.name) 88 cc_binary_outfile = actual_binary[DefaultInfo].files.to_list()[0] 89 90 ctx.actions.run_shell( 91 inputs = [cc_binary_outfile], 92 outputs = [outfile], 93 command = "cp %s %s" % (cc_binary_outfile.path, outfile.path), 94 ) 95 return [ 96 DefaultInfo( 97 executable = outfile, 98 data_runfiles = actual_binary[DefaultInfo].data_runfiles, 99 ), 100 ] 101 102# The purpose of this rule is to take a "set_flags" attribute, invoke a transition that sets 103# any of _flags to the specified values, then depend on a cc_binary whose deps will be able 104# to select() on those flags as if the user had set them via the CLI. 105transition_rule = rule( 106 implementation = _transition_rule_impl, 107 attrs = { 108 # set_flags is a dictionary with the keys being the short-form of a flag name 109 # (e.g. the part that comes after the colon) and the value being a list of values 110 # that the flag should be set to, regardless of the relevant CLI flags. 111 # https://bazel.build/rules/lib/attr#string_list_dict 112 "set_flags": attr.string_list_dict(), 113 # This is the cc_binary whose deps will select() on that feature. 114 # Note specifically how it is modified with _flag_transition, which 115 # ensures that the flags propagates down the graph. 116 # https://bazel.build/rules/lib/attr#label 117 "actual_binary": attr.label(cfg = with_flags_transition), 118 # This is a stock Bazel requirement for any rule that uses Starlark 119 # transitions. It's okay to copy the below verbatim for all such rules. 120 # 121 # The purpose of this requirement is to give the ability to restrict 122 # which packages can invoke these rules, since Starlark transitions 123 # make much larger graphs possible that can have memory and performance 124 # consequences for your build. The allowlist defaults to "everything". 125 # But you can redefine it more strictly if you feel that's prudent. 126 "_allowlist_function_transition": attr.label( 127 default = "@bazel_tools//tools/allowlists/function_transition_allowlist", 128 ), 129 }, 130 # Making this executable means it works with "$ bazel run". 131 executable = True, 132) 133 134def cc_binary_with_flags(name, set_flags = {}, copts = DEFAULT_COPTS, **kwargs): 135 """Builds a cc_binary as if set_flags were set on the CLI. 136 137 Args: 138 name: string, the name for the rule that is the binary, but with the flags changed via 139 a transition. Any dependents should use this name. 140 set_flags: dictionary of string to list of strings. The keys should be the name of the 141 flag, and the values should be the desired valid settings for that flag. 142 copts: a list of strings or select statements that control the compiler flags. 143 It has a sensible list of defaults. 144 **kwargs: Any flags that a cc_binary normally takes. 145 """ 146 cc_binary_name = name + "_native_binary" 147 transition_rule( 148 name = name, 149 actual_binary = ":%s" % cc_binary_name, 150 set_flags = set_flags, 151 testonly = kwargs.get("testonly", False), 152 ) 153 tags = kwargs.get("tags", []) 154 tags.append("manual") # We want to exclude this helper binary from bazel build foo/... 155 kwargs["tags"] = tags 156 native.cc_binary( 157 name = cc_binary_name, 158 copts = copts, 159 **kwargs 160 ) 161