How to Copy Contents of a Directory into Build Directory After Make with Cmake

How to copy contents of a directory into build directory after make with CMake?

You can use add_custom_command.

Say your target is called MyTarget, then you can do this:

add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/config/ $<TARGET_FILE_DIR:MyTarget>)

This executes every time you build MyTarget and copies the contents of "/config" into the directory where the target exe/lib will end up.

As Mark Lakata points out in a comment below, replacing PRE_BUILD with POST_BUILD in the add_custom_command ensures that copying will only happen if the build succeeds.

Explanation

  • ${CMAKE_COMMAND} is the path to CMake
  • -E makes CMake run commands instead of building
  • copy_directory is a Command-Line Tool
  • config is the directory (that falls under the root of the project) who's contents will be copied into the build target
  • $<TARGET_FILE_DIR:MyTarget> is a generator expression, described in the add_custom_command documentation.

Copy file from source directory to binary directory using CMake

You may consider using configure_file with the COPYONLY option:

configure_file(<input> <output> COPYONLY)

Unlike file(COPY ...) it creates a file-level dependency between input and output, that is:

If the input file is modified the build system will re-run CMake to re-configure the file and generate the build system again.

Copy all files with given extension to output directory using CMake

I've found the solution by myself:

file(GLOB MY_PUBLIC_HEADERS
"myDir/*.h"
)
file(COPY ${MY_PUBLIC_HEADERS} DESTINATION myDestination)

CMake: How to copy different files to build directory according to configuration and architecture

try this:

set(arch "i386")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(arch "x64")
endif()

set(FILES_TO_COPY "${CMAKE_SOURCE_DIR}/Externals/DLLs/bdb/$<CONFIG>/${arch}/libdb52$<$<CONFIG:Debug>:d>.dll")

Automatically copy executable to directory after build in CLion? (using CMake)

I don't know about CLion but generally you can add this as a post-build step to an executable target in CMake with:

add_executable(MyExe ...)
add_custom_command(TARGET MyExe
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:MyExe> SomeOtherDir)

See e.g. Copy target file to another location in a post build step in CMake

How to copy directory from source tree to binary tree?

With CMake 2.8 or later, use the file(COPY ...) command.

With CMake versions below 2.8, the following macro copies files from one directory to another. If you don't want to substitute variables in the copied files, then change the configure_file @ONLY argument (for example to COPYONLY).

# Copy 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)
endif(NOT IS_DIRECTORY ${srcTemplatePath})
endforeach(templateFile)
endmacro(configure_files)

Copy target file to another location in a post build step in CMake

Rather than using the obsolete LOCATION property, prefer using generator expressions:

add_custom_command(TARGET mylibrary POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mylibrary> ${targetfile}
)

You could also just generate the exe in the target directory directly by setting the target property RUNTIME_OUTPUT_DIRECTORY instead of copying it. This has per-configuration options (e.g. RUNTIME_OUTPUT_DIRECTORY_DEBUG).

set_target_properties(mylibrary PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG <debug path>
RUNTIME_OUTPUT_DIRECTORY_RELEASE <release path>
)

For further details run:

cmake --help-property "RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY_<CONFIG>"

Also, you should be able to use forward slashes throughout for path separators, even on Windows.



Related Topics



Leave a reply



Submit