From 31a38fff66df0b781b82b5870a1e65ecd6c42def Mon Sep 17 00:00:00 2001
From: Mederic Fourmy <mederic.fourmy@gmail.com>
Date: Wed, 11 May 2022 11:32:29 +0200
Subject: [PATCH] laser compiles against laser_scan_utils and can be installed
 in non root dir

---
 CMakeLists.txt                         |  67 +++++++++-----
 cmake_modules/Findcsm.cmake            |  64 --------------
 cmake_modules/wolflaserConfig.cmake    |  91 -------------------
 cmake_modules/wolflaserConfig.cmake.in |  13 +++
 internal/config.h.in                   |   6 +-
 test/CMakeLists.txt                    |  10 ---
 test/gtest/CMakeLists.txt              | 115 ++++++++++++++-----------
 7 files changed, 125 insertions(+), 241 deletions(-)
 delete mode 100755 cmake_modules/Findcsm.cmake
 delete mode 100644 cmake_modules/wolflaserConfig.cmake
 create mode 100644 cmake_modules/wolflaserConfig.cmake.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6a8ad1eab..c689dbbff 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,10 +1,6 @@
 # Pre-requisites about cmake itself
-CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
 
-if(COMMAND cmake_policy)
-  cmake_policy(SET CMP0005 NEW)
-  cmake_policy(SET CMP0003 NEW)
-endif(COMMAND cmake_policy)
 # MAC OSX RPATH
 SET(CMAKE_MACOSX_RPATH 1)
 
@@ -14,9 +10,11 @@ set(PLUGIN_NAME wolf${PROJECT_NAME})
 
 MESSAGE("Starting ${PROJECT_NAME} CMakeLists ...")
 
+# Paths
 SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)
 SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
-SET(CMAKE_INSTALL_PREFIX /usr/local)
+set(INCLUDE_INSTALL_DIR include/iri-algorithms/wolf)
+set(LIB_INSTALL_DIR lib/)
 
 IF (NOT CMAKE_BUILD_TYPE)
   SET(CMAKE_BUILD_TYPE "DEBUG")
@@ -69,10 +67,6 @@ if(BUILD_TESTS)
     enable_testing()
 endif()
 
-#CMAKE modules
-SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
-MESSAGE(STATUS ${CMAKE_MODULE_PATH})
-
 # Some wolf compilation options
 IF((CMAKE_BUILD_TYPE MATCHES DEBUG) OR (CMAKE_BUILD_TYPE MATCHES debug) OR (CMAKE_BUILD_TYPE MATCHES Debug))
   set(_WOLF_DEBUG true)
@@ -83,12 +77,17 @@ option(_WOLF_TRACE "Enable wolf tracing macro" ON)
 # ============ DEPENDENCIES ============ 
 FIND_PACKAGE(wolfcore REQUIRED)
 FIND_PACKAGE(laser_scan_utils REQUIRED)
-FIND_PACKAGE(csm QUIET)
+# found only to set the XXX_FOUND variables
+# -> It would be better to look for them in the the laser_scan_utils target
 FIND_PACKAGE(falkolib QUIET)
+find_package(PkgConfig)
+pkg_check_modules(csm QUIET csm)
+link_directories(${csm_LIBRARY_DIRS})
 
 # ============ CONFIG.H ============ 
-string(TOUPPER ${PROJECT_NAME} UPPER_NAME)
 set(_WOLF_ROOT_DIR ${CMAKE_SOURCE_DIR})
+# variable used to compile the config.h.in file
+string(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER)
 
 # Define the directory where will be the configured config.h
 SET(WOLF_CONFIG_DIR ${PROJECT_BINARY_DIR}/conf/${PROJECT_NAME}/internal)
@@ -108,8 +107,8 @@ message("CONFIG DIRECTORY ${PROJECT_BINARY_DIR}")
 include_directories("${PROJECT_BINARY_DIR}/conf")
 
 # ============ INCLUDES ============ 
-INCLUDE_DIRECTORIES(${wolfcore_INCLUDE_DIRS})
-INCLUDE_DIRECTORIES(${laser_scan_utils_INCLUDE_DIRS})
+INCLUDE_DIRECTORIES(BEFORE "${falkolib_INCLUDE_DIRS}")
+INCLUDE_DIRECTORIES(BEFORE "${csm_INCLUDE_DIRS}")
 INCLUDE_DIRECTORIES(BEFORE "include")
 
 # ============ HEADERS ============ 
