将外部源目录添加到CMake构建

时间:2012-08-12 09:15:53

标签: cmake gmock

我在我的项目中使用Google Mock,并且指示说与项目一起构建库会更好,因为不同的编译器标志可能会引入错误。我不想将gmock /目录添加到我的存储库;我宁愿将源作为外部依赖项并将其插入到我的构建过程中。这让我想到了一个问题:我如何指示CMake将外部源目录拉入构建(即在我的项目的构建目录中构建它)?我在这里找到了similar question,但答案需要严格的目录放置,我宁愿配置。还有其他办法吗?

2 个答案:

答案 0 :(得分:7)

您可以使用ExternalProject_Add,因为它允许您从项目的构建树中下载,配置和构建gmock,然后链接到gmock库。

@ arrowdodger的answer可能是更常见的方式。使用他的方法,您通常不会在构建树中拥有gmock源代码。根据你的需要,这可能是好事还是坏事。

使用ExternalProject_Add,每次构建gmock或依赖目标时都会拉取gmock源(svn update)。这使得构建稍微慢一些,但显然使源保持最新并且很方便(开发人员安装的依赖性较小)。对于像gmock这样经常不会改变的项目,虽然一直在更新的开销可能对你来说太过分了。

以下CMakeLists.txt使用Visual Studio 2010& 2012年 - 可能需要针对其他平台进行调整。特别是,gtest目前无法使用Visual Studio 2012构建框(请参阅此bug report),因此补丁文件和PATCH_COMMAND调用中的ExternalProject_Add

cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)
project(Test)

# Create main.cpp which uses gmock
file(WRITE src/main.cpp "#include \"gmock/gmock.h\"\n\n")
file(APPEND src/main.cpp "struct A {\n  virtual void Do() {}\n};\n\n")
file(APPEND src/main.cpp "struct MockA : public A {\n  MOCK_METHOD0(Do, void());\n};\n\n")
file(APPEND src/main.cpp "TEST(A, Do) {\n")
file(APPEND src/main.cpp "  MockA mock_a;\n")
file(APPEND src/main.cpp "  EXPECT_CALL(mock_a, Do()).Times(testing::AtLeast(1));\n")
file(APPEND src/main.cpp "  mock_a.Do();\n}\n\n")
file(APPEND src/main.cpp "int main(int argc, char **argv) {\n")
file(APPEND src/main.cpp "  testing::InitGoogleTest(&argc, argv);\n")
file(APPEND src/main.cpp "  return RUN_ALL_TESTS();\n")
file(APPEND src/main.cpp "}\n")

# Create patch file for gtest with MSVC 2012
if(MSVC_VERSION EQUAL 1700)
  file(WRITE gtest.patch "Index: cmake/internal_utils.cmake\n")
  file(APPEND gtest.patch "===================================================================\n")
  file(APPEND gtest.patch "--- cmake/internal_utils.cmake   (revision 643)\n")
  file(APPEND gtest.patch "+++ cmake/internal_utils.cmake   (working copy)\n")
  file(APPEND gtest.patch "@@ -66,6 +66,9 @@\n")
  file(APPEND gtest.patch "       # Resolved overload was found by argument-dependent lookup.\n")
  file(APPEND gtest.patch "       set(cxx_base_flags \"\${cxx_base_flags} -wd4675\")\n")
  file(APPEND gtest.patch "     endif()\n")
  file(APPEND gtest.patch "+    if (MSVC_VERSION EQUAL 1700)\n")
  file(APPEND gtest.patch "+      set(cxx_base_flags \"\${cxx_base_flags} -D_VARIADIC_MAX=10\")\n")
  file(APPEND gtest.patch "+    endif ()\n")
  file(APPEND gtest.patch "     set(cxx_base_flags \"\${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32\")\n")
  file(APPEND gtest.patch "     set(cxx_base_flags \"\${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN\")\n")
  file(APPEND gtest.patch "     set(cxx_exception_flags \"-EHsc -D_HAS_EXCEPTIONS=1\")\n")
  file(APPEND gtest.patch "Index: include/gtest/internal/gtest-tuple.h\n")
  file(APPEND gtest.patch "===================================================================\n")
  file(APPEND gtest.patch "--- include/gtest/internal/gtest-tuple.h (revision 643)\n")
  file(APPEND gtest.patch "+++ include/gtest/internal/gtest-tuple.h (working copy)\n")
  file(APPEND gtest.patch "@@ -1,3 +1,4 @@\n")
  file(APPEND gtest.patch "+#include <tuple> /*\n")
  file(APPEND gtest.patch " // This file was GENERATED by command:\n")
  file(APPEND gtest.patch " //     pump.py gtest-tuple.h.pump\n")
  file(APPEND gtest.patch " // DO NOT EDIT BY HAND!!!\n")
  file(APPEND gtest.patch "@@ -197,8 +198,8 @@\n")
  file(APPEND gtest.patch " class tuple<> {\n")
  file(APPEND gtest.patch "  public:\n")
  file(APPEND gtest.patch "   tuple() {}\n")
  file(APPEND gtest.patch "-  tuple(const tuple& /* t */)  {}\n")
  file(APPEND gtest.patch "-  tuple& operator=(const tuple& /* t */) { return *this; }\n")
  file(APPEND gtest.patch "+  tuple(const tuple& t)  {}\n")
  file(APPEND gtest.patch "+  tuple& operator=(const tuple&) { return *this; }\n")
  file(APPEND gtest.patch " };\n")
  file(APPEND gtest.patch " \n")
  file(APPEND gtest.patch " template <GTEST_1_TYPENAMES_(T)>\n")
  file(APPEND gtest.patch "@@ -946,7 +947,7 @@\n")
  file(APPEND gtest.patch " template <>\n")
  file(APPEND gtest.patch " struct SameSizeTuplePrefixComparator<0, 0> {\n")
  file(APPEND gtest.patch "   template <class Tuple1, class Tuple2>\n")
  file(APPEND gtest.patch "-  static bool Eq(const Tuple1& /* t1 */, const Tuple2& /* t2 */) {\n")
  file(APPEND gtest.patch "+  static bool Eq(const Tuple1&, const Tuple2&) {\n")
  file(APPEND gtest.patch "     return true;\n")
  file(APPEND gtest.patch "   }\n")
  file(APPEND gtest.patch " };\n")
