From fae9923ff2dedafc92dff6f79fff0c1ff50ff742 Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Sun, 6 Nov 2022 22:43:14 -0700 Subject: [PATCH] unittest: update coverage library non gcc toolchains When building with clang, the unittests were giving us an error: ``` error: undefined symbol: llvm_gcda_start_file ``` This seems to be from linking in `gcov` regardless of the toolchain. It appears that clang doesn't need any special library for coverage. With this change the following now produce identical coverage reports: ``` $ ZEPHYR_TOOLCHAIN_VARIANT=zephyr ./scripts/twister -p unit_testing \ --coverage -i -T tests/unit/intmath/ $ ZEPHYR_TOOLCHAIN_VARIANT=host ./scripts/twister -p unit_testing \ --coverage -i -T tests/unit/intmath/ $ ZEPHYR_TOOLCHAIN_VARIANT=llvm ./scripts/twister -p unit_testing \ --coverage -i --coverage-tool lcov \ --gcov-tool $(pwd)/scripts/utils/llvm-gcov.sh \ -T tests/unit/intmath/ ``` Signed-off-by: Yuval Peress --- cmake/compiler/clang/generic.cmake | 1 + cmake/linker/lld/linker_flags.cmake | 5 +++++ cmake/modules/FindDeprecated.cmake | 10 ++++++---- cmake/modules/FindHostTools.cmake | 2 +- cmake/modules/unittest.cmake | 22 ++++++++++------------ tests/unit/crc/CMakeLists.txt | 2 +- 6 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 cmake/linker/lld/linker_flags.cmake diff --git a/cmake/compiler/clang/generic.cmake b/cmake/compiler/clang/generic.cmake index 869a788e244..99402676ede 100644 --- a/cmake/compiler/clang/generic.cmake +++ b/cmake/compiler/clang/generic.cmake @@ -5,6 +5,7 @@ if(DEFINED TOOLCHAIN_HOME) endif() find_program(CMAKE_C_COMPILER clang ${find_program_clang_args}) +find_program(CMAKE_CXX_COMPILER clang++ ${find_program_clang_args}) find_program(CMAKE_LLVM_COV llvm-cov ${find_program_clang_args}) set(CMAKE_GCOV "${CMAKE_LLVM_COV} gcov") diff --git a/cmake/linker/lld/linker_flags.cmake b/cmake/linker/lld/linker_flags.cmake new file mode 100644 index 00000000000..43b96baeba5 --- /dev/null +++ b/cmake/linker/lld/linker_flags.cmake @@ -0,0 +1,5 @@ +# Copyright (c) 2022 Google LLC +# SPDX-License-Identifier: Apache-2.0 + +# Since lld is a drop in replacement for ld, we can just use ld's flags +include(${ZEPHYR_BASE}/cmake/linker/ld/${COMPILER}/linker_flags.cmake OPTIONAL) diff --git a/cmake/modules/FindDeprecated.cmake b/cmake/modules/FindDeprecated.cmake index d593846afc7..35d69875149 100644 --- a/cmake/modules/FindDeprecated.cmake +++ b/cmake/modules/FindDeprecated.cmake @@ -84,10 +84,12 @@ if(NOT "${Deprecated_FIND_COMPONENTS}" STREQUAL "") endif() if("SOURCES" IN_LIST Deprecated_FIND_COMPONENTS) - message(DEPRECATION - "Setting SOURCES prior to calling find_package() for unit tests is deprecated." - "To add sources after find_package() use:\n" - " target_sources(testbinary PRIVATE )") + if(SOURCES) + message(DEPRECATION + "Setting SOURCES prior to calling find_package() for unit tests is deprecated." + " To add sources after find_package() use:\n" + " target_sources(testbinary PRIVATE )") + endif() endif() set(Deprecated_FOUND True) diff --git a/cmake/modules/FindHostTools.cmake b/cmake/modules/FindHostTools.cmake index 87aba695823..c8f28f21285 100644 --- a/cmake/modules/FindHostTools.cmake +++ b/cmake/modules/FindHostTools.cmake @@ -66,7 +66,7 @@ find_program(BOSSAC bossac) find_program(IMGTOOL imgtool) # Pick host system's toolchain if we are targeting posix -if("${ARCH}" STREQUAL "posix") +if("${ARCH}" STREQUAL "posix" OR "${ARCH}" STREQUAL "unit_testing") if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "llvm") set(ZEPHYR_TOOLCHAIN_VARIANT "host") endif() diff --git a/cmake/modules/unittest.cmake b/cmake/modules/unittest.cmake index 7bb61b14ae1..5bc6765338d 100644 --- a/cmake/modules/unittest.cmake +++ b/cmake/modules/unittest.cmake @@ -2,14 +2,18 @@ cmake_minimum_required(VERSION 3.20.0) -enable_language(C CXX ASM) - include(root) include(boards) include(arch) include(configuration_files) include(kconfig) +find_package(TargetTools) + +enable_language(C CXX ASM) + +include(${ZEPHYR_BASE}/cmake/target_toolchain_flags.cmake) + # Parameters: # SOURCES: list of source files, default main.c # INCLUDE: list of additional include paths relative to ZEPHYR_BASE @@ -33,12 +37,13 @@ if((NOT DEFINED ZEPHYR_BASE) AND (DEFINED ENV_ZEPHYR_BASE)) set(ZEPHYR_BASE ${ENV_ZEPHYR_BASE} CACHE PATH "Zephyr base") endif() +find_package(Deprecated COMPONENTS SOURCES) + if(NOT SOURCES AND EXISTS main.c) set(SOURCES main.c) endif() add_executable(testbinary ${SOURCES}) -find_package(Deprecated COMPONENTS SOURCES) add_library(test_interface INTERFACE) target_link_libraries(testbinary PRIVATE test_interface) @@ -89,16 +94,9 @@ target_link_libraries(testbinary PRIVATE ) if(COVERAGE) - target_compile_options(test_interface INTERFACE - -fno-default-inline - -fno-inline - -fprofile-arcs - -ftest-coverage - ) + target_compile_options(test_interface INTERFACE $) - target_link_libraries(testbinary PRIVATE - -lgcov - ) + target_link_libraries(testbinary PRIVATE $) endif() if(LIBS) diff --git a/tests/unit/crc/CMakeLists.txt b/tests/unit/crc/CMakeLists.txt index cbbe675da6c..48726fcf170 100644 --- a/tests/unit/crc/CMakeLists.txt +++ b/tests/unit/crc/CMakeLists.txt @@ -2,6 +2,6 @@ cmake_minimum_required(VERSION 3.20.0) -project(crc) find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(crc) target_sources(testbinary PRIVATE main.c)