Do not add_library() gmock and gtest if targets already exist
* Do not add_library() gmock and gmock_main() if already existing
Fixes build problems with Google Cartographer (https://github.com/googlecartographer/cartographer_ros/issues/776), which provides its own `FindGMock.cmake`.
* Also check if gtest exists before calling add_library()
@@ -366,10 +366,14 @@ if(NOT GMOCK_FOUND)
endif()
else()
message(STATUS "Found gtest: gtests will be built")
+ if(NOT TARGET gtest)
add_library(gtest SHARED IMPORTED)
set_target_properties(gtest PROPERTIES IMPORTED_LOCATION "${GTEST_LIBRARIES}")
+ endif()
+ if(NOT TARGET gtest_main)
add_library(gtest_main SHARED IMPORTED)
set_target_properties(gtest_main PROPERTIES IMPORTED_LOCATION "${GTEST_MAIN_LIBRARIES}")
+ endif()
set(GTEST_FOUND ${GTEST_FOUND} CACHE INTERNAL "")
set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} CACHE INTERNAL "")
set(GTEST_LIBRARIES ${GTEST_LIBRARIES} CACHE INTERNAL "")
@@ -378,10 +382,14 @@ if(NOT GMOCK_FOUND)
endif()
else()
message(STATUS "Found gmock: gmock and gtests will be built")
+ if(NOT TARGET gmock)
add_library(gmock UNKNOWN IMPORTED)
set_target_properties(gmock PROPERTIES IMPORTED_LOCATION "${GMOCK_LIBRARIES}")
+ endif()
+ if(NOT TARGET gmock_main)
add_library(gmock_main UNKNOWN IMPORTED)
set_target_properties(gmock_main PROPERTIES IMPORTED_LOCATION "${GMOCK_MAIN_LIBRARIES}")
+ endif()
set(GMOCK_FOUND ${GMOCK_FOUND} CACHE INTERNAL "")
set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIRS} CACHE INTERNAL "")
set(GMOCK_LIBRARIES ${GMOCK_LIBRARIES} CACHE INTERNAL "")