@@ -244,10 +243,10 @@ ENDIF()
 
 #Link the created libraries
 #===============EXAMPLE=========================
-TARGET_LINK_LIBRARIES(${PLUGIN_NAME} ${wolfcore_LIBRARIES})
-TARGET_LINK_LIBRARIES(${PLUGIN_NAME} ${laser_scan_utils_LIBRARY})
+TARGET_LINK_LIBRARIES(${PLUGIN_NAME} wolfcore)
+TARGET_LINK_LIBRARIES(${PLUGIN_NAME} laser_scan_utils)
 IF (falkolib_FOUND)
-	TARGET_LINK_LIBRARIES(${PLUGIN_NAME} ${wolfcore_LIBRARIES} ${laser_scan_utils_LIBRARY} ${falkolib_LIBRARY})
+	TARGET_LINK_LIBRARIES(${PLUGIN_NAME} ${falkolib_LIBRARY})
 ENDIF(falkolib_FOUND)
 
 #Build tests
@@ -257,14 +256,38 @@ IF(BUILD_TESTS)
   add_subdirectory(test)
 ENDIF(BUILD_TESTS)
 
+
 #install library
 #=============================================================
 INSTALL(TARGETS ${PLUGIN_NAME} EXPORT ${PLUGIN_NAME}Targets
-      RUNTIME DESTINATION bin
-      LIBRARY DESTINATION lib
-      ARCHIVE DESTINATION lib)
+  RUNTIME DESTINATION bin
+  LIBRARY DESTINATION ${LIB_INSTALL_DIR}
+  ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
+)
+install(EXPORT ${PLUGIN_NAME}Targets DESTINATION lib/${PLUGIN_NAME}/cmake)
+
+# Configure the package installation
+include(CMakePackageConfigHelpers)
+configure_package_config_file(
+  ${CMAKE_SOURCE_DIR}/cmake_modules/${PLUGIN_NAME}Config.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_NAME}Config.cmake
+  INSTALL_DESTINATION ${LIB_INSTALL_DIR}/${PLUGIN_NAME}/cmake
+  PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR
+)
+
+install(
+  FILES 
+  ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_NAME}Config.cmake
+  DESTINATION 
+  ${LIB_INSTALL_DIR}/${PLUGIN_NAME}/cmake
+)
+
+# Specifies include directories to use when compiling the plugin target
+# This way, include_directories does not need to be called in plugins depending on this one
+target_include_directories(${PLUGIN_NAME} INTERFACE
+  $<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
+)
 
-install(EXPORT ${PLUGIN_NAME}Targets DESTINATION lib/cmake/${PLUGIN_NAME})
 #install headers
 INSTALL(FILES ${HDRS_CAPTURE}
   DESTINATION include/iri-algorithms/wolf/plugin_${PROJECT_NAME}/${PROJECT_NAME}/capture)
