cmake在其他任何事情之前执行进程

时间:2016-03-10 18:21:37

标签: cmake

在执行任何其他操作之前,我遇到了CMake执行进程的问题。

以下代码段显示了这种情况:

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

问题是,如上所述,应该添加的子目录中的CMakeLists.txt文件是在第一次CMake运行期间由特殊脚本(之前未知生成的源)动态生成的。从字面上看,我需要CMake等到if / else块中的所有语句都被执行并处理add_subdirectory语句,直到一切都完成(生成CMakeLists.txt)。这种用例是否有足够的解决方案?

感谢您的帮助,

菲利克斯

1 个答案:

答案 0 :(得分:0)

当使用COMMAND的多个execute_process时,它们会在管道中执行。从此CMake命令的description

  

使用每个进程管道的标准输出将一个或多个命令的给定序列运行到下一个的标准输入。

如果您希望执行命令的真正顺序,则需要使用每个命令execute_process一次调用

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"
)