Commit fae9923f authored by Yuval Peress's avatar Yuval Peress Committed by Anas Nashif
Browse files

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: default avatarYuval Peress <peress@google.com>
parent 59f3316d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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")
+5 −0
Original line number Diff line number Diff line
# 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)
+6 −4
Original line number Diff line number Diff line
@@ -84,11 +84,13 @@ if(NOT "${Deprecated_FIND_COMPONENTS}" STREQUAL "")
endif()

if("SOURCES" IN_LIST Deprecated_FIND_COMPONENTS)
  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 <source-file.c>)")
  endif()
endif()

set(Deprecated_FOUND True)
set(DEPRECATED_FOUND True)
+1 −1
Original line number Diff line number Diff line
@@ -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()
+10 −12
Original line number Diff line number Diff line
@@ -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_PROPERTY:compiler,coverage>)

  target_link_libraries(testbinary PRIVATE
    -lgcov
    )
  target_link_libraries(testbinary PRIVATE $<TARGET_PROPERTY:linker,coverage>)
endif()

if(LIBS)
Loading