@@ -289,8 +312,6 @@ INSTALL(FILES laser.found
 INSTALL(FILES "${WOLF_CONFIG_DIR}/config.h"
   DESTINATION include/iri-algorithms/wolf/plugin_${PROJECT_NAME}/${PROJECT_NAME}/internal)
 
-INSTALL(FILES "${CMAKE_SOURCE_DIR}/cmake_modules/${PLUGIN_NAME}Config.cmake" DESTINATION "lib/cmake/${PLUGIN_NAME}")
-
 INSTALL(DIRECTORY ${SPDLOG_INCLUDE_DIRS} DESTINATION "include/iri-algorithms/")
 
 export(PACKAGE ${PLUGIN_NAME})
diff --git a/cmake_modules/Findcsm.cmake b/cmake_modules/Findcsm.cmake
deleted file mode 100755
index 6dafd97de..000000000
--- a/cmake_modules/Findcsm.cmake
+++ /dev/null
@@ -1,64 +0,0 @@
-FIND_PATH(
-    csm_INCLUDE_DIR
-    NAMES algos.h
-    PATHS /usr/local/include/csm)
-IF(csm_INCLUDE_DIR)
-  MESSAGE("Found csm include dirs: ${csm_INCLUDE_DIR}")
-ELSE(csm_INCLUDE_DIR)
-  MESSAGE("Couldn't find csm include dirs")
-ENDIF(csm_INCLUDE_DIR)
-
-FIND_LIBRARY(
-    csm_LIBRARY
-    NAMES libcsm.so libcsm.dylib
-    PATHS /usr/local/lib)
-IF(csm_LIBRARY)
-  MESSAGE("Found csm lib: ${csm_LIBRARY}")
-ELSE(csm_LIBRARY)
-  MESSAGE("Couldn't find csm lib")
-ENDIF(csm_LIBRARY)
-
-IF (csm_INCLUDE_DIR AND csm_LIBRARY)
-   SET(csm_FOUND TRUE)
- ELSE(csm_INCLUDE_DIR AND csm_LIBRARY)
-   set(csm_FOUND FALSE)
-ENDIF (csm_INCLUDE_DIR AND csm_LIBRARY)
-
-IF (csm_FOUND)
-   IF (NOT csm_FIND_QUIETLY)
-      MESSAGE(STATUS "Found csm: ${csm_LIBRARY}")
-   ENDIF (NOT csm_FIND_QUIETLY)
-ELSE (csm_FOUND)
-   IF (csm_FIND_REQUIRED)
-      MESSAGE(FATAL_ERROR "Could not find csm")
-   ENDIF (csm_FIND_REQUIRED)
-ENDIF (csm_FOUND)
-
-
-macro(csm_report_not_found REASON_MSG)
-  set(csm_FOUND FALSE)
-  unset(csm_INCLUDE_DIR)
-  unset(csm_LIBRARIES)
-
-  # Reset the CMake module path to its state when this script was called.
-  set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
-
-  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by
-  # FindPackage() use the camelcase library name, not uppercase.
-  if (csm_FIND_QUIETLY)
-    message(STATUS "Failed to find csm- " ${REASON_MSG} ${ARGN})
-  elseif (csm_FIND_REQUIRED)
-    message(FATAL_ERROR "Failed to find csm - " ${REASON_MSG} ${ARGN})
-  else()
-    # Neither QUIETLY nor REQUIRED, use SEND_ERROR which emits an error
-    # that prevents generation, but continues configuration.
-    message(SEND_ERROR "Failed to find csm - " ${REASON_MSG} ${ARGN})
-  endif ()
-  return()
-endmacro(csm_report_not_found)
-
-if(NOT csm_FOUND)
-  csm_report_not_found("Something went wrong while setting up csm.")
-endif(NOT csm_FOUND)
-# Set the include directories for csm (itself).
-set(csm_FOUND TRUE)
\ No newline at end of file
diff --git a/cmake_modules/wolflaserConfig.cmake b/cmake_modules/wolflaserConfig.cmake
deleted file mode 100644
index 3344e8e03..000000000
--- a/cmake_modules/wolflaserConfig.cmake
+++ /dev/null
@@ -1,91 +0,0 @@
-#edit the following line to add the librarie's header files
-FIND_PATH(
-    wolflaser_INCLUDE_DIRS
-    NAMES laser.found
-    PATHS /usr/local/include/iri-algorithms/wolf/plugin_laser)
-IF(wolflaser_INCLUDE_DIRS)
-  MESSAGE("Found laser include dirs: ${wolflaser_INCLUDE_DIRS}")
-ELSE(wolflaser_INCLUDE_DIRS)
-  MESSAGE("Couldn't find laser include dirs")
-ENDIF(wolflaser_INCLUDE_DIRS)
-
-FIND_LIBRARY(
-    wolflaser_LIBRARIES
-    NAMES libwolflaser.so
-    PATHS /usr/local/lib)
-IF(wolflaser_LIBRARIES)
-  MESSAGE("Found laser lib: ${wolflaser_LIBRARIES}")
-ELSE(wolflaser_LIBRARIES)
-  MESSAGE("Couldn't find wolf laser lib")
-ENDIF(wolflaser_LIBRARIES)
-
-IF (wolflaser_INCLUDE_DIRS AND wolflaser_LIBRARIES)
-   SET(wolflaser_FOUND TRUE)
- ELSE(wolflaser_INCLUDE_DIRS AND wolflaser_LIBRARIES)
-   set(wolflaser_FOUND FALSE)
-ENDIF (wolflaser_INCLUDE_DIRS AND wolflaser_LIBRARIES)
-
-IF (wolflaser_FOUND)
-   IF (NOT wolflaser_FIND_QUIETLY)
-      MESSAGE(STATUS "Found laser: ${wolflaser_LIBRARIES}")
-   ENDIF (NOT wolflaser_FIND_QUIETLY)
-ELSE (wolflaser_FOUND)
-   IF (wolflaser_FIND_REQUIRED)
-      MESSAGE(FATAL_ERROR "Could not find wolf laser")
-   ENDIF (wolflaser_FIND_REQUIRED)
-ENDIF (wolflaser_FOUND)
-
-
-macro(wolf_report_not_found REASON_MSG)
-  set(wolflaser_FOUND FALSE)
-  unset(wolflaser_INCLUDE_DIRS)
-  unset(wolflaser_LIBRARIES)
-
-  # Reset the CMake module path to its state when this script was called.
-  set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
-
-  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by
-  # FindPackage() use the camelcase library name, not uppercase.
-  if (wolflaser_FIND_QUIETLY)
-    message(STATUS "Failed to find wolf laser- " ${REASON_MSG} ${ARGN})
-  elseif(wolflaser_FIND_REQUIRED)
-    message(FATAL_ERROR "Failed to find wolf laser - " ${REASON_MSG} ${ARGN})
-  else()
-    # Neither QUIETLY nor REQUIRED, use SEND_ERROR which emits an error
-    # that prevents generation, but continues configuration.
-    message(SEND_ERROR "Failed to find wolf laser - " ${REASON_MSG} ${ARGN})
-  endif ()
-  return()
-endmacro(wolf_report_not_found)
-
-if(NOT wolflaser_FOUND)
-  wolf_report_not_found("Something went wrong while setting up wolf vision.")
-endif(NOT wolflaser_FOUND)
-# Set the include directories for wolf (itself).
-set(wolflaser_FOUND TRUE)
-
-# Now we gather all the required dependencies for Wolf Laser
-
-FIND_PACKAGE(laser_scan_utils REQUIRED)
-list(APPEND wolflaser_INCLUDE_DIRS ${laser_scan_utils_INCLUDE_DIRS})
-list(APPEND wolflaser_LIBRARIES ${laser_scan_utils_LIBRARY})
-
-#Making sure Wolf is looked for
-if(NOT wolf_FOUND)
-  FIND_PACKAGE(wolfcore REQUIRED)
-
-  #We reverse in order to insert at the start
-  list(REVERSE wolflaser_INCLUDE_DIRS)
-  list(APPEND wolflaser_INCLUDE_DIRS ${wolfcore_INCLUDE_DIRS})
-  list(REVERSE wolflaser_INCLUDE_DIRS)
-
-  list(REVERSE wolflaser_LIBRARIES)
-  list(APPEND wolflaser_LIBRARIES ${wolfcore_LIBRARIES})
-  list(REVERSE wolflaser_LIBRARIES)
-
-endif()
-
-# provide both INCLUDE_DIR and INCLUDE_DIRS
-SET(wolflaser_INCLUDE_DIR ${wolflaser_INCLUDE_DIRS})
-# provide both LIBRARY and LIBRARIES 
-SET(wolflaser_LIBRARY ${wolflaser_LIBRARIES})
\ No newline at end of file
diff --git a/cmake_modules/wolflaserConfig.cmake.in b/cmake_modules/wolflaserConfig.cmake.in
new file mode 100644
index 000000000..b079ac9ac
--- /dev/null
+++ b/cmake_modules/wolflaserConfig.cmake.in
@@ -0,0 +1,13 @@
+set(@PLUGIN_NAME@_VERSION 0.0.1)
+
+
+@PACKAGE_INIT@
+
+include(CMakeFindDependencyMacro)
+FIND_DEPENDENCY(wolfcore REQUIRED)
+FIND_DEPENDENCY(laser_scan_utils REQUIRED)
+FIND_DEPENDENCY(Eigen3 3.3 REQUIRED)
+
+include("${CMAKE_CURRENT_LIST_DIR}/@PLUGIN_NAME@Targets.cmake")
+
+check_required_components(@PLUGIN_NAME@)
\ No newline at end of file
diff --git a/internal/config.h.in b/internal/config.h.in
index 7014ca320..35f346833 100644
--- a/internal/config.h.in
+++ b/internal/config.h.in
@@ -24,13 +24,13 @@
 //            which will be added to the include path for compilation,
 //            and installed with the public wolf headers.
 
-#ifndef WOLF_INTERNAL_${UPPER_NAME}_CONFIG_H_
-#define WOLF_INTERNAL_${UPPER_NAME}_CONFIG_H_
+#ifndef WOLF_INTERNAL_${PROJECT_NAME_UPPER}_CONFIG_H_
+#define WOLF_INTERNAL_${PROJECT_NAME_UPPER}_CONFIG_H_
 
 #cmakedefine _WOLF_DEBUG
 
 #cmakedefine _WOLF_TRACE
 
-#define _WOLF_${UPPER_NAME}_ROOT_DIR "${_WOLF_ROOT_DIR}"
+#define _WOLF_${PROJECT_NAME_UPPER}_ROOT_DIR "${_WOLF_ROOT_DIR}"
 
 #endif /* WOLF_INTERNAL_CONFIG_H_ */
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index c75a1957b..a9fc8fb77 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,37 +1,27 @@
 # Retrieve googletest from github & compile
 add_subdirectory(gtest)
 
-# Include gtest directory.
-include_directories(${GTEST_INCLUDE_DIRS})
-
 ############# USE THIS TEST AS AN EXAMPLE #################
 #                                                         #
 # Create a specific test executable for gtest_example     #
 wolf_add_gtest(gtest_example gtest_example.cpp)           #
-target_link_libraries(gtest_example ${PLUGIN_NAME})       #
 #                                                         #
 ###########################################################
 
 wolf_add_gtest(gtest_landmark_polyline gtest_landmark_polyline.cpp)
-target_link_libraries(gtest_landmark_polyline ${PLUGIN_NAME})
 
 if(csm_FOUND)
 	wolf_add_gtest(gtest_processor_odom_icp gtest_processor_odom_icp.cpp)
-	target_link_libraries(gtest_processor_odom_icp ${PLUGIN_NAME} ${wolf_LIBRARY})
 	
 	wolf_add_gtest(gtest_processor_loop_closure_icp gtest_processor_loop_closure_icp.cpp)
-	target_link_libraries(gtest_processor_loop_closure_icp ${PLUGIN_NAME} ${wolf_LIBRARY})
 endif(csm_FOUND)
 
 wolf_add_gtest(gtest_polyline_2d gtest_polyline_2d.cpp)
-target_link_libraries(gtest_polyline_2d ${PLUGIN_NAME})
 
 if(falkolib_FOUND)
 	wolf_add_gtest(gtest_processor_loop_closure_falko gtest_processor_loop_closure_falko.cpp)
-	target_link_libraries(gtest_processor_loop_closure_falko ${PLUGIN_NAME} ${wolf_LIBRARY})
 
 	if(csm_FOUND)
     	wolf_add_gtest(gtest_processor_loop_closure_falko_icp gtest_processor_loop_closure_falko_icp.cpp)
-    	target_link_libraries(gtest_processor_loop_closure_falko_icp ${PLUGIN_NAME} ${wolf_LIBRARY})
   	endif(csm_FOUND)
 endif(falkolib_FOUND)
diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt
index 559756b02..0e851c5fb 100644
--- a/test/gtest/CMakeLists.txt
+++ b/test/gtest/CMakeLists.txt
@@ -1,65 +1,80 @@
-cmake_minimum_required(VERSION 2.8.8)
-project(gtest_builder C CXX)
+if(${CMAKE_VERSION} VERSION_LESS "3.11.0") 
+  message("CMake version less than 3.11.0")
 
-# We need thread support
-#find_package(Threads REQUIRED)
+  # Enable ExternalProject CMake module
+  include(ExternalProject)
 
-# Enable ExternalProject CMake module
-include(ExternalProject)
+  set(GTEST_FORCE_SHARED_CRT ON)
+  set(GTEST_DISABLE_PTHREADS OFF)
 
-set(GTEST_FORCE_SHARED_CRT ON)
-set(GTEST_DISABLE_PTHREADS OFF)
+  # For some reason I need to disable PTHREADS
+  # with g++ (Ubuntu 4.9.3-8ubuntu2~14.04) 4.9.3
+  # This is a known issue for MinGW :
+  # https://github.com/google/shaderc/pull/174
+  #if(MINGW)
+  #    set(GTEST_DISABLE_PTHREADS ON)
+  #endif()
 
-# For some reason I need to disable PTHREADS
-# with g++ (Ubuntu 4.9.3-8ubuntu2~14.04) 4.9.3
-# This is a known issue for MinGW :
-# https://github.com/google/shaderc/pull/174
-#if(MINGW)
-    set(GTEST_DISABLE_PTHREADS ON)
-#endif()
+  # Download GoogleTest
+  ExternalProject_Add(googletest
+      GIT_REPOSITORY https://github.com/google/googletest.git
+      GIT_TAG        v1.8.x
+      # TIMEOUT 1 # We'll try this
+      CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
+      -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
+      -DCMAKE_CXX_FLAGS=${MSVC_COMPILER_DEFS}
+      -Dgtest_force_shared_crt=${GTEST_FORCE_SHARED_CRT}
+      -Dgtest_disable_pthreads=${GTEST_DISABLE_PTHREADS}
+      -DBUILD_GTEST=ON
+      PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
+      # Disable install step
+      INSTALL_COMMAND ""
+      UPDATE_DISCONNECTED 1 # 1: do not update googletest; 0: update googletest via github
+  )
 
-# Download GoogleTest
-ExternalProject_Add(googletest
-    GIT_REPOSITORY https://github.com/google/googletest.git
-    GIT_TAG        v1.8.x
-    # TIMEOUT 1 # We'll try this
-    CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
-    -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
-    -DCMAKE_CXX_FLAGS=${MSVC_COMPILER_DEFS}
-    -Dgtest_force_shared_crt=${GTEST_FORCE_SHARED_CRT}
-    -Dgtest_disable_pthreads=${GTEST_DISABLE_PTHREADS}
-    -DBUILD_GTEST=ON
-    PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
-    # Disable install step
-    INSTALL_COMMAND ""
-    UPDATE_DISCONNECTED 1 # 1: do not update googletest; 0: update googletest via github
-)
+  # Get GTest source and binary directories from CMake project
 
-# Get GTest source and binary directories from CMake project
+  # Specify include dir
+  ExternalProject_Get_Property(googletest source_dir)
+  set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include PARENT_SCOPE)
 
