Skip to content

Commit 9a3edb6

Browse files
Update CMakeList to optionally build UT/Coverity; Update coverity configuration (#276)
Update Coverity configuration to meet the latest coverity standard. Updated CMakelist to only build Coverity if required instead of building the CMock based unit tests as well. <!--- Title --> Description ----------- <!--- Describe your changes in detail. --> Test Steps ----------- <!-- Describe the steps to reproduce. --> Checklist: ---------- <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] I have tested my changes. No regression in existing tests. - [x] I have modified and/or added unit-tests to cover the code changes in this Pull Request. Related Issue ----------- <!-- If any, please provide issue ID. --> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: Soren Ptak <[email protected]>
1 parent 7b68936 commit 9a3edb6

File tree

3 files changed

+83
-73
lines changed

3 files changed

+83
-73
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
-G "Unix Makefiles" \
3232
-DCMAKE_BUILD_TYPE=Debug \
3333
-DBUILD_CLONE_SUBMODULES=ON \
34+
-DUNITTEST=1 \
3435
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
3536
make -C build/ all
3637
echo "::endgroup::"

test/CMakeLists.txt

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
cmake_minimum_required ( VERSION 3.13.0 )
2-
project ( "CoreMQTT unit test"
3-
VERSION 1.0.0
1+
cmake_minimum_required ( VERSION 3.22.0 )
2+
project ( "CoreMQTT tests"
3+
VERSION 2.1.0
44
LANGUAGES C )
55

66
# Allow the project to be organized into folders.
@@ -14,6 +14,12 @@ if( NOT DEFINED CMAKE_C_STANDARD_REQUIRED )
1414
set( CMAKE_C_STANDARD_REQUIRED ON )
1515
endif()
1616

17+
# If no configuration is defined, turn everything on.
18+
if( NOT DEFINED COV_ANALYSIS AND NOT DEFINED UNITTEST )
19+
set( COV_ANALYSIS TRUE )
20+
set( UNITTEST TRUE )
21+
endif()
22+
1723
# Do not allow in-source build.
1824
if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
1925
message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
@@ -35,61 +41,67 @@ set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
3541

3642
# ===================================== Coverity Analysis Configuration =================================================
3743

38-
# Include filepaths for source and include.
39-
include( ${MODULE_ROOT_DIR}/mqttFilePaths.cmake )
40-
41-
# Target for Coverity analysis that builds the library.
42-
add_library( coverity_analysis
43-
${MQTT_SOURCES}
44-
${MQTT_SERIALIZER_SOURCES} )
44+
if( COV_ANALYSIS )
45+
# Include filepaths for source and include.
46+
include( ${MODULE_ROOT_DIR}/mqttFilePaths.cmake )
4547

46-
# Build MQTT library target without custom config dependency.
47-
target_compile_definitions( coverity_analysis PUBLIC MQTT_DO_NOT_USE_CUSTOM_CONFIG=1 )
48-
target_compile_definitions( coverity_analysis PUBLIC NDEBUG=1 )
48+
# Target for Coverity analysis that builds the library.
49+
add_library( coverity_analysis
50+
${MQTT_SOURCES}
51+
${MQTT_SERIALIZER_SOURCES} )
4952

50-
# MQTT public include path.
51-
target_include_directories( coverity_analysis PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} )
52-
53-
# ==================================== Test Configuration ========================================
53+
# Build MQTT library target without custom config dependency.
54+
target_compile_definitions( coverity_analysis PUBLIC MQTT_DO_NOT_USE_CUSTOM_CONFIG=1 )
5455

55-
# Define a CMock resource path.
56-
set( CMOCK_DIR ${MODULE_ROOT_DIR}/test/unit-test/CMock CACHE INTERNAL "CMock library source directory." )
56+
# MQTT public include path.
57+
target_include_directories( coverity_analysis PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} )
5758

58-
# Include CMock build configuration.
59-
include( unit-test/cmock_build.cmake )
59+
# Remove inclusion of assert.
60+
add_compile_definitions( NDEBUG=1 )
61+
endif()
6062

61-
# Check if the CMock source directory exists, and if not present, clone the submodule
62-
# if BUILD_CLONE_SUBMODULES configuration is enabled.
63-
if( NOT EXISTS ${CMOCK_DIR}/src )
64-
# Attempt to clone CMock.
65-
if( ${BUILD_CLONE_SUBMODULES} )
66-
clone_cmock()
67-
else()
68-
message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
63+
# ==================================== Test Configuration ========================================
64+
if( UNITTEST )
65+
# Define a CMock resource path.
66+
set( CMOCK_DIR ${MODULE_ROOT_DIR}/test/unit-test/CMock CACHE INTERNAL "CMock library source directory." )
67+
68+
# Include CMock build configuration.
69+
include( unit-test/cmock_build.cmake )
70+
71+
# Check if the CMock source directory exists, and if not present, clone the submodule
72+
# if BUILD_CLONE_SUBMODULES configuration is enabled.
73+
if( NOT EXISTS ${CMOCK_DIR}/src )
74+
# Attempt to clone CMock.
75+
if( ${BUILD_CLONE_SUBMODULES} )
76+
clone_cmock()
77+
else()
78+
message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set\
79+
BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
80+
endif()
6981
endif()
70-
endif()
7182

