cmake execute process before anything else

2019-07-27 04:23发布

I've a problem with CMake executing a process before doing anything else.

The following code snippet shows the situation:

if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/generated")
  file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generated")
  execute_process(
    # This tool generates library sources, which are not known before
    COMMAND "source_file_generator_command"
    # This tool iterates over the generated source tree and creates
    # a proper CMakeLists.txt in the 'generated' directory from the
    # source files found there
    COMMAND "cmake_lists_generator_command"
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generated"
  )
endif()

# At this point the generated subdirectory (with the also
# generated CMakeLists.txt file) shall be included
add_subdirectory(
  "${CMAKE_CURRENT_BINARY_DIR}/generated"
  "${CMAKE_CURRENT_BINARY_DIR}/generated_build"
)
# But the 'add_subdirectory' statement fails due to non-existing
# CMakeLists.txt in the 'generated' source directory at this point

The problem is, as commented above, that the CMakeLists.txt file in the subdirectory which should be added is generated on the fly by a special script (the generated sources are not known before) during the first CMake run. Literally, I need CMake to wait until all the statements within the if/else block are executed and process the add_subdirectory statement not until everything is done (the CMakeLists.txt is generated). Is there an adequate solution for such a use case?

Thanks for your help,

Felix

标签: cmake
1条回答
我想做一个坏孩纸
2楼-- · 2019-07-27 05:16

When several COMMANDs for execute_process are used, they are executed in pipe. From the description of this CMake command:

Runs the given sequence of one or more commands with the standard output of each process piped to the standard input of the next.

If you want true sequential execution of the commands, you need to use one call to execute_process per COMMAND:

execute_process(
    COMMAND "source_file_generator_command"
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generated"
)
execute_process(
    COMMAND "cmake_lists_generator_command"
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generated"
)
查看更多
登录 后发表回答