TLDR :
我的问题是CMake在开始执行此功能之前 下载资源库。我想声明一个依赖 该函数在ExternalProject_Add上,以便CMake理解它 应该下载,构建然后运行该功能。
上下文:
我有一个cmake模块SomeModule.cmake
,该模块应该从其存储库中添加 flatbuffers 作为外部项目并进行构建。该构建将生成Flatbuffers编译器可执行文件,我打算在some/directory/CMakeLists.txt
文件中使用该可执行文件,以从Flatbuffers模式生成c ++头文件。因此,在我使用ExternalProject_Add
的同一CMake模块中,我声明了一个CMake函数,该函数从一组给定的模式文件生成头文件,并且在some/directory/CMakeLists.txt
的某个地方调用了该函数。
我的问题是CMake在下载资源库之前开始执行此功能。我想在ExternalProject_Add上声明该函数的依赖关系,以便CMake理解它应该下载,构建然后运行该函数。
谈话足够。这是代码的相关部分:
SomeModule.cmake
:
include(ExternalProject)
set(flatbuffers_CMAKE_ARGS
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DFLATBUFFERS_BUILD_TESTS=OFF"
"-DFLATBUFFERS_BUILD_FLATC=ON"
"-DFLATBUFFERS_BUILD_FLATHASH=OFF"
"-DCMAKE_INSTALL_PREFIX=${OTS_DEPENDENCIES}"
)
ExternalProject_Add(
flatbuffers
GIT_REPOSITORY "https://github.com/google/flatbuffers.git"
GIT_TAG "v1.9.0"
SOURCE_DIR "${OTS_DEPDENDENCIES_DIR}/flatbuffers"
BINARY_DIR "${OTS_DEPDENDENCIES_DIR}/flatbuffers"
CMAKE_ARGS "${flatbuffers_CMAKE_ARGS}"
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(flatbuffers SOURCE_DIR)
ExternalProject_Get_Property(flatbuffers BINARY_DIR)
set(flatbuffers_SOURCE_DIR ${SOURCE_DIR})
set(flatbuffers_BINARY_DIR ${BINARY_DIR})
set(flatbuffers_INCLUDE_DIR ${flatbuffers_SOURCE_DIR}/include)
set(flatbuffers_FLATC_EXECUTABLE ${flatbuffers_BINARY_DIR}/flatc)
# please assume that the variables above are all set to appropriate values
function(FlatbuffersGenerateCpp SCHEMA_FILES GENERATED_DIR GENERATED_CXX)
foreach(SCHEMA_FILE ${SCHEMA_FILES})
get_filename_component(NAME ${SCHEMA_FILE} NAME_WE)
set(GENERATED_HEADER_FILE_PATH ${GENERATED_DIR}/${NAME}_generated.h)
message(STATUS "attempting to generate: ${GENERATED_HEADER_FILE_PATH}")
add_custom_command(
DEPENDS ${flatbuffers_FLATC_EXECUTABLE}
OUTPUT ${GENERATED_HEADER_FILE_PATH}
COMMAND ${flatbuffers_FLATC_EXECUTABLE} -o ${GENERATED_DIR} -c ${SCHEMA_FILE}
COMMENT "generating flatbuffers c++ header file: ${GENERATED_HEADER_FILE_PATH}"
)
list(APPEND GENERATED_FILES ${GENERATED_HEADER_FILE_PATH})
endforeach()
message(STATUS "generated c++ header files: ${GENERATED_FILES}")
set(${GENERATED_CXX} ${GENERATED_FILES} PARENT_SCOPE)
endfunction()
还有some/directory/CMakeLists.txt
:
# cmake module path is properly set so the following works:
include(SomeModule)
set(flatbuffers_GENERATED_INCLUDES_DIR
${CMAKE_BINARY_DIR}/generated/config/flatbuffers
)
FlatbuffersGenerateCpp(
"${flatbuffers_SCHEMAS}"
"${flatbuffers_GENERATED_INCLUDES_DIR}"
flatbuffers_GENERATED_CXX
)
add_library(
my_framework
SHARED
${THE_PUBLIC_HEADER_FILES}
${THE_IMPL_SOURCE_FILES}
${THE_IMPL_HEADER_FILES}
${flatbuffers_GENERATED_CXX}
)
add_dependencies(my_framework flatbuffers ${flatbuffers_GENERATED_CXX})
target_include_directories(my_framework PRIVATE ${flatbuffers_INCLUDE_DIR})
target_include_directories(my_framework PRIVATE ${CMAKE_SOURCE_DIR})
target_include_directories(my_framework PRIVATE ${CMAKE_BINARY_DIR}/generated)
set_source_files_properties(${flatbuffers_GENERATED_CXX} PROPERTIES GENERATED TRUE)
答案 0 :(得分:2)
我确实开始根据Tsyvarev发表的评论来修改代码:
CMake函数在配置阶段执行,因此您也需要在配置阶段构建外部项目。
虽然我相信他提出的解决方案会起作用,但是我有点不舒服,并一直认为必须有一个更优雅的解决方案。我咨询了一位同事,并提出了一个更好的解决方案,该解决方案与以下差异(删除${flatbuffers_GENERATED_CXX}
)一样简单。
- add_dependencies(my_framework flatbuffers ${flatbuffers_GENERATED_CXX})
+ add_dependencies(my_framework flatbuffers)
我们回顾了所涉及代码的问题在于,CMake照原样读取add_dependencies(my_framework flatbuffers ${flatbuffers_GENERATED_CXX})
并理解它需要${flatbuffers_GENERATED_CXX}
作为构建my_framework
的目标,因此它可以继续运行功能。但是它无法理解该功能取决于外部项目。现在,如果我们删除${flatbuffers_GENERATED_CXX}
的显式依赖项声明,则CMake会在解决其他依赖项(flatbuffers目标)后推迟运行该函数,这将在运行项目之前有效地下载并构建外部项目。