Conditional Clause of add_custom_command

為{幸葍}努か 提交于 2021-02-17 05:37:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!