我有一个项目是这样安排的:
CMakeLists.txt
|
|--------subdir1
| |--CMakeLists.txt
| |--sourcefiles
| |--filetocopy1
|
|--------subdir2
|--CMakeLists.txt
|--sourcefiles
|--filetocopy2
Run Code Online (Sandbox Code Playgroud)
我想将 filetocopy1 和 filetocopy2 复制到构建文件夹中的指定输出目录。所以在两者中,我都有类似的东西
add_custom_command(
TARGET nameoftargetinsubdir1
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1"
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
)
Run Code Online (Sandbox Code Playgroud)
问题是,如果 filetocopy1 或 filetocopy2 更改,但源文件没有更改,则在构建文件夹中调用 make 不会复制文件。有什么方法可以强制它复制这些文件吗?我感觉我可能必须将复制命令放在顶级 CMakeLists.txt 文件中。
POST_BUILD自定义命令的主要目的是在重建目标可执行文件/库时运行。如果您不需要此类行为,请使用带有OUTPUT和DEPENDS选项的常见自定义命令,并结合add_custom_target:
# If someone needs file "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/filetocopy1",
# this command will create it if the file doesn't exist or is older than
# "${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1".
add_custom_command(
OUTPUT "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/filetocopy1"
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1"
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/filetocopy1"
)
# Custom target for activate the custom command above
add_custom_target(copy_file1 DEPENDS "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/filetocopy1")
# Whenever target 'nameoftargetinsubdir1' is requested, the custom target will be evaluated.
add_dependencies(nameoftargetinsubdir1 copy_file1)
Run Code Online (Sandbox Code Playgroud)