CMake ExternalProject_add 子文件夹上有 CMakeLists

CMake ExternalProject_add with CMakeLists on subfolder

我正在尝试使用 ExternalProject_Add 命令从 cmake 下载并构建 DevIL,但由于某种原因,git 存储库的子文件夹中有 CMakeLists.txt 文件项目。

我似乎找不到使 ExternalProject_add 在正确的文件夹上调用 cmake 的方法。 我尝试使用前缀和 source_dir,但仍然失败。

然后,我在网上看到一个答案,其中涉及创建一个临时 CMakeLists.txt,它将调用正确的 cmake 路径。但由于我不知道如何提供 cmake 调用的路径,这意味着我必须将它放在下载的 git 存储库中,后来证明这很痛苦...... 这是我正在使用的当前命令: 包括 ( "ExternalProject" )

######### Search for required third-party libraries. If not found, attempt to download and build them ########

set ( DEVIL_ROOT "${THIRD_PARTY_OUTPUT_PATH}/DevIL" )
option ( BUILD_DEVIL "Download and build DevIL" FALSE )

find_package ( Devil )
if ( NOT DEVIL_FOUND )
    message ( STATUS "DevIL not found. Forcing to build own copy...")
    set ( BUILD_DEVIL TRUE )
endif()

if ( BUILD_DEVIL )
set ( DEVIL_BUILD_PATH "${THIRD_PARTY_BUILD_PATH}/DevIL/" )
set ( DEVIL_OUTPUT_PATH "${THIRD_PARTY_OUTPUT_PATH}/DevIL" )

message ( STATUS "Downloading DevIL and building...")
file( WRITE ${DEVIL_BUILD_PATH}/DevilCMakeLists.txt "cmake_minimum_required( VERSION 3.0 ) \n  add_subdirectory ( DevIL )" )
ExternalProject_Add ( ThirdParty_DevIL
    PREFIX "${DEVIL_BUILD_PATH}"
    GIT_REPOSITORY "https://github.com/DentonW/DevIL"
    PATCH_COMMAND cmake -E copy "${DEVIL_BUILD_PATH}/DevilCMakeLists.txt" "${DEVIL_BUILD_PATH}/src/ThirdParty_DevIL/DevIL/"
    INSTALL_DIR ${DEVIL_OUTPUT_PATH}
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> )

list ( APPEND THIRD_PARTY_DEPENDENCIES ThirdParty_DevIL )
set ( DEVIL_INCLUDE_DIR "${DEVIL_OUTPUT_PATH}/include" )
set ( DEVIL_LIBRARY devil )
add_definitions ( -DDEVIL )
endif()

list ( APPEND THIRD_PARTY_INCLUDE_DIRS "${DEVIL_INCLUDE_DIR}" )
list ( APPEND THIRD_PARTY_LIBRARIES ${DEVIL_LIBRARY} )

我试过像这样对路径进行硬编码(“/src/ThirdParty_DevIL/”部分),但它非常丑陋,如果 cmake 决定在将来更改它,它就会崩溃。 此外,由于这实际上是在 git 树中复制文件,cmake 稍后会抱怨

Failed to unstash changes in:
'<yadda yadda>/ThirdParty/build//DevIL//src/ThirdParty_DevIL/'.


You will have to resolve the conflicts manually

有没有办法使这个解决方案(创建一个临时 CMakeListst.txt)起作用? 或者更好的是,是否有另一种解决方案,只涉及正确配置 ExternalProject_add ?

我对参数、路径等没有任何要求,所以请随意提出任何建议。 我怎样才能解决这个问题? 谢谢

您可以提供 CONFIGURE_COMMAND 选项,而不是修补源树,它使用正确的目录作为源:

ExternalProject_Add ( ThirdParty_DevIL
    PREFIX "${DEVIL_BUILD_PATH}"
    GIT_REPOSITORY "https://github.com/DentonW/DevIL"
    INSTALL_DIR ${DEVIL_OUTPUT_PATH}
    CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
        <SOURCE_DIR>/DevIL # Tell CMake to use subdirectory as source.
)

在那种情况下,您应该手动向此命令添加内容,这些内容通常与 CMAKE_ARGSCMAKE_CACHE_ARGS 等选项一起提供 等等。但这应该不是一个真正的问题。


或者,您可以将 ExternalProject_Add 分成两个调用:一个只下载包,另一个执行其他所有操作。在这些调用中,您可以使用不同的 SOURCE_DIR 选项:

ExternalProject_Add ( ThirdParty_DevIL_download
    PREFIX "${DEVIL_BUILD_PATH}"
    SOURCE_DIR "${DEVIL_BUILD_PATH}/src/ThirdParty_DevIL" # Explicitely set
    GIT_REPOSITORY "https://github.com/DentonW/DevIL"
    CONFIGURE_COMMAND "" # Disable all other steps
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
)

ExternalProject_Add ( ThirdParty_DevIL
    PREFIX "${DEVIL_BUILD_PATH}"
    DOWNLOAD_COMMAND "" # Disable download step
    SOURCE_DIR "${DEVIL_BUILD_PATH}/src/ThirdParty_DevIL/DevIL" # Source dir for configuration
    INSTALL_DIR ${DEVIL_OUTPUT_PATH}
    CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)

# Add dependency between to calls
add_dependencies(ThirdParty_DevIL ThirdParty_DevIL_download)

因为SOURCE_DIR基于PREFIX的计算依赖于外部项目的名称,并且两次调用的名称不同,这选项已明确设置。

此方法的缺点是构建日志显示有关许多 "not performed" 步骤的行(第一个 ExternalProject_Add() 调用的配置、构建和安装以及第二个调用的下载、路径和更新)。

我认为使用参数 SOURCE_SUBDIR 可以解决这个问题,例如:

ExternalProject_Add ( ThirdParty_DevIL
    PREFIX "${DEVIL_BUILD_PATH}"
    GIT_REPOSITORY "https://github.com/DentonW/DevIL"
    SOURCE_SUBDIR "DevIL"
    ....
)