2c65579aad
The primary goal of this organization structure is to keep our top level BUILD.bazel file short, with as little logic as feasible. The logic required to control which files to include, which third_party deps are needed, what system libraries should be linked again, etc, should be in the BUILD.bazel file best should be as close to the affected files as feasible. In essence, we use filegroup() rules to bubble up the files needed to build Skia (all as one big cc_library call) and cc_library rules to bubble up the other components needed to build. For example, //src/ports/SkFontHost_FreeType.cpp needs FreeType, but only if we are compiling Skia with that type of font support. With the new organization structure in this CL, //src/ports/BUILD.bazel should have the logic that determines if the cpp file should be included in the build of Skia and if it is, that the Skia build should depend on //third_party:freetype2 Another example is //src/gpu/ganesh/BUILD.bazel, which chooses which of the dawn, gl, vulkan, etc backend sources, and the associated dependencies to include in the build. It does not specify what those are, but delegates to the BUILD.bazel files in the subdirectories housing the backend-specific code. The structure guidelines for BUILD.bazel files are as follows: - Have a filegroup() called "hdrs" (for public headers) or "srcs" (for private headers and all .cpp files) that is visible to the parent directory. This should list the files from the containing directory to include in the build. See //include/core/BUILD.bazel and //src/effects/BUILD.bazel as examples. - filegroup() rules can list a child directory's "hdrs" or "srcs" in their "srcs" attributes, but should not contain select statements pertaining to child directory files. See //include/gpu/BUILD.bazel and //src/gpu/ganesh/BUILD.bazel as examples. - May have a cc_library() called "deps". This can specify dependencies, cc_opts, and linkopts, but not srcs or hdrs. [1] See //src/codec/BUILD.bazel as an example. These should be visible to the parent directory. - "hdrs", "srcs", and "deps" for the primary Skia build (currently called "skia_core") should bubble up through //include/BUILD.bazel and //src/BUILD.bazel, one directory at a time. This CL demonstrates a very basic build of Skia with many features turned off (CPU only, no fonts, no codecs). Follow-on CLs will add to these rules as more targets are supported. See bazel/Makefile for the builds that work with just this CL. Suggested Review Order: - //BUILD.bazel to see the very small skia_core rule which delegates all the logic down stack. Note that it has a dependency on //bazel:defines_from_flags which will set all the defines listed there when compiling all the .cpp and .h files in skia_core *and* anything that depends on skia_core, but *not* //src:deps. - //include/BUILD.bazel and other BUILD.bazel files in the subdirectories of that folder. Note that the filegroups in //include/private/... are called "srcs" to be similar to how Bazel wants "private headers" to be in the "srcs" of cc_library, cc_binary, etc. and only public headers are to be in "hdrs" [2]. - //src/BUILD.bazel and other BUILD.bazel files in the subdirectories of that folder. //src/gpu/ganesh/... will be filled in for dawn, vulkan, and GL in the next CL. - //PRESUBMIT.py, which adds a check that runs buildifier [3] on modified BUILD.bazel files to make sure they stay consistently formatted. - //bazel/... to see the new option I added to make sksl opt-in or opt-out, so one could build Skia with sksl, but not with a gpu backend. - Misc .h and .cpp files, whose includes were removed if unnecessary or #ifdef'd out to make the minimal build work without GPU or SkSL includes. - //bazel/Makefile to see the builds that work with this CL. [1] Setting srcs or hdrs is error-prone at best, because those files will be compiled with a different set of defines than the rest of skia_core, because they wouldn't depend on //bazel:defines_from_flags. [2] https://bazel.build/reference/be/c-cpp#cc_library.hdrs [3] https://github.com/bazelbuild/buildtools/releases Change-Id: I5e0e3ae01ad42d672506d5aad1239f2512188191 Bug: skia:12541 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/543977 Reviewed-by: Leandro Lovisolo <lovisolo@google.com> Reviewed-by: Ben Wagner <bungeman@google.com>
144 lines
6.0 KiB
Python
144 lines
6.0 KiB
Python
"""
|
|
This file contains a way to set flags from BUILD.bazel instead of requiring users to set them from
|
|
the CLI.
|
|
|
|
It is based off of https://github.com/bazelbuild/examples/tree/main/rules/starlark_configurations/cc_binary_selectable_copts
|
|
|
|
"""
|
|
|
|
_bool_flags = [
|
|
"//bazel/common_config_settings:enable_sksl",
|
|
"//bazel/common_config_settings:enable_sksl_tracing",
|
|
"//bazel/common_config_settings:enable_skslc",
|
|
"//bazel/common_config_settings:is_skia_dev_build",
|
|
"//bazel/common_config_settings:use_icu",
|
|
]
|
|
|
|
_string_flags = [
|
|
"//bazel/common_config_settings:fontmgr_factory",
|
|
"//bazel/common_config_settings:with_gl_standard",
|
|
]
|
|
|
|
_string_list_flags = [
|
|
"//bazel/common_config_settings:gpu_backend",
|
|
"//bazel/common_config_settings:include_decoder",
|
|
"//bazel/common_config_settings:include_encoder",
|
|
"//bazel/common_config_settings:include_fontmgr",
|
|
"//bazel/common_config_settings:shaper_backend",
|
|
]
|
|
|
|
# These are the flags that we support setting via set_flags
|
|
_flags = _bool_flags + _string_flags + _string_list_flags
|
|
|
|
def _flag_transition_impl(settings, attr):
|
|
rv = {}
|
|
for key in settings:
|
|
# Get the short form of the name. This the short form used as the keys in the
|
|
# set_flags dictionary.
|
|
flag_name = key.split(":")[1]
|
|
|
|
# If there is an entry in set_flags for the short-version of a flag, use that
|
|
# value or values. If not, use whatever value is set via flags.
|
|
flag_setting = attr.set_flags.get(flag_name, settings[key])
|
|
if key in _string_list_flags:
|
|
if type(flag_setting) == "list":
|
|
rv[key] = flag_setting
|
|
else:
|
|
rv[key] = [flag_setting] # This usually happens when the default value is used.
|
|
elif key in _string_flags:
|
|
if type(flag_setting) == "list":
|
|
rv[key] = flag_setting[0]
|
|
else:
|
|
rv[key] = flag_setting # we know flag_setting is a string (e.g. the default).
|
|
elif key in _bool_flags:
|
|
if type(flag_setting) == "list":
|
|
rv[key] = flag_setting[0] == "True"
|
|
else:
|
|
rv[key] = flag_setting # flag_setting will be a boolean, the default
|
|
return rv
|
|
|
|
# This defines a Starlark transition and which flags it reads and writes.
|
|
_flag_transition = transition(
|
|
implementation = _flag_transition_impl,
|
|
inputs = _flags,
|
|
outputs = _flags,
|
|
)
|
|
|
|
# The implementation of transition_rule: all this does is copy the cc_binary's output to
|
|
# its own output and propagate its runfiles and executable to use for "$ bazel run".
|
|
#
|
|
# This makes transition_rule as close to a pure wrapper of cc_binary as possible.
|
|
def _transition_rule_impl(ctx):
|
|
actual_binary = ctx.attr.actual_binary[0]
|
|
outfile = ctx.actions.declare_file(ctx.label.name)
|
|
cc_binary_outfile = actual_binary[DefaultInfo].files.to_list()[0]
|
|
|
|
ctx.actions.run_shell(
|
|
inputs = [cc_binary_outfile],
|
|
outputs = [outfile],
|
|
command = "cp %s %s" % (cc_binary_outfile.path, outfile.path),
|
|
)
|
|
return [
|
|
DefaultInfo(
|
|
executable = outfile,
|
|
data_runfiles = actual_binary[DefaultInfo].data_runfiles,
|
|
),
|
|
]
|
|
|
|
# The purpose of this rule is to take a "set_flags" attribute, invoke a transition that sets
|
|
# any of _flags to the specified values, then depend on a cc_binary whose deps will be able
|
|
# to select() on those flags as if the user had set them via the CLI.
|
|
transition_rule = rule(
|
|
implementation = _transition_rule_impl,
|
|
attrs = {
|
|
# set_flags is a dictionary with the keys being the short-form of a flag name
|
|
# (e.g. the part that comes after the colon) and the value being a list of values
|
|
# that the flag should be set to, regardless of the relevant CLI flags.
|
|
# https://docs.bazel.build/versions/main/skylark/lib/attr.html#string_list_dict
|
|
"set_flags": attr.string_list_dict(),
|
|
# This is the cc_binary whose deps will select() on that feature.
|
|
# Note specifically how it is modified with _flag_transition, which
|
|
# ensures that the flags propagates down the graph.
|
|
# https://docs.bazel.build/versions/main/skylark/lib/attr.html#label
|
|
"actual_binary": attr.label(cfg = _flag_transition),
|
|
# This is a stock Bazel requirement for any rule that uses Starlark
|
|
# transitions. It's okay to copy the below verbatim for all such rules.
|
|
#
|
|
# The purpose of this requirement is to give the ability to restrict
|
|
# which packages can invoke these rules, since Starlark transitions
|
|
# make much larger graphs possible that can have memory and performance
|
|
# consequences for your build. The allowlist defaults to "everything".
|
|
# But you can redefine it more strictly if you feel that's prudent.
|
|
"_allowlist_function_transition": attr.label(
|
|
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
|
|
),
|
|
},
|
|
# Making this executable means it works with "$ bazel run".
|
|
executable = True,
|
|
)
|
|
|
|
def cc_binary_with_flags(name, set_flags = {}, **kwargs):
|
|
"""Builds a cc_binary as if set_flags were set on the CLI.
|
|
|
|
Args:
|
|
name: string, the name for the rule that is the binary, but with the flags changed via
|
|
a transition. Any dependents should use this name.
|
|
set_flags: dictionary of string to list of strings. The keys should be the name of the
|
|
flag, and the values should be the desired valid settings for that flag.
|
|
**kwargs: Any flags that a cc_binary normally takes.
|
|
"""
|
|
cc_binary_name = name + "_native_binary"
|
|
transition_rule(
|
|
name = name,
|
|
actual_binary = ":%s" % cc_binary_name,
|
|
set_flags = set_flags,
|
|
testonly = kwargs.get("testonly", False),
|
|
)
|
|
tags = kwargs.get("tags", [])
|
|
tags.append("manual") # We want to exclude this helper binary from bazel build foo/...
|
|
kwargs["tags"] = tags
|
|
native.cc_binary(
|
|
name = cc_binary_name,
|
|
**kwargs
|
|
)
|