• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[[bbv2.tasks]]
2= Common tasks
3
4This section describes main targets types that B2 supports
5out-of-the-box. Unless otherwise noted, all mentioned main target rules
6have the common signature, described in
7link:#bbv2.overview.targets[the section called “Declaring Targets”].
8
9[[bbv2.tasks.programs]]
10== Programs
11
12Programs are created using the `exe` rule, which follows the
13link:#bbv2.main-target-rule-syntax[common syntax]. For example:
14
15[source]
16----
17exe hello
18    : hello.cpp some_library.lib /some_project//library
19    : <threading>multi
20    ;
21----
22
23This will create an executable file from the sources--in this case,
24one {CPP} file, one library file present in the same directory, and
25another library that is created by B2. Generally, sources can
26include C and {CPP} files, object files and libraries. B2 will
27automatically try to convert targets of other types.
28
29TIP: On Windows, if an application uses shared libraries, and both the
30application and the libraries are built using B2, it is not
31possible to immediately run the application, because the `PATH` environment
32variable should include the path to the libraries. It means you have to either
33add the paths manually, or have the build place the application and the
34libraries into the same directory. See
35link:#bbv2.tasks.installing[the section called “Installing”].
36
37[[bbv2.tasks.libraries]]
38== Libraries
39
40Library targets are created using the `lib` rule, which follows the
41link:#bbv2.main-target-rule-syntax[common syntax]. For example:
42
43[source]
44----
45lib helpers : helpers.cpp ;
46----
47
48This will define a library target named `helpers` built from the
49`helpers.cpp` source file. It can be either a static library or a shared
50library, depending on the value of the
51link:#bbv2.builtin.features.link[`<link>`] feature.
52
53Library targets can represent:
54
55* Libraries that should be built from source, as in the example above.
56* Prebuilt libraries which already exist on the system. Such libraries
57can be searched for by the tools using them (typically with the linker's
58`-l` option) or their paths can be known in advance by the build system.
59
60The syntax for prebuilt libraries is given below:
61
62[source]
63----
64lib z : : <name>z <search>/home/ghost ;
65lib compress : : <file>/opt/libs/compress.a ;
66----
67
68The `name` property specifies the name of the library without the
69standard prefixes and suffixes. For example, depending on the system,
70`z` could refer to a file called `z.so`, `libz.a`, or `z.lib`, etc. The
71`search` feature specifies paths in which to search for the library in
72addition to the default compiler paths. `search` can be specified
73several times or it can be omitted, in which case only the default
74compiler paths will be searched. The `file` property specifies the file
75location.
76
77The difference between using the `file` feature and using a combination
78of the `name` and `search` features is that `file` is more precise.
79
80[WARNING]
81====
82The value of the `search` feature is just added to the linker search
83path. When linking to multiple libraries, the paths specified by
84`search` are combined without regard to which `lib` target each path
85came from. Thus, given
86
87[source]
88----
89lib a : : <name>a <search>/pool/release ;
90lib b : : <name>b <search>/pool/debug ;
91----
92
93If /pool/release/a.so, /pool/release/b.so, /pool/debug/a.so, and
94/pool/release/b.so all exist, the linker will probably take both `a` and
95`b` from the same directory, instead of finding `a` in /pool/release and
96`b` in /pool/debug. If you need to distinguish between multiple
97libraries with the same name, it's safer to use `file`.
98====
99
100For convenience, the following syntax is allowed:
101
102[source]
103----
104lib z ;
105lib gui db aux ;
106----
107
108which has exactly the same effect as:
109
110[source]
111----
112lib z : : <name>z ;
113lib gui : : <name>gui ;
114lib db : : <name>db ;
115lib aux : : <name>aux ;
116----
117
118When a library references another library you should put that other
119library in its list of sources. This will do the right thing in all
120cases. For portability, you should specify library dependencies even for
121searched and prebuilt libraries, otherwise, static linking on Unix will
122not work. For example:
123
124[source]
125----
126lib z ;
127lib png : z : <name>png ;
128----
129
130[NOTE]
131====
132When a library has a shared library as a source, or a static library has
133another static library as a source then any target linking to the first
134library with automatically link to its source library as well.
135
136On the other hand, when a shared library has a static library as a
137source then the first library will be built so that it completely
138includes the second one.
139
140If you do not want a shared library to include all the libraries
141specified in its sources (especially statically linked ones), you would
142need to use the following:
143
144[source]
145----
146lib b : a.cpp ;
147lib a : a.cpp : <use>b : : <library>b ;
148----
149
150This specifies that library `a` uses library `b`, and causes all
151executables that link to `a` to link to `b` also. In this case, even for
152shared linking, the `a` library will not refer to `b`.
153====
154
155link:#bbv2.overview.targets[Usage requirements] are often very useful
156for defining library targets. For example, imagine that you want you
157build a `helpers` library and its interface is described in its
158`helpers.hpp` header file located in the same directory as the
159`helpers.cpp` source file. Then you could add the following to the
160Jamfile located in that same directory:
161
162[source]
163----
164lib helpers : helpers.cpp : : : <include>. ;
165----
166
167which would automatically add the directory where the target has been
168defined (and where the library's header file is located) to the
169compiler's include path for all targets using the `helpers` library.
170This feature greatly simplifies Jamfiles.
171
172[[bbv2.tasks.alias]]
173== Alias
174
175The `alias` rule gives an alternative name to a group of targets. For
176example, to give the name `core` to a group of three other targets with
177the following code:
178
179[source]
180----
181alias core : im reader writer ;
182----
183
184Using `core` on the command line, or in the source list of any other
185target is the same as explicitly using `im`, `reader`, and `writer`.
186
187Another use of the `alias` rule is to change build properties. For
188example, if you want to link statically to the Boost Threads
189library, you can write the following:
190
191[source]
192----
193alias threads : /boost/thread//boost_thread : <link>static ;
194----
195
196and use only the `threads` alias in your Jamfiles.
197
198You can also specify usage requirements for the `alias` target. If you
199write the following:
200
201[source]
202----
203alias header_only_library : : : :  <include>/usr/include/header_only_library ;
204----
205
206then using `header_only_library` in sources will only add an include
207path. Also note that when an alias has sources, their usage requirements
208are propagated as well. For example:
209
210[source]
211----
212lib library1 : library1.cpp : : : <include>/library/include1 ;
213lib library2 : library2.cpp : : : <include>/library/include2 ;
214alias static_libraries : library1 library2 : <link>static ;
215exe main : main.cpp static_libraries ;
216----
217
218will compile `main.cpp` with additional includes required for using the
219specified static libraries.
220
221[[bbv2.tasks.installing]]
222== Installing
223
224This section describes various ways to install built targets and
225arbitrary files.
226
227=== Basic install
228
229For installing a built target you should use the `install` rule, which
230follows the link:#bbv2.main-target-rule-syntax[common syntax]. For
231example:
232
233[source]
234----
235install dist : hello helpers ;
236----
237
238will cause the targets `hello` and `helpers` to be moved to the `dist`
239directory, relative to the Jamfile's directory. The directory can be
240changed using the `location` property:
241
242[source]
243----
244install dist : hello helpers : <location>/usr/bin ;
245----
246
247While you can achieve the same effect by changing the target name to
248`/usr/bin`, using the `location` property is better as it allows you to
249use a mnemonic target name.
250
251The `location` property is especially handy when the location is not
252fixed, but depends on the build variant or environment variables:
253
254[source]
255----
256install dist : hello helpers :
257    <variant>release:<location>dist/release
258    <variant>debug:<location>dist/debug ;
259install dist2 : hello helpers : <location>$(DIST) ;
260----
261
262See also link:#bbv2.reference.variants.propcond[conditional properties]
263and link:#bbv2.faq.envar[environment variables]
264
265=== Installing with all dependencies
266
267Specifying the names of all libraries to install can be boring. The
268`install` allows you to specify only the top-level executable targets to
269install, and automatically install all dependencies:
270
271[source]
272----
273install dist : hello :
274    <install-dependencies>on <install-type>EXE
275    <install-type>LIB
276    ;
277----
278
279will find all targets that `hello` depends on, and install all of those
280which are either executables or libraries. More specifically, for each
281target, other targets that were specified as sources or as dependency
282properties, will be recursively found. One exception is that targets
283referred with the link:#bbv2.builtin.features.use[`use`] feature are not
284considered, as that feature is typically used to refer to header-only
285libraries. If the set of target types is specified, only targets of that
286type will be installed, otherwise, all found target will be installed.
287
288=== Preserving Directory Hierarchy
289
290By default, the `install` rule will strip paths from its sources. So, if
291sources include `a/b/c.hpp`, the `a/b` part will be ignored. To make the
292`install` rule preserve the directory hierarchy you need to use the
293`<install-source-root>` feature to specify the root of the hierarchy you
294are installing. Relative paths from that root will be preserved. For
295example, if you write:
296
297[source]
298----
299install headers
300    : a/b/c.h
301    : <location>/tmp <install-source-root>a
302    ;
303----
304
305the a file named `/tmp/b/c.h` will be created.
306
307The link:#bbv2.reference.glob-tree[`glob-tree`] rule can be used to find
308all files below a given directory, making it easy to install an entire
309directory tree.
310
311=== Installing into Several Directories
312
313The link:#bbv2.tasks.alias[`alias`] rule can be used when targets need
314to be installed into several directories:
315
316[source]
317----
318alias install : install-bin install-lib ;
319install install-bin : applications : /usr/bin ;
320install install-lib : helper : /usr/lib ;
321----
322
323Because the `install` rule just copies targets, most free features
324footnote:[see the definition of "free" in
325link:#bbv2.reference.features.attributes[the section called “Feature Attributes”].]
326have no effect when used in requirements of the `install` rule. The only two
327that matter are
328link:#bbv2.builtin.features.dependency[`dependency`] and, on Unix,
329link:#bbv2.builtin.features.dll-path[`dll-path`].
330
331NOTE: (Unix specific) On Unix, executables built using B2 typically
332contain the list of paths to all used shared libraries. For installing,
333this is not desired, so B2 relinks the executable with an empty
334list of paths. You can also specify additional paths for installed
335executables using the `dll-path` feature.
336
337[[bbv2.builtins.testing]]
338== Testing
339
340B2 has convenient support for running unit tests. The simplest
341way is the `unit-test` rule, which follows the
342link:#bbv2.main-target-rule-syntax[common syntax]. For example:
343
344[source]
345----
346unit-test helpers_test : helpers_test.cpp helpers ;
347----
348
349The `unit-test` rule behaves like the link:#bbv2.tasks.programs[exe]
350rule, but after the executable is created it is also run. If the
351executable returns an error code, the build system will also return an
352error and will try running the executable on the next invocation until
353it runs successfully. This behavior ensures that you can not miss a
354unit test failure.
355
356There are few specialized testing rules, listed below:
357
358[source]
359----
360rule compile ( sources : requirements * : target-name ? )
361rule compile-fail ( sources : requirements * : target-name ? )
362rule link ( sources + : requirements * : target-name ? )
363rule link-fail ( sources + : requirements * : target-name ? )
364----
365
366They are given a list of sources and requirements. If the target name is
367not provided, the name of the first source file is used instead. The
368`compile*` tests try to compile the passed source. The `link*` rules try
369to compile and link an application from all the passed sources. The
370`compile` and `link` rules expect that compilation/linking succeeds. The
371`compile-fail` and `link-fail` rules expect that the
372compilation/linking fails.
373
374There are two specialized rules for running executables, which are more
375powerful than the `unit-test` rule. The `run` rule has the following
376signature:
377
378[source]
379----
380rule run ( sources + : args * : input-files * : requirements * : target-name ?
381    : default-build * )
382----
383
384The rule builds application from the provided sources and runs it,
385passing `args` and `input-files` as command-line arguments. The `args`
386parameter is passed verbatim and the values of the `input-files`
387parameter are treated as paths relative to containing Jamfile, and are
388adjusted if `b2` is invoked from a different directory. The `run-fail`
389rule is identical to the `run` rule, except that it expects that the run
390fails.
391
392All rules described in this section, if executed successfully, create a
393special manifest file to indicate that the test passed. For the
394`unit-test` rule the files is named `target-name.passed` and for the other
395rules it is called `target-name.test`. The `run*` rules also capture all
396output from the program, and store it in a file named `target-name.output`.
397
398If the `preserve-test-targets` feature has the
399value `off`, then `run` and the `run-fail` rules will remove the
400executable after running it. This somewhat decreases disk space
401requirements for continuous testing environments. The default value of
402`preserve-test-targets` feature is `on`.
403
404It is possible to print the list of all test targets (except for
405`unit-test`) declared in your project, by passing the `--dump-tests`
406command-line option. The output will consist of lines of the form:
407
408[source]
409----
410boost-test(test-type) path : sources
411----
412
413It is possible to process the list of tests, B2 output and the
414presence/absence of the `*.test` files created when test passes into
415human-readable status table of tests. Such processing utilities are not
416included in B2.
417
418The following features adjust behavior of the testing metatargets.
419
420`testing.arg`::
421
422Defines an argument to be passed to the target when it is executed
423before the list of input files.
424+
425[source]
426----
427unit-test helpers_test
428    : helpers_test.cpp helpers
429    : <testing.arg>"--foo bar"
430    ;
431----
432
433`testing.input-file`::
434
435Specifies a file to be passed to the executable on the command line
436after the arguments. All files must be specified in alphabetical order
437due to constraints in the current implementation.
438
439`testing.launcher`::
440
441By default, the executable is run directly. Sometimes, it is desirable
442to run the executable using some helper command. You should use this
443property to specify the name of the helper command. For example, if
444you write:
445+
446[source]
447----
448unit-test helpers_test
449    : helpers_test.cpp helpers
450    : <testing.launcher>valgrind
451    ;
452----
453+
454The command used to run the executable will be:
455+
456[source,shell]
457----
458valgrind bin/$toolset/debug/helpers_test
459----
460
461`test-info`::
462
463A description of the test. This is displayed as part of the
464`--dump-tests` command-line option.
465
466[[bbv2.builtins.raw]]
467== Custom commands
468
469For most main target rules, B2 automatically figures out the
470commands to run. When you want to use new file types or support new
471tools, one approach is to extend B2 to support them smoothly,
472as documented in link:#bbv2.extender[Extender Manual]. However, if the new
473tool is only used in a single place, it might be easier just to specify the
474commands to run explicitly.
475
476Three main target rules can be used for that. The `make` rule allows you to
477construct a single file from any number of source file, by running a command
478you specify. The `notfile` rule allows you to run an arbitrary command,
479without creating any files. And finally, the `generate` rule allows you to
480describe a transformation using B2's virtual targets. This is
481higher-level than the file names that the `make` rule operates with and
482allows you to create more than one target, create differently named targets
483depending on properties, or use more than one tool.
484
485The `make` rule is used when you want to create one file from a number
486of sources using some specific command. The `notfile` is used to
487unconditionally run a command.
488
489Suppose you want to create the file `file.out` from the file `file.in`
490by running the command `in2out`. Here is how you would do this in B2:
491
492[source]
493----
494make file.out : file.in : @in2out ;
495actions in2out
496{
497    in2out $(<) $(>)
498}
499----
500
501If you run `b2` and `file.out` does not exist, B2 will run the
502`in2out` command to create that file. For more details on specifying
503actions, see
504link:#bbv2.overview.jam_language.actions[the section called “Boost.Jam Language”].
505
506It could be that you just want to run some command unconditionally, and
507that command does not create any specific files. For that you can use
508the `notfile` rule. For example:
509
510[source]
511----
512notfile echo_something : @echo ;
513actions echo
514{
515    echo "something"
516}
517----
518
519The only difference from the `make` rule is that the name of the target
520is not considered a name of a file, so B2 will unconditionally
521run the action.
522
523The `generate` rule is used when you want to express transformations
524using B2's virtual targets, as opposed to just filenames. The
525`generate` rule has the standard main target rule signature, but you are
526required to specify the `generating-rule` property. The value of the
527property should be in the form `@_rule-name_`, the named rule should have the
528following signature:
529
530[source]
531----
532rule generating-rule ( project name : property-set : sources * )
533----
534
535and will be called with an instance of the `project-target` class, the
536name of the main target, an instance of the `property-set` class
537containing build properties, and the list of instances of the
538`virtual-target` class corresponding to sources. The rule must return a
539list of `virtual-target` instances. The interface of the
540`virtual-target` class can be learned by looking at the
541`build/virtual-target.jam` file. The `generate` example contained in the
542B2 distribution illustrates how the `generate` rule can be
543used.
544
545[[bbv2.reference.precompiled_headers]]
546== Precompiled Headers
547
548Precompiled headers is a mechanism to speed up compilation by creating a
549partially processed version of some header files, and then using that
550version during compilations rather then repeatedly parsing the original
551headers. B2 supports precompiled headers with gcc and msvc
552toolsets.
553
554To use precompiled headers, follow the following steps:
555
5561. Create a header that includes headers used by your project that you
557want precompiled. It is better to include only headers that are
558sufficiently stable -- like headers from the compiler and external
559libraries. Please wrap the header in `#ifdef BOOST_BUILD_PCH_ENABLED`, so
560that the potentially expensive inclusion of headers is not done when PCH is
561not enabled. Include the new header at the top of your source files.
562
5632. Declare a new B2 target for the precompiled header and add
564that precompiled header to the sources of the target whose compilation
565you want to speed up:
566+
567[source]
568----
569cpp-pch pch : pch.hpp ;
570exe main : main.cpp pch ;
571----
572+
573You can use the `c-pch` rule if you want to use the precompiled header
574in C programs.
575
576The `pch` example in B2 distribution can be used as reference.
577
578Please note the following:
579
580* The inclusion of the precompiled header must be the first thing in a
581source file, before any code or preprocessor directives.
582* The build properties used to compile the source files and the
583precompiled header must be the same. Consider using project requirements
584to assure this.
585* Precompiled headers must be used purely as a way to improve
586compilation time, not to save the number of `#include` statements. If a
587source file needs to include some header, explicitly include it in the
588source file, even if the same header is included from the precompiled
589header. This makes sure that your project will build even if precompiled
590headers are not supported.
591* Prior to version 4.2, the gcc compiler did not allow anonymous
592namespaces in precompiled headers, which limits their utility. See the
593http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29085[bug report] for
594details.
595
596[[bbv2.reference.generated_headers]]
597== Generated headers
598
599Usually, B2 handles implicit dependencies completely
600automatically. For example, for {CPP} files, all `#include` statements are
601found and handled. The only aspect where user help might be needed is
602implicit dependency on generated files.
603
604By default, B2 handles such dependencies within one main
605target. For example, assume that main target "app" has two sources,
606"app.cpp" and "parser.y". The latter source is converted into "parser.c"
607and "parser.h". Then, if "app.cpp" includes "parser.h", B2 will
608detect this dependency. Moreover, since "parser.h" will be generated
609into a build directory, the path to that directory will automatically be
610added to the include path.
611
612Making this mechanism work across main target boundaries is possible,
613but imposes certain overhead. For that reason, if there is implicit
614dependency on files from other main targets, the `<implicit-dependency>`
615feature must be used, for example:
616
617[source]
618----
619lib parser : parser.y ;
620exe app : app.cpp : <implicit-dependency>parser ;
621----
622
623The above example tells the build system that when scanning all sources
624of "app" for implicit-dependencies, it should consider targets from
625"parser" as potential dependencies.
626
627[[bbv2.tasks.crosscompile]]
628== Cross-compilation
629
630B2 supports cross compilation with the gcc and msvc toolsets.
631
632When using gcc, you first need to specify your cross compiler in
633`user-config.jam` (see
634link:#bbv2.overview.configuration[the section called “Configuration”]), for
635example:
636
637[source]
638----
639using gcc : arm : arm-none-linux-gnueabi-g++ ;
640----
641
642After that, if the host and target os are the same, for example Linux,
643you can just request that this compiler version be used:
644
645[source,shell]
646----
647b2 toolset=gcc-arm
648----
649
650If you want to target a different operating system from the host, you
651need to additionally specify the value for the `target-os` feature, for
652example:
653
654[source,bat]
655----
656# On windows box
657b2 toolset=gcc-arm target-os=linux
658# On Linux box
659b2 toolset=gcc-mingw target-os=windows
660----
661
662For the complete list of allowed operating system names, please see the
663documentation for link:#bbv2.builtin.features.target-os[target-os
664feature].
665
666When using the msvc compiler, it's only possible to cross-compile to a
66764-bit system on a 32-bit host. Please see
668link:#bbv2.reference.tools.compiler.msvc.64[the section called “64-bit support”]
669for details.
670
671[[bbv2.tasks.packagemanagers]]
672== Package Managers
673
674B2 support automatic, or manual, loading of generated build files
675from package managers. For example using the Conan package manager which
676generates `conanbuildinfo.jam` files B2 will load that files automatically
677when it loads the project at the same location. The included file can
678define targets and other project declarations in the context of the
679project it's being loaded into. Control over what package manager file
680is loaded can be controlled with (in order of priority):
681
682* With the `use-packages` rule.
683* Command line argument `--use-package-manager=X`.
684* Environment variable `PACKAGE_MANAGER_BUILD_INFO`.
685* Built-in detection of the file. Currently this includes: "conan".
686
687**`use-packages` rule:**
688
689[source]
690----
691rule use-packages ( name-or-glob-pattern ? )
692----
693
694The `use-packages` rule allows one to specify in the projects themselves kind
695of package definitions to use either as the ones for a built-in package
696manager support. For example:
697
698[source]
699----
700use-packages conan ;
701----
702
703Or to specify a `glob` pattern to find the file with the definitions. For
704instance:
705
706[source]
707----
708use-packages "packages.jam" ;
709----
710
711**`--use-package-manager` command line option:**
712
713The `--use-package-manager=NAME` command line option allows one to
714non-intrusively specify per invocation which of the built-in package manager
715types to use.
716
717**`PACKAGE_MANAGER_BUILD_INFO` variable:**
718
719The `PACKAGE_MANAGER_BUILD_INFO` variable, which is taken from the environment
720or defined with the `-sX=Y` option, specifies a `glob` pattern to use to find
721the package definitions.
722
723**Built-in detection:**
724
725There are a number of built-in `glob` patterns to support popular package
726managers. Currently the supported ones are:
727
728* Conan (`conan`): currently supports the
729  link:https://bintray.com/bfgroup/public-conan/b2gen%3Abfgroup[`b2gen`]
730  generator.
731