Skip to content

Commit 3c97ebe

Browse files
committed
build(cmake): Decouple gtest lib/headers and avoid overwriting
1 parent 557aea8 commit 3c97ebe

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

tests/CMakeLists.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,43 @@ link_directories(${Boost_LIBRARY_DIRS})
2323
#---------------------------
2424
if (DEFINED ENV{GTEST_ROOT})
2525
set(GTEST_ROOT $ENV{GTEST_ROOT})
26-
else ()
26+
elseif (NOT DEFINED GTEST_ROOT)
2727
if (EXISTS "/usr/local/include/gtest/gtest.h")
2828
set(GTEST_ROOT /usr/local)
2929
elseif (EXISTS "/opt/homebrew/include/gtest/gtest.h")
3030
set(GTEST_ROOT /opt/homebrew)
3131
endif ()
3232
endif ()
3333

34-
if (NOT EXISTS "${GTEST_ROOT}/include/gtest/gtest.h")
34+
# include dir
35+
if (EXISTS "${GTEST_ROOT}/include/gtest/gtest.h")
36+
set(GTEST_INCLUDE_DIR ${GTEST_ROOT}/include)
37+
else ()
38+
if (NOT DEFINED GTEST_INCLUDE_DIR)
39+
set(GTEST_INCLUDE_DIR ENV{GTEST_INCLUDE_DIR})
40+
endif()
41+
endif ()
42+
if (NOT DEFINED GTEST_INCLUDE_DIR)
3543
message(FATAL_ERROR "Could not find headers for gtest!")
3644
endif ()
3745

38-
if (NOT (EXISTS "${GTEST_ROOT}/lib/libgtest_main.a"
46+
if ((EXISTS "${GTEST_ROOT}/lib/libgtest_main.a"
3947
OR EXISTS "${GTEST_ROOT}/lib/libgtest_main.so"
4048
OR EXISTS "${GTEST_ROOT}/lib/gtest_main.lib"))
49+
set(GTEST_LIBRARY_DIR ${GTEST_ROOT}/lib)
50+
else ()
51+
if (NOT DEFINED GTEST_LIBRARY_DIR)
52+
set(GTEST_LIBRARY_DIR ENV{GTEST_LIBRARY_DIR})
53+
endif()
54+
endif ()
55+
if (NOT DEFINED GTEST_LIBRARY_DIR)
4156
message(FATAL_ERROR "Could not find library for gtest!")
4257
endif ()
4358

4459
message(STATUS "googletest root directory: ${GTEST_ROOT}")
4560

46-
include_directories(SYSTEM ${GTEST_ROOT}/include)
47-
link_directories(${GTEST_ROOT}/lib ${GTEST_ROOT}/bin)
61+
include_directories(SYSTEM ${GTEST_INCLUDE_DIR})
62+
link_directories(${CMAKE_LIBRARY_DIR} ${GTEST_ROOT}/bin)
4863

4964

5065
#---------------------------

0 commit comments

Comments
 (0)