CMake Best Practices

Book description

Explore a compendium of tips, tricks, and techniques for leveraging CMake and empowering your software development workflow.

Key Features

  • Understand what CMake is, how it works, and how to interact with it
  • Discover how to properly create and maintain well-structured CMake projects
  • Explore tools and techniques to get the most out of your CMake project

Book Description

CMake is a powerful tool used to perform a wide variety of tasks, so finding a good starting point for learning CMake is difficult. This book cuts to the core and covers the most common tasks that can be accomplished with CMake without taking an academic approach. While the CMake documentation is comprehensive, it is often hard to find good examples of how things fit together, especially since there are lots of dirty hacks and obsolete solutions available on the internet. This book focuses on helping you to tie things together and create clean and maintainable projects with CMake.

You'll not only get to grips with the basics but also work through real-world examples of structuring large and complex maintainable projects and creating builds that run in any programming environment. You'll understand the steps to integrate and automate various tools for improving the overall software quality, such as testing frameworks, fuzzers, and automatic generation of documentation. And since writing code is only half of the work, the book also guides you in creating installers and packaging and distributing your software. All this is tailored to modern development workflows that make heavy use of CI/CD infrastructure.

By the end of this CMake book, you'll be able to set up and maintain complex software projects using CMake in the best way possible.

What you will learn

  • Get to grips with architecting a well-structured CMake project
  • Modularize and reuse CMake code across projects
  • Integrate various tools for static analysis, linting, formatting, and documentation into a CMake project
  • Get hands-on with performing cross-platform builds
  • Discover how you can easily use different toolchains with CMake
  • Get started with crafting a well-defined and portable build environment for your project

Who this book is for

This book is for software engineers and build system maintainers working with C or C++ on a regular basis and trying to use CMake to better effect for their everyday tasks. Basic C++ and general programming knowledge will help you to better understand the examples covered in the book.

