Skip to content

Commit

Permalink
Require C++17
Browse files Browse the repository at this point in the history
Policy information:
https://opensource.google/documentation/policies/cplusplus-support#c_language_standard
https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

Some small fixes are included for C++17 compatibility.

We had no tests for MSVC C++17 or C++20, so those tests that failed
and had no obvious fix are disabled and a tracking bug has been filed.

PiperOrigin-RevId: 726090558
Change-Id: I4d37d47e87c11f85bfd572deb10f67ca3eb2a9b5
  • Loading branch information
derekmauro authored and copybara-github committed Feb 12, 2025
1 parent 4a00a24 commit c00fd25
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 57 deletions.
72 changes: 45 additions & 27 deletions ci/linux-presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,68 @@ if [[ -z ${GTEST_ROOT:-} ]]; then
fi

if [[ -z ${STD:-} ]]; then
STD="c++14 c++17 c++20"
STD="c++17 c++20"
fi

# Test the CMake build
for cc in /usr/local/bin/gcc /opt/llvm/clang/bin/clang; do
for cmake_off_on in OFF ON; do
time docker run \
--volume="${GTEST_ROOT}:/src:ro" \
--tmpfs="/build:exec" \
--workdir="/build" \
--rm \
--env="CC=${cc}" \
--env=CXXFLAGS="-Werror -Wdeprecated" \
${LINUX_LATEST_CONTAINER} \
/bin/bash -c "
cmake /src \
-DCMAKE_CXX_STANDARD=14 \
-Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \
-Dcxx_no_exception=${cmake_off_on} \
-Dcxx_no_rtti=${cmake_off_on} && \
make -j$(nproc) && \
ctest -j$(nproc) --output-on-failure"
done
# Test CMake + GCC
for cmake_off_on in OFF ON; do
time docker run \
--volume="${GTEST_ROOT}:/src:ro" \
--tmpfs="/build:exec" \
--workdir="/build" \
--rm \
--env="CC=/usr/local/bin/gcc" \
--env=CXXFLAGS="-Werror -Wdeprecated" \
${LINUX_LATEST_CONTAINER} \
/bin/bash -c "
cmake /src \
-DCMAKE_CXX_STANDARD=17 \
-Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \
-Dcxx_no_exception=${cmake_off_on} \
-Dcxx_no_rtti=${cmake_off_on} && \
make -j$(nproc) && \
ctest -j$(nproc) --output-on-failure"
done

# Test CMake + Clang
for cmake_off_on in OFF ON; do
time docker run \
--volume="${GTEST_ROOT}:/src:ro" \
--tmpfs="/build:exec" \
--workdir="/build" \
--rm \
--env="CC=/opt/llvm/clang/bin/clang" \
--env=CXXFLAGS="-Werror -Wdeprecated --gcc-toolchain=/usr/local" \
${LINUX_LATEST_CONTAINER} \
/bin/bash -c "
cmake /src \
-DCMAKE_CXX_STANDARD=17 \
-Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \
-Dcxx_no_exception=${cmake_off_on} \
-Dcxx_no_rtti=${cmake_off_on} && \
make -j$(nproc) && \
ctest -j$(nproc) --output-on-failure"
done

# Do one test with an older version of GCC
# TODO(googletest-team): This currently uses Bazel 5. When upgrading to a
# version of Bazel that supports Bzlmod, add --enable_bzlmod=false to keep test
# coverage for the old WORKSPACE dependency management.
time docker run \
--volume="${GTEST_ROOT}:/src:ro" \
--workdir="/src" \
--rm \
--env="CC=/usr/local/bin/gcc" \
--env="BAZEL_CXXOPTS=-std=c++14" \
--env="BAZEL_CXXOPTS=-std=c++17" \
${LINUX_GCC_FLOOR_CONTAINER} \
/usr/local/bin/bazel test ... \
--copt="-Wall" \
--copt="-Werror" \
--copt="-Wuninitialized" \
--copt="-Wundef" \
--copt="-Wno-error=pragmas" \
--enable_bzlmod=false \
--features=external_include_paths \
--keep_going \
--show_timestamps \
Expand Down
11 changes: 7 additions & 4 deletions ci/macos-presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

set -euox pipefail

# Use Xcode 16.0
sudo xcode-select -s /Applications/Xcode_16.0.app/Contents/Developer

