b – build system driver


b --help
b --version
b [options] [variables] [buildspec]


The build2 driver performs a set of meta-operations on operations on targets according to the build specification, or buildspec for short. This process can be controlled by specifying driver options and build system variables.

Note that options, variables, and buildspec fragments can be specified in any order. To avoid treating an argument that starts with '-' as an option, add the '--' separator. To avoid treating an argument that contains '=' as a variable, add the second '--' separator.

The buildspec has the following form:


All components in the buildspec can be omitted. If meta-operation is omitted, then it defaults to perform. If operation is omitted, then it defaults to the default operation for this meta-operation. For perform it is update. Finally, if target is omitted, then it defaults to the current working directory. Some operations and meta-operations may take additional parameters. For example:

b                       # perform(update(./))
b foo/                  # perform(update(foo/))
b foo/ bar/             # perform(update(foo/ bar/))
b update                # perform(update(./))
b 'clean(../)'          # perform(clean(../))
b perform               # perform(update(./))
b configure             # configure(?(./))
b 'configure(../)'      # configure(?(../))
b clean update          # perform(clean(./) update(./))
b configure update      # configure(?(./)) perform(update(./))
b 'create(conf/, cxx)'  # create(?(conf/), cxx)

Notice the question mark used to show the (imaginary) default operation for the configure meta-operation. For configure the default operation is "all operations". That is, it will configure all the operations for the specified target.

You can also "generate" multiple operations for the same set of targets. Compare:

b 'clean(foo/ bar/)' 'update(foo/ bar/)'
b '{clean update}(foo/ bar/)'

Some more useful buildspec examples:

b '{clean update}(...)'        # rebuild
b '{clean update clean}(...)'  # make sure builds
b '{clean test clean}(...)'    # make sure passes tests
b '{clean disfigure}(...)'     # similar to distclean

In POSIX shells parenthesis are special characters and must be quoted when used in a buildspec. Besides being an inconvenience in itself, quoting also inhibits path auto-completion. To help with this situation a shortcut syntax is available for executing a single operation or meta-operation, for example:

b clean: foo/ bar/                # clean(foo/ bar/)
b configure: src/@out/            # configure(src/@out/)
b create: conf/, cxx              # create(conf/, cxx)
b configure: config.cxx=g++ src/  # configure(src/) config.cxx=g++

To activate the shortcut syntax the first buildspec argument must start with an operation or meta-operation name and end with a colon (:). To transform the shortcut syntax to the normal buildspec syntax the colon is replaced with the opening parenthesis ((), the rest of the buildspec arguments are treated as is, and the final closing parenthesis ()) is added.

For each target the driver expects to find buildfile either in the target's directory or, if the directory is part of the out tree (out_base), in the corresponding src directory (src_base).

For example, assuming foo/ is the source directory of a project:

b foo/              # out_base=src_base=foo/
b foo-out/          # out_base=foo-out/ src_base=foo/
b foo-out/exe{foo}  # out_base=foo-out/ src_base=foo/

An exception to this requirement is a directory target in which case, provided the directory has subdirectories, an implied buildfile with the following content is assumed:

# Implied directory buildfile: build all subdirectories.
./: */

In the above example, we assumed that the build2 driver was able to determine the association between out_base and src_base. In case src_base and out_base are not the same directory, this is achieved in one of two ways: the config module (which implements the configure, disfigure, and create meta-operations) saves this association as part of the configuration process. If, however, the association hasn't been saved, then we have to specify src_base explicitly using the following extended target syntax:


Continuing with the previous example:

b foo/@foo-out/exe{foo}  # out_base=foo-out/ src_base=foo/

Normally, you would need to specify src_base explicitly only once, during configuration. For example, a typical usage would be:

b 'configure(foo/@foo-out/)'  # src_base is saved
b foo-out/                    # no need to specify src_base
b 'clean(foo-out/exe{foo})'   # no need to specify src_base

The ability to specify build2 variables as part of the command line is normally used to pass configuration values, for example:

b config.cxx=clang++ config.cxx.coptions=-O3

Similar to buildspec, POSIX shells often inhibit path auto-completion on the right hand side of a variable assignment. To help with this situation the assignment can be broken down into three separate command line arguments, for example:

b config.import.libhello = ../libhello/

The build system has the following built-in and pre-defined meta-operations:

Perform an operation.
Configure all operations supported by a project and save the result in the project's build/ file. Implemented by the config module. For example:
b config.cxx=clang++ config.cxx.coptions=-O3 \
  config.install.root=/usr/local config.install.root.sudo=sudo \
Disfigure all operations supported by a project and remove the project's build/ file. Implemented by the config module.
Create and configure a configuration project. Implemented by the config module.

Normally a build2 project is created manually by writing the and files, adding source files, and so on. However, a special kind of project, which we call configuration, is often useful. Such a project doesn't have any source files of its own. Instead, it serves as an amalgamation for building other projects as part of it. Doing it this way has two major benefits: sub-projects automatically resolve their imports to other projects in the amalgamation and sub-projects inherits their configuration from the amalgamation (which means if we want to change something, we only need to do it in one place).

As an example, let's assume we have two C++ projects: the libhello library in libhello/ and the hello executable that imports it in hello/. And we want to build hello with clang++.

One way to do it would be to configure and build each project in its own directory, for example:

