Running a bash command via CMake

天大地大妈咪最大 提交于 2019-11-30 01:10:44

问题


I'm trying to have CMake either run three bash commands or a bash script. However, I can't seem to get it to work.

The bash commands are:

    cd ${CMAKE_SOURCE_DIR}/dependencies/library
    make
    cd ${CMAKE_BINARY_DIR}

Essentially, I would like CMake to build the library in that directory if it does not already exist.

Here's the CMake code I tried:

if(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
   execute_process(COMMAND cd ${CMAKE_SOURCE_DIR}/dependencies/library)
   execute_process(COMMAND make)
   execute_process(COMMAND cd ${CMAKE_BINARY_DIR})
endif(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")

However, it's not building anything. What am I doing wrong?

Also, while I'm here asking this: should the third command, to move to the binary folder, be included?

Thanks!


回答1:


execute_process() is executed during configure time. But you want this to run at build time, thus add_custom_command() and add_custom_target() is what you're looking for.

In this special case you want to generate an output file, so you should go for add_custom_command() (both are essentially the same, but command produces one or multiple output files, while target does not.

The cmake snippet for this should look something like the following:

add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/dependencies/library/lib.o
    WORKING_DIR ${CMAKE_SOURCE_DIR}/dependencies/library
    COMMAND make
)

You then have to add the output file in another target as dependency, and everything should (hopefully) work as expected.

You can also add DEPENDS statements to the add_custom_command() call to rebuild the object file in case some input sources have changed.



来源:https://stackoverflow.com/questions/25687890/running-a-bash-command-via-cmake

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