xref: /oneTBB/cmake/README.md (revision 68e075cb)
1# Build system description
2
3The project uses CMake build configuration.
4
5The following controls are available during the configure stage:
6```
7TBB_TEST:BOOL - Enable testing (ON by default)
8TBB_STRICT:BOOL - Treat compiler warnings as errors (ON by default)
9TBB_SANITIZE:STRING - Sanitizer parameter, passed to compiler/linker
10TBB_SIGNTOOL:FILEPATH - Tool for digital signing, used in post install step for libraries if provided.
11TBB_SIGNTOOL_ARGS:STRING - Additional arguments for TBB_SIGNTOOL, used if TBB_SIGNTOOL is set.
12TBB4PY_BUILD:BOOL - Enable  Intel(R) oneAPI Threading Building Blocks (oneTBB) Python module build (OFF by default)
13TBB_CPF:BOOL - Enable preview features of the library (OFF by default)
14TBB_INSTALL_VARS:BOOL - Enable auto-generated vars installation(packages generated by `cpack` and `make install` will also include the vars script)(OFF by default)
15TBB_VALGRIND_MEMCHECK:BOOL - Enable scan for memory leaks using Valgrind (OFF by default)
16TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH - Disable HWLOC automatic search by pkg-config tool (OFF by default)
17```
18
19## Configure, build and test
20
21### Prerequisites
22
23*  CMake >= 3.1
24
25### Preparation
26
27In order to perform out-of-source build you have to create a build directory and go there:
28
29```bash
30mkdir /tmp/my-build
31cd /tmp/my-build
32```
33
34### Configure
35
36```bash
37cmake <options> <repo_root>
38```
39
40Some useful options:
41- `-G <generator>` - specify particular project generator, see `cmake --help` for details.
42- `-DCMAKE_BUILD_TYPE=Debug` - specify for Debug build, it isn't applicable for multi-config generators, e.g. for Visual Studio generator).
43
44#### TBBBind library configuration
45
46The TBBbind library has three versions: `tbbbind`, `tbbbind_2_0` and `tbbbind_2_5`. Each of these versions is linked with corresponding HWLOC library version: `tbbbind` links with HWLOC 1.11.x , `tbbbind_2_0` links with HWLOC 2.1-2.4, `tbbbind_2_5` links with HWLOC 2.5 and later.
47
48The search for a suitable version of the HWLOC library is enabled by default, but if you want to use a specific version of the library, you can specify the path to it manually using the following CMake variables:
49
50 - `CMAKE_HWLOC_<HWLOC_VER>_LIBRARY_PATH` - path to the corresponding HWLOC version shared library on Linux or path to `.lib` file on Windows.
51 - `CMAKE_HWLOC_<HWLOC_VER>_INCLUDE_PATH` - path to the corresponding HWLOC version includes directory.
52
53Windows requires one additional variable for correct TBBBind library building:
54 - `CMAKE_HWLOC_<HWLOC_VER>_DLL_PATH` - path to the corresponding HWLOC version `.dll` file.
55
56`HWLOC_VER` substring used above can be replaced with one of the three values: `1_11` for the `tbbbind` library configuration, `2` for the `tbbbind_2_0`, and `2_5` for the `tbbbind_2_5` library configuration.
57
58If you specify variables for several TBBBind versions, then building process for all of these versions will be performed during single build session.
59
60Specify the `TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH` to disable HWLOC libraries automatic search.
61
62### Build
63
64```bash
65cmake --build . <options>
66```
67
68Some useful options:
69- `--target <target>` - specific target, "all" is default.
70- `--config <Release|Debug>` - build configuration, applicable only for multi-config generators, e.g. Visual Studio generator.
71
72Binaries are placed to `./<compiler-id>_<compiler-ver>_cxx<stdver>_<build-type>` (for example, `./gnu_4.8_cxx11_release`)
73
74#### Build for 32-bit
75
76* Intel Compiler: just source Intel(R) C++ Compiler with `ia32` and build as usual.
77* MSVC: use switch for [generator](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) (e.g. `-A Win32` for [VS2019](https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2016%202019.html)) during the configuration stage and then build as usual.
78* GCC/Clang: specify `-m32` during the configuration: `CXXFLAGS=-m32 cmake ..` or `cmake -DCMAKE_CXX_FLAGS=-m32 ..`
79* Any other compiler which builds for 64-bit by default: specify 32-bit compiler key during the configuration as above.
80
81#### Windows specific builds (CMake 3.15 or newer is required)
82
83* Dynamic linkage with CRT: default behavior, can be explicitly specified by setting `CMAKE_MSVC_RUNTIME_LIBRARY` to `MultiThreadedDLL` or `MultiThreadedDebugDLL`.
84```bash
85cmake ..  # dynamic linkage is used by default
86```
87```bash
88cmake -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL ..
89```
90```bash
91cmake -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL -DCMAKE_BUILD_TYPE=Debug ..
92```
93* Static linkage with CRT: set `CMAKE_MSVC_RUNTIME_LIBRARY` to `MultiThreaded` or `MultiThreadedDebug`.
94```bash
95cmake -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded ..
96```
97```bash
98cmake -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebug -DCMAKE_BUILD_TYPE=Debug ..
99```
100* Windows 10 Universal Windows application build: set `CMAKE_SYSTEM_NAME` to `WindowsStore` and `CMAKE_SYSTEM_VERSION` to `10.0`.
101
102_Note: set `TBB_NO_APPCONTAINER` to `ON` in order to apply `/APPCONTAINER:NO` option during the compilation (used for testing)._
103```bash
104cmake -DCMAKE_SYSTEM_NAME:STRING=WindowsStore -DCMAKE_SYSTEM_VERSION:STRING=10.0 ..
105```
106* Universal Windows Driver build: set `TBB_WINDOWS_DRIVER` to `ON` and use static linkage with CRT (see above).
107
108```bash
109cmake -DTBB_WINDOWS_DRIVER=ON -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded ..
110```
111
112### Test
113
114#### Build test
115Using default ('all') target:
116```
117cmake --build .
118```
119
120or using specific test target:
121```
122cmake --build . --target <test> # e.g. test_version
123```
124
125#### Run test
126
127Using CTest:
128```bash
129ctest
130```
131
132or using 'test' target:
133```bash
134cmake --build . --target test # currently doesn't work on Windows
135```
136
137## Sanitizers - сonfigure, build and run
138
139```bash
140mkdir build
141cd build
142cmake -DTBB_SANITIZE=thread ..  # or -DTBB_SANITIZE=memory or any other sanitizer
143make -j
144ctest -V
145```
146
147## Valgrind memcheck - configure, build and run
148
149### Prerequisites
150* Valgrind tool executable
151### Example
152```bash
153mkdir build
154cd build
155cmake -DTBB_VALGRIND_MEMCHECK=ON ..
156make -j memcheck-<test name> # or memcheck-all to scan all tests
157```
158
159## Test specification (Doxygen)
160
161```bash
162mkdir build
163cd build
164cmake -DTBB_TEST_SPEC=ON ..
165make test_spec
166```
167
168## Intallation and packaging
169
170**NOTE: be careful about installation: avoid commands like `make install` unless you fully understand the consequences.**
171
172The following install components are supported:
173- `runtime` - oneTBB runtime package (core shared libraries and `.dll` files on Windows).
174- `devel` - oneTBB development package (header files, CMake integration files, library symbolic links and `.lib` files on Windows).
175- `tbb4py` - [oneTBB Module for Python](#onetbb-python-module-support).
176
177How to install specific components after configuration and build:
178
179```bash
180cmake -DCOMPONENT=<component> [-DBUILD_TYPE=<build-type>] -P cmake_install.cmake
181```
182
183Simple packaging using CPack is supported.
184The following commands allow to create a simple portable package which includes header files, libraries and integration files for CMake:
185
186```bash
187cmake <options> ..
188cpack
189```
190
191## TBBConfig - integration of binary packages
192
193It is a configuration module that is used for integration of prebuilt oneTBB. It consists of two files (TBBConfig.cmake and TBBConfigVersion.cmake) and can be used via [find_package](https://cmake.org/cmake/help/latest/command/find_package.html) function.
194
195How to use this module in your CMake project:
196 1. Let CMake know where to search for TBBConfig, e.g. specify location of TBBConfig.cmake in `TBB_DIR` (for more details about search paths see [find_package](https://cmake.org/cmake/help/latest/command/find_package.html)).
197 2. Use [find_package](https://cmake.org/cmake/help/latest/command/find_package.html) to find oneTBB.
198 3. Use provided variables and/or imported targets (described below) to work with the found oneTBB.
199
200Example:
201
202```cmake
203add_executable(foo foo.cpp)
204find_package(TBB)
205target_link_libraries(foo TBB::tbb)
206```
207
208oneTBB components can be passed to [find_package](https://cmake.org/cmake/help/latest/command/find_package.html) after keyword ``COMPONENTS`` or ``REQUIRED``.
209Use basic names of components (`tbb`, `tbbmalloc`, etc.).
210
211If components are not specified then the default set is used: `tbb`, `tbbmalloc` and ``tbbmalloc_proxy``.
212
213If `tbbmalloc_proxy` is requested, `tbbmalloc` component will also be added and set as dependency for `tbbmalloc_proxy`.
214
215TBBConfig creates [imported targets](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#imported-targets>) as
216shared libraries using the following format: `TBB::<component>` (for example, `TBB::tbb`, `TBB::tbbmalloc`).
217
218Set `TBB_FIND_RELEASE_ONLY` to `TRUE` before calling `find_package` in order to search only for release oneTBB version. This variable helps to avoid simultaneous linkage of release and debug oneTBB versions when CMake configuration is `Debug` but a third-party component depends on release oneTBB version.
219
220Variables set during TBB configuration:
221
222Variable | Description
223--- | ---
224`TBB_FOUND`             | oneTBB is found
225`TBB_<component>_FOUND` | Specific oneTBB component is found
226`TBB_VERSION`           | oneTBB version (format: `<major>.<minor>.<patch>.<tweak>`)
227`TBB_IMPORTED_TARGETS`  | All created oneTBB imported targets (not supported for builds from source code)
228
229Starting from [oneTBB 2021.1](https://github.com/oneapi-src/oneTBB/releases/tag/v2021.1) GitHub release TBBConfig files in the binary packages are located under `<tbb-root>/lib/cmake/TBB`.
230For example, `TBB_DIR` should be set to `<tbb-root>/lib/cmake/TBB`.
231
232TBBConfig files are automatically created during the build from source code and can be installed together with the library.
233Also oneTBB provides a helper function that creates TBBConfig files from predefined templates: see `tbb_generate_config` in `cmake/config_generation.cmake`.
234
235## oneTBB Python Module support
236`TBB4PY_BUILD` Cmake option provides ability to build Python module for oneTBB.
237
238### Targets:
239 - `irml` - build IPC RML server
240 - `python_build` - build oneTBB module for Python
241
242`python_build` target requirements:
243 - Python version 3.5 or newer
244 - SWIG version 3.0.6 or newer
245
246## CMake files
247
248### Compile/link options
249
250Compile and link options may be specific for certain compilers. This part is handled in `cmake/compilers/*` files.
251
252Options in TBB CMake are handled via variables in two ways for convenience:
253
254*  by options group
255*  by specific option
256
257#### Options group
258
259Naming convention is the following: `TBB_<SCOPE>_<STAGE>_<CATEGORY>`, where
260
261*  `<SCOPE>` could be
262    *  `LIB` - options applied during libraries build.
263    *  `TEST` - options applied during test build.
264    *  `BENCH` - options applied during benchmarks build.
265    *  `COMMON` - options applied during all (libraries, test, benchmarks) builds.
266*  `<STAGE>` could be
267    *  `COMPILE` - options applied during the compilation.
268    *  `LINK` - options applied during the linkage.
269*  `<CATEGORY>` could be
270    *  `FLAGS` - list of flags
271    *  `LIBS` - list of libraries
272
273*Examples*
274
275Variable | Description
276--- | ---
277`TBB_COMMON_COMPILE_FLAGS` | Applied to libraries, tests and benchmarks as compile options
278`TBB_LIB_LINK_FLAGS`       | Applied to libraries as link options
279`TBB_LIB_LINK_LIBS `       | Applied to libraries as link libraries
280`TBB_TEST_COMPILE_FLAGS`   | Applied to tests as compile options
281
282Please specify the `LINK` options prefixed with dash(-) for MSVC(Visual Studio) compiler with CMake < 3.13 to avoid issues caused by `target_link_libraries` CMake command usage.
283
284#### Specific options
285
286If the option used only in part of the places (library, tests, benchmarks) as well as adding this option to the group of other options is not possible,
287then the option must be named using common sense.
288
289Warnings supperssions should be added into `TBB_WARNING_SUPPRESS` variable which is applied during the compilation of libraries, tests and benchmarks.
290Additional warnings should be added into `TBB_WARNING_TEST_FLAGS` variable which is applied during the compilation of tests.
291