问题
I want to take advantage of Ninja's "job pools" in my custom commands; something finally directly supported in cmake 3.15.0. Many/most people won't have this version, so I would like to simply ADD this support without requiring that anyone update their cmake version.
A more general question is...
"What's the best way to specify a conditional clause of a custom command …?"
add_custom_command(
OUTPUT foo
COMMAND ${CMAKE_COMMAND} -E touch foo
if("${CMAKE_VERSION}" STRGREATER_EQUAL "3.15.0") # <-- syntax error
JOB_POOL my_job_pool # <-- syntax error
endif() # <-- syntax error
VERBATIM
)
Maybe...?
if("${CMAKE_VERSION}" STRGREATER_EQUAL "3.15.0")
set(USE_JOB_POOL JOB_POOL my_job_pool)
endif()
add_custom_command(
OUTPUT foo
COMMAND ${CMAKE_COMMAND} -E touch foo
${USE_JOB_POOL}
VERBATIM
)
Or...?
add_custom_command(
OUTPUT foo
COMMAND ${CMAKE_COMMAND} -E touch foo
$<IF:$<VERSION_GREATER_EQUAL:${CMAKE_VERSION},3.15.0>:JOB_POOL my_job_pool>
VERBATIM
)
回答1:
According to @Tsyvarev, my approach is reasonable. In my code, which "works", I implemented something similar to this:
set(USE_JOB_POOL 0)
if("${CMAKE_GENERATOR}" STREQUAL "Ninja" AND
"${CMAKE_VERSION}" STRGREATER_EQUAL "3.15.0")
set(USE_JOB_POOL JOB_POOL my_job_pool)
endif()
...
add_custom_command(
OUTPUT foo
COMMAND ${CMAKE_COMMAND} -E touch foo
$<${JOB_POOL}:${JOB_POOL}>
VERBATIM
)
来源:https://stackoverflow.com/questions/56800326/conditional-clause-of-add-custom-command