0
我們的應用程序需要提供一個.xsd文件,該文件由多個其他.xsd文件連接在一起組成。連接的源列表可以通過遍歷所有庫依賴關係並檢查其上的屬性來派生。在add_custom_command中連接多個文件
我最終什麼樣的主意是,應用程序的可的CMakeLists.txt只是調用一個函數,它會「做正確的事」:工作
function(make_config_xsd)
set(xsd_config ${CMAKE_CURRENT_BINARY_DIR}/config.xsd)
# build up a list of config files that are going to be concatenated
set(config_list ${appcommon_SOURCE_DIR}/config/common.xsd)
# iterate over the library dependencies and pull out config_file properties
get_target_property(libraries ${PROJECT_NAME} LINK_LIBRARIES)
foreach(lib ${libraries})
get_target_property(conf ${lib} config_file)
if(conf)
list(APPEND config_list ${conf})
endif()
endforeach()
# finally, add the app specific one last
list(APPEND config_list ${PROJECT_SOURCE_DIR}/config/config.xsd)
add_custom_command(OUTPUT ${xsd_config}
COMMAND echo \"<?xml version=\\"1.0\\"?><xs:schema xmlns:xs=\\"http://www.w3.org/2001/XMLSchema\\">\" > ${xsd_config}
COMMAND cat ${config_list} >> ${xsd_config}
COMMAND echo \"</xs:schema>\" >> ${xsd_config}
DEPENDS "${config_list}")
add_custom_target(generate-config DEPENDS ${xsd_config})
add_dependencies(${PROJECT_NAME} generate-config)
endfunction()
這出現。但我不確定它是否真的是解決這個問題的「正確方法」,並假設add_custom_target()
只取決於add_custom_command()
的輸出,這樣我就可以做add_dependencies()
似乎也不錯。有沒有一種更直接的方式來執行這種生成文件的依賴關係?
如果其他自定義命令或自定義的目標取決於您的配置文件,無需要創建額外的定製目標。在庫或可執行文件的情況下,可以通過將文件添加到源文件列表來指定對文件的依賴性。 – Tsyvarev
1.關於add_custom_command(TARGET $ {PROJECT_NAME} POST_BUILD ....)怎麼辦? 2.比較喜歡使用COMMAND $ {CMAKE_COMMAND} -E,不幸的是沒有任何cat concat 3.您可以使用FILE(READ ...)FILE(WRITE)cf. [在cmake郵件列表上的這篇文章](https://cmake.org/pipermail/cmake/2010-July/038028.html) –