From 8b35c75d236a517a3dbfbc7493b18be54e7c595c Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Tue, 15 Sep 2020 19:41:12 +0000 Subject: [PATCH] Updates for cmake modules (#1267) adding licence for cmake modules --- CMakeLists.txt | 2 +- cmake-modules/AddTestCMocka.cmake | 98 ++++++++++++------- cmake-modules/CheckAndIncludeCodeCov.cmake | 9 ++ cmake-modules/CodeCoverage.cmake | 11 ++- cmake-modules/CreateCodeCoverageTargets.cmake | 11 ++- cmake-modules/create_map_file.cmake | 3 + cmake-modules/gcc-arm-toolchain.cmake | 6 ++ 7 files changed, 99 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9394138676..1d338c3473 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ project(az LANGUAGES C) enable_testing () include(eng/cmake/global_compile_options.txt) -include(cmake-modules/create_map_file.cmake) +include(create_map_file) # Include function for creating code coverage targets include(CreateCodeCoverageTargets) diff --git a/cmake-modules/AddTestCMocka.cmake b/cmake-modules/AddTestCMocka.cmake index 6b9d2b77fc..125ff1287e 100644 --- a/cmake-modules/AddTestCMocka.cmake +++ b/cmake-modules/AddTestCMocka.cmake @@ -1,39 +1,73 @@ +# +# Copyright (c) 2007 Daniel Gollub +# Copyright (c) 2007-2018 Andreas Schneider +# Copyright (c) 2018 Anderson Toshiyuki Sasaki +# +# Redistribution and use is allowed according to the terms of the BSD license. +# +# Modifed version from https://github.com/xbmc/libssh/blob/667fb5f9a9c96f210583dbfb11755c43250c5e55/cmake/Modules/AddCMockaTest.cmake +#.rst: +# AddTestCMocka +# ------------- +# +# This file provides a function to add a test +# +# Functions provided +# ------------------ +# +# :: +# +# add_cmocka_test(target_name +# SOURCES src1 src2 ... srcN +# [COMPILE_OPTIONS opt1 opt2 ... optN] +# [LINK_OPTIONS lopt1 lop2 .. loptN] +# [PRIVATE_ACCESS ON/OFF] +# [LINK_TARGETS target1 target2 .. targetN] +# ) +# +# ``target_name``: +# Required, expects the name of the test which will be used to define a target +# +# ``SOURCES``: +# Required, expects one or more source files names +# +# ``COMPILE_OPTIONS``: +# Optional, expects one or more options to be passed to the compiler +# +# ``LINK_OPTIONS``: +# Optional, expects one or more options to be passed to the linker +# +# ``PRIVATE_ACCESS``: +# Optional, when ON, tests are granted access to az_core private layer +# +# ``LINK_TARGETS``: +# Optional, expects one or more targets from the same project to be passed to cmake target linker +# +# +# Example: +# +# .. code-block:: cmake +# +# add_cmocka_test(my_test +# SOURCES my_test.c other_source.c +# COMPILE_OPTIONS -g -Wall +# LINK_OPTIONS -Wl,--enable-syscall-fixup +# PRIVATE_ACCESS ON +# LINK_TARGETS target1, target2 +# ) +# +# Where ``my_test`` is the name of the test, ``my_test.c`` and +# ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler +# options to be used, ``-Wl,--enable-syscall-fixup`` is an option passed to the linker, +# ``PRIVATE_ACCESS`` is ON to let tests access private layer from az_core and ``LINK_TAGETS``` +# list all the cmake tagets to link to +# + find_package(cmocka CONFIG REQUIRED) enable_testing() include(CTest) -if (CMAKE_CROSSCOMPILING) - if (WIN32) - find_program(WINE_EXECUTABLE - NAMES wine) - set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "") - endif() -endif() - -function(ADD_CMOCKA_TEST_ENVIRONMENT _TARGET_NAME) - if (WIN32 OR CYGWIN OR MINGW) - file(TO_NATIVE_PATH "${cmocka-library_BINARY_DIR}" CMOCKA_DLL_PATH) - - if (TARGET_SYSTEM_EMULATOR) - set(DLL_PATH_ENV "WINEPATH=${CMOCKA_DLL_PATH};$ENV{WINEPATH}") - else() - set(DLL_PATH_ENV "PATH=${CMOCKA_DLL_PATH};$ENV{PATH}") - endif() - # - # IMPORTANT NOTE: The set_tests_properties(), below, internally - # stores its name/value pairs with a semicolon delimiter. - # because of this we must protect the semicolons in the path - # - string(REPLACE ";" "\\;" DLL_PATH_ENV "${DLL_PATH_ENV}") - - set_tests_properties(${_TARGET_NAME} - PROPERTIES - ENVIRONMENT - "${DLL_PATH_ENV}") - endif() -endfunction() - set(MATH_LIB_UNIX "") if (UNIX) set(MATH_LIB_UNIX "m") @@ -122,6 +156,4 @@ function(ADD_CMOCKA_TEST _TARGET_NAME) ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME} ) - add_cmocka_test_environment(${_TARGET_NAME}) - endfunction (ADD_CMOCKA_TEST) diff --git a/cmake-modules/CheckAndIncludeCodeCov.cmake b/cmake-modules/CheckAndIncludeCodeCov.cmake index e82241d672..1755f0c0ca 100644 --- a/cmake-modules/CheckAndIncludeCodeCov.cmake +++ b/cmake-modules/CheckAndIncludeCodeCov.cmake @@ -1,3 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT +# +# Checks if code coverage can be generated for a target. This is true only when: +# 1.- env var AZ_SDK_CODE_COV is set +# 2.- cmake project is set to use gcc +# 3.- cmake project is set to build type Debug +# + if(DEFINED ENV{AZ_SDK_CODE_COV} AND CMAKE_C_COMPILER_ID MATCHES "GNU") if(CMAKE_BUILD_TYPE STREQUAL "Debug") include(CodeCoverage) diff --git a/cmake-modules/CodeCoverage.cmake b/cmake-modules/CodeCoverage.cmake index d1e41aecc5..d26a5fcdbb 100644 --- a/cmake-modules/CodeCoverage.cmake +++ b/cmake-modules/CodeCoverage.cmake @@ -70,7 +70,8 @@ # # 1. Copy this file into your cmake modules path. # -# 2. Add the following line to your CMakeLists.txt: +# 2. Add the following line to your CMakeLists.txt (best inside an if-condition +# using a CMake option() to enable it just optionally): # include(CodeCoverage) # # 3. Append necessary compiler flags: @@ -227,14 +228,14 @@ function(setup_target_for_coverage_lcov) COMMAND ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS} # Capturing lcov counters and generating report - COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --rc lcov_branch_coverage=1 --gcov-tool ${GCOV_PATH} --directory . -b ${BASEDIR} --capture --output-file ${Coverage_NAME}.capture + COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --directory . -b ${BASEDIR} --capture --output-file ${Coverage_NAME}.capture # add baseline counters - COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --rc lcov_branch_coverage=1 --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.capture --output-file ${Coverage_NAME}.total + COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.capture --output-file ${Coverage_NAME}.total # filter collected data to final coverage report - COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --rc lcov_branch_coverage=1 --gcov-tool ${GCOV_PATH} --remove ${Coverage_NAME}.total ${LCOV_EXCLUDES} --output-file ${Coverage_NAME}.info + COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --remove ${Coverage_NAME}.total ${LCOV_EXCLUDES} --output-file ${Coverage_NAME}.info # Generate HTML output - COMMAND ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS} --rc lcov_branch_coverage=1 -o ${Coverage_NAME} ${Coverage_NAME}.info + COMMAND ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS} -o ${Coverage_NAME} ${Coverage_NAME}.info # Set output files as GENERATED (will be removed on 'make clean') BYPRODUCTS diff --git a/cmake-modules/CreateCodeCoverageTargets.cmake b/cmake-modules/CreateCodeCoverageTargets.cmake index cc19b1f460..10b370d5f7 100644 --- a/cmake-modules/CreateCodeCoverageTargets.cmake +++ b/cmake-modules/CreateCodeCoverageTargets.cmake @@ -1,3 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT +# +# Cmake module used by any project's target for creating a code coverage target for it. +# + # codeCoverage macro(create_code_coverage_targets code_cov_target) if(DEFINED ENV{AZ_SDK_CODE_COV} AND CMAKE_C_COMPILER_ID MATCHES "GNU") @@ -7,7 +13,8 @@ macro(create_code_coverage_targets code_cov_target) # Basic coverage using lcov (gcc integrated) setup_target_for_coverage_lcov(NAME ${code_cov_target}_cov EXECUTABLE ${code_cov_target}_test - EXCLUDE ${COV_EXCLUDE}) + EXCLUDE ${COV_EXCLUDE} + LCOV_ARGS --rc lcov_branch_coverage=1) # HTML and XML - Coverage using gcovr (Needs to be installed into system) setup_target_for_coverage_gcovr_html(NAME ${code_cov_target}_cov_html EXECUTABLE ${code_cov_target}_test) @@ -17,4 +24,4 @@ macro(create_code_coverage_targets code_cov_target) file(APPEND ${CMAKE_BINARY_DIR}/coverage_targets.txt " ${code_cov_target}_cov_xml") endif() endif() -endmacro() \ No newline at end of file +endmacro() diff --git a/cmake-modules/create_map_file.cmake b/cmake-modules/create_map_file.cmake index 75354d3c66..b20093cc7d 100644 --- a/cmake-modules/create_map_file.cmake +++ b/cmake-modules/create_map_file.cmake @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT +# # Instructs linker to generate map files and optimize build for minimal size # Requires CMake version >= 3.13 to use add_link_options diff --git a/cmake-modules/gcc-arm-toolchain.cmake b/cmake-modules/gcc-arm-toolchain.cmake index eaa2a419b6..320e873ad4 100644 --- a/cmake-modules/gcc-arm-toolchain.cmake +++ b/cmake-modules/gcc-arm-toolchain.cmake @@ -1,3 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT +# +# Sets the cmake project as gcc-arm. +# + set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_PROCESSOR arm) set(CMAKE_C_COMPILER arm-none-eabi-gcc)