当repo(googletest)有多个库时,如何处理CMake ExternalProject?

时间:2015-10-20 07:42:54

标签: github cmake googletest

当项目的repo不是我想要使用的库的根目录时,如何在CMake中添加外部项目,但实际上包含两个目录,这些目录是我想要的repos的根目录在我的项目中使用?

我正在努力建立一个使用Google Test和Mock进行测试的框架CMake项目,但是当我尝试使用ExternalProject_Add下载google test repo(https://github.com/google/googletest)时,在构建上抱怨它无法找到项目的来源。嗯,这是因为Google已将googletest和googlemock合并为一个项目,除了它现在有两个项目。

一些repo的文件结构:

googletest-master/
├──[...no CMakeFiles.txt exists here...]
├──googletest/
│  ├──src/
│  └──CMakeFiles.txt
└──googlemock/
   ├──src/
   └──CMakeFiles.txt

当我做以下事情时......

ExternalProject_Add(
    gtest
    GIT_REPOSITORY https://github.com/google/googletest.git
    TIMEOUT 10
    INSTALL_COMMAND ""
    LOG_DOWNLOAD ON
    LOG_CONFIGURE ON
    LOG_BUILD ON
    PREFIX "googletest-master"
)

...它会将实际回购下载到googletest-master/src/gtest,因为我在回购商的前面添加" googletest-master"保持它不受我的主要源代码的影响,它假定我下载的项目只是源代码而且该源代码位于根目录中。

所以我想完成两件事:

  1. 将repo下载到googletest-master目录中,就像我在那里克隆了repo一样,或者从GitHub下载zip并将其解压缩。
  2. 在我的CMake项目中构建并包含googletest和googlemock

2 个答案:

答案 0 :(得分:8)

您需要单个下载步骤,但两个构建步骤。不同的ExternalProject_add命令调用不能共享步骤,但您可以将所有这些步骤安排到不同的调用中,并在它们之间具有适当的依赖关系:

# Single download(git clone)
ExternalProject_Add(
    googletest-master
    DOWNLOAD_DIR "googletest-master/src" # The only dir option which is required
    GIT_REPOSITORY https://github.com/google/googletest.git
    TIMEOUT 10
    LOG_DOWNLOAD ON
    # Disable all other steps
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
)

# Build gtest from existing sources
ExternalProject_Add(
    gtest
    DOWNLOAD_COMMAND "" # No download required
    SOURCE_DIR "googletest-master/src/googletest" # Use specific source dir
    PREFIX "googletest-master" # But use prefix for compute other dirs
    INSTALL_COMMAND ""
    LOG_CONFIGURE ON
    LOG_BUILD ON
)

# gtest should be build after being downloaded
add_dependencies(gtest googletest-master)

# Build gmock from existing sources
ExternalProject_Add(
    gmock
    DOWNLOAD_COMMAND "" # No download required
    SOURCE_DIR "googletest-master/src/googlemock" # Use specific source dir
    PREFIX "googletest-master" # But use prefix for compute other dirs
    INSTALL_COMMAND ""
    LOG_CONFIGURE ON
    LOG_BUILD ON
)

# gmock should be build after being downloaded
add_dependencies(gmock googletest-master)

答案 1 :(得分:0)

我有一个repository,我可以根据需要使用CMake导入gtest和gmock库,但是使用旧的SVN源代码库而不是GIT。

我认为关键是只为gmock获取源代码,因为它包含gtest源,然后导出两个库。

CMakeLists.txt看起来像这样:

cmake_minimum_required(VERSION 2.8)

include(ExternalProject)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(GMOCK_VERSION "1.7.0")
set(GMOCK_DIR "${CMAKE_CURRENT_BINARY_DIR}/gmock-${GMOCK_VERSION}")
ExternalProject_Add(project_gmock
    SVN_REPOSITORY http://googlemock.googlecode.com/svn/tags/release-${GMOCK_VERSION}
    PREFIX ${GMOCK_DIR}
    CMAKE_ARGS -DCMAKE_C_COMPILER:PATH=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER}
    # Disable update step
    UPDATE_COMMAND ""
    # Disable install step
    INSTALL_COMMAND ""
)
ExternalProject_Get_Property(project_gmock source_dir)
ExternalProject_Get_Property(project_gmock binary_dir)

include_directories(${source_dir}/gtest/include)
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${binary_dir}/gtest/libgtest.a)
add_dependencies(gtest project_gmock)
add_library(gtest_main STATIC IMPORTED)
set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION ${binary_dir}/gtest/libgtest_main.a)
add_dependencies(gtest_main project_gmock)
include_directories(${source_dir}/include)
add_library(gmock STATIC IMPORTED)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${binary_dir}/libgmock.a)
add_dependencies(gmock project_gmock)
add_library(gmock_main STATIC IMPORTED)
set_property(TARGET gmock_main PROPERTY IMPORTED_LOCATION ${binary_dir}/libgmock_main.a)
add_dependencies(gmock_main project_gmock)

include_directories(${cpp_utest_SOURCE_DIR}/src)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
#add_executable(utests )
#target_link_libraries(utests gmock_main gmock gtest pthread)