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