34

是否有适当的方法来查找使用FindPackage()构建的库(通过)ExternalProject_Add()

问题是 CMake 在 CMake 时找不到库,因为外部库是在编译时构建的。我知道在超级构建中构建库和项目时可以结合这两个 CMake 函数,但我想在普通的 CMake 项目中使用它。

事实上,我想构建 VTK 6并在我的 CMake 项目中ExternalProject_Add找到它。FindPackage

4

3 回答 3

23

有办法做到这一点。但这有点骇人听闻。您基本上添加了一个自定义目标,该目标在构建期间重新运行 cmake。

您将不得不在一个小型测试项目中尝试这个,以确定它是否适合您

find_package(Beaengine)


############################################
#
#    BeaEngine
#
include(ExternalProject)
externalproject_add(BeaEngine
    SOURCE_DIR            ${PROJECT_SOURCE_DIR}/beaengine   
    SVN_REPOSITORY        http://beaengine.googlecode.com/svn/trunk/
    CMAKE_ARGS            -DoptHAS_OPTIMIZED=TRUE -DoptHAS_SYMBOLS=FALSE -DoptBUILD_64BIT=FALSE -DoptBUILD_DLL=FALSE -DoptBUILD_LITE=FALSE
    INSTALL_COMMAND       ""
 )


if(NOT ${Beaengine_FOUND})
    #rerun cmake in initial build
    #will update cmakecache/project files on first build
    #so you may have to reload project after first build
    add_custom_target(Rescan ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} DEPENDS BeaEngine)
else()
    #Rescan becomes a dummy target after first build
    #this prevents cmake from rebuilding cache/projects on subsequent builds
    add_custom_target(Rescan)
endif()




add_executable(testapp testapp.cpp )
add_dependencies(testapp Rescan)
if(${Beaengine_FOUND})
    target_link_libraries(testapp ${Beaengine_LIBRARY})
endif()

这似乎适用于 mingw makefiles / eclipse makefile 项目。vs 将在第一次构建后请求重新加载所有项目。

于 2013-07-04T15:04:26.447 回答
14

You can force a build using the build_external_project function below.

It works by generating a simple helper project inside the build tree and then calling the cmake configuration and the cmake build on the helper.

Customize at will for the actual ExternalProject_add command.

Note that the trailing arguments are used to pass CMAKE_ARGS. Furthur enhancements are left as an exercise to the reader :-)

# This function is used to force a build on a dependant project at cmake configuration phase.
# 
function (build_external_project target prefix url) #FOLLOWING ARGUMENTS are the CMAKE_ARGS of ExternalProject_Add

    set(trigger_build_dir ${CMAKE_BINARY_DIR}/force_${target})

    #mktemp dir in build tree
    file(MAKE_DIRECTORY ${trigger_build_dir} ${trigger_build_dir}/build)

    #generate false dependency project
    set(CMAKE_LIST_CONTENT "
        cmake_minimum_required(VERSION 2.8)

        include(ExternalProject)
        ExternalProject_add(${target}
            PREFIX ${prefix}/${target}
            URL ${url}
            CMAKE_ARGS ${ARGN}
            INSTALL_COMMAND \"\"
            )

        add_custom_target(trigger_${target})
        add_dependencies(trigger_${target} ${target})
    ")

    file(WRITE ${trigger_build_dir}/CMakeLists.txt "${CMAKE_LIST_CONTENT}")

    execute_process(COMMAND ${CMAKE_COMMAND} ..
        WORKING_DIRECTORY ${trigger_build_dir}/build
        )
    execute_process(COMMAND ${CMAKE_COMMAND} --build .
        WORKING_DIRECTORY ${trigger_build_dir}/build
        )

endfunction()
于 2014-05-09T17:31:44.583 回答
8

时间过去了,CMake 实现了一个允许从 ExternalProject_Add 引用目标的本机版本。

此功能在FetchContent模块中实现。它允许下载并立即使用在配置时定义的目标。

正如我之前的回答所暗示的,它使用了一个临时构建目录,但在一个更集成的 API 中。

于 2020-03-09T18:48:32.210 回答