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

喜你入骨 提交于 2021-02-04 18:09:29

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/56001683/in-cmake-how-do-turn-a-multi-line-output-of-a-command-into-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!