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有多種表達方式。布爾比較,字符串比較,數值比較,複合表達式等。羅列一下:html
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語法,比較經常使用的就是字符串比較和有沒有定義這個變量的比較。express
第一種,對於變量是否認義,能夠作以下寫法:less
if(DEFINED var) else() endif() 或者 if(var) else() endif()
兩種均可以驗證這個變量有沒有定義過,注意:僅僅表明定義過,好比你在CMake命令行中隨便寫了-Dvar=xxx,就表示定義過了,對裏面的值沒有作任何限制。ide
第二種,經常使用的用法就是字符串比較ui
if(${var} STREQUAL "ON") elseif(${var} STREQUAL "OFF")
endif()
可是須要注意的是在這個時候,你的腳本已經假設你對於var已經有了 已被定義的默認要求 !若是沒有定義,腳本會報錯退出。那有沒有解決方法給個默認值呢?有!lua
option(address "This is a default option for var" ON)
這樣就對var設置了默認值,即便命令行沒有定義var,腳本里面也有默認值ON。用戶若想更改,就在命令行顯示定義:spa
cmake -Dvar=OFF .
可是在腳本中,這個var是在option以後纔會被認爲定義,在此以前依然是未定義的!命令行
最後貼一段稍微完整點的代碼code
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")