Table of contents

  1. CMake Best Practices
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1: The Basics
  7. Chapter 1: Kickstarting CMake
    1. Technical requirements
    2. CMake in a nutshell
    3. Installing CMake
      1. Building CMake from source
    4. Building your first project
      1. A minimal CMakeLists.txt file
    5. Understanding the CMake build process
      1. Source folders and build folders
    6. Writing CMake files
      1. The CMake language – a 10,000-feet overview
    7. Different toolchains and build types
      1. Build types
    8. Maintaining good build configurations with presets
    9. Summary
    10. Further reading
    11. Questions
  8. Chapter 2: Accessing CMake in Best Ways
    1. Technical requirements
    2. Using CMake via a command-line interface
      1. Learning the basics of the CMake CLI
    3. Advanced configuration using CMake-GUI and ccmake
      1. Learning how to use ccmake (CMake curses GUI)
      2. Using CMake via cmake-gui
      3. Tweaking environment variables
      4. Evaluating regular expressions with CMake
    4. Using CMake in Visual Studio, Visual Studio Code, and Qt Creator
      1. Visual Studio
      2. Visual Studio Code
      3. Qt Creator
    5. Summary
    6. Questions
    7. Further reading
  9. Chapter 3: Creating a CMake Project
    1. Technical requirements
    2. Setting up a project
      1. Working with nested projects
    3. Creating a "hello world" executable
    4. Creating a simple library
      1. Naming libraries
      2. Symbol visibility in shared libraries
      3. Interface or header-only libraries
    5. Bringing it together – using your libraries
      1. Setting compiler and linker options
      2. Library aliases
    6. Summary
    7. Questions
  10. Part 2: Practical CMake – Getting Your Hands Dirty with CMake
  11. Chapter 4: Packaging, Deploying, and Installing a CMake Project
    1. Prerequisites
    2. Making CMake targets installable
      1. The install() command
    3. Supplying configuration information for others using your project
      1. Entering the CMake package world – Config-file packages
    4. Creating an installable package with CPack
    5. Summary
    6. Questions
  12. Chapter 5: Integrating Third-Party Libraries and Dependency Management
    1. Technical requirements
    2. Finding files, programs, and paths with CMake
      1. Finding files and paths
      2. Finding programs
      3. Finding libraries
    3. Using third-party libraries in your CMake project
      1. Writing your own find module
    4. Using package managers with CMake
      1. Getting dependencies from Conan
      2. Using vcpkg for dependency management
    5. Getting the dependencies as source code
      1. Downloading dependencies as the source using pure CMake
    6. Summary
    7. Questions
  13. Chapter 6: Automatically Generating Documentation with CMake
    1. Technical requirements
    2. Generating documentation from your code
      1. Understanding what Doxygen is
      2. Using Doxygen with CMake
      3. Embedding custom UML diagrams into documentation
    3. Packaging and distributing documentation with CPack
    4. Creating dependency graphs of CMake targets
    5. Summary
    6. Questions
  14. Chapter 7: Seamlessly Integrating Code Quality Tools with CMake
    1. Technical requirements
    2. Defining, discovering, and running tests
      1. Automatically discovering tests
      2. Advanced ways to determine test success or failure
      3. Handling timeouts and repeating tests
      4. Writing test fixtures
      5. Running tests in parallel and managing test resources
    3. Generating code coverage reports
      1. Generating coverage reports using Clang or GCC
      2. Creating coverage reports for MSVC
    4. Sanitizing your code
    5. Static code analysis using CMake
    6. Creating custom build types for quality tools
    7. Summary
    8. Questions
  15. Chapter 8: Executing Custom Tasks with CMake
    1. Technical requirements
    2. Using external programs with CMake
    3. Executing custom tasks at build time
      1. Adding custom tasks to existing targets
      2. Generating files with custom tasks
    4. Executing custom tasks at configuration time
    5. Copying and modifying files
    6. Using CMake for platform-independent commands
      1. Executing CMake files as scripts
    7. Summary
    8. Questions
  16. Chapter 9: Creating Reproducible Build Environments
    1. Technical requirements
    2. Using CMake presets
      1. Inheriting from presets
      2. Conditions for presets
    3. Using build containers with CMake
    4. Using sysroots to isolate build environments
    5. Summary
    6. Questions
  17. Chapter 10: Handling Big Projects and Distributed Repositories in a Superbuild
    1. Technical requirements
    2. Requirements and prerequisites for a super-build
    3. Building across multiple code repositories
      1. The recommended way – FetchContent
      2. The legacy way – ExternalProject_Add
      3. Bonus – using the Qt 6 framework with a super-build
    4. Ensuring version consistency in a super-build
    5. Summary
    6. Questions
  18. Chapter 11: Automated Fuzzing with CMake
    1. Technical requirements
    2. A quick glance into fuzzing in CMake projects
    3. Integrating AFL++/libFuzzer into your project
      1. Using libFuzzer in your CMake project
      2. Using AFL++ in your CMake project
    4. Summary
    5. Questions
  19. Part 3: Mastering the Details
  20. Chapter 12: Cross-Platform Compiling and Custom Toolchains
    1. Technical requirements
    2. Using existing cross-platform toolchain files
    3. Creating toolchain files
      1. Defining the target system
      2. Selecting the build tools
      3. Setting the sysroot
      4. Cross-compiling for Android
      5. Cross-compiling for iOS, tvOS, or watchOS
    4. Testing cross-compiled binaries
      1. Testing a toolchain for supported features
      2. Common checks for toolchain and language features
    5. Summary
    6. Questions
  21. Chapter 13: Reusing CMake Code
    1. Technical requirements
    2. What is a CMake module?
    3. Fundamental building blocks of modules – functions and macros
      1. Functions
      2. Macros
    4. Writing your first very own CMake module
      1. Case study – dealing with project metadata files
      2. Recommendations for sharing CMake modules between projects
    5. Summary
    6. Questions
  22. Chapter 14: Optimizing and Maintaining CMake Projects
    1. Technical requirements
    2. Keeping a CMake project maintainable
    3. Profiling a CMake build
    4. Optimizing build performance
      1. Using unity builds
      2. Precompiled headers
      3. Using a compiler cache (ccache) to speed up rebuilds
    5. Summary
    6. Questions
  23. Chapter 15: Migrating to CMake
    1. Technical requirements
    2. High-level migration strategies
    3. Migrating small projects
    4. Migrating large projects to CMake
      1. Integrating legacy projects when migrating top down
    5. Summary
    6. Questions
  24. Chapter 16: Contributing to CMake and Further Reading Material
    1. Prerequisites
    2. Where to find the CMake community
      1. Stack Overflow
      2. Reddit (r/cmake)
      3. The CMake Discourse forum
      4. The Kitware CMake GitLab repository
    3. Contributing to CMake
    4. Recommended books and blogs
    5. Summary
  25. Assessments
    1. Chapter 1, Kickstarting CMake
    2. Chapter 2, Accessing CMake in the Best Ways
    3. Chapter 3, Creating a CMake Project
    4. Chapter 4, Packaging, Deploying, and Installing a CMake Project
    5. Chapter 5, Integrating Third-Party Libraries and Dependency Management
    6. Chapter 6, Automatically Generating Documentation
    7. Chapter 7, Seamlessly Integrating Code-Quality Tools with CMake
    8. Chapter 8, Executing Custom Tasks with CMake
    9. Chapter 9, Creating Reproducible Build Environments
    10. Chapter 10, Handling Big Projects and Distributed Repositories in a Superbuild
    11. Chapter 11, Automated Fuzzing with CMake
    12. Chapter 12, Cross-Platform-Compiling Custom Toolchains
    13. Chapter 13, Reusing CMake Code
    14. Chapter 14, Optimizing and Maintaining CMake Projects
    15. Chapter 15, Migrating to CMake
    16. Why subscribe?
  26. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: CMake Best Practices
  • Author(s): Dominik Berner, Mustafa Kemal Gilor
  • Release date: May 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803239729