Reputation: 1379
I am using CMake to build my system and my unit tests.
I am also doing an out-of-source build.
I've found with the ADD_TEST() command, that you don't need to install the test executable (it will just be run when you run make install, which is great).
However, my unit tests depend on some input files, which need to be copied to where the executable is built.
As far as I am aware, I can't use INSTALL() to copy the files there, because I haven't specified where there is - it depends on where the build command is called.
Is there some way I can tell CMake to copy my test files to the same location that it builds the executable?
Upvotes: 15
Views: 10795
Reputation: 13588
To copy a whole directory at build time (and only if the build succeeds) you can do this after you added your target (e. g. unit test executable):
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/testDataDir $<TARGET_FILE_DIR:${PROJECT_NAME}>/testDataDir)
Upvotes: 3
Reputation: 656
The question is quite old, but in my opinion there is a better solution to the problem than copying the files you need to ${CMAKE_CURRENT_BINARY_DIR})
. The add_test
command has a WORKING_DIRECTORY
option that allows to chose the directory where the tests are run. So I would suggest the following solution:
add_executable(testA testA.cpp)
add_test(NAME ThisIsTestA COMMAND testA WORKING_DIRECTORY ${DIRECTORY_WITH_TEST_DATA})
This avoids needless copying of your input files.
Upvotes: 9
Reputation: 34421
Sure, you can do this on the configuration step this way:
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${fileFrom} ${fileTo})
If your input files depend on something produced by build, you can create a target for it and add it to the all
target:
add_custom_target(copy_my_files ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${fileFrom} ${fileTo}
DEPENDS ${fileFrom}
)
Upvotes: 4
Reputation: 1379
This may not be the best solution, but currently I am doing this:
file(COPY my_directory DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
Which seems to be doing the trick.
Upvotes: 7
Reputation: 948
You may use configure_file with parameter COPYONLY. And perform copying to your build dir: ${CMAKE_CURRENT_BINARY_DIR}
Upvotes: 9