[Writing in front]
CMake's generator expressions are used to perform conditional judgments and logical operations at the build system level, and they are typically used in the context of target properties and generator expressions. These expressions allow you to customize the build process for different platforms, configurations, or compilers.
Links to documents cited in this article:
cmake Generator Expressions (7) - CMake 3.26.4 Documentation
[Text Begins]
This post will coverConditional Expressions
respond in singingLogical Operators
。
- Conditional expressions:
A basic class of generator expressions relates to conditional logic. Two forms of conditional generator expressions are supported:
$<condition:true_string>
in the event that
condition
is "1", it evaluates to "true_string", if thecondition
evaluates to "0", then evaluates to an empty string.condition
of any other value will result in an error.
$<IF:condition,true_string,false_string>
Added in version 3.8.
in the event that
condition
is "1", it evaluates to "true_string", if thecondition
is "0", it evaluates to "false_string".condition
of any other value will result in an error.
Usually.condition
itself is a generator expression. For example, when usingDebug
When configured, the following expression expands toDEBUG_MODE
, all other configurations are empty strings:
$<$<CONFIG:Debug>:DEBUG_MODE>
Example of use:
#conditional expression $<condition:true_string>
#equivalenceC++ if (condition == "1") return true_string else return ""
add_custom_command(
TARGET MyExecutable PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "[1]: WIN32: ${WIN32} | $<${WIN32}:is win32> | $<0:is 0>"
)
#conditional expression $<IF:condition,true_string,false_string>
#equivalenceC++ condition ? true_string : false_string
set(IS_DEBUG "1")
add_custom_command(
TARGET MyExecutable PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "[2]: IS_DEBUG: ${IS_DEBUG} | $<IF:${IS_DEBUG},is debug,is release>"
)
apart from1
maybe0
Boolean-like other thancondition
The value can be changed by using the$<BOOL:...>
Generator expressions wrap them for processing:
$<BOOL:string>
commander-in-chief (military)
string
convert to0
maybe1
. If any of the following is true, the evaluation is "0":
string
is empty.
string
is the case-insensitive equivalent of0
、FALSE
、OFF
、N
、NO
、IGNORE
maybeNOTFOUND
string
suffix-NOTFOUND
Ending (case sensitive).Otherwise it is calculated as "1".
When CMake variables providecondition
When using the$<BOOL:...>
Generator expression:
$<$<BOOL:${HAVE_SOME_FEATURE}>:-DENABLE_SOME_FEATURE>
Example of use:
#conditional expression $<BOOL:string>
#equivalenceC++ if (string) return "1" else return "0"
set(STRING2BOOL "not empty")
add_custom_command(
TARGET MyExecutable PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "[3]: STRING2BOOL: ${STRING2BOOL} | $<BOOL:${STRING2BOOL}> | $<$<BOOL:${STRING2BOOL}>:STRING2BOOL is not empty>"
)
The output of the three examples is as follows:
- logical operator
Common Boolean logical operators are supported:
$<AND:conditions>
included among these
conditions
is a comma-separated list of Boolean expressions, all of which must be computed as1
maybe0
If all the conditions are "1", the result of the whole expression is "1". If all the conditions are "1", the result of the whole expression is "1". If any condition is "0", the result of the whole expression is "0".
$<OR:conditions>
included among these
conditions
is a comma-separated list of Boolean expressions. All of these must evaluate to "1" or "0". Ifcondition
at least one of them is "1", then the result of the whole expression is "1". If allcondition
The result of the whole expression is "0".
$<NOT:condition>
condition
must be0
maybe1
. Ifcondition
is "1", the result of the expression is "0", otherwise it is "1".
Example of use:
#logical operator $<AND:conditions>
#equivalenceC++ if (condition && condition && ...) return "1" else return "0"
add_custom_command(
TARGET MyExecutable PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "[1]: (1 && 0 && 1) = $<AND:1,0,1>"
)
#logical operator $<OR:conditions>
#equivalenceC++ if (condition || condition || ...) return "1" else return "0"
add_custom_command(
TARGET MyExecutable PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "[2]: (1 || 0 || 0) = $<OR:1,0,0>"
)
#logical operator $<NOT:condition>
#equivalenceC++ if (!condition) return "1" else return "0"
add_custom_command(
TARGET MyExecutable PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "[3]: !1 = $<NOT:1>"
)
The results are as follows:
[Conclusion]
Link to the project (more star ah.... ⭐_⭐):
Github Address:/mengps/LearnCMake