First, basic grammar
if(<condition>)
<commands>
elseif(<condition>) # optional block, can be repeated
<commands>
else(a)# optional block
<commands>
endif(a)Copy the code
If-else statements are used if you need to conditionally compile an item.
Two, basic usage
For example, you need to execute different code depending on the operating system:
# CMakeLists.txt
# CMake Minimum version requirements
cmake_minimum_required(VERSION 3.5)
# Project name
project(test_6)
if(WIN32)
message(STATUS "Now is windows")
elseif(APPLE)
message(STATUS "Now is Apple systens.")
elseif(UNIX)
message(STATUS "Now is UNIX-like OS's.")
endif(a)Copy the code
Other usage
1. Logical operation
# take the inverse operation
if(NOT <condition>)
# and operation
if(<cond1> AND <cond2>)
# or operation
if(<cond1> OR <cond2>)
Copy the code
2. Special statements
if(COMMAND command-name) if the given name is a command, macro, or function that can be calledtrue.if(POLICY policy-id) if the given name is an existing policy in the format of CMP<NNNN>True 。
if(TARGET target-name) if the given name is an existing logical target name created by invoking the callTrue.add_executable(),add_library(), eitheradd_custom_target() The command that has been invoked (in any directory).if(TEST test-name) if the given name is an existing test name created by the testerTrue add_test() command.if(EXISTS path-to-file-or-directory) If the specified file or directory existsTrue. Define behavior only for full paths. Resolves symbolic links, that is, if the named file or directory is a symbolic link, and returns if the target of the symbolic link existstrue.if(file1 IS_NEWER_THANFile2) if file1 is a newer version of file2 or one of the two files does not existtrue. Define behavior only for full paths. If the file timestamps are identical, thenIS_NEWER_THANThe comparison will returntrue, so that any related build operations will take place in the case of a tie. This includes passing the same file name for file1 and file2.if(IS_DIRECTORYPath-to-directory) if the given name is a directoryTrue. Define behavior only for full paths.if(IS_SYMLINK file-name) if the given name is a symbolic linkTrue. Define behavior only for full paths.if(IS_ABSOLUTEPath) if the given path is an absolute pathTrue.if(<variable|string> MATCHESRegex) if the value of the given string or variable matches the given general conditiontrue. For regular expression formatting, see the regular expression specification. The () group is captured in the CMAKE_MATCH_<n> variable.if(<variable|string> LESS <variable|string>) if the value of the given string or variable is a significant number and less than the number on the righttrue.if(<variable|string> GREATER <variable|string>) if the value of the given string or variable is a significant number and greater than the number on the righttrue.if(<variable|string> EQUAL <variable|string>) if the value of the given string or variable is a significant number and equal to the number on the righttrue.if(<variable|string> LESS_EQUAL <variable|string>) if the value of the given string or variable is a significant number and less than or equal to the number on the righttrue.if(<variable|string> GREATER_EQUAL <variable|string>) if the value of the given string or variable is a significant number and greater than or equal to the number on the righttrue.if(<variable|string> STRLESS <variable|string>) if the given string or variable has a dictionary value less than the string or variable on the righttrue.if(<variable|string> STRGREATER <variable|string>) if the value of the given string or variable is greater in the dictionary than the string or variable on the righttrue.if(<variable|string> STREQUAL <variable|string>) if the value of the given string or variable is dictionary equal to the string or variable on the righttrue.if(<variable|string> STRLESS_EQUAL <variable|string>) if the value of the given string or variable is less than or equal to the string or variable on the right in the dictionarytrue.if(<variable|string> STRGREATER_EQUAL <variable|string>) if the value of the given string or variable is greater than or equal to the string or variable on the right in the dictionarytrue.if(<variable|string> VERSION_LESS <variable|string>) by integer version number (the version format is major[.minor[.patch[.tweak]]]. Omitted components are regarded as zero). Any non-integer version component or the non-integer ending of the version component effectively truncates the string at that point.if(<variable|string> VERSION_GREATER <variable|string>) by integer version number (the version format is major[.minor[.patch[.tweak]]]. Omitted components are regarded as zero). Any non-integer version component or the non-integer ending of the version component effectively truncates the string at that point.if(<variable|string> VERSION_EQUAL <variable|string>) by integer version number (the version format is major[.minor[.patch[.tweak]]]. Omitted components are regarded as zero). Any non-integer version component or the non-integer ending of the version component effectively truncates the string at that point.if(<variable|string> VERSION_LESS_EQUAL <variable|string>) by integer version number (the version format is major[.minor[.patch[.tweak]]]. Omitted components are regarded as zero). Any non-integer version component or the non-integer ending of the version component effectively truncates the string at that point.if(<variable|string> VERSION_GREATER_EQUAL <variable|string>) by integer version number (the version format is major[.minor[.patch[.tweak]]]. Omitted components are regarded as zero). Any non-integer version component or the non-integer ending of the version component effectively truncates the string at that point.if(<variable|string> IN_LIST<variable>) if the given element is contained in a named list variabletrue.if(DEFINED< name > | CACHE {< name >} | ENV {< name >}) if the < name > definition with the given variables, CACHE variable or environment variables, it istrue. The value of the variable is irrelevant. Note that macro parameters are not variables.if((condition) AND (condition OR(condition))) evaluates the conditions in parentheses first, and then the remaining conditions as in the previous example. If there are nested parentheses, the innermost parentheses are evaluated as part of the condition that contains them.Copy the code
Reference: cmake.org/cmake/help/… Blog.csdn.net/sean_8180/a…