How to add source files to an external project in

2019-08-21 16:35发布

问题:

I want to integrate SQLite into my project using ExternalProject_Add.

cmake_minimum_required(VERSION 2.8.8)
include(ExternalProject)

# Download, configure, build and install SQLite
ExternalProject_Add(SQLite
    PREFIX            ${CMAKE_SOURCE_DIR}
    TMP_DIR           ${CMAKE_SOURCE_DIR}/temp
    STAMP_DIR         ${CMAKE_SOURCE_DIR}/stamp
    #--Download step--------------
    DOWNLOAD_DIR      ${CMAKE_SOURCE_DIR}/download
    URL               http://www.sqlite.org/2014/sqlite-autoconf-3080704.tar.gz
    URL_HASH          SHA1=70ca0b8884a6b145b7f777724670566e2b4f3cde
    #--Update/Patch step----------
    UPDATE_COMMAND    ""
    #--Configure step-------------
    SOURCE_DIR        ${CMAKE_SOURCE_DIR}/source
    CONFIGURE_COMMAND "" # How to add sqlite3.c to the target here?
    #--Build step-----------------
    BINARY_DIR        ${CMAKE_SOURCE_DIR}/build
    BUILD_COMMAND     "cmake --build ."
    #--Install step---------------
    INSTALL_DIR       ${CMAKE_SOURCE_DIR}/install
)

The build command would use the native compiler to build all source files added to the target SQLite. However, there are non. How can I add the only source file sqlite3.c to the external project within the CONFIGURE_COMMAND?

回答1:

ExternalProject_Add assumes that the project you want to pull in already ships with a (possibly complex, possibly non-CMake-based) working build system.

You have two possibilities here:

  • You can stick with the amalgamated autoconf version of sqlite that you are currently using. In that case the CONFIGURE_COMMAND would invoke configure and the BUILD_COMMAND would invoke make. Note that this approach will not be portable to platforms that do not have autoconf installed.
  • You can switch to the bare-source amalgamated version of sqlite and provide your own CMakeLists.txt for building. Since sqlite can be built with a minimum of configuration and the amalgamation only consists of a single source and header file, this is not as hard as it may sound. In this case you can simply invoke cmake for configuation and building.

Note however that you cannot provide this information in-line with ExternalProject_Add. You will need an external build script, whether that is CMake, autoconf or something else.



回答2:

Building on the correct answer above, this is what I came up with. Instead of adding a second file to my repository, it gets generated from the existing CMake file. Since the source directory of the external project gets cleaned on build, the generated file must be stored in a temporary location and copied into the source directory in a later step of the external project, in this case the update command.

# SQLite
cmake_minimum_required(VERSION 2.8.8)
include(ExternalProject)

# Add CMake project file
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/temp)
file(WRITE ${CMAKE_SOURCE_DIR}/temp/CMakeLists.txt
    "cmake_minimum_required(VERSION 2.8.8)\n"
    "set(PROJECT_NAME sqlite)\n"
    "include_directories(${CMAKE_SOURCE_DIR}/source)\n"
    "add_library(sqlite3 ${CMAKE_SOURCE_DIR}/source/sqlite3.c)\n"
    "install(TARGETS sqlite3 DESTINATION lib)\n"
    "install(FILES sqlite3.h DESTINATION include)\n")

# Download, configure, build and install.
ExternalProject_Add(SQLite
    # DEPENDS
    PREFIX            ${CMAKE_SOURCE_DIR}
    TMP_DIR           ${CMAKE_SOURCE_DIR}/temp
    STAMP_DIR         ${CMAKE_SOURCE_DIR}/stamp
    #--Download step--------------
    DOWNLOAD_DIR      ${SFML_PREFIX}/download
    URL               http://www.sqlite.org/2014/sqlite-autoconf-3080704.tar.gz
    URL_HASH          SHA1=70ca0b8884a6b145b7f777724670566e2b4f3cde
    #--Update/Patch step----------
    UPDATE_COMMAND    ${CMAKE_COMMAND} -E copy
                          ${CMAKE_SOURCE_DIR}/temp/CMakeLists.txt
                          ${CMAKE_SOURCE_DIR}/source/CMakeLists.txt
    #--Configure step-------------
    SOURCE_DIR        ${CMAKE_SOURCE_DIR}/source
    CMAKE_ARGS        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_SOURCE_DIR}/install
                      -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                      -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
    #--Build step-----------------
    BINARY_DIR        ${CMAKE_SOURCE_DIR}/build
    BUILD_COMMAND     ${CMAKE_COMMAND} --build .
    #--Install step---------------
    INSTALL_DIR       ${CMAKE_SOURCE_DIR}/install
)