-# Specify include dir
-ExternalProject_Get_Property(googletest source_dir)
-set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include PARENT_SCOPE)
+  # Specify MainTest's link libraries
+  ExternalProject_Get_Property(googletest binary_dir)
+  set(GTEST_LIBS_DIR ${binary_dir}/googlemock/gtest PARENT_SCOPE)
 
-# Specify MainTest's link libraries
-ExternalProject_Get_Property(googletest binary_dir)
-set(GTEST_LIBS_DIR ${binary_dir}/googlemock/gtest PARENT_SCOPE)
+  # Create a libgtest target to be used as a dependency by test programs
+  add_library(libgtest IMPORTED STATIC GLOBAL)
+  add_dependencies(libgtest googletest)
 
-# Create a libgtest target to be used as a dependency by test programs
-add_library(libgtest IMPORTED STATIC GLOBAL)
-add_dependencies(libgtest googletest)
+  # Set libgtest properties
+  set_target_properties(libgtest PROPERTIES
+      "IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a"
+      "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
+  )
 
-# Set libgtest properties
-set_target_properties(libgtest PROPERTIES
-    "IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a"
-    "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
-)
+else()
 
+  message("CMake version equal or greater than 3.11.0")
+
+  include(FetchContent)
+
+  FetchContent_Declare(
+    googletest
+    GIT_REPOSITORY https://github.com/google/googletest.git 
+    GIT_TAG main)
+
+  FetchContent_MakeAvailable(googletest)
+    
+endif()
+  
 function(wolf_add_gtest target)
   add_executable(${target} ${ARGN})
-  add_dependencies(${target} libgtest)
-  target_link_libraries(${target} libgtest)
-
-  #WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
+  if(${CMAKE_VERSION} VERSION_LESS "3.11.0") 
+    add_dependencies(${target} libgtest)
+    target_link_libraries(${target} libgtest ${PLUGIN_NAME})
+    target_include_directories(${target} ${GTEST_INCLUDE_DIRS})
+  else()
+    target_link_libraries(${target} gtest_main ${PLUGIN_NAME})
+  endif()
   add_test(NAME ${target} COMMAND ${target})
 endfunction()
-- 
GitLab