Add GTEST_BUILD_USING_STD_MODULE guards around #include of standard library headers and add import std;

This commit is contained in:
David Hunter 2025-09-27 16:23:11 -06:00
parent 50b8600c63
commit 18835aba47
17 changed files with 235 additions and 109 deletions

View File

@ -26,6 +26,13 @@ option(
"Build gtest with internal symbols hidden in shared libraries." "Build gtest with internal symbols hidden in shared libraries."
OFF) OFF)
# set(GTEST_BUILD_USING_STD_MODULE ON)
# set(CMAKE_CXX_STANDARD 23)
# set(CMAKE_CXX_EXTENSIONS OFF)
# set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD ON)
# set(CMAKE_CXX_SCAN_FOR_MODULES ON)
# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build(). # Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
include(cmake/hermetic_build.cmake OPTIONAL) include(cmake/hermetic_build.cmake OPTIONAL)
@ -120,7 +127,44 @@ include_directories(${gtest_build_include_dirs})
# Google Test libraries. We build them using more strict warnings than what # Google Test libraries. We build them using more strict warnings than what
# are used for other targets, to ensure that gtest can be compiled by a user # are used for other targets, to ensure that gtest can be compiled by a user
# aggressive about warnings. # aggressive about warnings.
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_library(gtest "${cxx_strict}"
src/gtest-all.cc
# src/gtest.cc
# src/gtest-port.cc
# src/gtest-assertion-result.cc
# src/gtest-death-test.cc
# src/gtest-filepath.cc
# src/gtest-internal-inl.h
# src/gtest-matchers.cc
# src/gtest-port.cc
# src/gtest-printers.cc
# src/gtest-test-part.cc
# src/gtest-typed-test.cc
include/gtest/gtest.h
include/gtest/gtest_pred_impl.h
include/gtest/gtest_prod.h
include/gtest/gtest-assertion-result.h
include/gtest/gtest-death-test.h
include/gtest/gtest-matchers.h
include/gtest/gtest-message.h
include/gtest/gtest-param-test.h
include/gtest/gtest-printers.h
include/gtest/gtest-spi.h
include/gtest/gtest-test-part.h
include/gtest/gtest-typed-test.h
include/gtest/internal/gtest-death-test-internal.h
include/gtest/internal/gtest-filepath.h
include/gtest/internal/gtest-internal.h
include/gtest/internal/gtest-param-util.h
include/gtest/internal/gtest-port-arch.h
include/gtest/internal/gtest-port.h
include/gtest/internal/gtest-string.h
include/gtest/internal/gtest-type-util.h
include/gtest/internal/custom/gtest.h
include/gtest/internal/custom/gtest-port.h
include/gtest/internal/custom/gtest-printers.h
)
set_target_properties(gtest PROPERTIES VERSION ${GOOGLETEST_VERSION}) set_target_properties(gtest PROPERTIES VERSION ${GOOGLETEST_VERSION})
if(GTEST_HAS_ABSL) if(GTEST_HAS_ABSL)
target_compile_definitions(gtest PUBLIC GTEST_HAS_ABSL=1) target_compile_definitions(gtest PUBLIC GTEST_HAS_ABSL=1)
@ -135,6 +179,11 @@ if(GTEST_HAS_ABSL)
re2::re2 re2::re2
) )
endif() endif()
if(GTEST_BUILD_USING_STD_MODULE)
message(STATUS GTEST_BUILD_USING_STD_MODULE=${GTEST_BUILD_USING_STD_MODULE})
target_compile_definitions(gtest PRIVATE GTEST_BUILD_USING_STD_MODULE)
endif()
cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc) cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
set_target_properties(gtest_main PROPERTIES VERSION ${GOOGLETEST_VERSION}) set_target_properties(gtest_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
string(REPLACE ";" "$<SEMICOLON>" dirs "${gtest_build_include_dirs}") string(REPLACE ";" "$<SEMICOLON>" dirs "${gtest_build_include_dirs}")

View File

@ -38,10 +38,14 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_
#include <memory> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <ostream> import std;
#include <string> #else
#include <type_traits> #include <memory>
#include <ostream>
#include <string>
#include <type_traits>
#endif
#include "gtest/gtest-message.h" #include "gtest/gtest-message.h"
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"

View File

@ -39,12 +39,16 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
#include <atomic> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <functional> import std;
#include <memory> #else
#include <ostream> #include <atomic>
#include <string> #include <functional>
#include <type_traits> #include <memory>
#include <ostream>
#include <string>
#include <type_traits>
#endif
#include "gtest/gtest-printers.h" #include "gtest/gtest-printers.h"
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"

View File

@ -48,11 +48,15 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
#include <limits> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <memory> import std;
#include <ostream> #else
#include <sstream> #include <limits>
#include <string> #include <memory>
#include <ostream>
#include <sstream>
#include <string>
#endif
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"

View File

@ -174,9 +174,13 @@ TEST_P(DerivedTest, DoesBlah) {
#endif // 0 #endif // 0
#include <functional> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <iterator> import std;
#include <utility> #else
#include <functional>
#include <iterator>
#include <utility>
#endif
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-param-util.h" // IWYU pragma: export #include "gtest/internal/gtest-param-util.h" // IWYU pragma: export

View File

@ -104,20 +104,24 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
#include <any> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <functional> import std;
#include <memory> #else
#include <optional> #include <any>
#include <ostream> // NOLINT #include <functional>
#include <sstream> #include <memory>
#include <string> #include <optional>
#include <string_view> #include <ostream> // NOLINT
#include <tuple> #include <sstream>
#include <type_traits> #include <string>
#include <typeinfo> #include <string_view>
#include <utility> #include <tuple>
#include <variant> #include <type_traits>
#include <vector> #include <typeinfo>
#include <utility>
#include <variant>
#include <vector>
#endif
#ifdef GTEST_HAS_ABSL #ifdef GTEST_HAS_ABSL
#include "absl/strings/has_absl_stringify.h" #include "absl/strings/has_absl_stringify.h"
@ -127,11 +131,15 @@
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_INTERNAL_HAS_STD_SPAN #if GTEST_INTERNAL_HAS_STD_SPAN
#include <span> // NOLINT #if !defined(GTEST_BUILD_USING_STD_MODULE)
#include <span> // NOLINT
#endif
#endif // GTEST_INTERNAL_HAS_STD_SPAN #endif // GTEST_INTERNAL_HAS_STD_SPAN
#if GTEST_INTERNAL_HAS_COMPARE_LIB #if GTEST_INTERNAL_HAS_COMPARE_LIB
#include <compare> // NOLINT #if !defined(GTEST_BUILD_USING_STD_MODULE)
#include <compare> // NOLINT
#endif
#endif // GTEST_INTERNAL_HAS_COMPARE_LIB #endif // GTEST_INTERNAL_HAS_COMPARE_LIB
namespace testing { namespace testing {

View File

@ -33,7 +33,11 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
#include <string> #if defined(GTEST_BUILD_USING_STD_MODULE)
import std;
#else
#include <string>
#endif
#include "gtest/gtest.h" #include "gtest/gtest.h"

View File

@ -34,10 +34,14 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
#include <iosfwd> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <ostream> import std;
#include <string> #else
#include <vector> #include <iosfwd>
#include <ostream>
#include <string>
#include <vector>
#endif
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-string.h" #include "gtest/internal/gtest-string.h"

View File

@ -49,16 +49,20 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_H_
#include <cstddef> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <cstdint> import std;
#include <limits> #else
#include <memory> #include <cstddef>
#include <ostream> #include <cstdint>
#include <set> #include <limits>
#include <sstream> #include <memory>
#include <string> #include <ostream>
#include <type_traits> #include <set>
#include <vector> #include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#endif
#include "gtest/gtest-assertion-result.h" // IWYU pragma: export #include "gtest/gtest-assertion-result.h" // IWYU pragma: export
#include "gtest/gtest-death-test.h" // IWYU pragma: export #include "gtest/gtest-death-test.h" // IWYU pragma: export

View File

@ -41,8 +41,12 @@
#include <stdio.h> #include <stdio.h>
#include <memory> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <string> import std;
#else
#include <memory>
#include <string>
#endif
#include "gtest/gtest-matchers.h" #include "gtest/gtest-matchers.h"
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"

View File

@ -42,8 +42,12 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
#include <string> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <utility> import std;
#else
#include <string>
#include <utility>
#endif
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#include "gtest/internal/gtest-string.h" #include "gtest/internal/gtest-string.h"

View File

@ -48,23 +48,28 @@
#include <unistd.h> #include <unistd.h>
#endif // GTEST_OS_LINUX #endif // GTEST_OS_LINUX
#if GTEST_HAS_EXCEPTIONS
#include <stdexcept>
#endif
#include <ctype.h> #include <ctype.h>
#include <float.h> #include <float.h>
#include <string.h> #include <string.h>
#include <cstdint> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <functional> import std;
#include <limits> #else
#include <map> #if GTEST_HAS_EXCEPTIONS
#include <set> #include <stdexcept>
#include <string> #endif
#include <type_traits>
#include <utility> #include <cstdint>
#include <vector> #include <functional>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#endif
#include "gtest/gtest-message.h" #include "gtest/gtest-message.h"
#include "gtest/internal/gtest-filepath.h" #include "gtest/internal/gtest-filepath.h"

View File

@ -36,21 +36,25 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
#include <ctype.h> #if defined(GTEST_BUILD_USING_STD_MODULE)
import std;
#include <cassert> #else
#include <functional> #include <ctype.h>
#include <iterator>
#include <map> #include <cassert>
#include <memory> #include <functional>
#include <ostream> #include <iterator>
#include <set> #include <map>
#include <string> #include <memory>
#include <tuple> #include <ostream>
#include <type_traits> #include <set>
#include <unordered_map> #include <string>
#include <utility> #include <tuple>
#include <vector> #include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#endif
#include "gtest/gtest-printers.h" #include "gtest/gtest-printers.h"
#include "gtest/gtest-test-part.h" #include "gtest/gtest-test-part.h"

View File

@ -247,6 +247,10 @@
// Int32FromGTestEnv() - parses an int32_t environment variable. // Int32FromGTestEnv() - parses an int32_t environment variable.
// StringFromGTestEnv() - parses a string environment variable. // StringFromGTestEnv() - parses a string environment variable.
#if defined(GTEST_BUILD_USING_STD_MODULE)
import std;
#endif
// The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can // The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can
// potentially be used as an #include guard. // potentially be used as an #include guard.
#if defined(_MSVC_LANG) #if defined(_MSVC_LANG)
@ -285,20 +289,22 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <cerrno> #if !defined(GTEST_BUILD_USING_STD_MODULE)
// #include <condition_variable> // Guarded by GTEST_IS_THREADSAFE below #include <cerrno>
#include <cstdint> // #include <condition_variable> // Guarded by GTEST_IS_THREADSAFE below
#include <iostream> #include <cstdint>
#include <limits> #include <iostream>
#include <locale> #include <limits>
#include <memory> #include <locale>
#include <ostream> #include <memory>
#include <string> #include <ostream>
// #include <mutex> // Guarded by GTEST_IS_THREADSAFE below #include <string>
#include <tuple> // #include <mutex> // Guarded by GTEST_IS_THREADSAFE below
#include <type_traits> #include <tuple>
#include <vector> #include <type_traits>
#include <vector>
#endif
#ifndef _WIN32_WCE #ifndef _WIN32_WCE
#include <sys/stat.h> #include <sys/stat.h>
@ -575,9 +581,11 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// It's this header's responsibility to #include <typeinfo> when RTTI // It's this header's responsibility to #include <typeinfo> when RTTI
// is enabled. // is enabled.
#if !defined(GTEST_BUILD_USING_STD_MODULE)
#if GTEST_HAS_RTTI #if GTEST_HAS_RTTI
#include <typeinfo> #include <typeinfo>
#endif #endif
#endif
// Determines whether Google Test can use the pthreads library. // Determines whether Google Test can use the pthreads library.
#ifndef GTEST_HAS_PTHREAD #ifndef GTEST_HAS_PTHREAD
@ -810,11 +818,13 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif // GTEST_IS_THREADSAFE #endif // GTEST_IS_THREADSAFE
#if !defined(GTEST_BUILD_USING_STD_MODULE)
#ifdef GTEST_IS_THREADSAFE #ifdef GTEST_IS_THREADSAFE
// Some platforms don't support including these threading related headers. // Some platforms don't support including these threading related headers.
#include <condition_variable> // NOLINT #include <condition_variable> // NOLINT
#include <mutex> // NOLINT #include <mutex> // NOLINT
#endif // GTEST_IS_THREADSAFE #endif // GTEST_IS_THREADSAFE
#endif
// GTEST_API_ qualifies all symbols that must be exported. The definitions below // GTEST_API_ qualifies all symbols that must be exported. The definitions below
// are guarded by #ifndef to give embedders a chance to define GTEST_API_ in // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in
@ -2355,7 +2365,9 @@ using StringView = ::absl::string_view;
// Otherwise for C++17 and higher use std::string_view for Matcher<> // Otherwise for C++17 and higher use std::string_view for Matcher<>
// specializations. // specializations.
#define GTEST_INTERNAL_HAS_STRING_VIEW 1 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
#if !defined(GTEST_BUILD_USING_STD_MODULE)
#include <string_view> #include <string_view>
#endif
namespace testing { namespace testing {
namespace internal { namespace internal {
using StringView = ::std::string_view; using StringView = ::std::string_view;

View File

@ -50,9 +50,13 @@
#include <string.h> #include <string.h>
#include <cstdint> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <sstream> import std;
#include <string> #else
#include <cstdint>
#include <sstream>
#include <string>
#endif
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"

View File

@ -37,9 +37,13 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
#include <string> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <type_traits> import std;
#include <typeinfo> #else
#include <string>
#include <type_traits>
#include <typeinfo>
#endif
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"

View File

@ -41,13 +41,17 @@
#include <stdlib.h> // For strtoll/_strtoul64/malloc/free. #include <stdlib.h> // For strtoll/_strtoul64/malloc/free.
#include <string.h> // For memmove. #include <string.h> // For memmove.
#include <algorithm> #if defined(GTEST_BUILD_USING_STD_MODULE)
#include <cstdint> import std;
#include <memory> #else
#include <set> #include <algorithm>
#include <string> #include <cstdint>
#include <unordered_map> #include <memory>
#include <vector> #include <set>
#include <string>
#include <unordered_map>
#include <vector>
#endif
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"