CMake Cookbook

Book description

Learn CMake through a series of task-based recipes that provide you with practical, simple, and ready-to-use CMake solutions for your code

Key Features

  • Learn to configure, build, test, and package software written in C, C++, and Fortran
  • Progress from simple to advanced tasks with examples tested on Linux, macOS, and Windows
  • Manage code complexity and library dependencies with reusable CMake building blocks

Book Description

CMake is cross-platform, open-source software for managing the build process in a portable fashion. This book features a collection of recipes and building blocks with tips and techniques for working with CMake, CTest, CPack, and CDash.

CMake Cookbook includes real-world examples in the form of recipes that cover different ways to structure, configure, build, and test small- to large-scale code projects. You will learn to use CMake's command-line tools and master modern CMake practices for configuring, building, and testing binaries and libraries. With this book, you will be able to work with external libraries and structure your own projects in a modular and reusable way. You will be well-equipped to generate native build scripts for Linux, MacOS, and Windows, simplify and refactor projects using CMake, and port projects to CMake.

What you will learn

  • Configure, build, test, and install code projects using CMake
  • Detect operating systems, processors, libraries, files, and programs for conditional compilation
  • Increase the portability of your code
  • Refactor a large codebase into modules with the help of CMake
  • Build multi-language projects
  • Know where and how to tweak CMake configuration files written by somebody else
  • Package projects for distribution
  • Port projects to CMake

Who this book is for

