CMake: execute a macro/function as the command of add_custom_command

南楼画角 提交于 2019-12-05 22:10:51

To prevent that function to run, just wrap it into if:

if(NOT EXISTS ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp)
   run_your_provided_command(BLAH_BLAH)
endif()

Easy!

Update: To run it when config file has changed just use little more complicated condition:

if(
   NOT EXISTS ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp OR
   ${CMAKE_SOURCE_DIR}/blah-blah.config IS_NEWER_THAN ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp
  )
...

and use add_dependencies command to make sure your binary will be rebuild in case of config file modifications:

add_executable(
    YourBinary
    ...
    ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp
  )
add_dependencies(YourBinary ${CMAKE_SOURCE_DIR}/blah-blah.config)
cromod

Take a look to this SO post.

You can call your function in a separate CMake script, call this script with add_custom_target and cmake -P then add a dependency to your binary :

add_custom_target(run_script COMMAND ${CMAKE_COMMAND} -P separate_script.cmake)
add_executable(your_binary ...)
# or add_library(your_binary ...)
add_dependencies(your_binary run_script)

Is there a way to pass a parameter to the separate_script.cmake?

You can use the cmake variables to pass values when you call the script e.g.

"COMMAND ${CMAKE_COMMAND} -DPARAM=value -P separate_script.cmake" 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!