Skip to main content

Set up configuration modifiers

A project has to enable configuration modifiers before it can use them. Until it does, a modifier on the command line is an error, and Buck2 ignores the ones in PACKAGE files and on targets.

Enable modifiers​

Add this to the PACKAGE file in the root of the project, creating the file if there is none:

load(
"@prelude//cfg/modifier:cfg_constructor.bzl",
"cfg_constructor_post_constraint_analysis",
"cfg_constructor_pre_constraint_analysis",
)
load("@prelude//cfg/modifier:common.bzl", "MODIFIER_METADATA_KEY")

native.set_cfg_constructor(
stage0 = cfg_constructor_pre_constraint_analysis,
stage1 = cfg_constructor_post_constraint_analysis,
key = MODIFIER_METADATA_KEY,
aliases = struct(),
extra_data = struct(),
)

set_cfg_constructor tells Buck2 which functions turn a target's platform and modifiers into its configuration:

  • stage0 and stage1 are those functions. The two from the prelude apply the modifiers of PACKAGE files, then of the target, then of the command line.
  • key takes the prelude's MODIFIER_METADATA_KEY.
  • aliases gives short names to modifiers, for use on the command line.
  • extra_data is passed on to the functions. The prelude's need nothing in it.

set_cfg_constructor can be called once, and only in the root PACKAGE file of the project.

Declare a constraint and aliases for its values​

A modifier is a value of a constraint. This one, in config/BUCK, has the build modes of the project:

constraint(
name = "build_mode",
default = "debug",
values = [
"debug",
"release",
],
)

Its values are the targets root//config:build_mode[debug] and root//config:build_mode[release]. To give them the names debug and release, list them in the aliases of the root PACKAGE file:

native.set_cfg_constructor(
stage0 = cfg_constructor_pre_constraint_analysis,
stage1 = cfg_constructor_post_constraint_analysis,
key = MODIFIER_METADATA_KEY,
aliases = struct(
debug = "root//config:build_mode[debug]",
release = "root//config:build_mode[release]",
),
extra_data = struct(),
)

An alias can also stand for a list of modifiers.

A constraint changes a build once something selects on it. Usually that is a toolchain that the project declares itself: the demo toolchains that buck2 init sets up have no flags to set. In the toolchains/BUCK of Setting up a new project, the C++ toolchain can take its flags from the build mode:

system_cxx_toolchain(
name = "cxx",
compiler = "gcc",
compiler_type = "gcc",
cxx_compiler = "g++",
cxx_flags = ["-std=c++20", "-Wall"] + select({
"root//config:build_mode[debug]": ["-O0", "-g"],
"root//config:build_mode[release]": ["-O2", "-DNDEBUG"],
}),
linker = "g++",
visibility = ["PUBLIC"],
)

Use it​

With a cxx_binary named //app:server:

buck2 build //app:server              # debug, the default of the constraint
buck2 build //app:server -m release
buck2 build '//app:server?release' # the same

To check which configuration a target gets:

$ buck2 ctargets //app:server -m release
root//app:server (cfg:<empty>#952593b9488909b5)

$ buck2 audit configurations 'cfg:<empty>#952593b9488909b5'
cfg:<empty>#952593b9488909b5:
prelude//cpu/constraints:cpu[x86_64] (prelude//cpu/constraints:cpu)
prelude//os/constraints:os[linux] (prelude//os/constraints:os)
root//config:build_mode[release] (root//config:build_mode)

Modifiers can also be set for a directory and on a target.