如何设置cmake的包含顺序

时间:2019-04-23 10:41:44

标签: cmake

我正在使用cmake为项目生成Makefile。在顶部CMakeLists.txt中,我需要包括一些子模块。就像这样:

include(cmake/Modules/pcre-8.40.cmake)
include(cmake/Modules/curl-7.64.1.cmake)
include(cmake/Modules/openssl-1.1.1.cmake)

curl-7.64.1.cmake就像:


include(ExternalProject)

set (PCAP_ROOT        ${CMAKE_BINARY_DIR}/third_party/curl)
set (PCAP_LIB_DIR     ${PCAP_ROOT}/lib)
set (PCAP_INCLUDE_DIR ${PCAP_ROOT}/include)
set (OPENSSL_DIR      ${CMAKE_BINARY_DIR}/third_party/openssl/build)
set (OPENSSL_I_DIR  ${OPENSSL_DIR}/include)
set (OPENSSL_L_DIR    ${OPENSSL_DIR}/lib)

set (PCAP_CONFIGURE    cd ${PCAP_ROOT}/src/curl-7.64.1 && cmake -DOPENSSL_ROOT_DIR=${OPENSSL_DIR} -DOPENSSL_INCLUDE_DIR=${OPENSSL_I_DIR} -DOPENSSL_SSL_LIBRARY=${OPENSSL_L_DIR}/libssl.a -DOPENSSL_CRYPTO_LIBRARY=${OPENSSL_L_DIR}/libcrypto.a -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX=${PCAP_ROOT} -DBUILD_CURL_EXE=OFF . -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE})
set (PCAP_MAKE         cd ${PCAP_ROOT}/src/curl-7.64.1 && make)
set (PCAP_INSTALL      cd ${PCAP_ROOT}/src/curl-7.64.1 && make install)

ExternalProject_Add(curl-7.64.1
        URL         ${CMAKE_SOURCE_DIR}/third_party/curl/curl-7.64.1.zip
        DOWNLOAD_NAME       curl-7.64.1
        PREFIX          ${PCAP_ROOT}
        CONFIGURE_COMMAND   ${PCAP_CONFIGURE}
        BUILD_COMMAND       ${PCAP_MAKE}
        INSTALL_COMMAND     ${PCAP_INSTALL}
        )

include_directories (${CMAKE_BINARY_DIR}/third_party/curl/include)
link_directories (${CMAKE_BINARY_DIR}/third_party/curl/lib)

openssl-1.1.1.cmake:

include(ExternalProject)

set (OPENSSL_ROOT        ${CMAKE_BINARY_DIR}/third_party/openssl)
set (OPENSSL_LIB_DIR     ${OPENSSL_ROOT}/lib)
set (OPENSSL_INCLUDE_DIR ${OPENSSL_ROOT}/include)
set (OPENSSL_INSTALL_DIR ${CMAKE_BINARY_DIR}/third_party/openssl/build)
set (OPENSSL_ROOT_DIR    ${CMAKE_BINARY_DIR}/third_party/openssl)

if (DEFINED CROSS)
    set (OPENSSL_CONFIGURE    cd ${OPENSSL_ROOT}/src/openssl && 
    ./Configure linux-aarch64 no-shared no-asm --prefix=${OPENSSL_INSTALL_DIR} 
    --cross-compile-prefix=${CROSS}- --sysroot=${CMAKE_SYSROOT})  
    set (OPENSSL_MAKE         cd ${OPENSSL_ROOT}/src/openssl && make CC=${CROSS}-gcc AR=${CROSS}-ar  RANLIB=${CROSS}-ranlib)
else()
    set (OPENSSL_CONFIGURE    cd ${OPENSSL_ROOT}/src/openssl && ./config no-shared no-asm --prefix=${OPENSSL_INSTALL_DIR})
    set (OPENSSL_MAKE         cd ${OPENSSL_ROOT}/src/openssl && make)
endif()
set (OPENSSL_INSTALL      cd ${OPENSSL_ROOT}/src/openssl && make install)

ExternalProject_Add(openssl
        URL         ${CMAKE_SOURCE_DIR}/third_party/openssl/openssl-OpenSSL_1_1_1-stable.zip
        DOWNLOAD_NAME       openssl
        PREFIX          ${OPENSSL_ROOT}
        CONFIGURE_COMMAND   ${OPENSSL_CONFIGURE}
        BUILD_COMMAND       ${OPENSSL_MAKE}
        INSTALL_COMMAND     ${OPENSSL_INSTALL}
        )

include_directories (${CMAKE_BINARY_DIR}/third_party/openssl/build/include)
link_directories (${CMAKE_BINARY_DIR}/third_party/openssl/build/lib)

如您所见,curl需要openssl,但是我无法控制顺序。因此,在不同的计算机中,会发生错误,因为openssl是在curl之后编译的。那么,如何确保openssl首先被编译?

1 个答案:

答案 0 :(得分:0)

ExternalProject_Add的第一个参数是目标的名称,该名称将为外部项目创建。使用add_dependencies命令可能会限制构建外部项目的顺序:

# Curl is built *after* the openssl have been built.
add_dependencies(curl-7.64.1 openssl)