Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3267 cmake googletest #3290

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
# sudo -E ./ci/setup_gcc10.sh
# sudo -E ./ci/setup_cmake.sh
# sudo -E ./ci/setup_ci_environment.sh
# sudo -E ./ci/setup_googletest.sh
# git submodule update --init third_party/googletest
# sudo -E ./ci/install_abseil.sh
# sudo -E ./ci/install_protobuf.sh
# - name: run otlp exporter tests
Expand All @@ -52,8 +52,8 @@ jobs:
CC: /usr/bin/gcc-12
CXX: /usr/bin/g++-12
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed. The submodules are initialized by the checkout action above.

- name: run cmake tests (without otlp-exporter)
env:
CC: /usr/bin/gcc-12
Expand All @@ -75,8 +75,8 @@ jobs:
PROTOBUF_VERSION: 21.12
run: |
sudo apt remove needrestart #refer: https://github.com/actions/runner-images/issues/9937
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
sudo -E ./ci/install_protobuf.sh
- name: setup grpc
run: |
Expand Down Expand Up @@ -111,8 +111,8 @@ jobs:
PROTOBUF_VERSION: 21.12
run: |
sudo apt remove needrestart #refer: https://github.com/actions/runner-images/issues/9937
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
sudo -E ./ci/install_protobuf.sh
- name: setup grpc
run: |
Expand Down Expand Up @@ -147,8 +147,8 @@ jobs:
PROTOBUF_VERSION: 21.12
run: |
sudo apt remove needrestart #refer: https://github.com/actions/runner-images/issues/9937
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
sudo -E ./ci/install_protobuf.sh
- name: setup grpc
run: |
Expand Down Expand Up @@ -183,8 +183,8 @@ jobs:
PROTOBUF_VERSION: 21.12
run: |
sudo apt remove needrestart #refer: https://github.com/actions/runner-images/issues/9937
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
sudo -E ./ci/install_protobuf.sh
- name: setup grpc
run: |
Expand Down Expand Up @@ -219,8 +219,8 @@ jobs:
PROTOBUF_VERSION: 21.12
run: |
sudo apt remove needrestart #refer: https://github.com/actions/runner-images/issues/9937
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
sudo -E ./ci/install_protobuf.sh
- name: setup grpc
run: |
Expand Down Expand Up @@ -296,8 +296,8 @@ jobs:
CC: /usr/bin/gcc-12
CXX: /usr/bin/g++-12
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run cmake tests (without otlp-exporter)
env:
CC: /usr/bin/gcc-12
Expand All @@ -314,8 +314,8 @@ jobs:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run cmake tests (enable abseil-cpp)
run: |
sudo ./ci/install_abseil.sh
Expand All @@ -330,8 +330,8 @@ jobs:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run cmake tests (enable opentracing-shim)
run: ./ci/do_ci.sh cmake.opentracing_shim.test

