Common Syntax of CMake (Environment Variables)

Previous Exciting Content:CMake Hello, WorldCMake VariablesCMake Official Tutorial (Basic Project Setup)CMake Official Tutorial (Library Creation)CMake Official Tutorial (Usage Requirements)CMake Official Tutorial (Installation and Testing)CMake Common Syntax (if Statement)CMake Common Syntax (Cache Variables)

In CMake, environment variables have the following characteristics:

  1. 1. The scope of environment variables is global and they are not cached.
  2. 2. They only affect the current CMake process, do not affect the calling process of CMake, do not affect the system environment, and do not affect the environment of subsequent build or test processes.

Environment variables in CMake can be divided into three main categories:

  1. 1. Custom environment variables
  2. 2. System environment variables
  3. 3. Predefined environment variables

Custom Environment Variables

Setting (defining) an environment variable must meet the following format:

#Set Environment Variable
set(ENV{<variable>} [<value>])</value></variable>

Regardless of the type of environment variable, it can be read in the following format:

#Read environment variable
$ENV{<variable>}</variable>

You can test the global nature of environment variables:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(envTest)
set(ENV{ENV_TEST} "Hello")
add_subdirectory(sub1)
message("Top: ENV{ENV_TEST} = $ENV{ENV_TEST}")             
message("Top: ENV{ENV_SUB_TEST} = $ENV{ENV_SUB_TEST}") 

# sub1/CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(envTestSub1)
message("sub1: ENV{ENV_TEST} = $ENV{ENV_TEST}")
set(ENV{ENV_TEST} "Hello World")
set(ENV{ENV_SUB_TEST} "Hello Tomorrow")

The output result is:

sub1: ENV{ENV_TEST} = Hello
Top: ENV{ENV_TEST} = Hello World
Top: ENV{ENV_SUB_TEST} = Hello Tomorrow

As can be seen: subdirectories and parent directories can access each other’s environment variables defined in their respective CMakeLists.txt files. Therefore, the scope of environment variables is global.

System Environment Variables

System environment variables are those provided by the operating system, such as the commonly known PATH.

CMake can read and write system environment variables:

message("path=$ENV{PATH}")          #read
set(ENV{PATH} $ENV{PATH}:/tmp)      #set
message("path=$ENV{PATH}")

The running result is:

path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/tmp

After the CMake process is completed, the system variable is not affected:

root@ubuntu:/mnt/hgfs/21_repo_client# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

Predefined Environment Variables

Predefined environment variables are those that are already defined by CMake. There are many predefined environment variables, which can be referenced in the official links below:

https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html
https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html

The official documentation provides detailed explanations for each predefined environment variable, which will not be elaborated here.

Leave a Comment