CMake set 语法

时间:2024-04-07 15:05:36

参考CMake官方文档:https://cmake.org/cmake/help/v3.14/command/set.html

1. 普通变量

set(<variable> <value>... [PARENT_SCOPE])

设置变量<variable> 的值为 <value>

如果后面增加了 PARENT_SCOPE 选项的话, 表示 在上层作用域/目录 设置<variable> 的值为 <value>, 但是当前作用域/目录 该<variable> 的值不变。

2. 设置缓存条目

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

设置给定的缓存变量<variable> 的值为 <value>.

由于缓存项旨在提供用户可设置的值,因此默认情况下不会覆盖现有缓存项。使用FORCE选项覆盖现有条目。

<type> 必须是下列类型之一:

BOOL
Boolean ON/OFF value. 
FILEPATH
文件路径. 
PATH
目录路径. 
STRING
一行文本. cmake-gui(1) offers a text field or a drop-down selection if the STRINGS cache entry property is set.
INTERNAL
一行文本. cmake-gui(1) does not show internal entries. They may be used to store variables persistently across runs. Use of this type implies FORCE.

The <docstring> must be specified as a line of text providing a quick summary of the option for presentation to cmake-gui(1) users.

3. 设置环境变量

set(ENV{<variable>} [<value>])

设置环境变量<variable>的值为<value>. 后面使用 $ENV{<variable>} 可以获取该值.

This command affects only the current CMake process, not the process from which CMake was called, nor the system environment at large, nor the environment of subsequent build or test processes.

If no argument is given after ENV{<variable>} or if <value> is an empty string, then this command will clear any existing value of the environment variable.

Arguments after <value> are ignored. If extra arguments are found, then an author warning is issued.