Expand All @@ -345,7 +345,7 @@ jobs:
- name: setup
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
git submodule update --init third_party/googletest
- name: run tests (enable stl)
env:
CXX_STANDARD: '14'
Expand All @@ -361,7 +361,7 @@ jobs:
- name: setup
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
git submodule update --init third_party/googletest
- name: run tests (enable stl)
env:
CXX_STANDARD: '17'
Expand All @@ -377,7 +377,7 @@ jobs:
- name: setup
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
git submodule update --init third_party/googletest
- name: run tests
env:
CXX_STANDARD: '20'
Expand All @@ -401,7 +401,7 @@ jobs:
CXXFLAGS: "-stdlib=libc++"
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
git submodule update --init third_party/googletest
- name: run tests
env:
CC: /usr/bin/clang
Expand All @@ -427,7 +427,7 @@ jobs:
- name: setup
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
git submodule update --init third_party/googletest
- name: run tests
env:
CXX_STANDARD: '23'
Expand All @@ -451,7 +451,7 @@ jobs:
CXXFLAGS: "-stdlib=libc++"
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
git submodule update --init third_party/googletest
- name: run tests
env:
CC: /usr/bin/clang
Expand All @@ -476,8 +476,8 @@ jobs:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run otlp exporter tests
run: |
sudo ./ci/setup_grpc.sh
Expand Down Expand Up @@ -505,8 +505,8 @@ jobs:
ABSEIL_CPP_VERSION: '20230125.3'
CXX_STANDARD: '14'
run: |
sudo ./ci/setup_googletest.sh
sudo ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
sudo -E ./ci/install_abseil.sh
sudo -E ./ci/install_protobuf.sh
- name: run otlp exporter tests
Expand All @@ -526,8 +526,8 @@ jobs:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run otlp exporter tests
run: |
sudo ./ci/setup_grpc.sh
Expand All @@ -542,8 +542,8 @@ jobs:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run otlp exporter tests
run: |
sudo ./ci/setup_grpc.sh -T
Expand All @@ -558,8 +558,8 @@ jobs:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run cmake install (with abseil)
run: |
sudo ./ci/install_abseil.sh
Expand All @@ -580,8 +580,8 @@ jobs:
CC: /usr/bin/gcc-12
CXX: /usr/bin/g++-12
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run tests
env:
CC: /usr/bin/gcc-12
Expand Down Expand Up @@ -907,8 +907,8 @@ jobs:
CC: /usr/bin/gcc-10
CXX: /usr/bin/g++-10
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run tests and generate report
env:
CC: /usr/bin/gcc-10
Expand Down Expand Up @@ -978,8 +978,8 @@ jobs:
CC: /usr/bin/gcc-12
CXX: /usr/bin/g++-12
run: |
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/setup_ci_environment.sh
git submodule update --init third_party/googletest
- name: run w3c trace-context test server (background)
env:
CXX_STANDARD: '14'
Expand Down
96 changes: 81 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -620,19 +620,8 @@ list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")

include(CTest)
if(BUILD_TESTING)
if(EXISTS ${CMAKE_BINARY_DIR}/lib/libgtest.a)
# Prefer GTest from build tree. GTest is not always working with
# CMAKE_PREFIX_PATH
set(GTEST_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googletest/include
${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googlemock/include)
if(TARGET gtest)
set(GTEST_BOTH_LIBRARIES gtest gtest_main)
else()
set(GTEST_BOTH_LIBRARIES ${CMAKE_BINARY_DIR}/lib/libgtest.a
${CMAKE_BINARY_DIR}/lib/libgtest_main.a)
endif()
elseif(WIN32)

if(WIN32)
Copy link
Contributor

@dbarker dbarker Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general approach looks good - 1) try to find an installed gtest cmake package, 2) build the submodule for gtest if no package is found.

We should try to find the gtest package first on all platforms though - not just windows. The cmake install github workflow has jobs that install gtest with conan on Ubuntu and MacOS and we should ensure those packages are found instead of building the submodule.

# Make sure we are always bootsrapped with vcpkg on Windows
find_package(GTest)
if(NOT (GTEST_FOUND OR GTest_FOUND))
Expand All @@ -648,9 +637,81 @@ if(BUILD_TESTING)
endif()
endif()
else()
# Prefer GTest installed by OS distro, brew or vcpkg package manager
find_package(GTest REQUIRED)
# Build googletest from sources. This is the preferred way.
set(GTEST_SRC_DIR_DEFAULT ${CMAKE_SOURCE_DIR}/third_party/googletest)
set(GTEST_SRC_DIR ${GTEST_SRC_DIR_DEFAULT} CACHE STRING "Location of gtest sources")
if(EXISTS ${GTEST_SRC_DIR}/googletest/src/gtest-all.cc)

include(ExternalProject)

# Build googletest/googletest from sources available as third party
ExternalProject_Add(
googletest
DOWNLOAD_COMMAND ""
SOURCE_DIR ${GTEST_SRC_DIR}/googletest
INSTALL_COMMAND ""
LOG_CONFIGURE ON
LOG_BUILD ON
CMAKE_ARGS -DBUILD_SHARED_LIBS=ON
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
#BUILD_BYPRODUCTS
#${CMAKE_BINARY_DIR}/googletest-prefix/src/googletest-build/lib/libgtest.so
#${CMAKE_BINARY_DIR}/googletest-prefix/src/googletest-build/lib/libgtest_main.so
)
message(STATUS "Building googletest from sources in ${GTEST_SRC_DIR}")

