如何在 CMake 中将多个命令的输出收集到一个文件中?

How to collect output of several commands into one file in CMake?

我正在尝试将 Git 信息收集到文件中并正在做

execute_process(
    COMMAND git log -1 --format=full && echo "Modified files:" && git ls-files -m
    OUTPUT_FILE "git_log"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )

execute_process(
    COMMAND git log -1 --format=full 
    COMMAND echo "Modified files:"
    COMMAND git ls-files -m
    OUTPUT_FILE "git_log"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )

并且在这两种情况下都看不到完整内容。在第一种情况下,我看到空文件,在第二种情况下,我只看到最后一条命令的输出。

如何将所有内容收集到一个文件中?

in first case I see empty file

当然 - 没有使用 shell,并且 &&git 的无效参数,命令失败。请参阅文档 No intermediate shell is used, so shell operators such as > are treated as normal arguments

in second -- only output of last command.

当然 - 请参阅文档 Commands are executed concurrently as a pipeline, with the standard output of each process piped to the standard input of the nextgit ... | echo ... | git ... 只会输出最后一个 - gitecho 忽略标准输入。


execute_process(
    COMMAND sh -c "
       git log -1 --format=full && 
       echo \"Modified files:\" &&
       git ls-files -m"
    OUTPUT_FILE "git_log1"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )

execute_process(
    COMMAND git log -1 --format=full 
    OUTPUT_FILE "git_log1"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )
execute_process(
    COMMAND echo ...
    OUTPUT_FILE "git_log2"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )
execute_process(
    COMMAND git ...
    OUTPUT_FILE "git_log3"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )
execute_process(
    COMMAND ${CMAKE_COMMAND} -E cat git_log1 git_log2 git_log3
    OUTPUT_FILE "git_log"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    )