Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CMake how do turn a multi line output of a command into a list?

I want to do do something like this

execute_process(
    COMMAND bash -c "git --git-dir ${CMAKE_SOURCE_DIR}/.git ls-files"
OUTPUT_VARIABLE TRACKED_FILES)

add_custom_target(all_file_project SOURCES ${TRACKED_FILES})

And the command itself seems to work as expected but the generated variable "TRACKED_FILES" contains only one logical entry (one multi line string) rather than a list of files.

Can I somehow turn a string containing multiple lines separated by a newline ("\n") into a list in CMake?

like image 200
frans Avatar asked May 06 '19 08:05

frans


1 Answers

One option (as the title of my question suggests) is to actively split the string manually rather than interpreting a variable as list in the first place:

string(REPLACE "\n" ";" ADDITIONAL_PROJECT_FILES_LIST ${ADDITIONAL_PROJECT_FILES})

This works for me but it would be very nice to have something more abstract and less platform specific (e.g. I don't know whether this works on all OSes including Windows)

Something like execute_process(COMMAND find -type f OUTPUT_LIST_VARIABLE MY_LIST)

Or at least set(MY_LIST FROM_MULTILINE MY_MULTILINE_STRING)

like image 149
frans Avatar answered Oct 21 '22 05:10

frans