Introduction

Get an overview of CMake language and its importance.

CMake in practice

Writing in the CMake Language is a bit tricky. When we read a CMake listfile for the first time, we may be under the impression that the language in it is so simple that it doesn't require any special training or preparation. What follows is very often a practical attempt to introduce changes and experiment with the code without a thorough understanding of how it works. We programmers are usually very busy and are overly keen to tackle any build-related issues with little investment. We tend to make gut-based changes hoping they just might do the trick. This approach to solving technical problems is called voodoo programming.

The CMake Language appears simple: after we have completed our small addition, fix, or hack, or added a one-liner, we realize that something isn’t working. The time spent on debugging is often longer than that spent on actually studying the subject. Luckily, this won’t be our fate—because this chapter covers the vast majority of the critical knowledge needed to use the CMake Language in practice.

Role of a CMake programmer

We'll not only learn about the building blocks of the CMake Language—comments, commands, variables, and control structures—but we'll also give the necessary background and try them out in a clean and modern CMake example. CMake puts us in a bit of a unique position. On the one hand, we perform a role of a building engineer; we need to understand all the intricacies of the compilers, the platforms, and everything else in between. On the other hand, we're a developer; we're writing code that generates a buildsystem. Writing good code is hard and requires thinking on many levels at the same time—it should work and be easy to read, but it should also be easy to analyze, extend, and maintain. This is exactly what we're going to talk about here.

Lastly, we'll introduce some of the most useful and common commands in CMake. Commands that aren't used that often will be placed in the "Appendix" section (this will include a complete reference guide for the string, list, and file manipulation commands).

Topics to cover

In this chapter, we're going to cover the following main topics:

  • The basics of the CMake Language syntax

  • Working with variables

  • Using lists

  • Understanding control structures in CMake

  • Useful commands

CMake language syntax: basics

Composing CMake code is like writing in any other imperative language: lines are executed from top to bottom and from left to right, occasionally stepping into an included file or a called function. Depending on the mode, the execution begins from the root file of the source tree (CMakeLists.txt) or a .cmake script file that was passed as an argument to cmake.

As we discussed in the previous chapter, scripts support the majority of the CMake Language (with the exclusion of any project-related functionality). As a result, they're a great way to start practicing the CMake syntax itself, and that's why we'll be using them here. After becoming comfortable writing basic listfiles, we'll start preparing actual project files. If we remember, scripts can be run with the following command:

cmake -P script.cmake

Note: CMake supports 7-bit ASCII text files for portability across all platforms. We can use both \n or \r\n line endings. UTF-8 with optional Byte Order Markers (BOMs) is supported in CMake versions above 3.0, and UTF-16 is supported in CMake versions above 3.2.

Everything in a CMake listfile is either a command invocation or a comment.