176

I'm trying to create a simple project on CLion. It uses CMake to generate Makefiles to build project (or some sort of it)

All I need to is transfer some non-project file (some sort of resource file) to binary directory each time when I run the my code.

That file contains test data and application open it to read them. I tried several ways to do so:

  • Via file(COPY ...

    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
            DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/input.txt
    

    Looking good but it work just once and not recopy file after next run.

  • Via add_custom_command

    • OUTPUT version

      add_custom_command(
              OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/input.txt
              COMMAND ${CMAKE_COMMAND} -E copy
                      ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
                      ${CMAKE_CURRENT_BINARY_DIR}/input.txt)
      
    • TARGET version

      add_custom_target(foo)
      add_custom_command(
              TARGET foo
              COMMAND ${CMAKE_COMMAND} copy
                      ${CMAKE_CURRENT_BINARY_DIR}/test/input.txt
                      ${CMAKE_SOURCE_DIR})
      

    But no one of it work.

What am I doing wrong?

starball
  • 20,030
  • 7
  • 43
  • 238
mongolrgata
  • 1,867
  • 2
  • 11
  • 9

8 Answers8

211

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.

tamas.kenez
  • 7,301
  • 4
  • 24
  • 34
  • 14
    Please note that `configure_file` isn't going to work with sub-directories, even if you use GLOB to create a list of files. – Tarantula Nov 15 '18 at 20:09
  • 6
    Also note that this isn't a "file-level dependency" between input and output: it's a dependency between the input and *the cmake configure step*. So instead of a regular build dependency where changing a source file rebuilds only the corresponding object file, this causes the whole of cmake to reconfigure, which may have other side effects. – Seth Johnson Nov 07 '20 at 00:02
  • how we use this command to copy recursive directory files? – H.M Apr 09 '22 at 11:26
112

Both option are valid and targeting two different steps of your build:

  1. file(COPY ... copies the file during the configuration step and only in this step. When you rebuild your project without having changed your cmake configuration, this command won't be executed.
  2. add_custom_command is the preferred choice when you want to copy the file around on each build step.

The right version for your task would be:

add_custom_command(
        TARGET foo POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_SOURCE_DIR}/test/input.txt
                ${CMAKE_CURRENT_BINARY_DIR}/input.txt)

you can choose between PRE_BUILD, PRE_LINK, POST_BUILD best is you read the documentation of add_custom_command

An example on how to use the first version can be found here: Use CMake add_custom_command to generate source for another target

starball
  • 20,030
  • 7
  • 43
  • 238
jenseb
  • 1,593
  • 1
  • 10
  • 13
  • 1
    Is it CMAKE_SOURCE_DIR or CMAKE_CURRENT_SOURCE_DIR? – Syaiful Nizam Yahya Feb 08 '19 at 13:42
  • 4
    @SyaifulNizamYahya use `CMAKE_CURRENT_SOURCE_DIR` if the `test/input.txt` file is relative to the current `CMakeLists.txt` file. If it's relative to the root `CMakeLists.txt`, then use `CMAKE_SOURCE_DIR`. – Mark Ingram Apr 10 '19 at 08:27
  • Is there a way to get the targets filename in cmake? without hardcoding the filename? e.g. I'm building a lib file, does cmake have a variable to get the targte filename? like `libSomething.so`? – Hossein Sep 22 '20 at 06:10
  • 1
    Note: to copy a directory recursively instead of a file, use `${CMAKE_COMMAND} -E copy_directory` – jorisv92 Jan 13 '21 at 10:21
  • @jorisv92 how would you force copy or copy if different using copy_directory or some other recursive operation? – hiradyazdan May 05 '21 at 13:59
25

The first of option you tried doesn't work for two reasons.

First, you forgot to close the parenthesis.

Second, the DESTINATION should be a directory, not a file name. Assuming that you closed the parenthesis, the file would end up in a folder called input.txt.

To make it work, just change it to

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
     DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
German Capuano
  • 5,183
  • 6
  • 23
  • 35
12

I would suggest TARGET_FILE_DIR if you want the file to be copied to the same folder as your .exe file.

$ Directory of main file (.exe, .so.1.2, .a).

add_custom_command(
  TARGET ${PROJECT_NAME} POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy 
    ${CMAKE_CURRENT_SOURCE_DIR}/input.txt 
    $<TARGET_FILE_DIR:${PROJECT_NAME}>)

In VS, this cmake script will copy input.txt to the same file as your final exe, no matter it's debug or release.

  • This is great, but only guaranteed to work with POST_BUILD. Reference: https://gitlab.kitware.com/cmake/cmake/-/issues/21364#note_849331 – phcerdan Jun 02 '22 at 19:03
11

The suggested configure_file is probably the easiest solution. However, it will not rerun the copy command to if you manually deleted the file from the build directory. To also handle this case, the following works for me:

add_custom_target(copy-test-makefile ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/input.txt)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/input.txt
                   COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
                                                    ${CMAKE_CURRENT_BINARY_DIR}/input.txt
                   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
ar31
  • 3,576
  • 1
  • 25
  • 22
  • 1
    IMO this is the only correct solution here. The first command tells cmake what files you need to exist and the second command tells cmake how to create them. – Watcom May 29 '21 at 20:44
11

if you want to copy folder from currant directory to binary (build folder) folder

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/yourFolder/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/yourFolder/)

then the syntexe is :

file(COPY pathSource DESTINATION pathDistination)
Amirouche Zeggagh
  • 3,428
  • 1
  • 25
  • 22
9

This is what I used to copy some resource files: the copy-files is an empty target to ignore errors

 add_custom_target(copy-files ALL
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_BINARY_DIR}/SOURCEDIRECTORY
    ${CMAKE_BINARY_DIR}/DESTINATIONDIRECTORY
    )
Cory Lewis
  • 559
  • 5
  • 6
  • 3
    I would also add `add_dependencies(MainTarget copy-files)` to make it run automatically when I build MainTarget – IC_ Apr 21 '20 at 03:43
  • This seems like the best answer (+Herrgott's comment) as it actually ensures that the current version of the source is always in the destination post build. For small copy jobs this works well, thanks. Putting `add_dependencies(MainTarget copy-files)` in the root `CMakeLists.txt` file means that this can be used throughout the project. – satnhak Jun 03 '20 at 21:33
0

If you want to put the content of example into install folder after build:

code/
  src/
  example/
  CMakeLists.txt

try add the following to your CMakeLists.txt:

install(DIRECTORY example/ DESTINATION example)
lingjiankong
  • 355
  • 3
  • 5
  • 1
    But the 'install' command has a side effect: the installed files will appear in the cpack package . . . – gary133 May 15 '22 at 03:18
  • This is a correct answer to a different question. :) Build (`cmake --build ...`) is a separate stage from install (`cmake --install...`). In particular, VSCode normally runs only the build command and debugs the program directly from `CMAKE_BINARY_DIR`. The question is kinda mushy at which stage, configure or build, the file needs to be copied, but the install phase is certainly too late. Yes the files will require separate install description with the `install()` command, best using target properties in the Modern CMake framework, but that's out of scope. – kkm inactive - support strike Dec 05 '22 at 06:11