• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[[bbv2.overview]]
2= Overview
3
4This section will provide the information necessary to create your own
5projects using B2. The information provided here is relatively
6high-level, and the link:#bbv2.reference[Reference] as well as the on-line
7help system must be used to obtain low-level documentation (see
8link:#bbv2.overview.invocation.options.help[`--help`]).
9
10B2 has two parts — a build engine with its own interpreted
11language, and B2 itself, implemented in that language. The
12chain of events when you type `b2` on the command line is as follows:
13
141.  The B2 executable tries to find B2 modules and
15loads the top-level module. The exact process is described in
16link:#bbv2.reference.init[the section called “Initialization”]
172.  The top-level module loads user-defined configuration files,
18`user-config.jam` and `site-config.jam`, which define available
19toolsets.
203.  The Jamfile in the current directory is read. That in turn might
21cause reading of further Jamfiles. As a result, a tree of projects is
22created, with targets inside projects.
234.  Finally, using the build request specified on the command line,
24B2 decides which targets should be built and how. That
25information is passed back to Boost.Jam, which takes care of actually
26running the scheduled build action commands.
27
28So, to be able to successfully use B2, you need to know only
29four things:
30
31* link:#bbv2.overview.configuration[How to configure B2]
32* link:#bbv2.overview.targets[How to declare targets in Jamfiles]
33* link:#bbv2.overview.build_process[How the build process works]
34* Some Basics about the Boost.Jam language. See
35link:#bbv2.overview.jam_language[the section called “Boost.Jam Language”].
36
37[[bbv2.overview.concepts]]
38== Concepts
39
40B2 has a few unique concepts that are introduced in this
41section. The best way to explain the concepts is by comparison with more
42classical build tools.
43
44When using any flavor of make, you directly specify _targets_ and
45commands that are used to create them from other target. The below
46example creates `a.o` from `a.c` using a hardcoded compiler invocation
47command.
48
49[source,make]
50----
51a.o: a.c
52    g++ -o a.o -g a.c
53----
54
55This is a rather low-level description mechanism and it's hard to adjust
56commands, options, and sets of created targets depending on the compiler
57and operating system used.
58
59To improve portability, most modern build system provide a set of
60higher-level functions that can be used in build description files.
61Consider this example:
62
63[source,cmake]
64----
65add_program ("a", "a.c")
66----
67
68This is a function call that creates the targets necessary to create an
69executable file from the source file `a.c`. Depending on configured
70properties, different command lines may be used. However, `add_program`
71is higher-level, but rather thin level. All targets are created
72immediately when the build description is parsed, which makes it
73impossible to perform multi-variant builds. Often, change in any build
74property requires a complete reconfiguration of the build tree.
75
76In order to support true multi-variant builds, B2 introduces the
77concept of a metatarget definition main target metatarget
78_metatarget_ -- an object that is created when the build description is
79parsed and can be called later with specific build properties to
80generate actual targets.
81
82Consider an example:
83
84[source]
85----
86exe a : a.cpp ;
87----
88
89When this declaration is parsed, B2 creates a metatarget, but
90does not yet decide what files must be created, or what commands must be
91used. After all build files are parsed, B2 considers the
92properties requested on the command line. Supposed you have invoked
93B2 with:
94
95[source,shell]
96----
97b2 toolset=gcc toolset=msvc
98----
99
100In that case, the metatarget will be called twice, once with
101`toolset=gcc` and once with `toolset=msvc`. Both invocations will
102produce concrete targets, that will have different extensions and use
103different command lines.
104
105Another key concept is _build property_. A build
106property is a variable that affects the build process. It can be
107specified on the command line, and is passed when calling a metatarget.
108While all build tools have a similar mechanism, B2 differs by
109requiring that all build properties are declared in advance, and
110providing a large set of properties with portable semantics.
111
112The final concept is _property propagation_.
113B2 does not require that every metatarget is called with the
114same properties. Instead, the "top-level" metatargets are called with
115the properties specified on the command line. Each metatarget can elect
116to augment or override some properties (in particular, using the
117requirements mechanism, see
118link:#bbv2.overview.targets.requirements[the section called “Requirements”]).
119Then, the dependency metatargets are called with the modified properties and
120produce concrete targets that are then used in the build process. Of
121course, dependency metatargets maybe in turn modify build properties and
122have dependencies of their own.
123
124For a more in-depth treatment of the requirements and concepts, you may
125refer to http://syrcose.ispras.ru/2009/files/04_paper.pdf[SYRCoSE 2009
126B2 article].
127
128[[bbv2.overview.jam_language]]
129== Boost.Jam Language
130
131This section will describe the basics of the Boost.Jam language—just
132enough for writing Jamfiles. For more information, please see the
133link:#bbv2.jam[Boost.Jam] documentation.
134
135link:#bbv2.jam[Boost.Jam] has an interpreted, procedural language. On
136the lowest level, a link:#bbv2.jam[Boost.Jam] program consists of
137variables and _rules_ (the Jam term for functions). They are grouped
138into modules—there is one global module and a number of named modules.
139Besides that, a link:#bbv2.jam[Boost.Jam] program contains classes and
140class instances.
141
142Syntactically, a link:#bbv2.jam[Boost.Jam] program consists of two kinds
143of elements—keywords (which have a special meaning to
144link:#bbv2.jam[Boost.Jam]) and literals. Consider this code:
145
146[source]
147----
148a = b ;
149----
150
151which assigns the value `b` to the variable `a`. Here, `=` and `;` are
152keywords, while `a` and `b` are literals.
153
154WARNING: All syntax elements, even keywords, must be separated by spaces. For
155example, omitting the space character before `;` will lead to a syntax
156error.
157
158If you want to use a literal value that is the same as some keyword, the
159value can be quoted:
160
161[source]
162----
163a = "=" ;
164----
165
166All variables in link:#bbv2.jam[Boost.Jam] have the same type—list of
167strings. To define a variable one assigns a value to it, like in the
168previous example. An undefined variable is the same as a variable with
169an empty value. Variables can be accessed using the `$(variable)`
170syntax. For example:
171
172[source]
173----
174a = $(b) $(c) ;
175----
176
177Rules are defined by specifying the rule name, the parameter names, and
178the allowed value list size for each parameter.
179
180[source]
181----
182rule example
183 (
184     parameter1 :
185     parameter2 ? :
186     parameter3 + :
187     parameter4 *
188 )
189 {
190    # rule body
191 }
192----
193
194When this rule is called, the list passed as the first argument must
195have exactly one value. The list passed as the second argument can
196either have one value of be empty. The two remaining arguments can be
197arbitrarily long, but the third argument may not be empty.
198
199The overview of link:#bbv2.jam[Boost.Jam] language statements is given
200below:
201
202[source]
203----
204helper 1 : 2 : 3 ;
205x = [ helper 1 : 2 : 3 ] ;
206----
207
208This code calls the named rule with the specified arguments. When the
209result of the call must be used inside some expression, you need to add
210brackets around the call, like shown on the second line.
211
212[source]
213----
214if cond { statements } [ else { statements } ]
215----
216
217This is a regular if-statement. The condition is composed of:
218
219* Literals (true if at least one string is not empty)
220* Comparisons: `a operator b` where _operator_ is one of `=`, `!=`, `<`,
221`>`, `<=` or `>=`. The comparison is done pairwise between each string
222in the left and the right arguments.
223* Logical operations: `! a`, `a && b`, `a || b`
224* Grouping: `( cond )`
225
226[source]
227----
228for var in list { statements }
229----
230
231Executes statements for each element in list, setting the variable `var`
232to the element value.
233
234[source]
235----
236while cond { statements }
237----
238
239Repeatedly execute statements while cond remains true upon entry.
240
241[source]
242----
243return values ;
244----
245
246This statement should be used only inside a rule and returns `values` to
247the caller of the rule.
248
249[source]
250----
251import module ;
252import module : rule ;
253----
254
255The first form imports the specified module. All rules from that module
256are made available using the qualified name: `module.rule`. The second form
257imports the specified rules only, and they can be called using unqualified
258names.
259
260[[bbv2.overview.jam_language.actions]]
261Sometimes, you need to specify the actual command lines to be used when
262creating targets. In the jam language, you use named actions to do this.
263For example:
264
265[source]
266----
267actions create-file-from-another
268{
269    create-file-from-another $(<) $(>)
270}
271----
272
273This specifies a named action called `create-file-from-another`. The text
274inside braces is the command to invoke. The `$(<)` variable will be expanded
275to a list of generated files, and the `$(>)` variable will be expanded to a
276list of source files.
277
278To adjust the command line flexibly, you can define a rule with the same
279name as the action and taking three parameters -- targets, sources and
280properties. For example:
281
282[source]
283----
284rule create-file-from-another ( targets * : sources * : properties * )
285{
286   if <variant>debug in $(properties)
287   {
288       OPTIONS on $(targets) = --debug ;
289   }
290}
291actions create-file-from-another
292{
293    create-file-from-another $(OPTIONS) $(<) $(>)
294}
295----
296
297In this example, the rule checks if a certain build property is
298specified. If so, it sets the variable `OPTIONS` that is then used
299inside the action. Note that the variables set "on a target" will be
300visible only inside actions building that target, not globally. Were
301they set globally, using variable named `OPTIONS` in two unrelated
302actions would be impossible.
303
304More details can be found in the Jam reference,
305link:#jam.language.rules[the section called “Rules”].
306
307[[bbv2.overview.configuration]]
308== Configuration
309
310On startup, B2 searches and reads three configuration files:
311`site-config.jam`, `user-config.jam`, and `project-config.jam`. The
312first one is usually installed and maintained by a system administrator,
313and the second is for the user to modify. You can edit the one in the
314top-level directory of your B2 installation or create a copy in
315your home directory and edit the copy. The third is used for project
316specific configuration. The following table explains where the files are
317searched.
318
319.Search paths for configuration files
320[cols="h,a,a,a"]
321|===
322| ^| site-config.jam ^| user-config.jam ^| project-config.jam
323
324.^| Linux
325|
326`/etc`
327
328`$HOME`
329
330`$BOOST_BUILD_PATH`
331|
332`$HOME`
333
334`$BOOST_BUILD_PATH`
335|
336`.`
337
338`..`
339
340`../..`
341
342...
343
344.^| Windows
345|
346`%SystemRoot%`
347
348`%HOMEDRIVE%%HOMEPATH%`
349
350`%HOME%`
351
352`%BOOST_BUILD_PATH%`
353|
354`%HOMEDRIVE%%HOMEPATH%`
355
356`%HOME%`
357
358`%BOOST_BUILD_PATH%`
359|
360`.`
361
362`..`
363
364`../..`
365
366...
367|===
368
369Any of these files may also be overridden
370link:#bbv2.reference.init.options.config[on the command line].
371
372TIP: You can use the `--debug-configuration` option to find which
373configuration files are actually loaded.
374
375Usually, `user-config.jam` just defines the available compilers and
376other tools (see link:#bbv2.recipes.site-config[the section called “Targets
377in site-config.jam”] for more advanced usage). A tool is configured using
378the following syntax:
379
380[source]
381----
382using tool-name : ... ;
383----
384
385The `using` rule is given the name of tool, and will make that tool
386available to B2. For example,
387
388[source]
389----
390using gcc ;
391----
392
393will make the http://gcc.gnu.org[GCC] compiler available.
394
395TIP: You can put `using <tool> ;` with no other argument in a Jamfile
396that needs the `tool`, provided that the `tool` supports this usage.
397In all other cases, the `using` rule should be in a configuration file.
398The general principle is that descriptions in Jamfile should be
399maintained as portable while configuration files are system specific.
400
401All the supported tools are documented in
402link:#bbv2.reference.tools[the section called “Builtin tools”], including the
403specific options they take. Some general notes that apply to most {CPP}
404compilers are below.
405
406For all the {CPP} compiler toolsets that B2 supports
407out-of-the-box, the list of parameters to `using` is the same:
408`toolset-name`, `version`, `invocation-command`, and `options`.
409
410If you have a single compiler, and the compiler executable
411
412* has its “usual name” and is in the `PATH`, or
413* was installed in a standard “installation directory”, or
414* can be found using a global system like the Windows registry.
415
416it can be configured by simply:
417
418[source]
419----
420using tool-name ;
421----
422
423If the compiler is installed in a custom directory, you should provide
424the command that invokes the compiler, for example:
425
426[source]
427----
428using gcc : : g++-3.2 ;
429using msvc : : "Z:/Programs/Microsoft Visual Studio/vc98/bin/cl" ;
430----
431
432Some B2 toolsets will use that path to take additional actions
433required before invoking the compiler, such as calling vendor-supplied
434scripts to set up its required environment variables. When the compiler
435executables for C and {CPP} are different, the path to the {CPP} compiler
436executable must be specified. The command can be any command allowed by
437the operating system. For example:
438
439[source]
440----
441using msvc : : echo Compiling && foo/bar/baz/cl ;
442----
443
444will work.
445
446To configure several versions of a toolset, simply invoke the `using`
447rule multiple times:
448
449[source]
450----
451using gcc : 3.3 ;
452using gcc : 3.4 : g++-3.4 ;
453using gcc : 3.2 : g++-3.2 ;
454using gcc : 5 ;
455using clang : 3.9 ;
456using msvc : 14.0 ;
457----
458
459Note that in the first call to `using`, the compiler found in the `PATH`
460will be used, and there is no need to explicitly specify the command.
461
462Many of toolsets have an `options` parameter to fine-tune the
463configuration. All of B2's standard compiler toolsets accept
464four options `cflags`, `cxxflags`, `compileflags` and `linkflags` as
465`options` specifying flags that will be always passed to the
466corresponding tools. There must not be a space between the tag for the
467option name and the value. Values of the `cflags` feature are passed
468directly to the C compiler, values of the `cxxflags` feature are passed
469directly to the {CPP} compiler, and values of the `compileflags` feature
470are passed to both. For example, to configure a `gcc` toolset so that it
471always generates 64-bit code you could write:
472
473[source]
474----
475using gcc : 3.4 : : <compileflags>-m64 <linkflags>-m64 ;
476----
477
478If multiple of the same type of options are needed, they can be
479concatenated with quotes or have multiple instances of the option tag.
480
481[source]
482----
483using gcc : 5 : : <cxxflags>"-std=c++14 -O2" ;
484using clang : 3.9 : : <cxxflags>-std=c++14 <cxxflags>-O2 ;
485----
486
487Multiple variations of the same tool can be used for most tools. These
488are delineated by the version passed in. Because the dash '-' cannot be
489used here, the convention has become to use the tilde '~' to delineate
490variations.
491
492[source]
493----
494using gcc : 5 : g++-5 : ; # default is C++ 98
495using gcc : 5~c++03 : g++-5 : <cxxflags>-std=c++03 ; # C++ 03
496using gcc : 5~gnu03 : g++-5 : <cxxflags>-std=gnu++03 ; # C++ 03 with GNU
497using gcc : 5~c++11 : g++-5 : <cxxflags>-std=c++11 ; # C++ 11
498using gcc : 5~c++14 : g++-5 : <cxxflags>-std=c++14 ; # C++ 14
499----
500
501These are then used as normal toolsets:
502
503[source,shell]
504----
505b2 toolset=gcc-5 stage
506b2 toolset=gcc-5~c++14 stage
507----
508
509WARNING: Although the syntax used to specify toolset options is very similar
510to syntax used to specify requirements in Jamfiles, the toolset options are
511not the same as features. Don't try to specify a feature value in
512toolset initialization.
513
514[[bbv2.overview.invocation]]
515== Invocation
516
517To invoke B2, type `b2` on the command line. Three kinds of
518command-line tokens are accepted, in any order:
519
520options::
521  Options start with either one or two dashes. The standard options are
522  listed below, and each project may add additional options
523properties::
524  Properties specify details of what you want to build (e.g. debug or
525  release variant). Syntactically, all command line tokens with an equal
526  sign in them are considered to specify properties. In the simplest
527  form, a property looks like `feature=value`
528target::
529  All tokens that are neither options nor properties specify what
530  targets to build. The available targets entirely depend on the project
531  you are building.
532
533[[bbv2.overview.invocation.examples]]
534=== Examples
535
536To build all targets defined in the Jamfile in the current directory
537with the default properties, run:
538
539[source,shell]
540----
541b2
542----
543
544To build specific targets, specify them on the command line:
545
546[source,shell]
547----
548b2 lib1 subproject//lib2
549----
550
551To request a certain value for some property, add `property=value` to the
552command line:
553
554[source,shell]
555----
556b2 toolset=gcc variant=debug optimization=space
557----
558
559[[bbv2.overview.invocation.options]]
560=== Options
561
562B2 recognizes the following command line options.
563
564[[bbv2.overview.invocation.options.help]]`--help`::
565  Invokes the online help system. This prints general information on how
566  to use the help system with additional --help* options.
567`--clean`::
568  Cleans all targets in the current directory and in any sub-projects.
569  Note that unlike the `clean` target in make, you can use `--clean`
570  together with target names to clean specific targets.
571`--clean-all`::
572  Cleans all targets, no matter where they are defined. In particular,
573  it will clean targets in parent Jamfiles, and targets defined under
574  other project roots.
575`--build-dir`::
576  Changes the build directories for all project roots being built. When
577  this option is specified, all Jamroot files must declare a project
578  name. The build directory for the project root will be computed by
579  concatenating the value of the `--build-dir` option, the project name
580  specified in Jamroot, and the build dir specified in Jamroot (or
581  `bin`, if none is specified).
582  The option is primarily useful when building from read-only media,
583  when you can't modify Jamroot.
584`--abbreviate-paths`::
585  Compresses target paths by abbreviating each component. This option is
586  useful to keep paths from becoming longer than the filesystem
587  supports. See also link:#bbv2.reference.buildprocess.targetpath[the
588  section called “Target Paths”].
589`--hash`::
590  Compresses target paths using an MD5 hash. This option is useful to
591  keep paths from becoming longer than the filesystem supports. This
592  option produces shorter paths than `--abbreviate-paths` does, but at the
593  cost of making them less understandable. See also
594  link:#bbv2.reference.buildprocess.targetpath[the section called “Target
595  Paths”].
596`--version`::
597  Prints information on the B2 and Boost.Jam versions.
598`-a`::
599  Causes all files to be rebuilt.
600`-n`::
601  Do not execute the commands, only print them.
602`-q`::
603  Stop at the first error, as opposed to continuing to build targets
604  that don't depend on the failed ones.
605`-j N`::
606  Run up to N commands in parallel. Default number of jobs is the number
607  of detected available CPU threads. Note: There are circumstances when that
608  default can be larger than the allocated cpu resources, for instance in some
609  virtualized container installs.
610`--config=filename`[[bbv2.reference.init.options.config]]::
611  Override all link:#bbv2.overview.configuration[configuration files]
612`--site-config=filename`::
613  Override the default link:#bbv2.overview.configuration[site-config.jam]
614`--user-config=filename`::
615  Override the default link:#bbv2.overview.configuration[user-config.jam]
616`--project-config=filename`::
617  Override the default link:#bbv2.overview.configuration[project-config.jam]
618`--debug-configuration`::
619  Produces debug information about the loading of B2 and
620  toolset files.
621`--debug-building`::
622  Prints what targets are being built and with what properties.
623`--debug-generators`::
624  Produces debug output from the generator search process. Useful for
625  debugging custom generators.
626`-d0`::
627  Suppress all informational messages.
628`-d N`::
629  Enable cumulative debugging levels from 1 to n. Values are:
630  +
631  1.  Show the actions taken for building targets, as they are executed
632  (the default).
633  2.  Show "quiet" actions and display all action text, as they are
634  executed.
635  3.  Show dependency analysis, and target/source timestamps/paths.
636  4.  Show arguments and timing of shell invocations.
637  5.  Show rule invocations and variable expansions.
638  6.  Show directory/header file/archive scans, and attempts at binding
639  to targets.
640  7.  Show variable settings.
641  8.  Show variable fetches, variable expansions, and evaluation of
642  '"if"' expressions.
643  9.  Show variable manipulation, scanner tokens, and memory usage.
644  10. Show profile information for rules, both timing and memory.
645  11. Show parsing progress of Jamfiles.
646  12. Show graph of target dependencies.
647  13. Show change target status (fate).
648`-d +N`::
649  Enable debugging level `N`.
650`-o file`::
651  Write the updating actions to the specified file instead of running
652  them.
653`-s var=value`::
654  Set the variable `var` to `value` in the global scope of the jam language
655  interpreter, overriding variables imported from the environment.
656
657[[bbv2.overview.invocation.properties]]
658=== Properties
659
660In the simplest case, the build is performed with a single set of
661properties, that you specify on the command line with elements in the
662form `feature=value`. The complete list of features can be found in
663link:#bbv2.overview.builtins.features[the section called “Builtin features”].
664The most common features are summarized below.
665
666[cols=",,a",options="header"]
667|===
668|Feature |Allowed values |Notes
669
670|variant
671|debug,release
672|
673
674|link
675|shared,static
676|Determines if B2 creates shared or static libraries
677
678|threading
679|single,multi
680|Cause the produced binaries to be thread-safe. This requires proper support
681in the source code itself.
682
683|address-model
684|32,64
685|Explicitly request either 32-bit or 64-bit code
686generation. This typically requires that your compiler is appropriately
687configured. Please refer to
688link:#bbv2.reference.tools.compilers[the section called “C++ Compilers”]
689and your compiler documentation in case of problems.
690
691|toolset
692|(Depends on configuration)
693|The {CPP} compiler to use. See
694link:#bbv2.reference.tools.compilers[the section called “C++ Compilers”]
695for a detailed list.
696
697|include
698|(Arbitrary string)
699|Additional include paths for C and {CPP} compilers.
700
701|define
702|(Arbitrary string)
703|Additional macro definitions for C and {CPP} compilers. The string should be
704either `SYMBOL` or `SYMBOL=VALUE`
705
706|cxxflags
707|(Arbitrary string)
708|Custom options to pass to the {CPP} compiler.
709
710|cflags
711|(Arbitrary string)
712|Custom options to pass to the C compiler.
713
714|linkflags
715|(Arbitrary string)
716|Custom options to pass to the {CPP} linker.
717
718|runtime-link
719|shared,static
720|Determines if shared or static version of C and {CPP} runtimes should be used.
721|===
722
723If you have more than one version of a given {CPP} toolset (e.g.
724configured in `user-config.jam`, or autodetected, as happens with msvc),
725you can request the specific version by passing `toolset-version` as the
726value of the `toolset` feature, for example `toolset=msvc-8.0`.
727
728If a feature has a fixed set of values it can be specified more than
729once on the command line. In which case, everything will be built
730several times -- once for each specified value of a feature. For
731example, if you use
732
733[source,shell]
734----
735b2 link=static link=shared threading=single threading=multi
736----
737
738Then a total of 4 builds will be performed. For convenience, instead of
739specifying all requested values of a feature in separate command line
740elements, you can separate the values with commas, for example:
741
742[source,shell]
743----
744b2 link=static,shared threading=single,multi
745----
746
747The comma has this special meaning only if the feature has a fixed set
748of values, so
749
750[source,shell]
751----
752b2 include=static,shared
753----
754
755is not treated specially.
756
757Multiple features may be grouped by using a forwards slash.
758
759[source,shell]
760----
761b2 gcc/link=shared msvc/link=static,shared
762----
763
764This will build 3 different variants, altogether.
765
766[[bbv2.overview.invocation.targets]]
767=== Targets
768
769All command line elements that are neither options nor properties are
770the names of the targets to build. See link:#bbv2.reference.ids[the section
771called “Target identifiers and references”]. If no target is specified, the
772project in the current directory is built.
773
774[[bbv2.overview.targets]]
775== Declaring Targets
776
777[[bbv2.overview.targets.main]]
778A Main target is a user-defined named entity that can be built, for
779example an executable file. Declaring a main target is usually done
780using one of the main target rules described in
781link:#bbv2.reference.rules[the section called “Builtin rules”]. The user can
782also declare custom main target rules as shown in
783link:#bbv2.extending.rules[the section called “Main target rules”].
784
785anchor:bbv2.main-target-rule-syntax[]
786Most main target rules in B2 have the same common signature:
787
788[source]
789----
790rule rule-name (
791     main-target-name :
792     sources + :
793     requirements * :
794     default-build * :
795     usage-requirements * )
796----
797
798* `main-target-name` is the name used to request the target on command
799line and to use it from other main targets. A main target name may
800contain alphanumeric characters, dashes (‘`-`’), and underscores
801(‘`_`’).
802* `sources` is the list of source files and other main targets that must
803be combined.
804* `requirements` is the list of properties that must always be present
805when this main target is built.
806* `default-build` is the list of properties that will be used unless
807some other value of the same feature is already specified, e.g. on the
808command line or by propagation from a dependent target.
809* `usage-requirements` is the list of properties that will be propagated
810to all main targets that use this one, i.e. to all its dependents.
811
812Some main target rules have a different list of parameters as explicitly
813stated in their documentation.
814
815The actual requirements for a target are obtained by refining the
816requirements of the project where the target is declared with the
817explicitly specified requirements. The same is true for
818usage-requirements. More details can be found in
819link:#bbv2.reference.variants.proprefine[the section called “Property refinement”].
820
821=== Name
822
823The name of main target has two purposes. First, it's used to refer to
824this target from other targets and from command line. Second, it's used
825to compute the names of the generated files. Typically, filenames are
826obtained from main target name by appending system-dependent suffixes
827and prefixes.
828
829The name of a main target can contain alphanumeric characters, dashes,
830underscores and dots. The entire name is significant when resolving
831references from other targets. For determining filenames, only the part
832before the first dot is taken. For example:
833
834[source]
835----
836obj test.release : test.cpp : <variant>release ;
837obj test.debug : test.cpp : <variant>debug ;
838----
839
840will generate two files named `test.obj` (in two different directories),
841not two files named `test.release.obj` and `test.debug.obj`.
842
843=== Sources
844
845The list of sources specifies what should be processed to get the
846resulting targets. Most of the time, it's just a list of files.
847Sometimes, you'll want to automatically construct the list of source
848files rather than having to spell it out manually, in which case you can
849use the link:#bbv2.reference.rules.glob[glob] rule. Here are two
850examples:
851
852[source]
853----
854exe a : a.cpp ; <1>
855exe b : [ glob *.cpp ] ; <2>
856----
857<1> `a.cpp` is the only source file
858<2> all `.cpp` files in this directory are sources
859
860Unless you specify a file with an absolute path, the name is considered
861relative to the source directory — which is typically the directory
862where the Jamfile is located, but can be changed as described in
863link:#bbv2.overview.projects.attributes.projectrule[the section called “Projects”].
864
865The list of sources can also refer to other main targets. Targets in the
866same project can be referred to by name, while targets in other projects
867must be qualified with a directory or a symbolic project name. The
868directory/project name is separated from the target name by a double
869forward slash. There is no special syntax to distinguish the directory
870name from the project name—the part before the double slash is first
871looked up as project name, and then as directory name. For example:
872
873[source]
874----
875lib helper : helper.cpp ;
876exe a : a.cpp helper ;
877exe b : b.cpp ..//utils ; <1>
878exe c : c.cpp /boost/program_options//program_options ;
879----
880<1> Since all project ids start with slash, "`..`" is a directory name.
881
882The first exe uses the library defined in the same project. The second
883one uses some target (most likely a library) defined by a Jamfile one
884level higher. Finally, the third target uses a http://boost.org[{CPP}
885Boost] library, referring to it using its absolute symbolic name. More
886information about target references can be found in
887link:#bbv2.tutorial.libs[the section called “Dependent Targets”] and
888link:#bbv2.reference.ids[the section called “Target identifiers and references”].
889
890[[bbv2.overview.targets.requirements]]
891=== Requirements
892
893Requirements are the properties that should always be present when
894building a target. Typically, they are includes and defines:
895
896[source]
897----
898exe hello : hello.cpp : <include>/opt/boost <define>MY_DEBUG ;
899----
900
901There are a number of other features, listed in
902link:#bbv2.overview.builtins.features[the section called “Builtin features”].
903For example if a library can only be built statically, or a file can't be
904compiled with optimization due to a compiler bug, one can use.
905
906[source]
907----
908lib util : util.cpp : <link>static ;
909obj main : main.cpp : <optimization>off ;
910----
911
912[[bbv2.overview.targets.requirements.conditional]]Sometimes, particular
913relationships need to be
914maintained among a target's build properties. This can be achieved with
915_conditional requirements_. For example, you might want to set specific
916`#defines` when a library is built as shared, or when a target's
917`release` variant is built in release mode.
918
919[source]
920----
921lib network : network.cpp
922    : <link>shared:<define>NETWORK_LIB_SHARED
923     <variant>release:<define>EXTRA_FAST
924    ;
925----
926
927In the example above, whenever `network` is built with `<link>shared`,
928`<define>NETWORK_LIB_SHARED` will be in its properties, too.
929
930You can use several properties in the condition, for example:
931
932[source]
933----
934lib network : network.cpp
935    : <toolset>gcc,<optimization>speed:<define>USE_INLINE_ASSEMBLER
936    ;
937----
938
939A more powerful variant of conditional requirements
940is _indirect conditional_ requirements. You can provide a rule that will
941be called with the current build properties and can compute additional
942properties to be added. For example:
943
944[source]
945----
946lib network : network.cpp
947    : <conditional>@my-rule
948    ;
949rule my-rule ( properties * )
950{
951    local result ;
952    if <toolset>gcc <optimization>speed in $(properties)
953    {
954        result += <define>USE_INLINE_ASSEMBLER ;
955    }
956    return $(result) ;
957}
958----
959
960This example is equivalent to the previous one, but for complex cases,
961indirect conditional requirements can be easier to write and understand.
962
963Requirements explicitly specified for a target are usually combined with
964the requirements specified for the containing project. You can cause a
965target to completely ignore a specific project requirement using the
966syntax by adding a minus sign before the property, for example:
967
968[source]
969----
970exe main : main.cpp : -<define>UNNECESSARY_DEFINE ;
971----
972
973This syntax is the only way to ignore free properties, such as defines,
974from a parent. It can be also useful for ordinary properties. Consider
975this example:
976
977[source]
978----
979project test : requirements <threading>multi ;
980exe test1 : test1.cpp ;
981exe test2 : test2.cpp : <threading>single ;
982exe test3 : test3.cpp : -<threading>multi ;
983----
984
985Here, `test1` inherits the project requirements and will always be built
986in multi-threaded mode. The `test2` target _overrides_ the project's
987requirements and will always be built in single-threaded mode. In
988contrast, the `test3` target _removes_ a property from the project
989requirements and will be built either in single-threaded or
990multi-threaded mode depending on which variant is requested by the user.
991
992Note that the removal of requirements is completely textual: you need to
993specify exactly the same property to remove it.
994
995=== Default Build
996
997The `default-build` parameter is a set of properties to be used if the
998build request does not otherwise specify a value for features in the
999set. For example:
1000
1001[source]
1002----
1003exe hello : hello.cpp : : <threading>multi ;
1004----
1005
1006would build a multi-threaded target unless the user explicitly requests
1007a single-threaded version. The difference between the requirements and
1008the default-build is that the requirements cannot be overridden in any
1009way.
1010
1011=== Additional Information
1012
1013The ways a target is built can be so different that describing them
1014using conditional requirements would be hard. For example, imagine that
1015a library actually uses different source files depending on the toolset
1016used to build it. We can express this situation using _target alternatives_:
1017
1018[source]
1019----
1020lib demangler : dummy_demangler.cpp ;                # alternative 1
1021lib demangler : demangler_gcc.cpp : <toolset>gcc ;   # alternative 2
1022lib demangler : demangler_msvc.cpp : <toolset>msvc ; # alternative 3
1023----
1024
1025In the example above, when built with `gcc` or `msvc`, `demangler` will
1026use a source file specific to the toolset. Otherwise, it will use a
1027generic source file, `dummy_demangler.cpp`.
1028
1029It is possible to declare a target inline, i.e. the "sources" parameter
1030may include calls to other main rules. For example:
1031
1032[source]
1033----
1034exe hello : hello.cpp
1035    [ obj helpers : helpers.cpp : <optimization>off ] ;
1036----
1037
1038Will cause "helpers.cpp" to be always compiled without optimization.
1039When referring to an inline main target, its declared name must be
1040prefixed by its parent target's name and two dots. In the example above,
1041to build only helpers, one should run `b2 hello..helpers`.
1042
1043When no target is requested on the command line, all targets in the
1044current project will be built. If a target should be built only by
1045explicit request, this can be expressed by the
1046link:#bbv2.reference.rules.explicit[explicit] rule:
1047
1048[source]
1049----
1050explicit install_programs ;
1051----
1052
1053[[bbv2.overview.projects]]
1054== Projects
1055
1056As mentioned before, targets are grouped into projects, and each Jamfile
1057is a separate project. Projects are useful because they allow us to
1058group related targets together, define properties common to all those
1059targets, and assign a symbolic name to the project that can be used in
1060referring to its targets.
1061
1062Projects are named using the `project` rule, which has the following
1063syntax:
1064
1065[source]
1066----
1067project id : attributes ;
1068----
1069
1070Here, _attributes_ is a sequence of rule arguments, each of which begins
1071with an attribute-name and is followed by any number of build
1072properties. The list of attribute names along with its handling is also
1073shown in the table below. For example, it is possible to write:
1074
1075[source]
1076----
1077project tennis
1078    : requirements <threading>multi
1079    : default-build release
1080    ;
1081----
1082
1083The possible attributes are listed below.
1084
1085_Project id_ is a short way to denote a project, as opposed to the
1086Jamfile's pathname. It is a hierarchical path, unrelated to filesystem,
1087such as "boost/thread". link:#bbv2.reference.ids[Target references] make
1088use of project ids to specify a target.
1089
1090_Source location_ specifies the directory where sources for the project
1091are located.
1092
1093_Project requirements_ are requirements that apply to all the targets in
1094the projects as well as all sub-projects.
1095
1096_Default build_ is the build request that should be used when no build
1097request is specified explicitly.
1098
1099[[bbv2.overview.projects.attributes.projectrule]]
1100The default values for those attributes are given in the table below.
1101
1102[cols=",,,",options="header",]
1103|===
1104|Attribute |Name |Default value |Handling by the `project` rule
1105
1106|Project id |none |none |Assigned from the first parameter of the
1107'project' rule. It is assumed to denote absolute project id.
1108
1109|Source location |`source-location` |The location of jamfile for the
1110project |Sets to the passed value
1111
1112|Requirements |`requirements` |The parent's requirements |The parent's
1113requirements are refined with the passed requirement and the result is
1114used as the project requirements.
1115
1116|Default build |`default-build` |none |Sets to the passed value
1117
1118|Build directory |`build-dir` |Empty if the parent has no build
1119directory set. Otherwise, the parent's build directory with the relative
1120path from parent to the current project appended to it. |Sets to the
1121passed value, interpreted as relative to the project's location.
1122|===
1123
1124Besides defining projects and main targets, Jamfiles often invoke
1125various utility rules. For the full list of rules that can be directly
1126used in Jamfile see
1127link:#bbv2.reference.rules[the section called “Builtin rules”].
1128
1129Each subproject inherits attributes, constants and rules from its parent
1130project, which is defined by the nearest Jamfile in an ancestor
1131directory above the subproject. The top-level project is declared in a
1132file called `Jamroot`, or `Jamfile`. When loading a project,
1133B2 looks for either `Jamroot` or `Jamfile`. They are handled
1134identically, except that if the file is called `Jamroot`, the search for
1135a parent project is not performed. A `Jamfile` without a parent project
1136is also considered the top-level project.
1137
1138Even when building in a subproject directory, parent project files are
1139always loaded before those of their sub-projects, so that every
1140definition made in a parent project is always available to its children.
1141The loading order of any other projects is unspecified. Even if one
1142project refers to another via the `use-project` or a target reference,
1143no specific order should be assumed.
1144
1145NOTE: Giving the root project the special name “`Jamroot`” ensures that
1146B2 won't misinterpret a directory above it as the project root
1147just because the directory contains a Jamfile.
1148
1149[[bbv2.overview.build_process]]
1150== The Build Process
1151
1152When you've described your targets, you want B2 to run the
1153right tools and create the needed targets. This section will describe
1154two things: how you specify what to build, and how the main targets are
1155actually constructed.
1156
1157The most important thing to note is that in B2, unlike other
1158build tools, the targets you declare do not correspond to specific
1159files. What you declare in a Jamfile is more like a “metatarget.”
1160Depending on the properties you specify on the command line, each
1161metatarget will produce a set of real targets corresponding to the
1162requested properties. It is quite possible that the same metatarget is
1163built several times with different properties, producing different
1164files.
1165
1166TIP: This means that for B2, you cannot directly obtain a build
1167variant from a Jamfile. There could be several variants requested by the
1168user, and each target can be built with different properties.
1169
1170[[bbv2.overview.build_request]]
1171=== Build Request
1172
1173The command line specifies which targets to build and with which
1174properties. For example:
1175
1176[source,shell]
1177----
1178b2 app1 lib1//lib1 toolset=gcc variant=debug optimization=full
1179----
1180
1181would build two targets, "app1" and "lib1//lib1" with the specified
1182properties. You can refer to any targets, using
1183link:#bbv2.reference.ids[target id] and specify arbitrary properties.
1184Some of the properties are very common, and for them the name of the
1185property can be omitted. For example, the above can be written as:
1186
1187[source,shell]
1188----
1189b2 app1 lib1//lib1 gcc debug optimization=full
1190----
1191
1192The complete syntax, which has some additional shortcuts, is described
1193in link:#bbv2.overview.invocation[the section called “Invocation”].
1194
1195=== Building a main target
1196
1197When you request, directly or indirectly, a build of a main target with
1198specific requirements, the following steps are done. Some brief
1199explanation is provided, and more details are given in
1200link:#bbv2.reference.buildprocess[the section called “Build process”].
1201
12021.  Applying default build. If the default-build property of a target
1203specifies a value of a feature that is not present in the build request,
1204that value is added.
12052.  Selecting the main target alternative to use. For each alternative
1206we look how many properties are present both in alternative's
1207requirements, and in build request. The alternative with largest number
1208of matching properties is selected.
12093.  Determining "common" properties. The build request is
1210link:#bbv2.reference.variants.proprefine[refined] with target's
1211requirements. The conditional properties in requirements are handled as
1212well. Finally, default values of features are added.
12134.  Building targets referred by the sources list and dependency
1214properties. The list of sources and the properties can refer to other
1215target using link:#bbv2.reference.ids[target references]. For each
1216reference, we take all
1217link:#bbv2.reference.features.attributes.propagated[propagated]
1218properties, refine them by explicit properties specified in the target
1219reference, and pass the resulting properties as build request to the
1220other target.
12215.  Adding the usage requirements produced when building dependencies to
1222the "common" properties. When dependencies are built in the previous
1223step, they return both the set of created "real" targets, and usage
1224requirements. The usage requirements are added to the common properties
1225and the resulting property set will be used for building the current
1226target.
12276.  Building the target using generators. To convert the sources to the
1228desired type, B2 uses "generators" -- objects that correspond
1229to tools like compilers and linkers. Each generator declares what type
1230of targets it can produce and what type of sources it requires. Using
1231this information, B2 determines which generators must be run to
1232produce a specific target from specific sources. When generators are
1233run, they return the "real" targets.
12347.  Computing the usage requirements to be returned. The conditional
1235properties in usage requirements are expanded and the result is
1236returned.
1237
1238=== Building a Project
1239
1240Often, a user builds a complete project, not just one main target. In
1241fact, invoking `b2` without arguments builds the project defined in the
1242current directory.
1243
1244When a project is built, the build request is passed without
1245modification to all main targets in that project. It's is possible to
1246prevent implicit building of a target in a project with the `explicit`
1247rule:
1248
1249[source]
1250----
1251explicit hello_test ;
1252----
1253
1254would cause the `hello_test` target to be built only if explicitly
1255requested by the user or by some other target.
1256
1257The Jamfile for a project can include a number of `build-project` rule
1258calls that specify additional projects to be built.
1259