cmake执行过程之前没有其他 [英] cmake execute process before anything else

查看:88
本文介绍了cmake执行过程之前没有其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行其他操作之前执行CMake时遇到问题。

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

问题是,如上所述,应该添加的子目录中的CMakeLists.txt文件是由特殊程序动态生成的脚本(第一次生成时不知道生成的源)。从字面上看,我需要CMake等待,直到执行if / else块中的所有语句,然后再处理add_subdirectory语句,直到一切都完成为止(生成CMakeLists.txt)。

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?

感谢您的帮助,

Felix

推荐答案

当多个 COMMAND s用于 execute_process ,它们在管道中执行。从此CMake命令的说明

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


运行给定序列的一个或多个命令,每个过程的标准输出 piped 到下一个的标准输入。 / p>

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

如果要真正顺序执行命令,则需要使用调用<每个命令code> execute_process

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

这篇关于cmake执行过程之前没有其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