else()
  file(WRITE gtest.patch "")
endif()

# Enable ExternalProject CMake module
include(ExternalProject)

# Set default ExternalProject root directory
set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/ThirdParty)

# Add gmock
ExternalProject_Add(
    googlemock
    SVN_REPOSITORY http://googlemock.googlecode.com/svn/trunk/
    TIMEOUT 30
    PATCH_COMMAND svn patch ${CMAKE_SOURCE_DIR}/gtest.patch ${CMAKE_BINARY_DIR}/ThirdParty/src/googlemock/gtest
    # Force separate output paths for debug and release builds to allow easy
    # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
    CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
               -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
               -Dgtest_force_shared_crt=ON
    # Disable install step
    INSTALL_COMMAND ""
    # Wrap download, configure and build steps in a script to log output
    LOG_DOWNLOAD ON
    LOG_CONFIGURE ON
    LOG_BUILD ON)

# Specify include dir for googlemock and googletest
ExternalProject_Get_Property(googlemock source_dir)
include_directories(${source_dir}/include)
include_directories(${source_dir}/gtest/include)

if(MSVC_VERSION EQUAL 1700)
  add_definitions(-D_VARIADIC_MAX=10)
endif()

# Add test executable target
add_executable(MainTest ${PROJECT_SOURCE_DIR}/src/main.cpp)

# Create dependency of MainTest on googlemock
add_dependencies(MainTest googlemock)

# Specify MainTest's link libraries
ExternalProject_Get_Property(googlemock binary_dir)
target_link_libraries(MainTest
                      debug ${binary_dir}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}gmock${CMAKE_FIND_LIBRARY_SUFFIXES}
                      optimized ${binary_dir}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}gmock${CMAKE_FIND_LIBRARY_SUFFIXES})

如果在空目录(比如MyTest)中将其创建为CMakeLists.txt,则:

cd MyTest
mkdir build
cd build
cmake ..

这应该在MyTest / src中创建一个基本的main.cpp并创建一个项目文件(Windows上的MyTest / build / Test.sln)

当你构建项目时,它应该将gmock源代码下载到MyTest / build / ThirdParty / src / googlemock,并在MyTest / build / ThirdParty / src / googlemock-build中构建它们。然后,您应该能够成功运行MainTest目标。

有关ExternalProject_Add命令的详细信息,请参阅this article entitled Building External Projects with CMake 2.8

此处a gist包含此CMakeLists.txt

答案 1 :(得分:6)

我看到Google Mock支持CMake。在这种情况下,您只需添加此行

即可
add_subdirectory(${GOOGLE_MOCK_SOURCE_DIR})

进入根CMakeLists.txt并让用户在配置步骤中设置GOOGLE_MOCK_SOURCE_DIR变量:

set(GOOGLE_MOCK_SOURCE_DIR "" CACHE PATH "Path to the GMock source")
if(NOT ${GOOGLE_MOCK_SOURCE_DIR} OR NOT EXISTS "${GOOGLE_MOCK_SOURCE_DIR}/CMakeLists.txt")
  message(FATAL_ERROR "GOOGLE_MOCK_SOURCE_DIR isn't set properly!")
endif()