cmake3 external project add step loop

305 views Asked by At

Currently I have External_Project_Add_Step to copy directory from "source dir" to "dest dir". Lately I have realized that I am copying around 4 GB for data in this process, while I would actually need to copy selective files ( many files but total size ~1 GB).

 ExternalProject_Add_Step(${EXTERNAL_TARGET} lib_step 
         COMMAND ${CMAKE_COMMAND} -E make_directory ${TGT_DIR}
         COMMAND  ${CMAKE_COMMAND} -E copy_directory ${SRC_DIR} ${TGT_DIR}
         COMMENT "Copying lib from ${SRC_DIR} to  ${TGT_DIR}"
         DEPENDEES install)

However I do have list of files which I want to copy. I was wondering if I can put loop inside External_Project_AddStep to have single step to copy all the files required. Something as below (It doesn't work though)

ExternalProject_Add_Step(${EXTERNAL_TARGET} lib_step 
         COMMAND ${CMAKE_COMMAND} -E make_directory ${TGT_DIR}
         foreach(copy_file ${ALL_FILES})
            file(copy ${SRC_DIR}/${copy_file} ${TGT_DIR})
         endforeach(copy_file)
         COMMENT "Copying lib from ${SRC_DIR} to  ${TGT_DIR}"
         DEPENDEES install)

I believe probable solution could be have script being called via ExternalProject_Add_Step which does copy internally or have loop over ExternalProject_Add_Step with different step name. Not sure which is cleaner/better approach

1

There are 1 answers

0
Tsyvarev On

In CMake functions calls and other constructions cannot be nested one into another. So, using foreach() command inside ExternalProject_Add_Step parameters is wrong.

But you may use foreach() for generate arguments for ExternalProject_Add_Step:

# This variable will contain list of 'COMMAND' clauses:
#    COMMAND cmake -P copy <file-src> <file-dst>
set(COMMAND_COPY_FILES)
foreach(copy_file ${ALL_FILES})
    list(APPEND COMMAND_COPY_FILES
        COMMAND ${CMAKE_COMMAND} -E copy ${SRC_DIR}/${copy_file} ${TGT_DIR}/${copy_file}
    )
endforeach(copy_file)

ExternalProject_Add_Step(${EXTERNAL_TARGET} lib_step 
     COMMAND ${CMAKE_COMMAND} -E make_directory ${TGT_DIR}
     ${COMMAND_COPY_FILES}
     COMMENT "Copying lib from ${SRC_DIR} to  ${TGT_DIR}"
     DEPENDEES install)

In case of your files are direct childs of the source directory (that is, ALL_FILES does not contain subdirectories), you may use single invocation of copy:

# This variable will contain single 'COMMAND' clause but with many files:
#    COMMAND cmake -P copy <src-files> <dest-dir>
set(COMMAND_COPY_FILES COMMAND ${CMAKE_COMMAND} -E copy)
foreach(copy_file ${ALL_FILES})
    list(APPEND COMMAND_COPY_FILES ${SRC_DIR}/${copy_file})
endforeach(copy_file)
list(APPEND COMMAND_COPY_FILES ${TGT_DIR})