72-
# Add unit test and coverage configuration.
83+
# Add unit test and coverage configuration.
7384

74-
# Use CTest utility for managing test runs. This has to be added BEFORE
75-
# defining test targets with add_test()
76-
enable_testing()
85+
# Use CTest utility for managing test runs. This has to be added BEFORE
86+
# defining test targets with add_test()
87+
enable_testing()
7788

78-
# Add build targets for CMock and Unit, required for unit testing.
79-
add_cmock_targets()
89+
# Add build targets for CMock and Unit, required for unit testing.
90+
add_cmock_targets()
8091

81-
# Add function to enable CMock based tests and coverage.
82-
include( ${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake )
92+
# Add function to enable CMock based tests and coverage.
93+
include( ${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake )
8394

84-
# Include build configuration for unit tests.
85-
add_subdirectory( unit-test )
95+
# Include build configuration for unit tests.
96+
add_subdirectory( unit-test )
8697

87-
# ==================================== Coverage Analysis configuration ========================================
98+
# ==================================== Coverage Analysis configuration ========================================
8899

89-
# Add a target for running coverage on tests.
90-
add_custom_target( coverage
91-
COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${CMOCK_DIR}
92-
-P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
93-
DEPENDS cmock unity core_mqtt_utest core_mqtt_serializer_utest core_mqtt_state_utest
94-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
95-
)
100+
# Add a target for running coverage on tests.
101+
add_custom_target( coverage
102+
COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${CMOCK_DIR}
103+
-P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
104+
DEPENDS cmock unity core_mqtt_utest core_mqtt_serializer_utest core_mqtt_state_utest
105+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
106+
)
107+
endif()

tools/coverity/misra.config

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
// MISRA C-2012 Rules
2-
31
{
4-
version : "2.0",
5-
standard : "c2012",
6-
title: "Coverity MISRA Configuration",
7-
deviations : [
8-
// Disable the following rules.
9-
{
10-
deviation: "Directive 4.8",
11-
reason: "Allow inclusion of unused types. Header files for a specific port, which are needed by all files, may define types that are not used by a specific file."
12-
},
2+
"version" : "2.0",
3+
"standard" : "c2012",
4+
"title": "Coverity MISRA Configuration",
5+
"deviations" : [
136
{
14-
deviation: "Directive 4.9",
15-
reason: "Allow inclusion of function like macros. Logging is done using function like macros."
7+
"deviation": "Directive 4.8",
8+
"reason": "Allow inclusion of unused types. Header files for a specific port, which are needed by all files, may define types that are not used by a specific file."
169
},
1710
{
18-
deviation: "Rule 2.3",
19-
reason: "Allow unused types. Library headers may define types intended for the application's use, but not used within the library files."
11+
"deviation": "Directive 4.9",
12+
"reason": "Allow inclusion of function like macros. Logging is done using function like macros."
2013
},
2114
{
22-
deviation: "Rule 2.4",
23-
reason: "Allow unused tags. Some compilers warn if types are not tagged."
15+
"deviation": "Rule 2.3",
16+
"reason": "Allow unused types. Library headers may define types intended for the application's use, but not used within the library files."
2417
},
2518
{
26-
deviation: "Rule 2.5",
27-
reason: "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file."
19+
"deviation": "Rule 2.4",
20+
"reason": "Allow unused tags. Some compilers warn if types are not tagged."
2821
},
2922
{
30-
deviation: "Rule 3.1",
31-
reason: "Allow nested comments. Documentation blocks contain comments for example code."
23+
"deviation": "Rule 2.5",
24+
"reason": "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file."
3225
},
3326
{
34-
deviation: "Rule 8.7",
35-
reason: "API functions are not used by the library outside of the files they are defined; however, they must be externally visible in order to be used by an application."
27+
"deviation": "Rule 3.1",
28+
"reason": "Allow nested comments. Documentation blocks contain comments for example code."
3629
},
3730
{
38-
deviation: "Rule 11.5",
39-
reason: "Allow casts from `void *`. The payload buffers are stored as `void *` and are cast to various types for use in functions."
31+
"deviation": "Rule 8.7",
32+
"reason": "API functions are not used by the library outside of the files they are defined; however, they must be externally visible in order to be used by an application."
4033
},
34+
{
35+
"deviation": "Rule 11.5",
36+
"reason": "Allow casts from `void *`. The payload buffers are stored as `void *` and are cast to various types for use in functions."
37+
}
4138
]
4239
}
4340

0 commit comments

Comments
 (0)