- define custom flags for your project, obsoleting the need for
--define - write
transitions to configure deps in
different configurations than their parents
(such as
--compilation_mode=optor--cpu=arm) - bake better defaults into rules (such as automatically build
//my:android_appwith a specified SDK)
bazelbuild/examples repo for
examples.
User-defined build settings
A build setting is a single piece of configuration information. Think of a configuration as a key/value map. Setting--cpu=ppc
and --copt="-DFoo" produces a configuration that looks like
{cpu: ppc, copt: "-DFoo"}. Each entry is a build setting.
Traditional flags like cpu and copt are native settings —
their keys are defined and their values are set inside native bazel java code.
Bazel users can only read and write them via the command line
and other APIs maintained natively. Changing native flags, and the APIs
that expose them, requires a bazel release. User-defined build
settings are defined in .bzl files (and thus, don’t need a bazel release to
register changes). They also can be set via the command line
(if they’re designated as flags, see more below), but can also be
set via user-defined transitions.
Defining build settings
End to end exampleThe build_setting rule() parameter
Build settings are rules like any other rule and are differentiated using the
Starlark rule() function’s build_setting
attribute.
build_setting attribute takes a function that designates the type of the
build setting. The type is limited to a set of basic Starlark types like
bool and string. See the config module
documentation for details. More complicated typing can be
done in the rule’s implementation function. More on this below.
The config module’s functions takes an optional boolean parameter, flag,
which is set to false by default. if flag is set to true, the build setting
can be set on the command line by users as well as internally by rule writers
via default values and transitions.
Not all settings should be settable by users. For example, if you as a rule
writer have some debug mode that you’d like to turn on inside test rules,
you don’t want to give users the ability to indiscriminately turn on that
feature inside other non-test rules.
Using ctx.build_setting_value
Like all rules, build setting rules have implementation functions. The basic Starlark-type value of the build settings can be accessed via thectx.build_setting_value method. This method is only available to
ctx objects of build setting rules. These implementation
methods can directly forward the build settings value or do additional work on
it, like type checking or more complex struct creation. Here’s how you would
implement an enum-typed build setting:
Defining multi-set string flags
String settings have an additionalallow_multiple parameter which allows the
flag to be set multiple times on the command line or in bazelrcs. Their default
value is still set with a string-typed attribute:
{"//example:roasts": ["blonde", "medium,dark"]} and
ctx.build_setting_value returns the list ["blonde", "medium,dark"].
Instantiating build settings
Rules defined with thebuild_setting parameter have an implicit mandatory
build_setting_default attribute. This attribute takes on the same type as
declared by the build_setting param.
Predefined settings
End to end example The Skylib library includes a set of predefined settings you can instantiate without having to write custom Starlark. For example, to define a setting that accepts a limited set of string values:Using build settings
Depending on build settings
If a target would like to read a piece of configuration information, it can directly depend on the build setting via a regular attribute dependency.fragments no longer
exists as a hardcoded object in Starlark configuration world, one way to
translate this concept would be to use sets of common implicit attributes. For
example:
Using build settings on the command line
Similar to most native flags, you can use the command line to set build settings that are marked as flags. The build setting’s name is its full target path usingname=value syntax:
Using build setting aliases
You can set an alias for your build setting target path to make it easier to read on the command line. Aliases function similarly to native flags and also make use of the double-dash option syntax. Set an alias by adding--flag_alias=ALIAS_NAME=TARGET_PATH
to your .bazelrc . For example, to set an alias to coffee:
coffee set in the user’s .bazelrc:
.bazelrc reduces command line clutter.
Label-typed build settings
End to end example Unlike other build settings, label-typed settings cannot be defined using thebuild_setting rule parameter. Instead, bazel has two built-in rules:
label_flag and label_setting. These rules forward the providers of the
actual target to which the build setting is set. label_flag and
label_setting can be read/written by transitions and label_flag can be set
by the user like other build_setting rules can. Their only difference is they
can’t customely defined.
Label-typed settings will eventually replace the functionality of late-bound
defaults. Late-bound default attributes are Label-typed attributes whose
final values can be affected by configuration. In Starlark, this will replace
the configuration_field
API.
Build settings and select()
End to end example Users can configure attributes on build settings by usingselect(). Build setting targets can be passed to the flag_values attribute of
config_setting. The value to match to the configuration is passed as a
String then parsed to the type of the build setting for matching.
User-defined transitions
A configuration transition maps the transformation from one configured target to another within the build graph. Important: Transitions have memory and performance impact.Defining
Transitions define configuration changes between rules. For example, a request like “compile my dependency for a different CPU than its parent” is handled by a transition. Formally, a transition is a function from an input configuration to one or more output configurations. Most transitions are 1:1 such as “override the input configuration with--cpu=ppc”. 1:2+ transitions can also exist but come
with special restrictions.
In Starlark, transitions are defined much like rules, with a defining
transition()
function
and an implementation function.
transition() function takes in an implementation function, a set of
build settings to read(inputs), and a set of build settings to write
(outputs). The implementation function has two parameters, settings and
attr. settings is a dictionary {String:Object} of all settings declared
in the inputs parameter to transition().
attr is a dictionary of attributes and values of the rule to which the
transition is attached. When attached as an
outgoing edge transition, the values of these
attributes are all configured post-select() resolution. When attached as
an incoming edge transition, attr does not
include any attributes that use a selector to resolve their value. If an
incoming edge transition on --foo reads attribute bar and then also
selects on --foo to set attribute bar, then there’s a chance for the
incoming edge transition to read the wrong value of bar in the transition.
Note: Since transitions are attached to rule definitions and select()s are
attached to rule instantiations (such as targets), errors related to select()s on
read attributes will pop up when users create targets rather than when rules are
written. It may be worth taking extra care to communicate to rule users which
attributes they should be wary of selecting on or taking other precautions.
The implementation function must return a dictionary (or list of
dictionaries, in the case of
transitions with multiple output configurations)
of new build settings values to apply. The returned dictionary keyset(s) must
contain exactly the set of build settings passed to the outputs
parameter of the transition function. This is true even if a build setting is
not actually changed over the course of the transition - its original value must
be explicitly passed through in the returned dictionary.
Defining 1:2+ transitions
End to end example Outgoing edge transition can map a single input configuration to two or more output configurations. This is useful for defining rules that bundle multi-architecture code. 1:2+ transitions are defined by returning a list of dictionaries in the transition implementation function.Attaching transitions
End to end example Transitions can be attached in two places: incoming edges and outgoing edges. Effectively this means rules can transition their own configuration (incoming edge transition) and transition their dependencies’ configurations (outgoing edge transition). NOTE: There is currently no way to attach Starlark transitions to native rules. If you need to do this, contact bazel-discuss@googlegroups.com for help with figuring out workarounds.Incoming edge transitions
Incoming edge transitions are activated by attaching atransition object
(created by transition()) to rule()’s cfg parameter:
Outgoing edge transitions
Outgoing edge transitions are activated by attaching atransition object
(created by transition()) to an attribute’s cfg parameter:
Transitions on native options
End to end example Starlark transitions can also declare reads and writes on native build configuration options via a special prefix to the option name.Unsupported native options
Bazel doesn’t support transitioning on--define with
"//command_line_option:define". Instead, use a custom
build setting. In general, new usages of
--define are discouraged in favor of build settings.
Bazel doesn’t support transitioning on --config. This is because --config is
an “expansion” flag that expands to other flags.
Crucially, --config may include flags that don’t affect build configuration,
such as
--spawn_strategy
. Bazel, by design, can’t bind such flags to individual targets. This means
there’s no coherent way to apply them in transitions.
As a workaround, you can explicitly itemize the flags that are part of
the configuration in your transition. This requires maintaining the --config’s
expansion in two places, which is a known UI blemish.
Transitions on allow multiple build settings
When setting build settings that allow multiple values, the value of the setting must be set with a list.No-op transitions
If a transition returns{}, [], or None, this is shorthand for keeping all
settings at their original values. This can be more convenient than explicitly
setting each output to itself.
Accessing attributes with transitions
End to end example When attaching a transition to an outgoing edge (regardless of whether the transition is a 1:1 or 1:2+ transition),ctx.attr is forced to be a list
if it isn’t already. The order of elements in this list is unspecified.
1:2+ and sets custom keys, ctx.split_attr can be used
to read individual deps for each key:
Integration with platforms and toolchains
Many native flags today, like--cpu and --crosstool_top are related to
toolchain resolution. In the future, explicit transitions on these types of
flags will likely be replaced by transitioning on the
target platform.
Memory and performance considerations
Adding transitions, and therefore new configurations, to your build comes at a cost: larger build graphs, less comprehensible build graphs, and slower builds. It’s worth considering these costs when considering using transitions in your build rules. Below is an example of how a transition might create exponential growth of your build graph.Badly behaved builds: a case study
Figure 1. Scalability graph showing a top level target and its dependencies.
This graph shows a top level target, //pkg:app, which depends on two targets, a
//pkg:1_0 and //pkg:1_1. Both these targets depend on two targets, //pkg:2_0 and
//pkg:2_1. Both these targets depend on two targets, //pkg:3_0 and //pkg:3_1.
This continues on until //pkg:n_0 and //pkg:n_1, which both depend on a single
target, //pkg:dep.
Building //pkg:app requires \(2n+2\) targets:
//pkg:app//pkg:dep//pkg:i_0and//pkg:i_1for \(i\) in \([1..n]\)
--//foo:owner=<STRING> and //pkg:i_b applies
depConfig = myConfig + depConfig.owner=“(b)”
In other words, //pkg:i_b appends b to the old value of --owner for all
its deps.
This produces the following configured targets:
//pkg:dep produces \(2^n\) configured targets: config.owner=
”\(b_0b_1…b_n\)” for all \(b_i\) in \({0,1}\).
This makes the build graph exponentially larger than the target graph, with
corresponding memory and performance consequences.
TODO: Add strategies for measurement and mitigation of these issues.
Further reading
For more details on modifying build configurations, see:- Starlark Build Configuration
- Full set of end to end examples