Compare commits

...

4 Commits

Author SHA1 Message Date
Alexandre Bailon
8add6b4315 Create Zephyr external module
This update googletest to use it as Zephyr OS external module.
This allows downloading, configure and building googltest using
Zephyr west build system, without any user manipulation.

Signed-off-by: Alexandre Bailon <abailon@baylibre.com>
2026-07-29 05:41:53 +00:00
Alexandre Bailon
a7c5061b07 Add support of Zephyr OS
Although Zephyr has it own test suite, it doesn't work well with C++.
gtest and gmock seem more adapted.
This adds support of Zephyr OS in order to test C++ libraries and
applications.

Signed-off-by: Alexandre Bailon <abailon@baylibre.com>
2026-07-29 05:41:53 +00:00
Copybara-Service
3ff51c3e80 Merge pull request #5041 from alvinjaison:fix/get-test-suite-shuffle-index
PiperOrigin-RevId: 955317170
Change-Id: I40e01cdafe58768ccdb743dcf38717e48edcde41
2026-07-28 09:58:12 -07:00
Alvin Jaison
1269db9643 Fix GetTestSuite(i) ignoring shuffle order UnitTestImpl::GetTestSuite(int i) computes the shuffled index from test_suite_indices_ but then accesses test_suites_ using the raw parameter 'i' instead of the computed 'index'. This causes the public API UnitTest::GetTestSuite(i) to return test suites in original registration order rather than the active shuffle order. The mutable variant GetMutableSuiteCase(i) correctly uses 'index', and the equivalent function TestSuite::GetTestInfo(i) also uses 'index'. This was a copy-paste typo introduced in commit bd851333 (Sep 2009) when the shuffle feature was first added. The bug is observable from within TestEventListener callbacks during test execution when --gtest_shuffle is active. 2026-07-24 16:11:55 +01:00
9 changed files with 73 additions and 3 deletions

View File

@ -84,6 +84,9 @@
#define GTEST_OS_GNU_HURD 1
#elif defined(__GLIBC__) && defined(__FreeBSD_kernel__)
#define GTEST_OS_GNU_KFREEBSD 1
#elif defined(__ZEPHYR__)
// Define it before linux as it could be built as a linux application
#define GTEST_OS_ZEPHYR 1
#elif defined __linux__
#define GTEST_OS_LINUX 1
#if defined __ANDROID__

View File

@ -141,6 +141,7 @@
// GTEST_OS_WINDOWS_PHONE - Windows Phone
// GTEST_OS_WINDOWS_RT - Windows Store App/WinRT
// GTEST_OS_ZOS - z/OS
// GTEST_OS_ZEPHYR - Zephyr OS
//
// Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the
// most stable support. Since core members of the Google Test project
@ -516,6 +517,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif
#endif // GTEST_HAS_STD_WSTRING
#ifdef GTEST_OS_ZEPHYR
#define GTEST_HAS_FILE_SYSTEM 0
#endif
#ifndef GTEST_HAS_FILE_SYSTEM
// Most platforms support a file system.
#define GTEST_HAS_FILE_SYSTEM 1
@ -2063,6 +2068,18 @@ inline int RmDir(const char* dir) { return rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
#endif
#elif defined(GTEST_OS_ZEPHYR)
static inline int FileNo(FILE* file) {
if (file == stdin)
return 1;
else if (file == stdout)
return 2;
else if (file == stderr)
return 3;
return -EINVAL;
}
static inline int isatty(int fd) { return true; }
#else
typedef struct stat StatStruct;

View File

@ -587,7 +587,7 @@ class GTEST_API_ UnitTestImpl {
// total_test_suite_count() - 1. If i is not in that range, returns NULL.
const TestSuite* GetTestSuite(int i) const {
const int index = GetElementOr(test_suite_indices_, i, -1);
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)];
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
}
// Legacy API is deprecated but still available

View File

@ -43,7 +43,9 @@
#include <algorithm>
#include <chrono> // NOLINT
#include <cmath>
#ifndef GTEST_OS_ZEPHYR
#include <csignal> // NOLINT: raise(3) is used on some platforms
#endif
#include <cstdint>
#include <cstdlib>
#include <cstring>

View File

@ -47,8 +47,13 @@ void loop() { RUN_ALL_TESTS(); }
}
#endif
#elif defined(GTEST_OS_QURT)
// QuRT: program entry point is main, but argc/argv are unusable.
#elif defined(GTEST_OS_QURT) || defined(GTEST_OS_ZEPHYR)
// Program entry point is main, but argc/argv are unusable.
#if defined(GTEST_OS_ZEPHYR)
#undef GTEST_API_
#define GTEST_API_
#endif
GTEST_API_ int main() {
printf("Running main() from %s\n", __FILE__);

View File

@ -0,0 +1,17 @@
zephyr_library()
# Disable use of pthreads in GoogleTest
set(gtest_disable_pthreads ON CACHE BOOL "Build GoogleTest without Pthread")
zephyr_include_directories_ifdef(CONFIG_GTEST ${ZEPHYR_GOOGLETEST_MODULE_DIR}/googletest/)
zephyr_include_directories_ifdef(CONFIG_GTEST ${ZEPHYR_GOOGLETEST_MODULE_DIR}/googletest/include/)
zephyr_library_sources_ifdef(CONFIG_GTEST
${ZEPHYR_GOOGLETEST_MODULE_DIR}/googletest/src/gtest-all.cc
)
target_sources_ifdef(CONFIG_GTEST app PRIVATE
${ZEPHYR_GOOGLETEST_MODULE_DIR}/googletest/src/gtest_main.cc
)
zephyr_include_directories_ifdef(CONFIG_GMOCK ${ZEPHYR_GOOGLETEST_MODULE_DIR}/googlemock/)
zephyr_include_directories_ifdef(CONFIG_GMOCK ${ZEPHYR_GOOGLETEST_MODULE_DIR}/googlemock/include/)
zephyr_library_sources_ifdef(CONFIG_GMOCK ${ZEPHYR_GOOGLETEST_MODULE_DIR}/googlemock/src/gmock-all.cc)

View File

@ -0,0 +1,18 @@
config GTEST
bool "GoogleTest test framework"
select TEST
select CPP
select REQUIRES_FULL_LIBCPP
select FPU
config GMOCK
bool "GoogleMock mocking framework"
select GTEST
choice STD_CPP
default STD_CPP14 if GTEST
endchoice
choice LIBC_IMPLEMENTATION
default NEWLIB_LIBC if GTEST
endchoice

2
modules/modules.cmake Normal file
View File

@ -0,0 +1,2 @@
set(ZEPHYR_GOOGLETEST_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR}/googletest)
set(ZEPHYR_GOOGLETEST_KCONFIG ${CMAKE_CURRENT_LIST_DIR}/googletest/Kconfig)

6
zephyr/module.yml Normal file
View File

@ -0,0 +1,6 @@
name: googletest
build:
cmake-ext: true
kconfig-ext: true
settings:
module_ext_root: .