if [[ -z ${GTEST_ROOT:-} ]]; then
GTEST_ROOT="$(realpath $(dirname ${0})/..)"
fi
Expand All @@ -40,20 +43,20 @@ for cmake_off_on in OFF ON; do
BUILD_DIR=$(mktemp -d build_dir.XXXXXXXX)
cd ${BUILD_DIR}
time cmake ${GTEST_ROOT} \
-DCMAKE_CXX_STANDARD=14 \
-DCMAKE_CXX_STANDARD=17 \
-Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \
-Dcxx_no_exception=${cmake_off_on} \
-Dcxx_no_rtti=${cmake_off_on}
time make
time make -j$(nproc)
time ctest -j$(nproc) --output-on-failure
done

# Test the Bazel build

# If we are running on Kokoro, check for a versioned Bazel binary.
KOKORO_GFILE_BAZEL_BIN="bazel-7.0.0-darwin-x86_64"
KOKORO_GFILE_BAZEL_BIN="bazel-8.0.0-darwin-x86_64"
if [[ ${KOKORO_GFILE_DIR:-} ]] && [[ -f ${KOKORO_GFILE_DIR}/${KOKORO_GFILE_BAZEL_BIN} ]]; then
BAZEL_BIN="${KOKORO_GFILE_DIR}/${KOKORO_GFILE_BAZEL_BIN}"
chmod +x ${BAZEL_BIN}
Expand All @@ -67,7 +70,7 @@ for absl in 0 1; do
--copt="-Wall" \
--copt="-Werror" \
--copt="-Wundef" \
--cxxopt="-std=c++14" \
--cxxopt="-std=c++17" \
--define="absl=${absl}" \
--enable_bzlmod=true \
--features=external_include_paths \
Expand Down
38 changes: 25 additions & 13 deletions ci/windows-presubmit.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SETLOCAL ENABLEDELAYEDEXPANSION

SET BAZEL_EXE=%KOKORO_GFILE_DIR%\bazel-7.0.0-windows-x86_64.exe
SET BAZEL_EXE=%KOKORO_GFILE_DIR%\bazel-8.0.0-windows-x86_64.exe

SET PATH=C:\Python34;%PATH%
SET BAZEL_PYTHON=C:\python34\python.exe
Expand All @@ -11,21 +11,18 @@ SET CTEST_OUTPUT_ON_FAILURE=1
SET CMAKE_BUILD_PARALLEL_LEVEL=16
SET CTEST_PARALLEL_LEVEL=16

IF EXIST git\googletest (
CD git\googletest
) ELSE IF EXIST github\googletest (
CD github\googletest
)

SET GTEST_ROOT=%~dp0\..
IF %errorlevel% neq 0 EXIT /B 1

:: ----------------------------------------------------------------------------
:: CMake
MKDIR cmake_msvc2022
CD cmake_msvc2022
SET CMAKE_BUILD_PATH=cmake_msvc2022
MKDIR %CMAKE_BUILD_PATH%
CD %CMAKE_BUILD_PATH%

%CMAKE_BIN% .. ^
%CMAKE_BIN% %GTEST_ROOT% ^
-G "Visual Studio 17 2022" ^
-DCMAKE_CXX_STANDARD=17 ^
-DPYTHON_EXECUTABLE:FILEPATH=c:\python37\python.exe ^
-DPYTHON_INCLUDE_DIR:PATH=c:\python37\include ^
-DPYTHON_LIBRARY:FILEPATH=c:\python37\lib\site-packages\pip ^
Expand All @@ -40,8 +37,8 @@ IF %errorlevel% neq 0 EXIT /B 1
%CTEST_BIN% -C Debug --timeout 600
IF %errorlevel% neq 0 EXIT /B 1

CD ..
RMDIR /S /Q cmake_msvc2022
CD %GTEST_ROOT%
RMDIR /S /Q %CMAKE_BUILD_PATH%

:: ----------------------------------------------------------------------------
:: Bazel
Expand All @@ -50,11 +47,26 @@ RMDIR /S /Q cmake_msvc2022
:: because of Windows limitations on path length.
:: --output_user_root=C:\tmp causes Bazel to use a shorter path.
SET BAZEL_VS=C:\Program Files\Microsoft Visual Studio\2022\Community

:: C++17
%BAZEL_EXE% ^
--output_user_root=C:\tmp ^
test ... ^
--compilation_mode=dbg ^
--copt=/std:c++17 ^
--copt=/WX ^
--enable_bzlmod=true ^
--keep_going ^
--test_output=errors ^
--test_tag_filters=-no_test_msvc2017
IF %errorlevel% neq 0 EXIT /B 1