If you are a software developer keen to manage build systems using CMake or would like to understand and modify CMake code written by others, this book is for you. A basic knowledge of C++, C, or Fortran is required to understand the topics covered in this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. CMake Cookbook
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Additional reading resources
    5. Get in touch
      1. Reviews
  6. Setting up Your System
    1. Obtaining the code
    2. Docker image
    3. Installing prerequisite software
      1. Getting CMake
      2. Compilers
      3. Build-automation tools
      4. Python
      5. Additional software
        1. BLAS and LAPACK
        2. Message passing interface (MPI)
        3. The Eigen linear algebra template library
        4. The Boost libraries
        5. Cross-compilers
        6. ZeroMQ, pkg-config, UUID, and Doxygen
        7. Conda build and deployment tools
    4. Testing the recipes
    5. Reporting problems and suggesting improvements
  7. From a Simple Executable to Libraries
    1. Introduction
    2. Compiling a single source file into an executable
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Switching generators
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    4. Building and linking static and shared libraries
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    5. Controlling compilation with conditionals
      1. How to do it
      2. How it works
    6. Presenting options to the user
      1. How to do it
      2. How it works
      3. There is more
    7. Specifying the compiler
      1. How to do it
      2. How it works
      3. There is more
    8. Switching the build type
      1. How to do it
      2. How it works
      3. There is more
    9. Controlling compiler flags
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    10. Setting the standard for the language
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    11. Using control flow constructs
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  8. Detecting the Environment
    1. Introduction
    2. Discovering the operating system
      1. How to do it
      2. How it works
    3. Dealing with platform-dependent source code
      1. Getting ready
      2. How to do it
      3. How it works
    4. Dealing with compiler-dependent source code
      1. Getting ready
      2. How to do it
      3. How it works
    5. Discovering the host processor architecture
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Discovering the host processor instruction set
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    7. Enabling vectorization for the Eigen library
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  9. Detecting External Libraries and Programs
    1. Introduction
    2. Detecting the Python interpreter
      1. How to do it
      2. How it works
      3. There is more
    3. Detecting the Python library
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    4. Detecting Python modules and packages
      1. Getting ready
      2. How to do it
      3. How it works
    5. Detecting the BLAS and LAPACK math libraries
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Detecting the OpenMP parallel environment
      1. Getting ready
      2. How to do it
      3. How it works
    7. Detecting the MPI parallel environment
      1. Getting ready
      2. How to do it
      3. How it works
    8. Detecting the Eigen library
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    9. Detecting the Boost libraries
      1. Getting ready
      2. How to do it
      3. How it works
    10. Detecting external libraries: I. Using pkg-config
      1. Getting ready
      2. How to do it
      3. How it works
    11. Detecting external libraries: II. Writing a find-module
      1. How to do it
      2. How it works
      3. There is more
  10. Creating and Running Tests
    1. Introduction
    2. Creating a simple unit test
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Defining a unit test using the Catch2 library
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    4. Defining a unit test and linking against Google Test
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    5. Defining a unit test and linking against Boost test
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Using dynamic analysis to detect memory defects
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    7. Testing expected failures
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    8. Using timeouts for long tests
      1. Getting ready
      2. How to do it
      3. How it works
    9. Running tests in parallel
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    10. Running a subset of tests
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    11. Using test fixtures
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  11. Configure-time and Build-time Operations
    1. Introduction
    2. Using platform-independent file operations
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Running a custom command at configure time
      1. How to do it
      2. How it works
      3. There is more
    4. Running a custom command at build time: I. Using add_custom_command
      1. Getting ready
      2. How to do it
      3. How it works
    5. Running a custom command at build time: II. Using add_custom_target
      1. Getting ready
      2. How to do it
      3. How it works
    6. Running custom commands for specific targets at build time
      1. Getting ready
      2. How to do it
      3. How it works
    7. Probing compilation and linking
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    8. Probing compiler flags
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    9. Probing execution
      1. Getting ready
      2. How to do it
      3. How it works
    10. Fine-tuning configuration and compilation with generator expressions
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
  12. Generating Source Code
    1. Introduction
    2. Generating sources at configure time
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Generating source code at configure time using Python
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    4. Generating source code at build time using Python
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    5. Recording the project version information for reproducibility
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Recording the project version from a file
      1. Getting ready
      2. How to do it
      3. How it works
    7. Recording the Git hash at configure time
      1. Getting ready
      2. How to do it
      3. How it works
    8. Recording the Git hash at build time
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  13. Structuring Projects
    1. Introduction
    2. Code reuse with functions and macros
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Splitting CMake sources into modules
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    4. Writing a function to test and set compiler flags
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    5. Defining a function or macro with named arguments
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Redefining functions and macros
      1. Getting ready
      2. How to do it
      3. How it works
    7. Deprecating functions, macros, and variables
      1. Getting ready
      2. How to do it
      3. How it works
    8. Limiting scope with add_subdirectory
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    9. Avoiding global variables using target_sources
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    10. Organizing Fortran projects
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  14. The Superbuild Pattern
    1. Introduction
    2. Using the superbuild pattern
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Managing dependencies with a superbuild: I. The Boost libraries
      1. How to do it
      2. How it works
    4. Managing dependencies with a superbuild: II. The FFTW library
      1. Getting ready
      2. How to do it
      3. How it works
    5. Managing dependencies with a superbuild: III. The Google Test framework
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    6. Managing your project as a superbuild
      1. Getting ready
      2. How to do it
      3. How it works
  15. Mixed-language Projects
    1. Introduction
    2. Building Fortran projects that use C/C++ libraries
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    3. Building C/C++ projects that use Fortran libraries
      1. Getting ready
      2. How to do it
      3. How it works
    4. Building C++ and Python projects using Cython
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    5. Building C++ and Python projects using Boost.Python
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Building C++ and Python projects using pybind11
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    7. Mixing C, C++, Fortran, and Python using Python CFFI
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
  16. Writing an Installer
    1. Introduction
    2. Installing your project
      1. Getting ready
      2. How to do it
      3. How it works
        1. Installing to standard locations
        2. Target properties and RPATH handling
        3. Installation directives
      4. There is more
    3. Generating export headers
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    4. Exporting your targets
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    5. Installing a superbuild
      1. Getting ready
      2. How to do it
      3. How it works
  17. Packaging Projects
    1. Introduction
    2. Generating source and binary packages
      1. Getting ready
      2. How to do it
      3. How it works
        1. Source archives
        2. Binary archives
        3. Platform-native binary installers
      4. There is more
    3. Distributing a C++/Python project built with CMake/pybind11 via PyPI
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    4. Distributing a C/Fortran/Python project build with CMake/CFFI via PyPI
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    5. Distributing a simple project as Conda package
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
    6. Distributing a project with dependencies as Conda package
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  18. Building Documentation
    1. Introduction
    2. Building documentation using Doxygen
      1. Getting ready
      2. How to do it
      3. How it works
    3. Building documentation using Sphinx
      1. Getting ready
      2. How to do it
      3. How it works
    4. Combining Doxygen and Sphinx
      1. Getting ready
      2. How to do it
      3. How it works
  19. Alternative Generators and Cross-compilation
    1. Introduction
    2. Building a CMake project using Visual Studio 2017
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    3. Cross-compiling a hello world example
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    4. Cross-compiling a Windows binary with OpenMP parallelization
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
  20. Testing Dashboards
    1. Introduction
      1. Setting up a CDash dashboard
    2. Deploying tests to the CDash dashboard
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    3. Reporting test coverage to the CDash dashboard
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    4. Using the AddressSanitizer and reporting memory defects to CDash
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
    5. Using the ThreadSanitizer and reporting data races to CDash
      1. Getting ready
      2. How to do it
      3. How it works
      4. There is more
      5. See also
  21. Porting a Project to CMake
    1. Where to start
      1. Reproducing the porting example
      2. Creating a top-level CMakeLists.txt
      3. How to allow both conventional configuration and configuration with CMake at the same time
      4. Capturing a record of what the traditional build does
      5. Debugging the migration
      6. Implementing options
      7. Start with the executable and very few targets, later localize scope
    2. Generating files and writing platform checks
      1. How to structure files
      2. Configuring preprocessor definitions based on the system environment
      3. Configuring files with paths and compiler flags
      4. Executing shell scripts at configure time
    3. Detecting required dependencies and linking
    4. Reproducing compiler flags
      1. Defining compiler flags
      2. Scope of compiler flags
    5. Porting tests
      1. Getting started
      2. Implementing a multi-step test
      3. Recommendation for tests
    6. Porting install targets
    7. Further steps
    8. Summary and common pitfalls when converting projects to CMake
      1. Summary of code changes
      2. Common pitfalls
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: CMake Cookbook
  • Author(s): Radovan Bast, Roberto Di Remigio
  • Release date: September 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788470711