b 'configure(libhello/@libhello-clang/)' config.cxx=clang++
b 'configure(hello/@hello-clang/)' config.cxx=clang++ \

The two drawbacks, as mentioned above, are the need to explicitly resolve the import and having to make changes in multiple places should, for example, we want to switch from clang++ to g++.

We can, however, achieve the same end result but without any of the drawbacks using the configuration project:

b 'create(clang/, cxx)' config.cxx=clang++ # Creates clang/.
b 'configure(libhello/@clang/libhello/)'
b 'configure(hello/@clang/hello/)'

The targets passed to the create meta-operation must be directories which should either not exist or be empty. For each such directory create first initializes a project as described below and then configures it by executing the configure meta-operation.

The first optional parameter to create is the list of modules to load in By default, create appends .config to the names of these modules so that only their configurations are loaded. You can override this behavior by specifying the period (.) after the module name. You can also instruct create to use the optional module load by prefixing the module name with the question mark (?).

The second optional parameter is the list of modules to load in If not specified, then the test and install modules are loaded by default. The config module is always loaded first.

Besides creating project's and, create also writes the root buildfile with the following contents:

./: {*/ -build/}

If used, this buildfile will build all the sub-projects currently present in the configuration.

Prepare a distribution containing all files necessary to perform all operations in a project. Implemented by the dist module.

The build system has the following built-in and pre-defined operations:

Update a target.
Clean a target.
Test a target. Performs update as a pre-operation. Implemented by the test module.
Install a target. Performs update as a pre-operation. Implemented by the install module.

Note that buildspec and command line variable values are treated as buildfile fragments and so can use quoting and escaping as well as contain variable expansions and evaluation contexts. However, to be more usable on various platforms, escaping in these two situations is limited to the effective sequences of \', \", \\, \$, and \( with all other sequences interpreted as is. Together with double-quoting this is sufficient to represent any value. For example:

b config.install.root=c:\projects\install
b "config.install.root='c:\Program Files (x86)\test\'"
b 'config.cxx.poptions=-DFOO_STR="foo"'


Print actual commands being executed. This is equivalent to --verbose 2.
Print all underlying commands being executed. This is equivalent to --verbose 3.
Display build progress. If printing to a terminal the progress is displayed by default for low verbosity levels. Use --no-progress to suppress.
Don't display build progress.
Run quietly, only printing error messages. This is equivalent to --verbose 0.
--verbose level
Set the diagnostics verbosity to level between 0 and 6. Level 0 disables any non-error messages while level 6 produces lots of information, with level 1 being the default. The following additional types of diagnostics are produced at each level:
  1. High-level information messages.
  2. Essential underlying commands being executed.
  3. All underlying commands being executed.
  4. Information that could be helpful to the user.
  5. Information that could be helpful to the developer.
  6. Even more detailed information, including state dumps.
--jobs|-j num
Number of active jobs to perform in parallel. This includes both the number of active threads inside the build system as well as the number of external commands (compilers, linkers, etc) started but not yet finished. If this option is not specified or specified with the 0 value, then the number of available hardware threads is used.
--max-jobs|-J num
Maximum number of jobs (threads) to create. The default is 16x the number of active jobs (--jobs|j) on 32-bit architectures and 32x on 64-bit. See the build system scheduler implementation for details.
--queue-depth|-Q num
The queue depth as a multiplier over the number of active jobs. Normally we want a deeper queue if the jobs take long (for example, compilation) and shorter if they are quick (for example, simple tests). The default is 4. See the build system scheduler implementation for details.
Run serially and stop at the first error. This mode is useful to investigate build failures that are caused by build system errors rather than compilation errors. Note that if you don't want to keep going but still want parallel execution, add --jobs|-j (for example -j 0 for default concurrency).
Match the rules but do not execute the operation. This mode is primarily useful for profiling.
Don't print column numbers in diagnostics.
Don't print line and column numbers in diagnostics.
--buildfile path
The alternative file to read build information from. The default is buildfile. If path is '-', then read from STDIN. Note that this option only affects the files read as part of the buildspec processing. Specifically, it has no effect on the source and include directives. As a result, this option is primarily intended for testing rather than changing the build file names in real projects.
--config-guess path
The path to the config.guess(1) script that should be used to guess the host machine triplet. If this option is not specified, then b will fall back on to using the target it was built for as host.
--config-sub path
The path to the config.sub(1) script that should be used to canonicalize machine triplets. If this option is not specified, then b will use its built-in canonicalization support which should be sufficient for commonly-used platforms.
--pager path
The pager program to be used to show long text. Commonly used pager programs are less and more. You can also specify additional options that should be passed to the pager program with --pager-option. If an empty string is specified as the pager program, then no pager will be used. If the pager program is not explicitly specified, then b will try to use less. If it is not available, then no pager will be used.
--pager-option opt
Additional option to be passed to the pager program. See --pager for more information on the pager program. Repeat this option to specify multiple pager options.
Print usage information and exit.
Print version and exit.


Non-zero exit status is returned in case of an error.


The HOME environment variable is used to determine the user's home directory. If it is not set, then getpwuid(3) is used instead. This value is used to shorten paths printed in diagnostics by replacing the home directory with ~/. It is also made available to buildfile's as the build.home variable.


Send bug reports to the mailing list.