From fd27bed45ec291e5cab7eaee42fcb166d107d5b1 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Wed, 9 Aug 2023 16:42:58 +0200 Subject: [PATCH] cmake/toolchain: Support LLVM source profiling/coverage for native boards Add a a new source coverage for native builds and new kconfig choice of COVERAGE mode to select which: * COVERAGE_NATIVE_GCOV: what we had until now with native builds * COVERAGE_NATIVE_SOURCE: a new LLVM source coverage mode * COVERAGE_GCOV: the old COVERAGE_GCOV (embedded gcov data generation). Signed-off-by: Alberto Escolar Piedras --- cmake/compiler/clang/compiler_flags.cmake | 6 +++++- cmake/linker/ld/clang/linker_flags.cmake | 4 +++- subsys/testsuite/Kconfig | 25 +++++++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cmake/compiler/clang/compiler_flags.cmake b/cmake/compiler/clang/compiler_flags.cmake index 12868539d2f746..3658161123bed4 100644 --- a/cmake/compiler/clang/compiler_flags.cmake +++ b/cmake/compiler/clang/compiler_flags.cmake @@ -15,7 +15,11 @@ set_compiler_property(PROPERTY no_printf_return_value) check_set_compiler_property(PROPERTY hosted) # clang flags for coverage generation -set_property(TARGET compiler PROPERTY coverage --coverage -fno-inline) +if (CONFIG_COVERAGE_NATIVE_SOURCE) + set_compiler_property(PROPERTY coverage -fprofile-instr-generate -fcoverage-mapping) +else() + set_compiler_property(PROPERTY coverage --coverage -fno-inline) +endif() # clang flag for colourful diagnostic messages set_compiler_property(PROPERTY diagnostic -fcolor-diagnostics) diff --git a/cmake/linker/ld/clang/linker_flags.cmake b/cmake/linker/ld/clang/linker_flags.cmake index 9339e0c49a561f..cc53fab40c0aec 100644 --- a/cmake/linker/ld/clang/linker_flags.cmake +++ b/cmake/linker/ld/clang/linker_flags.cmake @@ -1,6 +1,8 @@ # The coverage linker flag is specific for clang. -if (NOT CONFIG_COVERAGE_GCOV) +if (CONFIG_COVERAGE_NATIVE_GCOV) set_property(TARGET linker PROPERTY coverage --coverage) +elseif(CONFIG_COVERAGE_NATIVE_SOURCE) + set_property(TARGET linker PROPERTY coverage -fprofile-instr-generate -fcoverage-mapping) endif() # Extra warnings options for twister run diff --git a/subsys/testsuite/Kconfig b/subsys/testsuite/Kconfig index cd528ec555a09f..e2e7fba764eb40 100644 --- a/subsys/testsuite/Kconfig +++ b/subsys/testsuite/Kconfig @@ -40,10 +40,29 @@ config COVERAGE For more information see https://docs.zephyrproject.org/latest/guides/coverage.html +choice + prompt "Coverage mode" + default COVERAGE_NATIVE_GCOV if NATIVE_BUILD + default COVERAGE_GCOV if !NATIVE_BUILD + depends on COVERAGE + +config COVERAGE_NATIVE_GCOV + bool "Host compiler gcov based code coverage" + depends on NATIVE_BUILD + help + Build natively with the compiler standard `--coverage` options, + that is with gcov/GCC-compatible coverage + +config COVERAGE_NATIVE_SOURCE + bool "Host compiler source based code coverage" + depends on NATIVE_BUILD + depends on "${ZEPHYR_TOOLCHAIN_VARIANT}" = "llvm" + help + Build natively with the compiler source based coverage options. + Today this is only supported with LLVM + config COVERAGE_GCOV bool "Create Coverage data from hardware platform" - default y - depends on COVERAGE depends on !NATIVE_BUILD help This option will select the custom gcov library. The reports will @@ -51,6 +70,8 @@ config COVERAGE_GCOV gen_gcov_files.py which creates the required .gcda files. These can be read by gcov utility. For more details see gcovr.com . +endchoice + config COVERAGE_GCOV_HEAP_SIZE int "Size of heap allocated for gcov coverage data dump" default 32768 if X86 || SOC_SERIES_MPS2