mercredi 22 juillet 2020

Unit testing c++ code into Zephyr with googletest

We are starting to work with Zephyr and have decided to use C++. I am now trying to figure out how to unittest the code. As I understand ztest and unity which is already integrated into zephyr/nrfconnect doesn't support c++. I have decided to use googletest. I have created a custom module to download, compile and integrate into zephyr. I have now some troubles.

The structure of my project look like this:

|-modules
|   |-googletest
|         |-zephyr
|            |-module.yml
|         |-CMakeLists.txt
|         |-CMakeLists.txt.in
|         |-Kconfig
|-src
|-tests
   |-button
   |    |-main.cpp
   |-CMakeLists.txt
   |-prj.conf
   |-testcase.yaml   

CMakeList of tests/button look like this:

cmake_minimum_required(VERSION 3.13.1)

set(BOARD native_posix)
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr)
set(TOOLCHAIN_ROOT ${ZEPHYR_BASE})
set(ZEPHYR_EXTRA_MODULES $ENV{ZEPHYR_BASE}/../iot_aleph/modules/googletest)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project("Button unit tests")

# Include event headers
zephyr_library_include_directories(../../src/board)

# Add test sources
target_sources(app PRIVATE src/main.cpp)
target_sources(app PRIVATE ../../src/board/button.cpp)

target_link_libraries(app PUBLIC gtest_main) 

CMakeLists.txt of module/googletest look like:

cmake_minimum_required(VERSION 3.1)

if(CONFIG_GOOGLETEST)

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

endif()

CMakeLists.txt.in

cmake_minimum_required(VERSION 3.1)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           release-1.10.0
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

The GoogleTest framework seems to compile correctly but when it come to linking, I hae a lot of errors:

[92/93] Linking CXX executable zephyr/zephyr.elf
FAILED: zephyr/zephyr.elf zephyr/zephyr.lst zephyr/zephyr.stat zephyr/zephyr.exe 
: && ccache /usr/bin/g++     zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj  -o zephyr/zephyr.elf  -Wl,-T  zephyr/linker.cmd  -Wl,-Map=/home/cyril/Documents/zephyr_workpsace/iot_aleph/tests/button/build/zephyr/zephyr_prebuilt.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/arch/posix/core/libarch__posix__core.a  zephyr/soc/posix/inf_clock/libsoc__posix__inf_clock.a  zephyr/boards/posix/native_posix/libboards__posix__native_posix.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  zephyr/CMakeFiles/offsets.dir/arch/posix/core/offsets/offsets.c.obj  -L/home/cyril/Documents/zephyr_workpsace/iot_aleph/tests/button/build/zephyr  lib/libgtest_main.a  lib/libgtest.a  -lpthread  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -lstdc++  -m32  -ldl  -pthread  -lm && cd /home/cyril/Documents/zephyr_workpsace/iot_aleph/tests/button/build/zephyr && cmake -E rename zephyr_prebuilt.map zephyr.map && /usr/bin/objdump -S zephyr.elf > zephyr.lst && /usr/bin/readelf -e zephyr.elf > zephyr.stat && /usr/bin/cmake -E copy zephyr.elf zephyr.exe
/usr/bin/ld: i386:x86-64 architecture of input file `lib/libgtest.a(gtest-all.cc.obj)' is incompatible with i386 output
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)':
...
gtest-all.cc:(.text+0x25bb): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::AssertionResult::AssertionResult(testing::AssertionResult const&)':
gtest-all.cc:(.text+0x2876): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::internal::(anonymous namespace)::SplitEscapedString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
gtest-all.cc:(.text+0x41d6): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x41f4): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x4250): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x427c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::substr(unsigned long, unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x42bc): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x42f0): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::substr(unsigned long, unsigned long) const'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::internal::StringStreamToString(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
gtest-all.cc:(.text+0x6c0a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::Test::Test()':
gtest-all.cc:(.text+0x7b75): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::TestInfo::TestInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, char const*, testing::internal::CodeLocation, void const*, testing::internal::TestFactoryBase*)':
gtest-all.cc:(.text+0x86b7): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: gtest-all.cc:(.text+0x8726): undefined reference to `operator new(unsigned long)'
...

Can someone help me?

Cyril

Aucun commentaire:

Enregistrer un commentaire