CMake : Generator expressions
在使用cmake
编写CMakeLists.txt
的过程中,时常会用到Generator expressions
。本片文章是对cmake中该部分用法的一个介绍。
Generator expressions不会在configure time的时候生成值(例如执行cmake命令解析CMakeLists.txt,(执行类似add_target()和message()等命令时). 此时,Generator expressions只会有字面值,而不会进行逻辑生成值。
Generator expressions在generate time的时候会进行求值 (这就是被称为“generator expressions”的原因). Generate time发生在所有的CMake code被解析和处理,并且CMake正在生成构建文件之后. 此时,重要的信息才会去对Generator expressions求值。
所以你只能在generate time或者之后使用 generator expressions(例如构建的时候)
1.Introduce
Generator expressions会在特定的构建项目中生成值,用于实现有条件的构建。
Generator expressions可以在很多的目标属性中使用:例如LINK_LIBRARIES
, INCLUDE_DIRECTORIES
, COMPILE_DEFINITIONS
,target_link_libraries()
, target_include_directories()
, target_compile_definitions()
Generator expressions可以视条件进行链接、编译、搜索头文件。条件可以是构建的配置信息,目标属性的平台信息以及其他。
Generator expressions的形式诸如$<...>
。多个Generator expressions也可以组合使用。
2.Boolean generator expressions
Boolean generator expressions在求值的时候为0
或1
。一般用作if
等类似命令的参数,作为其中的条件表达式。
Boolean generator expressions有如下的几种形式:
2.1.Logical Operators
$<BOOL:string>
把string转换成 0 或者 1. 以下是求值为0的情况:
- string为空
- string(不区分大小写)是 0, FALSE, OFF, N, NO, IGNORE, or NOTFOUND
- string(不区分大小写)的后缀是“ -NOTFOUND”
其他情况都为1
$<AND:conditions>
conditions由多个互相独立的boolean expressions构成.当所有condition都是1的时候,表达式才会值为1,否则为0
$<OR:conditions>
conditions由多个互相独立的boolean expressions构成.只要有一个condition是1,表达式值就是0,否则为0
$<NOT:condition>
condition是0的时候表达式值为1, condition是41时候表达式值为0
2.2.String Comparisons
$<STREQUAL:string1,string2>
string1和string2相等为1,不等为0(区分大小写)。如果想要不区分大小写,需要使用如下的形式:
# "1" if ${foo} is any of "BAR", "Bar", "bar", ...
$<STREQUAL:$<UPPER_CASE:${foo}>,"BAR">
$<EQUAL:value1,value2>
value1和value2数值相等为1,不等为0
$<IN_LIST:string,list>
若string是list中的成员则为1,否则为0。区分大小写
$<VERSION_LESS:v1,v2>
若v1的版本号小于v2则为1,否则为0
$<VERSION_GREATER:v1,v2>
若v1的版本号大于v2则为1,否则为0
$<VERSION_EQUAL:v1,v2>
若v1的版本号等于v2则为1,否则为0
$<VERSION_LESS_EQUAL:v1,v2>
若v1的版本号小于或等于v2则为1,否则为0
$<VERSION_GREATER_EQUAL:v1,v2>
若v1的版本号大于或等于v2则为1,否则为0
2.3.Variable Queries
$<TARGET_EXISTS:target>
target存在为1,不存在为0
3.String-Valued Generator Expressions
这些表达式用于拓展一些字符串表达式。例如:
include_directories(/usr/include/$<CXX_COMPILER_ID>/)
# 字符串会拓展为 /usr/include/GNU/ or /usr/include/Clang/,具体值需要根据编译器决定
String-valued expressions也可以由其他expressions组合而成。例如:
$<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
# 如果 CMAKE_CXX_COMPILER_VERSION 小于4.2.0的话,就选用老版本的编译器
String-valued expressions也可以由其他expressions嵌套而成,例如:
-I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
# 生成一个字符串,包含INCLUDE_DIRECTORIES的属性的条目,使用I作为参数去这样做
String-valued expressions一般的形式如下:
3.1.Escaped Characters
用作转义字符
$<ANGLE-R>
字符">"
$<COMMA>
字符","
$<SEMICOLON>
字符“;”
3.2.Conditional Expressions
Conditional generator expression基于boolean condition
$<condition:true_string>
当condition为1的时候,求值为true_string,否则为空字符串
$<IF:condition,true_string,false_string>
当condition为1的时候,求值为true_string,否则为 false_string
特别的,当condition是boolean generator expression. 例如:
$<$<CONFIG:Debug>:DEBUG_MODE>
当Debug被使用时,值为DEBUG_MODE,否则为空字符串
3.3.String Transformations
$<JOIN:list,string>
把string加到list中
$<REMOVE_DUPLICATES:list>
从list中一处重复的成员
$<FILTER:list,INCLUDE|EXCLUDE,regex>
从list中引用或移除与regex匹配的成员
$<LOWER_CASE:string>
把string转换成小写
$<UPPER_CASE:string>
把string转换成大写
3.4.Variable Queries
$<CONFIG>
Configuration name.
$<CONFIGURATION>
Configuration name. Deprecated since CMake 3.0. Use CONFIG instead.
$<PLATFORM_ID>
The current system’s CMake platform id. See also the CMAKE_SYSTEM_NAME variable.
$<C_COMPILER_ID>
The CMake’s compiler id of the C compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<CXX_COMPILER_ID>
The CMake’s compiler id of the CXX compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<CUDA_COMPILER_ID>
The CMake’s compiler id of the CUDA compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<OBJC_COMPILER_ID>
The CMake’s compiler id of the OBJC compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<OBJCXX_COMPILER_ID>
The CMake’s compiler id of the OBJCXX compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<Fortran_COMPILER_ID>
The CMake’s compiler id of the Fortran compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<ISPC_COMPILER_ID>
The CMake’s compiler id of the ISPC compiler used. See also the CMAKE_<LANG>_COMPILER_ID variable.
$<C_COMPILER_VERSION>
The version of the C compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<CXX_COMPILER_VERSION>
The version of the CXX compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<CUDA_COMPILER_VERSION>
The version of the CUDA compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<OBJC_COMPILER_VERSION>
The version of the OBJC compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<OBJCXX_COMPILER_VERSION>
The version of the OBJCXX compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<Fortran_COMPILER_VERSION>
The version of the Fortran compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<ISPC_COMPILER_VERSION>
The version of the ISPC compiler used. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
$<COMPILE_LANGUAGE>
The compile language of source files when evaluating compile options. See the related boolean expression $<COMPILE_LANGUAGE:language> for notes about the portability of this generator expression.
$<LINK_LANGUAGE>
The link language of target when evaluating link options. See the related boolean expression $<LINK_LANGUAGE:language> for notes about the portability of this generator expression.
3.5.Target-Dependent Queries
3.6.Output-Related Expressions
$<TARGET_NAME:...>
值为“...”,如果将目标导出到多个相关导出集,则需要此选项。这个必须是目标的文字名称,它不能包含生成器表达式。
$<INSTALL_INTERFACE:...>
当使用了install(EXPORT),表达式的值为"...",否则为空字符
$<BUILD_INTERFACE:...>
当目标属性是用export()导入的或者该目标被同一个构建项目中的其他目标使用时,表达式的值为"...",否则为空字符串