Skip to content

Commit

Permalink
Updates for cmake modules (#1267)
Browse files Browse the repository at this point in the history
adding licence for cmake modules
  • Loading branch information
vhvb1989 authored Sep 15, 2020
1 parent 05a9a77 commit 8b35c75
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
98 changes: 65 additions & 33 deletions cmake-modules/AddTestCMocka.cmake
Original file line number Diff line number Diff line change
@@ -1,39 +1,73 @@
#
# Copyright (c) 2007 Daniel Gollub <[email protected]>
# Copyright (c) 2007-2018 Andreas Schneider <[email protected]>
# Copyright (c) 2018 Anderson Toshiyuki Sasaki <[email protected]>
#
# 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")
Expand Down Expand Up @@ -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)
9 changes: 9 additions & 0 deletions cmake-modules/CheckAndIncludeCodeCov.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 6 additions & 5 deletions cmake-modules/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions cmake-modules/CreateCodeCoverageTargets.cmake
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
Expand All @@ -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()
endmacro()
3 changes: 3 additions & 0 deletions cmake-modules/create_map_file.cmake
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 6 additions & 0 deletions cmake-modules/gcc-arm-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 8b35c75

Please sign in to comment.