From b602bf2049bdf606bc75277ab88a0ad50fb8e528 Mon Sep 17 00:00:00 2001 From: Boubacar DIENE Date: Wed, 9 Dec 2020 16:15:01 +0100 Subject: [PATCH] [DEV] Use googletest installed on the system when available Also set project version to 20.12.00 --- CMakeLists.txt | 2 +- ci/Dockerfile | 13 +++++ cmake/frameworks/googletest.cmake | 86 +++++++++++++++++++++---------- test/CMakeLists.txt | 2 +- 4 files changed, 73 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30e064b..3ddd30f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ cmake_minimum_required(VERSION 3.17.2) # - version = .. # - The project's name will be used to name the executable project(networkservice - VERSION 20.05.00 + VERSION 20.12.00 LANGUAGES CXX DESCRIPTION "A native service to allow performing network\ commands more securely by using related tools\ diff --git a/ci/Dockerfile b/ci/Dockerfile index 4c357c6..5ea3091 100644 --- a/ci/Dockerfile +++ b/ci/Dockerfile @@ -97,6 +97,19 @@ ARG JSON_INSTALL_DIR=/usr/local/include RUN wget --no-check-certificate ${JSON_DOWNLOAD_URL}/v${JSON_VERSION}/json.hpp \ -P ${JSON_INSTALL_DIR} +# Install googletest and googlemock +ARG GTEST_VERSION=1.10.0 +ARG GTEST_DOWNLOAD_URL=https://github.com/google/googletest/archive +ARG GTEST_INSTALL_DIR=/opt + +RUN wget --no-check-certificate ${GTEST_DOWNLOAD_URL}/release-${GTEST_VERSION}.tar.gz -O - \ + | tar xzC ${GTEST_INSTALL_DIR} +RUN mkdir -p ${GTEST_INSTALL_DIR}/googletest-release-${GTEST_VERSION}/build \ + && cd ${GTEST_INSTALL_DIR}/googletest-release-${GTEST_VERSION}/build \ + && cmake .. \ + && make && make install \ + && cd ${GTEST_INSTALL_DIR} && rm -rf googletest-release-${GTEST_VERSION} + # Add user ENV USERNAME networkservice-usr ENV HOMEDIR /home/${USERNAME} diff --git a/cmake/frameworks/googletest.cmake b/cmake/frameworks/googletest.cmake index 48178c5..5cfb708 100644 --- a/cmake/frameworks/googletest.cmake +++ b/cmake/frameworks/googletest.cmake @@ -6,7 +6,9 @@ # \date April 2020 # # \brief A CMake script to fetch googletest (gtest, gmock) at -# configure time and also adds it to the main build +# configure time and also adds it to the main build. +# However, in case googletest is already present on the +# system, it will be used instead. # # \see https://cmake.org/cmake/help/latest/module/FetchContent.html # @@ -16,39 +18,67 @@ # Variables # ################################################################# -# Gtest and Gmock are built as static libraries so installing -# them is useless. We just need to link with those libraries (*.a -# files when compiling unit tests -# -# See build/_deps/googletest-src/CMakeLists.txt -set(INSTALL_GTEST OFF - CACHE BOOL "Disable installation of googletest") +set(GTEST_VERSION 1.10.0 + CACHE STRING "Which release are you interested in?" + FORCE) ################################################################# -# Fetch # +# Install # ################################################################# -include(FetchContent) -FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.10.0 -) +# Find installed googletest package +find_package(GTest ${GTEST_VERSION} EXACT QUIET) -# After the following call, the CMake targets defined by googletest -# will be defined and available to the rest of the build -FetchContent_MakeAvailable(googletest) +if(GTest_FOUND) -################################################################# -# Search directories # -################################################################# + #---------------------------------------------------------------# + # Search directories # + #---------------------------------------------------------------# + + # Add googletest headers to include directories + include_directories(SYSTEM ${GTEST_INCLUDE_DIRS}) + + # Add directory containing googletest/googlemock libraries + # to the linker's search path. pthread is required by gtest + link_libraries(${GTEST_LIBRARIES};pthread) + +else() + + #---------------------------------------------------------------# + # Fetch # + #---------------------------------------------------------------# + + # Gtest and Gmock are built as static libraries so installing + # them is useless. We just need to link with those libraries (*.a + # files when compiling unit tests + # + # See build/_deps/googletest-src/CMakeLists.txt + set(INSTALL_GTEST OFF + CACHE BOOL "Disable installation of googletest") + + include(FetchContent) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-${GTEST_VERSION} + ) + + # After the following call, the CMake targets defined by googletest + # will be defined and available to the rest of the build + FetchContent_MakeAvailable(googletest) + + #---------------------------------------------------------------# + # Search directories # + #---------------------------------------------------------------# + + # Add googletest headers to include directories + include_directories(SYSTEM ${googletest_SOURCE_DIR}/googletest/include) -# Add googletest headers to include directories -include_directories(SYSTEM ${googletest_SOURCE_DIR}/googletest/include) + # Add googlemock headers to include directories + include_directories(SYSTEM ${googlemock_SOURCE_DIR}/googlemock/include) -# Add googlemock headers to include directories -include_directories(SYSTEM ${googlemock_SOURCE_DIR}/googlemock/include) + # Add directory containing googletest/googlemock static libraries + # to the linker's search path + link_directories(SYSTEM ${CMAKE_BINARY_DIR}/lib) -# Add directory containing googletest/googlemock static libraries -# to the linker's search path -link_directories(SYSTEM ${CMAKE_BINARY_DIR}/lib) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ef16e19..ba4b1ae 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,7 +36,7 @@ set(TESTS_INSTALL_DIR ${CMAKE_INSTALL_BINDIR}/tests) # change the sign of the result [-Werror=sign-conversion] # # Add "-Wno-error=sign-conversion" to make the build pass. -message(STATUS "Downloading googletest...") +message(STATUS "Preparing googletest...") set(CMAKE_CXX_FLAGS -Wno-error=sign-conversion) include(${CMAKE_SOURCE_DIR}/cmake/frameworks/googletest.cmake)