From ab83f0365c71a9e84e4ec63cdbe419c6a309a55c Mon Sep 17 00:00:00 2001 From: Alexandre Bailon Date: Wed, 8 Jan 2025 11:19:53 +0100 Subject: [PATCH 1/2] 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 --- .../include/gtest/internal/gtest-port-arch.h | 3 +++ googletest/include/gtest/internal/gtest-port.h | 17 +++++++++++++++++ googletest/src/gtest.cc | 2 ++ googletest/src/gtest_main.cc | 9 +++++++-- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-port-arch.h b/googletest/include/gtest/internal/gtest-port-arch.h index 7ec968f3..a97b18f1 100644 --- a/googletest/include/gtest/internal/gtest-port-arch.h +++ b/googletest/include/gtest/internal/gtest-port-arch.h @@ -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__ diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index ca18513e..7e78ccff 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -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 @@ -525,6 +526,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 @@ -2060,6 +2065,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; diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 7ff82546..e45f8a5e 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -43,7 +43,9 @@ #include #include // NOLINT #include +#ifndef GTEST_OS_ZEPHYR #include // NOLINT: raise(3) is used on some platforms +#endif #include #include #include diff --git a/googletest/src/gtest_main.cc b/googletest/src/gtest_main.cc index 8141caf4..5f27a4bb 100644 --- a/googletest/src/gtest_main.cc +++ b/googletest/src/gtest_main.cc @@ -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__); From 8e3886acac2e6058f4f540269f43b1f4a2700adc Mon Sep 17 00:00:00 2001 From: Alexandre Bailon Date: Wed, 8 Jan 2025 11:27:27 +0100 Subject: [PATCH 2/2] 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 --- modules/googletest/CMakeLists.txt | 17 +++++++++++++++++ modules/googletest/Kconfig | 18 ++++++++++++++++++ modules/modules.cmake | 2 ++ zephyr/module.yml | 6 ++++++ 4 files changed, 43 insertions(+) create mode 100644 modules/googletest/CMakeLists.txt create mode 100644 modules/googletest/Kconfig create mode 100644 modules/modules.cmake create mode 100644 zephyr/module.yml diff --git a/modules/googletest/CMakeLists.txt b/modules/googletest/CMakeLists.txt new file mode 100644 index 00000000..c6028874 --- /dev/null +++ b/modules/googletest/CMakeLists.txt @@ -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) diff --git a/modules/googletest/Kconfig b/modules/googletest/Kconfig new file mode 100644 index 00000000..f2d30e27 --- /dev/null +++ b/modules/googletest/Kconfig @@ -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 diff --git a/modules/modules.cmake b/modules/modules.cmake new file mode 100644 index 00000000..0b79068d --- /dev/null +++ b/modules/modules.cmake @@ -0,0 +1,2 @@ +set(ZEPHYR_GOOGLETEST_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR}/googletest) +set(ZEPHYR_GOOGLETEST_KCONFIG ${CMAKE_CURRENT_LIST_DIR}/googletest/Kconfig) diff --git a/zephyr/module.yml b/zephyr/module.yml new file mode 100644 index 00000000..2415d80f --- /dev/null +++ b/zephyr/module.yml @@ -0,0 +1,6 @@ +name: googletest +build: + cmake-ext: true + kconfig-ext: true + settings: + module_ext_root: .