Skip to content

Commit 86a6fe0

Browse files
committed
cmake: Fix logic for locating gtest library.
1 parent ef2f3ca commit 86a6fe0

File tree

1 file changed

+63
-60
lines changed

1 file changed

+63
-60
lines changed

cdk/cmake/gtest.cmake

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,84 +39,87 @@ ENDMACRO(SETUP_GTEST)
3939

4040
SET(WITH_GTEST $ENV{WITH_GTEST} CACHE PATH "Location of gtest")
4141

42-
#
43-
# TODO: Try to find gtest in system-wide locations if WITH_GTEST is
44-
# not set
45-
#
4642

47-
if(DEFINED WITH_GTEST)
48-
set(GTEST_ROOT ${WITH_GTEST})
49-
endif()
43+
if(NOT WITH_GTEST)
5044

51-
find_package(GTest)
45+
#
46+
# Try to find gtest in system-wide locations
47+
#
5248

53-
if(NOT GTEST_FOUND)
49+
find_package(GTest)
5450

55-
IF(NOT DEFINED WITH_GTEST)
56-
MESSAGE(FATAL_ERROR "This project requires gtest libraries"
57-
" but WITH_GTEST option was not specified."
58-
" Please set it to the location where gtest"
59-
" was built from sources using cmake.")
60-
ENDIF()
51+
if(NOT GTEST_FOUND)
52+
message(FATAL_ERROR "This project requires gtest libraries"
53+
" but WITH_GTEST option was not specified."
54+
" Please set it to the location where gtest"
55+
" was built from sources using cmake.")
56+
endif()
6157

62-
message("Looking for gtest build in: ${WITH_GTEST}")
58+
list(GET GTEST_LIBRARIES 0 gtest_location)
59+
list(GET GTEST_MAIN_LIBRARIES 0 gtest_main_location)
6360

64-
#
65-
# TODO: Configure gtest build if sources location is given
66-
#
61+
else(NOT WITH_GTEST)
6762

68-
IF(NOT EXISTS "${WITH_GTEST}/CMakeCache.txt")
69-
MESSAGE(FATAL_ERROR
70-
"Could not find gtest build in this location: ${WITH_GTEST}"
71-
)
72-
ENDIF()
63+
message("Looking for gtest build in: ${WITH_GTEST}")
7364

74-
#
75-
# Read source location from build configuration cache and set
76-
# GTEST_INCLUDE_DIR.
77-
#
65+
#
66+
# TODO: Configure gtest build if source location is given
67+
#
7868

79-
LOAD_CACHE(${WITH_GTEST} READ_WITH_PREFIX GTEST_
80-
CMAKE_PROJECT_NAME)
81-
#MESSAGE(STATUS "Gtest project name: ${GTEST_CMAKE_PROJECT_NAME}")
69+
if(NOT EXISTS "${WITH_GTEST}/CMakeCache.txt")
70+
message(FATAL_ERROR
71+
"Could not find gtest build in this location: ${WITH_GTEST}"
72+
)
73+
endif()
8274

83-
LOAD_CACHE(${WITH_GTEST} READ_WITH_PREFIX GTEST_
84-
${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR)
75+
#
76+
# Read source location from build configuration cache and set
77+
# GTEST_INCLUDE_DIR.
78+
#
8579

86-
FIND_PATH(GTEST_INCLUDE_DIRS
87-
NAMES gtest/gtest.h
88-
PATHS ${GTEST_${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR}/include ${GTEST_${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR}/gtest/include
89-
NO_DEFAULT_PATH
90-
)
80+
load_cache(${WITH_GTEST} READ_WITH_PREFIX GTEST_
81+
CMAKE_PROJECT_NAME
82+
)
83+
#MESSAGE(STATUS "Gtest project name: ${GTEST_CMAKE_PROJECT_NAME}")
9184

92-
IF(NOT EXISTS "${GTEST_INCLUDE_DIRS}/gtest/gtest.h")
93-
MESSAGE(FATAL_ERROR "Could not find gtest headers at: ${GTEST_INCLUDE_DIRS}")
94-
ENDIF()
85+
load_cache(${WITH_GTEST} READ_WITH_PREFIX GTEST_
86+
${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR
87+
)
9588

96-
#
97-
# TODO: Run build if libraries can not be found in expected locations
98-
#
89+
message("Gtest sources at: ${GTEST_${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR}")
9990

100-
FIND_LIBRARY(gtest_location
101-
NAMES libgtest gtest
102-
PATHS ${WITH_GTEST} ${WITH_GTEST}/gtest
103-
PATH_SUFFIXES . Release RelWithDebInfo Debug
104-
NO_DEFAULT_PATH
105-
)
91+
find_path(GTEST_INCLUDE_DIRS
92+
NAMES gtest/gtest.h
93+
PATHS
94+
${GTEST_${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR}/include
95+
${GTEST_${GTEST_CMAKE_PROJECT_NAME}_SOURCE_DIR}/gtest/include
96+
NO_DEFAULT_PATH
97+
)
10698

107-
FIND_LIBRARY(gtest_main_location
108-
NAMES libgtest_main gtest_main
109-
PATHS ${WITH_GTEST} ${WITH_GTEST}/gtest
110-
PATH_SUFFIXES . Release RelWithDebInfo Debug
111-
NO_DEFAULT_PATH
112-
)
99+
if(NOT EXISTS "${GTEST_INCLUDE_DIRS}/gtest/gtest.h")
100+
message(FATAL_ERROR "Could not find gtest headers at: ${GTEST_INCLUDE_DIRS}")
101+
endif()
102+
103+
#
104+
# TODO: Run build if libraries can not be found in expected locations
105+
#
106+
107+
find_library(gtest_location
108+
NAMES libgtest gtest
109+
PATHS ${WITH_GTEST} ${WITH_GTEST}/gtest
110+
PATH_SUFFIXES . Release RelWithDebInfo Debug
111+
NO_DEFAULT_PATH
112+
)
113113

114-
else()
114+
find_library(gtest_main_location
115+
NAMES libgtest_main gtest_main
116+
PATHS ${WITH_GTEST} ${WITH_GTEST}/gtest
117+
PATH_SUFFIXES . Release RelWithDebInfo Debug
118+
NO_DEFAULT_PATH
119+
)
115120

116-
list(GET GTEST_LIBRARIES 0 gtest_location)
117-
list(GET GTEST_MAIN_LIBRARIES 0 gtest_main_location)
121+
endif(NOT WITH_GTEST)
118122

119-
endif(NOT GTEST_FOUND)
120123

121124
MESSAGE(STATUS "GTEST_INCLUDE_DIRS: ${GTEST_INCLUDE_DIRS}")
122125

0 commit comments

Comments
 (0)