Book Image

CMake Best Practices

By : Dominik Berner, Mustafa Kemal Gilor
5 (2)
Book Image

CMake Best Practices

5 (2)
By: Dominik Berner, Mustafa Kemal Gilor

Overview of this book

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.
Table of Contents (22 chapters)
Part 1: The Basics
Part 2: Practical CMake – Getting Your Hands Dirty with CMake
Part 3: Mastering the Details

What this book covers

Chapter 1, Kickstarting CMake, explains what CMake is in a nutshell and then jumps right into installing CMake and building something with CMake. You will learn how to install the latest stable version manually, even if it's not provided by your package manager. You'll also learn about the basic concepts behind CMake and why it is a build system generator and not a build system itself. You will learn how it fits into modern software development with C++ (and C).

Chapter 2, Accessing CMake in the Best Ways, shows how to best use CMake from the command line with a GUI and how CMake integrates with some common IDEs and editors.

Chapter 3, Creating a CMake Project, takes you through setting up a project to build an executable and a library and linking the two together.

Chapter 4, Packaging, Deploying, and Installing a CMake Project, shows you how to create a distributable version of your software project. You will learn how to add installation instructions and package the project using CMake and CPack (CMake's packaging program).

Chapter 5, Integrating Third-Party Libraries and Dependency Management, explains how to integrate existing third-party libraries into your project. It also shows you how to add libraries already installed on your system, external CMake projects, and non-CMake projects.

Chapter 6, Automatically Generating Documentation, explores how to generate documentation from your code with CMake as part of the build process with doxygen, dot (graphviz), and plantuml.

Chapter 7, Seamlessly Integrating Code-Quality Tools with CMake, shows you how to integrate unit testing, code sanitizers, static code analysis, and code coverage tools into your project. It will show you how CMake can help to discover and execute tests.

Chapter 8, Executing Custom Tasks with CMake, explains how you can integrate almost any tool into your build process. You will learn how to wrap external programs into custom targets or hook them into the build process to execute them. We will cover how custom tasks can be used to generate files and how they can consume files produced by other targets. You will also learn how to execute system commands during the configuration of the CMake build and how to create platform-agnostic commands using the CMake script mode.

Chapter 9, Creating Reproducible Build Environments, shows how you can build an environment portable between various machines including CI/CD pipelines, and how to work with Docker, sysroots, and CMake presets to make your build work "out of the box" everywhere.

Chapter 10, Handling Big Projects and Distributed Repositories in a Superbuild, simplifies managing projects that are distributed across multiple git repositories with CMake. You will learn how to create a super-build that allows you to build specific versions as well as the latest nightly builds. You will explore what prerequisites it needs and how to combine them.

Chapter 11, Automated Fuzzing with CMake, shows how you can integrate and use fuzzing tools with CMake.

Chapter 12, Cross-Platform Compiling and Custom Toolchains, demonstrates how you can use cross-platform toolchains. You will also learn how to write your own toolchain definitions and conveniently use different toolchains with CMake.

Chapter 13, Reusing CMake Code, explains CMake modules and how you can generalize your CMake files. You will learn how to write broadly used modules, which you can ship individually from your project.

Chapter 14, Optimizing and Maintaining CMake Projects, suggests how to get faster build times and provides tips and tricks for keeping a CMake project neat and tidy over a long period of time.

Chapter 15, Migrating to CMake, explains a high-level strategy on how to migrate a large existing codebase to CMake without the need to completely stop your development.

Chapter 16, Contributing to CMake and Further Reading Material, suggests where to go if you want to contribute, what is looked for, and basic contributing guidelines. It will also guide you on where to find additional in-depth information or more specific literature.