set(GTEST_LIBRARY_PATH ${CMAKE_BINARY_DIR}/googletest-prefix/src/googletest-build/lib/libgtest.so)
add_library(GTest::gtest SHARED IMPORTED)
set_target_properties(GTest::gtest PROPERTIES IMPORTED_LOCATION
${GTEST_LIBRARY_PATH})
add_dependencies(GTest::gtest googletest)

set(GTEST_MAIN_LIBRARY_PATH ${CMAKE_BINARY_DIR}/googletest-prefix/src/googletest-build/lib/libgtest_main.so)
add_library(GTest::gtest_main SHARED IMPORTED)
set_target_properties(GTest::gtest_main PROPERTIES IMPORTED_LOCATION
${GTEST_MAIN_LIBRARY_PATH})
add_dependencies(GTest::gtest_main googletest)

ExternalProject_Get_Property(googletest source_dir)
set(GTEST_INCLUDE_DIR ${source_dir}/include)

# Build googletest/googlemock from sources available as third party
ExternalProject_Add(
googlemock
DOWNLOAD_COMMAND ""
SOURCE_DIR ${GTEST_SRC_DIR}/googlemock
INSTALL_COMMAND ""
LOG_CONFIGURE ON
LOG_BUILD ON
CMAKE_ARGS -DBUILD_SHARED_LIBS=ON
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
)
message(STATUS "Building googlemock from sources in ${GTEST_SRC_DIR}")

set(GMOCK_LIBRARY_PATH ${CMAKE_BINARY_DIR}/googlemock-prefix/src/googlemock-build/lib/libgmock.so)
add_library(GTest::gmock SHARED IMPORTED)
set_target_properties(GTest::gmock PROPERTIES IMPORTED_LOCATION
${GMOCK_LIBRARY_PATH})
add_dependencies(GTest::gmock googlemock)

set(GMOCK_MAIN_LIBRARY_PATH ${CMAKE_BINARY_DIR}/googlemock-prefix/src/googlemock-build/lib/libgmock_main.so)
add_library(GTest::gmock_main SHARED IMPORTED)
set_target_properties(GTest::gmock_main PROPERTIES IMPORTED_LOCATION
${GMOCK_MAIN_LIBRARY_PATH})
add_dependencies(GTest::gtest_main googlemock)

set(GTEST_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googletest/include
${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googlemock/include)

set(GTEST_BOTH_LIBRARIES GTest::gtest_main GTest::gtest)
set(GMOCK_LIB GTest::gmock)

else()
message(FATAL_ERROR "UNIT_TEST is set but unable to find gtest library source code")
endif()
endif()

if(NOT GTEST_BOTH_LIBRARIES)
# New GTest package names
if(TARGET GTest::gtest)
Expand All @@ -664,6 +725,11 @@ if(BUILD_TESTING)
endif()
message(STATUS "GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
message(STATUS "GTEST_BOTH_LIBRARIES = ${GTEST_BOTH_LIBRARIES}")
message(STATUS "gtest library path ${GTEST_LIBRARY_PATH}")
message(STATUS "gtest main library path ${GTEST_MAIN_LIBRARY_PATH}")
message(STATUS "GMOCK_LIB = ${GMOCK_LIB}")
message(STATUS "gmock library path ${GMOCK_LIBRARY_PATH}")
message(STATUS "gmock main library path ${GMOCK_MAIN_LIBRARY_PATH}")

# Try to find gmock
if(NOT GMOCK_LIB AND TARGET GTest::gmock)
Expand Down
6 changes: 5 additions & 1 deletion ci/setup_ci_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ apt-get install --no-install-recommends --no-install-suggests -y \
valgrind \
lcov \
iwyu \
pkg-config
pkg-config \
libbenchmark-dev \
zlib1g-dev \
libcurl4-openssl-dev \
nlohmann-json3-dev
Loading