CMake ExternalProject_Add() and FindPackage()

前端 未结 3 1529
别那么骄傲
别那么骄傲 2020-11-29 23:03

Is there are proper way to find a library (via FindPackage()) which was built with ExternalProject_Add()?

The problem is that CMake cannot

3条回答
  •  无人及你
    2020-11-29 23:52

    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()
    

提交回复
热议问题