Patrick Storz
Patrick Storz

Reputation: 495

CMake: install(FILES ...) for a file containing a list of files

Consider I have a simple text file containing a list of (absolute) file paths.

Is there any easy way to make CMake install those files (as if install(FILES ...) was used)?
Directory structure (minus some constant leading path components) should be maintained.


Right now the only option I could come up with was to use

install(CODE "execute_process(COMMAND my_script.sh)")

and do it using normal shell commands, but that seems to defeat the purpose of using a build system in the first place...

Upvotes: 1

Views: 505

Answers (1)

RCYR
RCYR

Reputation: 1492

I believe this would do the trick:

# 'filename' is the file that contains a list ';' separated paths relative to that input file
function(install_my_files filename)
    file(READ ${filename} relative_paths)
    get_filename_component(parent_directory ${filename} DIRECTORY) # parent directory of input file
    foreach(relative_path ${relative_paths})
        get_filename_component(relative_directory ${relative_path} DIRECTORY)
        install(FILES "${parent_directory}/${relative_path}" DESTINATION ${relative_directory})
    endforeach()
endfunction()

Upvotes: 2

Related Questions