1

I am trying to copy files from one destination to another by using configure_file. I found the following solution How to copy directory from source tree to binary tree?.

function(USR_copy_directory srcDir destDir)
    make_directory(${destDir})
    file(GLOB_RECURSE files RELATIVE ${srcDir} ${srcDir}/*)
    foreach(file ${files})
        set(srcFile ${srcDir}/${file})
        if(NOT IS_DIRECTORY ${srcFile})
            configure_file(${srcFile} ${destDir}/${file} COPYONLY)
        endif(NOT IS_DIRECTORY ${srcFile})
    endforeach(file)
endfunction()

This solution allowed me to do the job. But when I tried to place the for in another function it stopped creating directories. It just flat copied the files without preserving the structure. Basically both snippets of code are the same, it's just I remove the for loop and placed it in another function, that's all. What am I doing wrong?

function(USR_copy_directory srcDir destDir)
    make_directory(${destDir})
    file(GLOB_RECURSE files RELATIVE ${srcDir} ${srcDir}/*)
    set(srcFile "")
    foreach(file ${files}) #this for loop allows me to append file and path
        list(APPEND srcFile "${srcDir}/${file}")
    endforeach(file)
    USR_copy_files("${srcFile}" ${destDir})
endfunction()


function(USR_copy_files files destDir)
    foreach(file ${files})
        if(NOT IS_DIRECTORY ${file})
            get_filename_component(filename ${file} NAME)
            configure_file(${file}  ${destDir}/${filename} COPYONLY)
        endif(NOT IS_DIRECTORY ${file})
    endforeach(file)
endfunction()

Kevin
  • 16,549
  • 8
  • 60
  • 74
gringo
  • 373
  • 2
  • 4
  • 15

1 Answers1

0

It copies files recursivly (with sub-directories from the srcDir to the destDir and it is based on the How to copy directory from source tree to binary tree? answer

# Copies files from source directory to destination directory, substituting any
# variables.  Create destination directory if it does not exist.

macro (configure_files srcDir destDir)
    message (STATUS "Configuring directory ${destDir}")
    make_directory (${destDir})

    file (GLOB templateFiles RELATIVE ${srcDir} "${srcDir}/*")
    foreach (templateFile ${templateFiles})
        set(srcTemplatePath ${srcDir}/${templateFile})
        if (NOT IS_DIRECTORY "${srcTemplatePath}")
            message(STATUS "Configuring file ${templateFile}")
            configure_file(
                    "${srcTemplatePath}"
                    "${destDir}/${templateFile}"
                    @ONLY)
        else (NOT IS_DIRECTORY "${srcTemplatePath}")
            configure_files("${srcTemplatePath}" "${destDir}/${templateFile}")
        endif (NOT IS_DIRECTORY "${srcTemplatePath}")
    endforeach (templateFile ${templateFiles})
endmacro (configure_files srcDir destDir)
  • what? Sorry i don't understand u just copied my question? – gringo Apr 15 '19 at 07:46
  • No, of course. Look at the following if statement: if (NOT IS_DIRECTORY "${srcTemplatePath}") <...> else (NOT IS_DIRECTORY "${srcTemplatePath}")<...>endif (NOT IS_DIRECTORY "${srcTemplatePath}") It allows us to process directories tree recursively with the tree structure preserving – Boris Uskov Apr 16 '19 at 07:14
  • P.S. It is easily to extend this method with third parameter mask and replace "${srcDir}/*" with "${srcDir}/${mask}" to filter files by the specified mask – Boris Uskov Apr 16 '19 at 07:19
  • I've understand what is the trouble in your code. You do not call USR_copy_directory in a recursive way. Because of this you do not process subdirectories, but only files. Fix this problem in your code or use my one – Boris Uskov Apr 16 '19 at 07:25