:: C++20
%BAZEL_EXE% ^
--output_user_root=C:\tmp ^
test ... ^
--compilation_mode=dbg ^
--copt=/std:c++14 ^
--copt=/std:c++20 ^
--copt=/WX ^
--enable_bzlmod=true ^
--keep_going ^
Expand Down
6 changes: 3 additions & 3 deletions googlemock/test/gmock-actions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ TEST(TypeTraits, IsInvocableRV) {
// In C++17 and above, where it's guaranteed that functions can return
// non-moveable objects, everything should work fine for non-moveable rsult
// types too.
#if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
// TODO(b/396121064) - Fix this test under MSVC
#ifndef _MSC_VER
{
struct NonMoveable {
NonMoveable() = default;
Expand All @@ -244,7 +244,7 @@ TEST(TypeTraits, IsInvocableRV) {
static_assert(!internal::is_callable_r<int, Callable>::value);
static_assert(!internal::is_callable_r<NonMoveable, Callable, int>::value);
}
#endif // C++17 and above
#endif // _MSC_VER

// Nothing should choke when we try to call other arguments besides directly
// callable objects, but they should not show up as callable.
Expand Down
7 changes: 3 additions & 4 deletions googlemock/test/gmock-spec-builders_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,8 @@ TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
"to be called at least once");
}

#if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L

// TODO(b/396121064) - Fix this test under MSVC
#ifndef _MSC_VER
// It should be possible to return a non-moveable type from a mock action in
// C++17 and above, where it's guaranteed that such a type can be initialized
// from a prvalue returned from a function.
Expand Down Expand Up @@ -847,7 +846,7 @@ TEST(ExpectCallTest, NonMoveableType) {
EXPECT_EQ(17, mock.AsStdFunction()().x);
}

#endif // C++17 and above
#endif // _MSC_VER

// Tests that the n-th action is taken for the n-th matching
// invocation.
Expand Down
2 changes: 1 addition & 1 deletion googletest/cmake/internal_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function(cxx_library_with_type name type cxx_flags)
target_link_libraries(${name} PUBLIC Threads::Threads)
endif()

target_compile_features(${name} PUBLIC cxx_std_14)
target_compile_features(${name} PUBLIC cxx_std_17)
endfunction()

########################################################################
Expand Down
4 changes: 2 additions & 2 deletions googletest/include/gtest/internal/gtest-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@
#endif

#if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \
GTEST_INTERNAL_CPLUSPLUS_LANG < 201402L
#error C++ versions less than C++14 are not supported.
GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
#error C++ versions less than C++17 are not supported.
#endif

// MSVC >= 19.11 (VS 2017 Update 3) supports __has_include.
Expand Down
8 changes: 7 additions & 1 deletion googletest/test/googletest-printers-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class UnprintableTemplateInGlobal {
// A user-defined streamable type in the global namespace.
class StreamableInGlobal {
public:
StreamableInGlobal() = default;
StreamableInGlobal(const StreamableInGlobal&) = default;
StreamableInGlobal& operator=(const StreamableInGlobal&) = default;
virtual ~StreamableInGlobal() = default;
};

Expand Down Expand Up @@ -572,14 +575,17 @@ TEST(PrintU8StringTest, Null) {
}

// Tests that u8 strings are escaped properly.
// TODO(b/396121064) - Fix this test under MSVC
#ifndef _MSC_VER
TEST(PrintU8StringTest, EscapesProperly) {
const char8_t* p = u8"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
EXPECT_EQ(PrintPointer(p) +
" pointing to u8\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
"hello \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
Print(p));
}
#endif
#endif // _MSC_VER
#endif // __cpp_lib_char8_t

// const char16_t*.
TEST(PrintU16StringTest, Const) {
Expand Down
4 changes: 2 additions & 2 deletions googletest/test/googletest-setuptestsuite-test_.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@

class SetupFailTest : public ::testing::Test {
protected:
static void SetUpTestSuite() { ASSERT_EQ("", "SET_UP_FAIL"); }
static void SetUpTestSuite() { ASSERT_STREQ("", "SET_UP_FAIL"); }
};

TEST_F(SetupFailTest, NoopPassingTest) {}

class TearDownFailTest : public ::testing::Test {
protected:
static void TearDownTestSuite() { ASSERT_EQ("", "TEAR_DOWN_FAIL"); }
static void TearDownTestSuite() { ASSERT_STREQ("", "TEAR_DOWN_FAIL"); }
};

TEST_F(TearDownFailTest, NoopPassingTest) {}

0 comments on commit c00fd25

Please sign in to comment.