CMake Cookbook
This repository collects sources for the recipes contained in the
CMake Cookbook
published by Packt and authored by Radovan Bast and
Roberto Di Remigio
Table of contents
Chapter 1: From a Simple Executable to Libraries
- 1. Compiling a single source file into an executable
- 2. Switching generators
- 3. Building and linking static and shared libraries
- 4. Controlling compilation with conditionals
- 5. Presenting options to the user
- 6. Specifying the compiler
- 7. Switching the build type
- 8. Controlling compiler flags
- 9. Setting the standard for the language
- 10. Using control flow constructs
Chapter 2: Detecting the Environment
- 1. Discovering the operating system
- 2. Dealing with platform-dependent source code
- 3. Dealing with compiler-dependent source code
- 4. Discovering the host processor architecture
- 5. Discovering the host processor instruction set
- 6. Enabling vectorization for the Eigen library
Chapter 3: Detecting External Libraries and Programs
- 1. Detecting the Python interpreter
- 2. Detecting the Python library
- 3. Detecting Python modules and packages
- 4. Detecting the BLAS and LAPACK math libraries
- 5. Detecting the OpenMP parallel environment
- 6. Detecting the MPI parallel environment
- 7. Detecting the Eigen library
- 8. Detecting the Boost libraries
- 9. Detecting external libraries: I. Using
pkg-config
- 10. Detecting external libraries: II. Writing a find-module
Chapter 4: Creating and Running Tests
- 1. Creating a simple unit test
- 2. Defining a unit test using the Catch2 library
- 3. Defining a unit test and linking against Google Test
- 4. Defining a unit test and linking against Boost test
- 5. Using dynamic analysis to detect memory defects
- 6. Testing expected failures
- 7. Using timeouts for long tests
- 8. Running tests in parallel
- 9. Running a subset of tests
- 10. Using test fixtures
Chapter 5: Configure-time and Build-time Operations
- 1. Using platform-independent file operations
- 2. Running a custom command at configure time
- 3. Running a custom command at build time: I. Using
add_custom_command
- 4. Running a custom command at build time: II. Using
add_custom_target
- 5. Running custom commands for specific targets at build time
- 6. Probing compilation and linking
- 7. Probing compiler flags
- 8. Probing execution
- 9. Fine-tuning configuration and compilation with generator expressions
Chapter 6: Generating Source Code
- 1. Generating sources at configure time
- 2. Generating source code at configure time using Python
- 3. Generating source code at build time using Python
- 4. Recording the project version information for reproducibility
- 5. Recording the project version from a file
- 6. Recording the Git hash at configure time
- 7. Recording the Git hash at build time
Chapter 7: Structuring Projects
- 1. Code reuse with functions and macros
- 2. Splitting CMake sources into modules
- 3. Writing a function to test and set compiler flags
- 4. Defining a function or macro with named arguments
- 5. Redefining functions and macros
- 6. Deprecating functions, macros, and variables
- 7. Limiting scope with
add_subdirectory
- 8. Avoiding global variables using
target_sources
- 9. Organizing Fortran projects
Chapter 8: The Superbuild Pattern
- 1. Using the superbuild pattern
- 2. Managing dependencies with a superbuild: I. The Boost libraries
- 3. Managing dependencies with a superbuild: II. The FFTW library
- 4. Managing dependencies with a superbuild: III. The Google Test framework
- 5. Managing your project as a superbuild
Chapter 9: Mixed-language Projects
- 1. Building Fortran projects that use C/C++ libraries
- 2. Building C/C++ projects that use Fortran libraries
- 3. Building C++ and Python projects using Cython
- 4. Building C++ and Python projects using Boost.Python
- 5. Building C++ and Python projects using pybind11
- 6. Mixing C, C++, Fortran, and Python using Python CFFI
Chapter 10: Writing an Installer
- 1. Installing your project
- 2. Generating export headers
- 3. Exporting your targets
- 4. Installing a superbuild
Chapter 11: Packaging Projects
- 1. Generating source and binary packages
- 2. Distributing a C++/Python project built with CMake/pybind11 via PyPI
- 3. Distributing a C/Fortran/Python project built with CMake/CFFI via PyPI
- 4. Distributing a simple project as Conda package
- 5. Distributing a project with dependencies as Conda package
Chapter 12: Building Documentation
- 1. Building documentation using Doxygen
- 2. Building documentation using Sphinx
- 3. Combining Doxygen and Sphinx
Chapter 13: Alternative Generators and Cross-compilation
- 1. Cross-compiling a hello world example
- 2. Cross-compiling a Windows binary with OpenMP parallelization
Chapter 14: Testing Dashboards
- 1. Deploying tests to the CDash dashboard
- 2. Reporting test coverage to the CDash dashboard
- 3. Using the AddressSanitizer and reporting memory defects to CDash
- 4. Using the ThreadSanitizer and reporting data races to CDash
Chapter 15: Porting a Project to CMake