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

Viewed 71

I am trying to collect Git information into file and am doing

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

or

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

And in both cases don't see full content. In first case I see empty file and in second -- only output of last command.

How to gather all in one file?

1 Answers

in first case I see empty file

Sure - no shell is used, and && is invalid argument to git, the command fails. See documentation No intermediate shell is used, so shell operators such as > are treated as normal arguments.

in second -- only output of last command.

Sure - see documentation Commands are executed concurrently as a pipeline, with the standard output of each process piped to the standard input of the next. git ... | echo ... | git ... will only output the last one - git and echo ignore standard input.


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

or

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}
    )
Related