我有一个相对较大的CMake项目,包含很多子文件夹和文件。我想在构建此项目时创建任何二进制文件后运行自定义命令。我一直在测试Hello World,我知道我需要这样的东西:
编辑:我已更新问题以反映我目前的进展。
# Get list of all files and folders in current dir recursively
set(SUBFDIRS)
file(GLOB_RECURSE ALLFILES LIST_DIRECTORIES true "*" "*")
foreach(SUBFILE ${ALLFILES})
IF(IS_DIRECTORY ${SUBFILE} AND NOT ${SUBFILE} MATCHES ".*CMakeFiles.*")
message("Adding directory: ${SUBFILE}")
LIST(APPEND SUBDIRS ${SUBFILE})
ENDIF()
endforeach()
# Also add current directory to directory list
LIST(APPEND SUBDIRS .)
LIST(REMOVE_DUPLICATES SUBDIRS)
message("-- Detected subdirectories: ${SUBDIRS}")
# Loop over all subdirectories
foreach(subdir ${SUBDIRS})
# Get a list of all targets
get_property(BUILDSYSTEM_TARGETS DIRECTORY ${subdir} PROPERTY BUILDSYSTEM_TARGETS)
# For each target, add a custom target that will run a custom command
foreach(TARGET_DEFINED ${BUILDSYSTEM_TARGETS})
get_target_property(target_type ${TARGET_DEFINED} TYPE)
if (target_type STREQUAL "EXECUTABLE")
message("-- Adding custom target for executable ${TARGET_DEFINED}...")
add_custom_target(custom_${TARGET_DEFINED} ALL)
add_custom_command(TARGET custom_${TARGET_DEFINED}
POST_BUILD
COMMAND my_custom_command $<TARGET_FILE:${TARGET_DEFINED}> $<TARGET_FILE:${TARGET_DEFINED}>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS $<TARGET_FILE:${TARGET_DEFINED}>
)
else()
message("-- Target ${TARGET_DEFINED} is not an executable. Ignoring!")
endif()
endforeach()
endforeach()
这似乎按我想要的方式工作,除了它在尝试获取目录的目标列表时产生类似CMake错误的事实:
CMake Error at CMakeLists.txt:34 (get_property):
get_property DIRECTORY scope provided but requested directory was not
found. This could be because the directory argument was invalid or, it is
valid but has not been processed yet.