This section will provide the information necessary to create your own
projects using Boost.Build. The information provided here is relatively
high-level, and the section called “Reference” as well as the on-line
help system must be used to obtain low-level documentation (see Boost.Build actually consists of two parts - Boost.Jam, a build engine with its own interpreted language, and Boost.Build itself, implemented in Boost.Jam's language. The chain of events when you type bjam on the command line is as follows:
So, to be able to successfully use Boost.Build, you need to know only four things:
This section will describe the basics of the Boost.Jam language— just enough for writing Jamfiles. For more information, please see the Boost.Jam documentation. Boost.Jam has an interpreted, procedural language. On the lowest level, a Boost.Jam program consists of variables and rules (Jam term for function). They are grouped into modules—there is one global module and a number of named modules. Besides that, a Boost.Jam program contains classes and class instances. Syntantically, a Boost.Jam program consists of two kind of elements—keywords (which have a special meaning to Boost.Jam) and literals. Consider this code: a = b ;
which assigns the value
If you want to use a literal value that is the same as some keyword, the value can be quoted: a = "=" ;
All variables in Boost.Jam have the same
type—list of strings. To define a variable one assigns a value to
it, like in the previous example. An undefined variable is the same as a
variable with an empty value. Variables can be accessed using the
a = $(b) $(c) ;
Rules are defined by specifying the rule name, the parameter names, and the allowed value list size for each parameter. rule When this rule is called, the list passed as the first argument must have exactly one value. The list passed as the second argument can either have one value of be empty. The two remaining arguments can be arbitrarily long, but the third argument may not be empty. The overview of Boost.Jam language statements is given below: helper 1 : 2 : 3 ; x = [ helper 1 : 2 : 3 ] ; This code calls the named rule with the specified arguments. When the result of the call must be used inside some expression, you need to add brackets around the call, like shown on the second line. if cond { statements } [ else { statements } ] This is a regular if-statement. The condition is composed of:
for var in list { statements }
Executes statements for each element in list, setting the variable
while cond { statements } Repeatedly execute statements while cond remains true upon entry. return values ;
This statement should be used only inside a rule and assigns
import
The first form imports the specified bjam module. All rules from that
module are made available using the qualified name: Sometimes, you'd need to specify the actual command lines to be used when creating targets. In jam language, you use named actions to do this. For example: actions create-file-from-another { create-file-from-another $(<) $(>) }
This specifies a named action called To flexibly adjust the command line, you can define a rule with the same name as the action and taking three parameters -- targets, sources and properties. For example: rule create-file-from-another ( targets * : sources * : properties * ) { if <variant>debug in $(properties) { OPTIONS on $(targets) = --debug ; } } actions create-file-from-another { create-file-from-another $(OPTIONS) $(<) $(>) }
In this example, the rule checks if certain build property is specified.
If so, it sets variable More details can be found in Jam reference, the section called “ Rules”.
On startup, Boost.Build searches and reads two configuration files:
Table?30.1.?Search paths for configuration files
Usually,
using
The using gcc ; will make the GCC compiler available. All the supported tools are documented in the section called “Builtin tools”, including the specific options they take. Some general notes that apply to most C++ compilers are below.
For all the C++ compiler toolsets Boost.Build supports
out-of-the-box, the list of parameters to
If you have a single compiler, and the compiler executable
it can be configured by simply:
using
If the compiler is installed in a custom directory, you should provide the command that invokes the compiler, for example: using gcc : : g++-3.2 ; using msvc : : "Z:/Programs/Microsoft Visual Studio/vc98/bin/cl" ; Some Boost.Build toolsets will use that path to take additional actions required before invoking the compiler, such as calling vendor-supplied scripts to set up its required environment variables. When compiler executables for C and C++ are different, path to the C++ compiler executable must be specified. The command can be any command allowed by the operating system. For example: using msvc : : echo Compiling && foo/bar/baz/cl ; will work.
To configure several versions of a toolset, simply invoke the
using gcc : 3.3 ; using gcc : 3.4 : g++-3.4 ; using gcc : 3.2 : g++-3.2 ;
Note that in the first call to
Many of toolsets have an using gcc : 3.4 : : <compileflags>-m64 <linkflags>-m64 ;
To invoke Boost.Build, type bjam on the command line. Three kinds of command-line tokens are accepted, in any order:
To build all targets defined in Jamfile in the current directory with default properties, run: bjam
To build specific targets, specify them on the command line: bjam lib1 subproject//lib2
To request a certain value for some property, add bjam toolset=gcc variant=debug optimization=space
Boost.Build recognizes the following command line options.
In the simplest case, the build is performed with a single set of properties,
that you specify on the command line with elements in the form
Table?30.2.?
If you have more than one version of a given C++ toolset (e.g. configured in user-config.jam , or autodetected, as happens with msvc), you can
request the specific version by passing
as
the value of the toolset feature, for example toolset=msvc-8.0 .
If a feature has a fixed set of values it can be specified more than once on the command line. In which case, everything will be built several times -- once for each specified value of a feature. For example, if you use bjam link=static link=shared threading=single threading=multi Then a total of 4 builds will be performed. For convenience, instead of specifying all requested values of a feature in separate command line elements, you can separate the values with commands, for example: bjam link=static,shared threading=single,multi The comma has special meaning only if the feature has a fixed set of values, so bjam include=static,shared is not treated specially. All command line elements that are neither options nor properties are the names of the targets to build. See the section called “Target identifiers and references”. If no target is specified, the project in the current directory is built. A Main target is a user-defined named entity that can be built, for example an executable file. Declaring a main target is usually done using one of the main target rules described in the section called “Builtin rules”. The user can also declare custom main target rules as shown in the section called “Main target rules”. Most main target rules in Boost.Build have the same common signature:
rule
Some main target rules have a different list of parameters as explicitly stated in their documentation. The actual requirements for a target are obtained by refining requirements of the project where a target is declared with the explicitly specified requirements. The same is true for usage-requirements. More details can be found in the section called “Property refinement” The name of main target has two purposes. First, it's used to refer to this target from other targets and from command line. Second, it's used to compute the names of the generated files. Typically, filenames are obtained from main target name by appending system-dependent suffixes and prefixes. The name of a main target can contain alphanumeric characters, dashes, undescores and dots. The entire name is significant when resolving references from other targets. For determining filenames, only the part before the first dot is taken. For example: obj test.release : test.cpp : <variant>release ; obj test.debug : test.cpp : <variant>debug ; will generate two files named The list of sources specifies what should be processed to
get the resulting targets. Most of the time, it's just a list of
files. Sometimes, you'll want to automatically construct the
list of source files rather than having to spell it out
manually, in which case you can use the
exe a : a.cpp ; # a.cpp is the only source file exe b : [ glob *.cpp ] ; # all .cpp files in this directory are sources Unless you specify a file with an absolute path, the name is considered relative to the source directory — which is typically the directory where the Jamfile is located, but can be changed as described in the section called “Projects”. The list of sources can also refer to other main targets. Targets in the same project can be referred to by name, while targets in other projects must be qualified with a directory or a symbolic project name. The directory/project name is separated from the target name by a double forward slash. There is no special syntax to distinguish the directory name from the project name—the part before the double slash is first looked up as project name, and then as directory name. For example: lib helper : helper.cpp ; exe a : a.cpp helper ; # Since all project ids start with slash, ".." is a directory name. exe b : b.cpp ..//utils ; exe c : c.cpp /boost/program_options//program_options ; The first exe uses the library defined in the same project. The second one uses some target (most likely a library) defined by a Jamfile one level higher. Finally, the third target uses a C++ Boost library, referring to it using its absolute symbolic name. More information about target references can be found in the section called “Dependent Targets” and the section called “Target identifiers and references”. Requirements are the properties that should always be present when building a target. Typically, they are includes and defines: exe hello : hello.cpp : <include>/opt/boost <define>MY_DEBUG ; There is a number of other features, listed in the section called “Builtin features”. For example if a library can only be built statically, or a file can't be compiled with optimization due to a compiler bug, one can use lib util : util.cpp : <link>static ; obj main : main.cpp : <optimization>off ;
Sometimes, particular relationships need to be maintained
among a target's build properties. This can be achieved with
conditional
requirements. For example, you might want to set
specific
lib network : network.cpp
: <link>shared:<define>NEWORK_LIB_SHARED
<variant>release:<define>EXTRA_FAST
;
In the example above, whenever You can use several properties in the condition, for example: lib network : network.cpp : <toolset>gcc,<optimization>speed:<define>USE_INLINE_ASSEMBLER ;
A more powerful variant of conditional requirements is indirect conditional requirements. You can provide a rule that will be called with the current build properties and can compute additional properties to be added. For example: lib network : network.cpp : <conditional>@my-rule ; rule my-rule ( properties * ) { local result ; if <toolset>gcc <optimization>speed in $(properties) { result += <define>USE_INLINE_ASSEMBLER ; } return $(result) ; } This example is equivalent to the previous one, but for complex cases, indirect conditional requirements can be easier to write and understand. Requirements explicitly specified for a target are usually combined with the requirements specified for the containing project. You can cause a target to completely ignore specific project's requirement using the syntax by adding a minus sign before a property, for example:
exe main : main.cpp : -<define>UNNECESSARY_DEFINE ;
This syntax is the only way to ignore free properties from a parent, such as defines. It can be also useful for ordinary properties. Consider this example: project test : requirements <threading>multi ; exe test1 : test1.cpp ; exe test2 : test2.cpp : <threading>single ; exe test3 : test3.cpp : -<threading>multi ;
Here, Note that the removal of requirements is completely textual: you need to specify exactly the same property to remove it. The exe hello : hello.cpp : : <threading>multi ; would build a multi-threaded target unless the user explicitly requests a single-threaded version. The difference between requirements and default-build is that requirements cannot be overridden in any way. The ways a target is built can be so different that describing them using conditional requirements would be hard. For example, imagine that a library actually uses different source files depending on the toolset used to build it. We can express this situation using target alternatives: lib demangler : dummy_demangler.cpp ; # alternative 1 lib demangler : demangler_gcc.cpp : <toolset>gcc ; # alternative 2 lib demangler : demangler_msvc.cpp : <toolset>msvc ; # alternative 3
In the example above, when built with It is possible to declare a target inline, i.e. the "sources" parameter may include calls to other main rules. For example: exe hello : hello.cpp [ obj helpers : helpers.cpp : <optimization>off ] ;
Will cause "helpers.cpp" to be always compiled without
optimization. When referring to an inline main target, its declared
name must be prefixed by its parent target's name and two dots. In
the example above, to build only helpers, one should run
When no target is requested on the command line, all targets in the
current project will be built. If a target should be built only by
explicit request, this can be expressed by the
explicit install_programs ;
As mentioned before, targets are grouped into projects, and each Jamfile is a separate project. Projects are useful because they allow us to group related targets together, define properties common to all those targets, and assign a symbolic name to the project that can be used in referring to its targets. Projects are named using the
project
Here, project tennis : requirements <threading>multi : default-build release ;
The possible attributes are listed below. Project id is a short way to denote a project, as opposed to the Jamfile's pathname. It is a hierarchical path, unrelated to filesystem, such as "boost/thread". Target references make use of project ids to specify a target. Source location specifies the directory where sources for the project are located. Project requirements are requirements that apply to all the targets in the projects as well as all subprojects. Default build is the build request that should be used when no build request is specified explicitly. The default values for those attributes are given in the table below. Table?30.3.?
Besides defining projects and main targets, Jamfiles often invoke various utility rules. For the full list of rules that can be directly used in Jamfile see the section called “Builtin rules”. Each subproject inherits attributes, constants and rules
from its parent project, which is defined by the nearest
Jamfile in an ancestor directory above
the subproject. The top-level project is declared in a file
called Even when building in a subproject directory, parent
project files are always loaded before those of their
subprojects, so that every definition made in a parent project
is always available to its children. The loading order of any
other projects is unspecified. Even if one project refers to
another via the
When you've described your targets, you want Boost.Build to run the right tools and create the needed targets. This section will describe two things: how you specify what to build, and how the main targets are actually constructed. The most important thing to note is that in Boost.Build, unlike other build tools, the targets you declare do not correspond to specific files. What you declare in a Jamfile is more like a “metatarget.” Depending on the properties you specify on the command line, each metatarget will produce a set of real targets corresponding to the requested properties. It is quite possible that the same metatarget is built several times with different properties, producing different files.
The command line specifies which targets to build and with which properties. For example: bjam app1 lib1//lib1 toolset=gcc variant=debug optimization=full would build two targets, "app1" and "lib1//lib1" with the specified properties. You can refer to any targets, using target id and specify arbitrary properties. Some of the properties are very common, and for them the name of the property can be omitted. For example, the above can be written as: bjam app1 lib1//lib1 gcc debug optimization=full The complete syntax, which has some additional shortcuts, is described in ???. When you request, directly or indirectly, a build of a main target with specific requirements, the following steps are done. Some brief explanation is provided, and more details are given in the section called “Build process”.
Often, a user builds a complete project, not just one main target. In fact, invoking bjam without arguments builds the project defined in the current directory. When a project is built, the build request is passed without
modification to all main targets in that project.
It's is possible to
prevent implicit building of a target in a project with the
explicit hello_test ;
would cause the The Jamfile for a project can include a number of
|