Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
CMake Best Practices

You're reading from   CMake Best Practices Discover proven techniques for creating and maintaining programming projects with CMake

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781803239729
Length 406 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Dominik Berner Dominik Berner
Author Profile Icon Dominik Berner
Dominik Berner
Mustafa Kemal Gilor Mustafa Kemal Gilor
Author Profile Icon Mustafa Kemal Gilor
Mustafa Kemal Gilor
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Part 1: The Basics
2. Chapter 1: Kickstarting CMake FREE CHAPTER 3. Chapter 2: Accessing CMake in Best Ways 4. Chapter 3: Creating a CMake Project 5. Part 2: Practical CMake – Getting Your Hands Dirty with CMake
6. Chapter 4: Packaging, Deploying, and Installing a CMake Project 7. Chapter 5: Integrating Third-Party Libraries and Dependency Management 8. Chapter 6: Automatically Generating Documentation with CMake 9. Chapter 7: Seamlessly Integrating Code Quality Tools with CMake 10. Chapter 8: Executing Custom Tasks with CMake 11. Chapter 9: Creating Reproducible Build Environments 12. Chapter 10: Handling Big Projects and Distributed Repositories in a Superbuild 13. Chapter 11: Automated Fuzzing with CMake 14. Part 3: Mastering the Details
15. Chapter 12: Cross-Platform Compiling and Custom Toolchains 16. Chapter 13: Reusing CMake Code 17. Chapter 14: Optimizing and Maintaining CMake Projects 18. Chapter 15: Migrating to CMake 19. Chapter 16: Contributing to CMake and Further Reading Material 20. Assessments 21. Other Books You May Enjoy

Maintaining good build configurations with presets

A common problem when building software with CMake is how to share good or working configurations to build a project. Often, people and teams have a preferred way of where the build artifacts should go, which generator to use on which platform, or just the desire that the CI environment should use the same settings to build as it does locally. Since CMake 3.19 came out in December 2020, this information can be stored in CMakePresets.json files, which are placed in the root directory of a project. Additionally, each user can superimpose their configuration with a CMakeUserPresets.json file. The basic presets are usually placed under version control, but the user presets are not checked into the version system. Both files follow the same JSON format, with the top-level outline being as follows:

{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [...],
"buildPresets": [...],
"testPresets": [...]
}
  1. The first line, "version": 3, denotes the schema version of the JSON file. CMake 3.21 supports up to version 3, but it is expected that new releases will bring new versions of the schema.
  2. Next, cmakeMinimumRequired{...} specifies which version of CMake to use. Although this is optional, it is good practice to put this in here and match the version with the one specified in the CMakeLists.txt file.
  3. After that, the various presets for the different build stages can be added with configurePresets, buildPresets, and testPresets. As the name suggests, configurePresets applies to the configure stage of CMake's build process, while the other two are used for the build and test stages. The build and test presets may inherit one or more configure presets. If no inheritance is specified, they apply to all the previous steps.

To see what presets have been configured in a project, run cmake --list-presets to see a list of available presets. To build using a preset, run cmake --build --preset name.

To see the full specification of the JSON schema, go to https://cmake.org/cmake/help/v3.21/manual/cmake-presets.7.html.

Presets are a good way to share knowledge about how to build a project in a very explicit way. At the time of writing, more and more IDEs and editors are adding support for CMake presets natively, especially for handling cross-compilation with toolchains. Here, we're only giving you the briefest overview of CMake presets; they will be covered in more depth in Chapter 12, Cross-Platform Compiling and Custom Toolchains.

You have been reading a chapter from
CMake Best Practices
Published in: May 2022
Publisher: Packt
ISBN-13: 9781803239729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime