0

I am trying to copy a directory of files after my project is built, every time my project is built.

In my project's CMakeLists.txt file I have the following:

# Copy resources
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BUILD_DIR}/)

This works the first time the project is built, and it works anytime I call make in the directory that CMAKE has generated the makefile in.

However, in my IDE (CLion) I think there is some sort of caching / checking to see if the project is already built.

As a result, if I only change an asset file, and not the underlying code, the files are never copied to the location of the binary.

Is there a way to force a post-build script to be executed after every time build is called?

Or, put another way, is there a way to force the CMakeLists.txt file to be every time I build my project?

This is specific to CLion but concerns cmake more generally.

I am using CMAKE 3.9.1

Thanks

Startec
  • 12,496
  • 23
  • 93
  • 160

1 Answers1

0

File(COPY file path) is executed at configure time, one of the three main phase of CMake flow Configure -> build -> install

If you want to execute a command after a build there is two ways to do it.

First with (Probably the better one)

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

Will copy the asset directory, to artifactDirectory/assets. You need to precise the assets directory in the destination. CMake documentation isn't that clear on this point

copy_directory ... Copy directories to directory. If directory does not exist it will be created.

Reference is there : add_custom_command (3.9.6)

Second with a custom target that execute at the end of the build of every targets and so depends on all targets.

Syntax would be

add_custom_target( MyTarget ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets $<TARGET_FILE_DIR:MyTarget>/assets DEPENDS MyOtherTargets)`

Will create a target called MyTarget that execute, a command when builded after target or files it depends on are builded/generated.

(This command have some unexpected behaviours when a project is built, with multiple cores.)

Reference is there : add_custom_target (3.9.6)

For information my environment is CLION 2017.3 and CMake 3.10

Noki
  • 383
  • 1
  • 9
  • In your answer you do not say how to copy files. I tried: `add_custom_command(TARGET MyTarget PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets $)` but I had the same issue. It looks like my IDE is caching the results of the build. i.e. something like message("this ran") is not even printed. – Startec Mar 02 '18 at 07:30
  • PRE BUILD will be copied before build not after . I ll try to improve my answer when I ll be on my computer with CLIOn – Noki Mar 02 '18 at 07:48
  • I updated my answer, tell me if it's solve your problem. – Noki Mar 02 '18 at 08:18
  • This is a better way to do it. *Note:* The real trick was making sure I ran `clean` from CLion in between builds. – Startec Mar 02 '18 at 09:58
  • Yeah but, it's just a workaround, you should be able to run it without running clean. Like checking new files in assets and copy at the right time. – Noki Mar 02 '18 at 12:22