cmake条件编译

CMake的条件编译基于if elseif endif。3.0版本具体语法如下
if(expression)
  # then section.
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
elseif(expression2)
  # elseif section.
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
else(expression)
  # else section.
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
endif(expression)

ref: https://cmake.org/cmake/help/v3.0/command/if.html?highlight=#command:if

expression有多种表达方式。布尔比较,字符串比较,数值比较,复合表达式等。罗列一下:

if(<constant>)
    True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number. False if the constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND. Named boolean constants are case-insensitive. If the argument is not one of these constants, it is treated as a variable.
if(<variable>)
    True if the variable is defined to a value that is not a false constant. False otherwise. (Note macro arguments are not variables.)
if(NOT <expression>)
    True if the expression is not true.
if(<expr1> AND <expr2>)
    True if both expressions would be considered true individually.
if(<expr1> OR <expr2>)
    True if either expression would be considered true individually.
if(COMMAND command-name)
    True if the given name is a command, macro or function that can be invoked.
if(POLICY policy-id)
    True if the given name is an existing policy (of the form CMP<NNNN>).
if(TARGET target-name)
    True if the given name is an existing logical target name such as those created by the add_executable(), add_library(), or add_custom_target() commands.
if(EXISTS path-to-file-or-directory)
    True if the named file or directory exists. Behavior is well-defined only for full paths.
if(file1 IS_NEWER_THAN file2)
    True if file1 is newer than file2 or if one of the two files doesn’t exist. Behavior is well-defined only for full paths. If the file time stamps are exactly the same, an IS_NEWER_THAN comparison returns true, so that any dependent build operations will occur in the event of a tie. This includes the case of passing the same file name for both file1 and file2.
if(IS_DIRECTORY path-to-directory)
    True if the given name is a directory. Behavior is well-defined only for full paths.
if(IS_SYMLINK file-name)
    True if the given name is a symbolic link. Behavior is well-defined only for full paths.
if(IS_ABSOLUTE path)
    True if the given path is an absolute path.
if(<variable|string> MATCHES regex)
    True if the given string or variable’s value matches the given regular expression.
if(<variable|string> LESS <variable|string>)
    True if the given string or variable’s value is a valid number and less than that on the right.
if(<variable|string> GREATER <variable|string>)
    True if the given string or variable’s value is a valid number and greater than that on the right.
if(<variable|string> EQUAL <variable|string>)
    True if the given string or variable’s value is a valid number and equal to that on the right.
if(<variable|string> STRLESS <variable|string>)
    True if the given string or variable’s value is lexicographically less than the string or variable on the right.
if(<variable|string> STRGREATER <variable|string>)
    True if the given string or variable’s value is lexicographically greater than the string or variable on the right.
if(<variable|string> STREQUAL <variable|string>)
    True if the given string or variable’s value is lexicographically equal to the string or variable on the right.
if(<variable|string> VERSION_LESS <variable|string>)
    Component-wise integer version number comparison (version format is major[.minor[.patch[.tweak]]]).
if(<variable|string> VERSION_EQUAL <variable|string>)
    Component-wise integer version number comparison (version format is major[.minor[.patch[.tweak]]]).
if(<variable|string> VERSION_GREATER <variable|string>)
    Component-wise integer version number comparison (version format is major[.minor[.patch[.tweak]]]).
if(DEFINED <variable>)
    True if the given variable is defined. It does not matter if the variable is true or false just if it has been set. (Note macro arguments are not variables.)
if((expression) AND (expression OR (expression)))
    The expressions inside the parenthesis are evaluated first and then the remaining expression is evaluated as in the previous examples. Where there are nested parenthesis the innermost are evaluated as part of evaluating the expression that contains them.

对于if语法,比较常用的就是字符串比较和有没有定义这个变量的比较。

第一种,对于变量是否定义,可以做如下写法:

if(DEFINED var)
else()
endif()

或者
if(var)
else()
endif()

两种都可以验证这个变量有没有定义过,注意:仅仅代表定义过,比如你在CMake命令行中随便写了-Dvar=xxx,就表示定义过了,对里面的值没有做任何限制。

第二种,常用的用法就是字符串比较

if(${var} STREQUAL "ON")
elseif(${var} STREQUAL "OFF")
endif()

但是需要注意的是在这个时候,你的脚本已经假设你对于var已经有了 已被定义的默认要求 !如果没有定义,脚本会报错退出。那有没有解决方法给个默认值呢?有!

option(address "This is a default option for var" ON)

 这样就对var设置了默认值,即使命令行没有定义var,脚本里面也有默认值ON。用户若想更改,就在命令行显示定义:

cmake -Dvar=OFF .

但是在脚本中,这个var是在option之后才会被认为定义,在此之前依然是未定义的!

 

最后贴一段稍微完整点的代码

option(PUBLIC "This is a default option for PUBLIC" OFF)
message(STATUS "build public platform switch: "${PUBLIC})
if(${PUBLIC} STREQUAL "ON")
    message(STATUS "start to build public platform.")
elseif(${PUBLIC} STREQUAL "OFF")
add_definitions(-DPUBLIC_CLOUD_PLATFORM)
message(STATUS "start to build private platform.")
endif(${PUBLIC} STREQUAL "ON")

 

posted @ 2016-07-07 10:00  王彬彬  阅读(9218)  评论(0编辑  收藏

专注于c++后端开发,擅长IM领域。 如有需求请联系邮箱 cugbinbin@163.com

欢迎点击个人自定义博客