skia2/bazel/common_config_settings/defs.bzl
Kevin Lubick 888d4efa77 [canvaskit] Add Freetype/Fonts to Bazel Build
This re-works src/ports/BUILD.bazel to work like our other
BUILD files, i.e. one rule "srcs" that brings in the necessary
private filegroups.

To work around an abort with LLVM [1], we have to go back to an
earlier version of emscripten (temporarily?).

Future work should look at using transitions [2] to allow various
executables (e.g. CanvasKit, DM) to set their own set of Bazel
flags, w/o the build invokers having to specify them.

These transitions might be able to handle more complex cases
that we currently use if statements in GN to deal with.

The Freetype build rule was created by taking the BUILD.gn
rule, adding in all the sources listed there and then playing
compile-whack-a-mole to add in all the headers and included
.c files.

Suggested Review Order:
 - third_party/BUILD.bazel to see freetype build rules
 - bazel/common_config_settings/ to see treatment of fontmgr
   like codecs (many possible) and fontmgr_factory (only one).
 - src/ports/BUILD.bazel
 - BUILD.bazel
 - modules/canvaskit/BUILD.bazel. Take note of the gen_rule that
   calls tools/embed_resources.py to produce the .cpp file
   containing the embedded font data.
 - Everything else.

[1] https://github.com/emscripten-core/emscripten/issues/15528
[2] https://github.com/bazelbuild/examples/tree/main/rules/starlark_configurations/cc_binary_selectable_copts
Bug: skia:12541
Change-Id: I08dab82a901d80507007b354ca20cbfad2c2388f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/471636
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
2021-11-17 13:06:35 +00:00

121 lines
5.5 KiB
Python

"""
This file contains helpers for defining build flags and options that are used to
configure the Skia build.
"""
# https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl
load("@bazel_skylib//rules:common_settings.bzl", "string_flag", skylib_bool_flag = "bool_flag")
# Forked from https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl
BuildSettingInfo = provider(
doc = "A singleton provider that contains the raw value of a multi-string build setting",
fields = ["values"],
)
def _multi_string_impl(ctx):
allowed_values = ctx.attr.values
values = ctx.build_setting_value
for v in values:
if v not in ctx.attr.values:
fail("Error setting " + str(ctx.label) + ": invalid value '" + v + "'. Allowed values are " + str(allowed_values))
return BuildSettingInfo(values = values)
multi_string_flag = rule(
implementation = _multi_string_impl,
build_setting = config.string(flag = True, allow_multiple = True),
attrs = {
"values": attr.string_list(
doc = "The list of allowed values for this setting. An error is raised if any other values are given.",
),
},
doc = "A string-typed build setting that can be set multiple times on the command line",
)
def string_flag_with_values(flag_name, values, default = "", multiple = False, name = ""):
"""Create a string flag and corresponding config_settings.
string_flag_with_values is a Bazel Macro that defines a flag with the given name and a set
of valid values for that flag. For each value, a config_setting is defined with the name
of the value, associated with the created flag.
This is defined to make the BUILD.bazel file easier to read w/o the boilerplate of defining
a string_flag rule and n config_settings
https://docs.bazel.build/versions/main/skylark/macros.html
Args:
flag_name: string, the name of the flag to create and use for the config_settings
values: list of strings, the valid values for this flag to be set to.
default: string, whatever the default value should be if the flag is not set. Can be
empty string for both a string_flag and a multi_string flag.
multiple: boolean, True if the flag should be able to be set multiple times on the CLI.
name: string unused, https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro
"""
if multiple:
multi_string_flag(
name = flag_name,
# We have to specify a default value, even if that value is empty string.
# https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings
build_setting_default = default,
# If empty string is the default, we need to make sure it is in the list
# of acceptable values. If the default is not empty string, we don't want
# to make empty string a valid value. Having duplicate values in the list
# does not cause any issues, so we can just add the default to achieve
# this affect.
values = values + [default],
)
else:
string_flag(
name = flag_name,
# We have to specify a default value, even if that value is empty string.
# https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings
build_setting_default = default,
# If empty string is the default, we need to make sure it is in the list
# of acceptable values. If the default is not empty string, we don't want
# to make empty string a valid value. Having duplicate values in the list
# does not cause any issues, so we can just add the default to achieve
# this affect.
values = values + [default],
)
# For each of the values given, we define a config_setting. This allows us to use
# select statements, on the given setting, e.g. referencing
# //bazel/common_config_settings:some_valid_value_for_a_flag
for v in values:
native.config_setting(
name = v,
flag_values = {
":" + flag_name: v,
},
)
def bool_flag(flag_name, default = True, name = ""):
"""Create a boolean flag and corresponding config_settings.
bool_flag is a Bazel Macro that defines a boolean flag with the given name two config_settings,
one for True, one for False. Reminder that Bazel has special syntax for unsetting boolean flags,
but this does not work well with aliases.
https://docs.bazel.build/versions/main/skylark/config.html#using-build-settings-on-the-command-line
Thus it is best to define both an "enabled" alias and a "disabled" alias.
Args:
flag_name: string, the name of the flag to create and use for the config_settings
default: boolean, if the flag should default to on or off.
name: string unused, https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro
"""
skylib_bool_flag(name = flag_name, build_setting_default = default)
native.config_setting(
name = flag_name + "_true",
flag_values = {
# The value must be a string, but it will be parsed to a boolean
# https://docs.bazel.build/versions/main/skylark/config.html#build-settings-and-select
":" + flag_name: "True",
},
)
native.config_setting(
name = flag_name + "_false",
flag_values = {
":" + flag_name: "False",
},
)