Skip to content

Commit

Permalink
Out with Eigen, in with FetchContent and gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
robertodr committed Aug 7, 2018
1 parent 1e80284 commit 3db1516
Show file tree
Hide file tree
Showing 26 changed files with 230 additions and 339 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
- [Using the superbuild pattern](chapter-08/recipe-01/README.md)
- [Managing dependencies with a superbuild I. The Boost libraries](chapter-08/recipe-02/README.md)
- [Managing dependencies with a superbuild II. The FFTW library](chapter-08/recipe-03/README.md)
- [Managing dependencies with a superbuild III. The Eigen library](chapter-08/recipe-04/README.md)
- [Managing dependencies with a superbuild III. The Google test framework](chapter-08/recipe-04/README.md)
- [Managing your project as a superbuild](chapter-08/recipe-05/README.md)


Expand Down
2 changes: 1 addition & 1 deletion chapter-08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
- [Using the superbuild pattern](recipe-01/README.md)
- [Managing dependencies with a superbuild I. The Boost libraries](recipe-02/README.md)
- [Managing dependencies with a superbuild II. The FFTW library](recipe-03/README.md)
- [Managing dependencies with a superbuild III. The Eigen library](recipe-04/README.md)
- [Managing dependencies with a superbuild III. The Google test framework](recipe-04/README.md)
- [Managing your project as a superbuild](recipe-05/README.md)
2 changes: 1 addition & 1 deletion chapter-08/recipe-04/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Managing dependencies with a superbuild III. The Eigen library
# Managing dependencies with a superbuild III. The Google test framework

Abstract to be written ...

Expand Down
73 changes: 46 additions & 27 deletions chapter-08/recipe-04/cxx-example-3.5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-04 LANGUAGES CXX)
# project name and language
project(recipe-03 LANGUAGES CXX)

# require C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subprojects)

option(Eigen3_FORCE_SUPERBUILD "Always build Eigen3 on our own" OFF)

add_subdirectory(external/upstream)

include(ExternalProject)
ExternalProject_Add(${PROJECT_NAME}_core
DEPENDS
eigen3_external
SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/src
CMAKE_ARGS
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
-DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}
-DEigen3_DIR=${Eigen3_DIR}
CMAKE_CACHE_ARGS
-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
-DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH}
BUILD_ALWAYS
1
INSTALL_COMMAND
""
)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# example library
add_library(sum_integers sum_integers.cpp)

# main code
add_executable(sum_up main.cpp)
target_link_libraries(sum_up sum_integers)

# we will use the network to fetch Google Test sources
# make it possible to disable unit tests when not on network
option(ENABLE_UNIT_TESTS "Enable unit tests" ON)
message(STATUS "Enable testing: ${ENABLE_UNIT_TESTS}")

include(googletest.cmake)

if(ENABLE_UNIT_TESTS)
fetch_googletest(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}/googletest
)

add_executable(cpp_test "")

target_sources(cpp_test
PRIVATE
test.cpp
)

target_link_libraries(cpp_test
PRIVATE
sum_integers
gtest_main
)

enable_testing()

add_test(
NAME google_test
COMMAND $<TARGET_FILE:cpp_test>
)
endif()

This file was deleted.

This file was deleted.

20 changes: 20 additions & 0 deletions chapter-08/recipe-04/cxx-example-3.5/googletest-download.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# code copied from https://crascit.com/2015/07/25/cmake-gtest/
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(googletest-download LANGUAGES NONE)

include(ExternalProject)

ExternalProject_Add(
googletest
SOURCE_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-src"
BINARY_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-build"
GIT_REPOSITORY
https://github.com/google/googletest.git
GIT_TAG
release-1.8.0
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
50 changes: 50 additions & 0 deletions chapter-08/recipe-04/cxx-example-3.5/googletest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# download and unpack googletest at configure time

# the following code to fetch googletest
# is inspired by and adapted after https://crascit.com/2015/07/25/cmake-gtest/

function(fetch_googletest _download_module_path _download_root)
# Variable used in googletest-download.cmake
set(GOOGLETEST_DOWNLOAD_ROOT ${_download_root})
configure_file(
${_download_module_path}/googletest-download.cmake
${_download_root}/CMakeLists.txt
@ONLY
)
unset(GOOGLETEST_DOWNLOAD_ROOT)

execute_process(
COMMAND
"${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY
${_download_root}
)
execute_process(
COMMAND
"${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY
${_download_root}
)

# Prevent GoogleTest from overriding our compiler/linker options
# when building with Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Prevent GoogleTest from using PThreads
set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)

# adds the targets: gtest, gtest_main, gmock, gmock_main
add_subdirectory(
${_download_root}/googletest-src
${_download_root}/googletest-build
)

# Silence std::tr1 warning on MSVC
if(MSVC)
foreach(_tgt gtest gtest_main gmock gmock_main)
target_compile_definitions(${_tgt}
PRIVATE
"_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING"
)
endforeach()
endif()
endfunction()
1 change: 1 addition & 0 deletions chapter-08/recipe-04/cxx-example-3.5/main.cpp
27 changes: 2 additions & 25 deletions chapter-08/recipe-04/cxx-example-3.5/menu.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,2 @@
appveyor-vs:
definitions:
- Eigen3_FORCE_SUPERBUILD: 'ON'

appveyor-msys:
definitions:
- Eigen3_FORCE_SUPERBUILD: 'ON'

travis-linux:
definitions:
- Eigen3_FORCE_SUPERBUILD: 'ON'

# OpenMP does not work with clang
travis-osx:
definitions:
- Eigen3_FORCE_SUPERBUILD: 'ON'
failing_generators:
- 'Unix Makefiles'
- 'Ninja'

local:
definitions:
- Eigen3_FORCE_SUPERBUILD: 'ON'
env:
- VERBOSE_OUTPUT: 'ON'
targets:
- test
22 changes: 0 additions & 22 deletions chapter-08/recipe-04/cxx-example-3.5/src/CMakeLists.txt

This file was deleted.

62 changes: 0 additions & 62 deletions chapter-08/recipe-04/cxx-example-3.5/src/linear-algebra.cpp

This file was deleted.

1 change: 1 addition & 0 deletions chapter-08/recipe-04/cxx-example-3.5/sum_integers.cpp
1 change: 1 addition & 0 deletions chapter-08/recipe-04/cxx-example-3.5/sum_integers.hpp
1 change: 1 addition & 0 deletions chapter-08/recipe-04/cxx-example-3.5/test.cpp
Loading

0 comments on commit 3db1516

Please sign in to comment.