Skip to content

Commit 88b016c

Browse files
Gtest (#378)
Update CMake code with specific variable allowing for use of external gtest library. Add CI tests for external gtest and clean up a few other warnings and tests. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 17c1050 commit 88b016c

File tree

11 files changed

+314
-84
lines changed

11 files changed

+314
-84
lines changed

.github/workflows/pip.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ jobs:
5959
run: |
6060
mkdir -p build
6161
cd build
62-
cmake .. -DUNITS_BUILD_SHARED_LIBRARY=ON -DUNITS_BUILD_STATIC_LIBRARY=OFF -DUNITS_INSTALL=ON
62+
cmake .. -DBUILD_SHARED_LIBS=ON -DUNITS_INSTALL=ON -DUNITS_ENABLE_TESTS=OFF
6363
make -j 4
6464
sudo make install
6565
cd ..
6666
python -m pip install pytest
67-
pip install -vv -C cmake.define.UNITS_BUILD_SHARED_LIBRARY:BOOL=ON -C cmake.define.UNITS_BUILD_STATIC_LIBRARY:BOOL=OFF .
67+
pip install -vv -C cmake.define.BUILD_SHARED_LIBS:BOOL=ON .
6868
6969
- name: Test
7070
run: |
@@ -91,7 +91,7 @@ jobs:
9191
- name: Build and install
9292
run: |
9393
python -m pip install pytest
94-
pip install -vv -C cmake.define.UNITS_BUILD_SHARED_LIBRARY:BOOL=ON -C cmake.define.UNITS_BUILD_STATIC_LIBRARY:BOOL=OFF -C cmake.define.UNITS_PYTHON_INSTALL_SHARED_LIBRARY:BOOL=ON .
94+
pip install -vv -C cmake.define.BUILD_SHARED_LIBS:BOOL=ON -C cmake.define.UNITS_PYTHON_INSTALL_SHARED_LIBRARY:BOOL=ON .
9595
9696
- name: Test
9797
run: python -m pytest ./test/python

CMakeLists.txt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,36 @@ add_subdirectory(units)
211211
if(UNITS_BUILD_FUZZ_TARGETS)
212212
add_subdirectory(FuzzTargets)
213213
elseif(UNITS_ENABLE_TESTS AND NOT CMAKE_VERSION VERSION_LESS 3.13)
214-
include(updateGitSubmodules)
215-
if(UNITS_ENABLE_SUBMODULE_UPDATE)
216-
enable_testing()
217-
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/ThirdParty/googletest/CMakeLists.txt")
218-
submod_update(ThirdParty/googletest)
219-
endif()
220-
else()
214+
enable_testing()
215+
option(
216+
UNITS_USE_EXTERNAL_GTEST
217+
"use an installed or external version of GTest instead of the included submodule"
218+
OFF
219+
)
220+
mark_as_advanced(UNITS_USE_EXTERNAL_GTEST)
221+
222+
if(UNITS_USE_EXTERNAL_GTEST)
221223
include(FindPkgConfig)
222224
find_package(GTest REQUIRED)
223225
pkg_check_modules(GMock gmock REQUIRED)
226+
else()
227+
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/ThirdParty/googletest/CMakeLists.txt")
228+
include(updateGitSubmodules)
229+
if(${UNITS_CMAKE_PROJECT_NAME}_ENABLE_SUBMODULE_UPDATE)
230+
submod_update(ThirdParty/googletest)
231+
else()
232+
include(FindPkgConfig)
233+
find_package(GTest REQUIRED)
234+
pkg_check_modules(GMock gmock REQUIRED)
235+
if(NOT GTest_FOUND)
236+
message(
237+
ERROR
238+
"GTest not found, and submodule not found and unable to update install gtest or enable submodule updating to build a local copy of gtest"
239+
)
240+
endif()
241+
endif()
242+
endif()
243+
224244
endif()
225245
if(BUILD_TESTING)
226246
add_subdirectory(test)

azure-pipelines.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,28 @@ jobs:
3232
- script: git diff --exit-code --color
3333
displayName: Check tidy
3434

35-
- job: Native
35+
- job: Native_gtest
3636
strategy:
3737
matrix:
3838
Linux14:
3939
vmImage: 'ubuntu-latest'
40+
units.options: -DUNITS_USE_EXTERNAL_GTEST=ON
4041
Linux14shared:
4142
vmImage: 'ubuntu-latest'
42-
units.options: -DUNITS_BUILD_SHARED_LBIRARY=ON
43+
units.options: -DBUILD_SHARED_LIBS=ON -DUNITS_USE_EXTERNAL_GTEST=ON
44+
pool:
45+
vmImage: $(vmImage)
46+
steps:
47+
- script: |
48+
sudo apt-get update -qq -y # It's good practice to update before installing
49+
sudo apt-get install -qq -y libgtest-dev libgmock-dev
50+
displayName: 'Install gtest' # Optional: Add a display name for the step
51+
- template: .ci/azure-build.yml
52+
- template: .ci/azure-test.yml
53+
54+
- job: Native
55+
strategy:
56+
matrix:
4357
macOS17:
4458
vmImage: 'macOS-latest'
4559
units.std: 17
@@ -48,7 +62,7 @@ jobs:
4862
units.std: 11
4963
Windows17:
5064
vmImage: 'windows-2019'
51-
units.options: -DUNITS_BUILD_SHARED_LBIRARY=ON
65+
units.options: -DUNITS_BUILD_SHARED_LIBRARY=ON -DUNITS_BUILD_STATIC_LIBRARY=OFF
5266
units.std: 17
5367
Windows32bit:
5468
vmImage: 'windows-2019'

config/AddGoogletest.cmake

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,34 @@
1111
# Add make check, as well, which gives output on failed tests without having to set an
1212
# environment variable.
1313
#
14+
1415
include(extraMacros)
15-
set(CMAKE_WARN_DEPRECATED
16-
OFF
17-
CACHE INTERNAL "" FORCE
18-
)
19-
set(gtest_force_shared_crt
20-
ON
21-
CACHE INTERNAL ""
22-
)
23-
24-
set(BUILD_SHARED_LIBS
25-
OFF
26-
CACHE INTERNAL ""
27-
)
28-
set(HAVE_STD_REGEX
29-
ON
30-
CACHE INTERNAL ""
31-
)
32-
33-
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS
34-
1
35-
CACHE INTERNAL ""
36-
)
37-
38-
if(NOT GTest_FOUND)
16+
17+
if(NOT UNITS_USE_EXTERNAL_GTEST AND NOT GTest_FOUND)
18+
19+
set(CMAKE_WARN_DEPRECATED
20+
OFF
21+
CACHE INTERNAL "" FORCE
22+
)
23+
set(gtest_force_shared_crt
24+
ON
25+
CACHE INTERNAL ""
26+
)
27+
28+
set(BUILD_SHARED_LIBS
29+
OFF
30+
CACHE INTERNAL ""
31+
)
32+
set(HAVE_STD_REGEX
33+
ON
34+
CACHE INTERNAL ""
35+
)
36+
37+
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS
38+
1
39+
CACHE INTERNAL ""
40+
)
41+
3942
add_subdirectory(
4043
${CMAKE_SOURCE_DIR}/ThirdParty/googletest
4144
${CMAKE_BINARY_DIR}/ThirdParty/googletest EXCLUDE_FROM_ALL
@@ -55,6 +58,36 @@ if(NOT GTest_FOUND)
5558
gmock_main PUBLIC "-Wno-undef" "-Wno-c++17-attribute-extensions"
5659
)
5760
endif()
61+
62+
hide_variable(gmock_build_tests)
63+
hide_variable(gtest_build_samples)
64+
hide_variable(gtest_build_tests)
65+
hide_variable(gtest_disable_pthreads)
66+
hide_variable(gtest_hide_internal_symbols)
67+
hide_variable(BUILD_GMOCK)
68+
hide_variable(BUILD_GTEST)
69+
hide_variable(INSTALL_GTEST)
70+
hide_variable(GTEST_HAS_ABSL)
71+
72+
set_target_properties(gtest gtest_main gmock gmock_main PROPERTIES FOLDER "Extern")
73+
74+
if(MSVC)
75+
# add_compile_options( /wd4459)
76+
if(MSVC_VERSION GREATER_EQUAL 1900)
77+
target_compile_definitions(
78+
gtest PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
79+
)
80+
target_compile_definitions(
81+
gtest_main PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
82+
)
83+
target_compile_definitions(
84+
gmock PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
85+
)
86+
target_compile_definitions(
87+
gmock_main PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
88+
)
89+
endif()
90+
endif()
5891
endif()
5992

