Blog

Using lists(CMake)

Content #

To store a list, CMake concatenates all elements into a string, using a semicolon(;) as a delimiter. The following command will have exactly the same effect:

set(myList a list of five elements)
set(myList "a;list;of;five;elements")
set(myList a list "of;five;elements")

CMake automatically unpacks lists in unquoted arguments.

CMake offers a list() command that provides a multitude of subcommands to read, search, modify, and order lists. Here’s a short summary:

...

Variable Scope(CMake)

Two scopes #

  1. Function scope

For when custom functions defined with function() are executed

  1. Directory scope

For when a CMakeLists.txt listfile in a nested directory is executed from the add_subdirectory() command

Nested scope #

When a nested scope is created, CMake simply fills it with copies of all the variables from the current scope. Subsequent commands will affect these copies. But as soon as the ececution of the nested scope is completed, all copies are deleted and the original, parent scope is restored.

...

Cache variables(CMake)

Cache variables #

Cache variables are not available in scripts. Set a cache variable:

set(<variable> <value> CACHE <type> <docstring> [FORCE])

Specifying CACHE as a set() argument means that we intend to change what was provided during the configuration stage, and it imposes a requirement to provide the variable <type> and <docstring> values. This is because these variables are configurable by the user and the GUI needs to know how to display it.

...

Command arguments(CMake)

Content #

The only data type recognized by CMake is a string.

Bracket argements #

Used to pass multiline strings, verbatim.

message([[[[multiline
bracket
argument
]]]])

message([==[
  because we used two equal-signs "=="
  following is still a single argument:
  { "petsArray" = [["mouse","cat"],["dog"]] }
]==])

使用"[==[“这种形式是因为参数中有double brackets。

Quoted arguments #

Will expand escaped sequences and variable references.

message("1. escape sequence: \" \n in a quoted argument")
message("2. multi...
line")
message("3. and a variable reference: ${CMAKE_VERSION}")

Unquoted arguments #

Both escape sequences and variable references, but semicolons(;) will be treated as delimiter.

...

CMAKE_BUILD_TYPE

Content #

可能的取值:

  • Debug
  • Release
  • MinSizeRel
  • RelWithDebInfo
cmake -S . -B build -C CMAKE_BUILD_TYPE=Release

Variables(CMake)

Content #

Three category:

  • normal (use ${})
  • cache (use ${} or $CACHE{})
  • environment (use $ENV{})

Key facts #

  1. Variable names are case-sensitive.
  2. All variables are stored internally as strings.
  3. The basic variable manipulation commands are set() and unset(), but there are other commands that can affect variables, such as string() and list()

Manipulate environment variables #

set(ENV{CXX} "clang++")
unset(ENV(VERBOSE))

Cache variables(CMake) #

CMAKE_BUILD_TYPE

Listfiles(CMake)

Content #

Files that contain the CMake language are called listfiles and can be included one in another, by calling include() and find_package(), or indirectly with add_subdirectory().

  1. They have a .cmake extension usually.
  2. As CMake walks the source tree and includes different listfiles, the following variables are set:
    • CMAKE_CURRENT_LIST_DIR
    • CMAKE_CURRENT_LIST_FILE
    • CMAKE_PARENT_LIST_FILE
    • CMAKE_CURRENT_LIST_LINE

From #

CMakeCache.txt

Content #

Cached variables will be generated from listfiles and stored in CMakeCache.txt when the configure state is run for the first time.

  1. Remove CMakeCache.txt will reset the project.
  2. Cache variables can be read and written from the listfiles.

list cache variables:

cmake -L[A][H] <path-to-source>

A means show variaables that are marked as ADVANCED. H means print help messages with variables.

From #