6093
if(GOOGLE_TEST_INDIVIDUAL)
@@ -85,35 +118,3 @@ macro(add_gtest TESTNAME)
85118
endif()
86119

87120
endmacro()
88-
89-
hide_variable(gmock_build_tests)
90-
hide_variable(gtest_build_samples)
91-
hide_variable(gtest_build_tests)
92-
hide_variable(gtest_disable_pthreads)
93-
hide_variable(gtest_hide_internal_symbols)
94-
hide_variable(BUILD_GMOCK)
95-
hide_variable(BUILD_GTEST)
96-
hide_variable(INSTALL_GTEST)
97-
hide_variable(GTEST_HAS_ABSL)
98-
99-
if(NOT GTest_FOUND)
100-
set_target_properties(gtest gtest_main gmock gmock_main PROPERTIES FOLDER "Extern")
101-
endif()
102-
103-
if(MSVC)
104-
# add_compile_options( /wd4459)
105-
if(MSVC_VERSION GREATER_EQUAL 1900)
106-
target_compile_definitions(
107-
gtest PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
108-
)
109-
target_compile_definitions(
110-
gtest_main PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
111-
)
112-
target_compile_definitions(
113-
gmock PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
114-
)
115-
target_compile_definitions(
116-
gmock_main PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
117-
)
118-
endif()
119-
endif()

0 commit comments

Comments
 (0)