|
| 1 | +From 21c753f75546c5821503cc3a026a17cf5ac9c927 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Philippe Coval <philippe.coval@silabs.com> |
| 3 | +Date: Thu, 6 Feb 2025 10:31:19 +0100 |
| 4 | +Subject: [PATCH] tests: build: cmake: Use Upstream cmock |
| 5 | + |
| 6 | +Not this is not latest version but it aligned to the version used at zwa, |
| 7 | +may unify-core align to it and synchronize all projects accordingly, |
| 8 | +or reconsider the archtecture. |
| 9 | + |
| 10 | +cmake: Adjust cmock config |
| 11 | + |
| 12 | +Relate-to: https://github.com/SiliconLabsSoftware/z-wave-protocol-controller/issues/60 |
| 13 | +Bug-SiliconLabs: SWPROT-8953 |
| 14 | +Relate-to: https://github.com/SiliconLabsSoftware/z-wave-engine-application-layer/issues/6 |
| 15 | +Signed-off-by: Philippe Coval <philippe.coval@silabs.com> |
| 16 | +--- |
| 17 | + CMakeLists.txt | 3 + |
| 18 | + cmake/modules/FindCMock.cmake | 58 +++++++++++++++++++ |
| 19 | + .../libs/testframework/CMakeLists.txt | 32 ++++++++-- |
| 20 | + .../testframework/target_add_unittest.cmake | 26 ++++----- |
| 21 | + 4 files changed, 100 insertions(+), 19 deletions(-) |
| 22 | + create mode 100644 cmake/modules/FindCMock.cmake |
| 23 | + |
| 24 | +diff --git a/CMakeLists.txt b/CMakeLists.txt |
| 25 | +index 1f3b5dcdfa..95951d40cb 100644 |
| 26 | +--- a/CMakeLists.txt |
| 27 | ++++ b/CMakeLists.txt |
| 28 | +@@ -43,6 +43,9 @@ include(cmake/include/package-helper.cmake) |
| 29 | + include(cmake/include/uic_helper.cmake) |
| 30 | + |
| 31 | + if(BUILD_TESTING) |
| 32 | ++ include(cmake/modules/FindCMock.cmake) |
| 33 | ++ set(THS-CMOCK_LOCATION "${cmock_SOURCE_DIR}") |
| 34 | ++ set(THS-UNITY_LOCATION "${cmock_SOURCE_DIR}/vendor/unity") |
| 35 | + include(components/testframework/target_add_unittest.cmake) |
| 36 | + endif() |
| 37 | + |
| 38 | +diff --git a/cmake/modules/FindCMock.cmake b/cmake/modules/FindCMock.cmake |
| 39 | +new file mode 100644 |
| 40 | +index 0000000000..3a96950060 |
| 41 | +--- /dev/null |
| 42 | ++++ b/cmake/modules/FindCMock.cmake |
| 43 | +@@ -0,0 +1,58 @@ |
| 44 | ++# SPDX-FileCopyrightText: Silicon Laboratories Inc. <https://www.silabs.com/> |
| 45 | ++# SPDX-License-Identifier: Zlib |
| 46 | ++# |
| 47 | ++# This recipe allows to download CMock |
| 48 | ++# It can be used by projects which are depending on it |
| 49 | ++# Feel free to copy this (up to date) file everywhere it is needed |
| 50 | ++ |
| 51 | ++include(FetchContent) |
| 52 | ++ |
| 53 | ++if(NOT DEFINED CMOCK_GIT_REPOSITORY) |
| 54 | ++ if(DEFINED ENV{CMOCK_GIT_REPOSITORY}) |
| 55 | ++ set(CMOCK_GIT_REPOSITORY $ENV{CMOCK_GIT_REPOSITORY}) |
| 56 | ++ endif() |
| 57 | ++endif() |
| 58 | ++if("${CMOCK_GIT_REPOSITORY}" STREQUAL "") |
| 59 | ++ set(CMOCK_GIT_REPOSITORY "https://github.com/ThrowTheSwitch/CMock") |
| 60 | ++endif() |
| 61 | ++ |
| 62 | ++if(NOT DEFINED CMOCK_GIT_TAG) |
| 63 | ++ if(DEFINED ENV{CMOCK_GIT_TAG}) |
| 64 | ++ set(CMOCK_GIT_TAG $ENV{CMOCK_GIT_TAG}) |
| 65 | ++ else() |
| 66 | ++ set(CMOCK_GIT_TAG "v2.5.3") |
| 67 | ++ endif() |
| 68 | ++endif() |
| 69 | ++ |
| 70 | ++file(GLOB CMOCK_PATCHES |
| 71 | ++ LIST_DIRECTORIES false |
| 72 | ++ ${PROJECT_SOURCE_DIR}/patches/cmock/*.patch |
| 73 | ++) |
| 74 | ++ |
| 75 | ++find_package(Git) |
| 76 | ++FetchContent_Declare( |
| 77 | ++ CMock |
| 78 | ++ GIT_REPOSITORY ${CMOCK_GIT_REPOSITORY} |
| 79 | ++ GIT_TAG ${CMOCK_GIT_TAG} |
| 80 | ++ GIT_SUBMODULES_RECURSE True |
| 81 | ++ GIT_SHALLOW 1 |
| 82 | ++ |
| 83 | ++ # Prevent "fatal: unable to auto-detect email address" |
| 84 | ++ GIT_CONFIG user.email=nobody@${CMAKE_PROJECT_NAME}.localhost |
| 85 | ++ |
| 86 | ++ PATCH_COMMAND ${GIT_EXECUTABLE} |
| 87 | ++ -C <SOURCE_DIR> am --ignore-whitespace |
| 88 | ++ "${CMOCK_PATCHES}" |
| 89 | ++) |
| 90 | ++ |
| 91 | ++message(STATUS "${CMAKE_PROJECT_NAME}: Depends: ${CMOCK_GIT_REPOSITORY}#${CMOCK_GIT_TAG}") |
| 92 | ++string(REGEX MATCH ".*/?main/?.*" CMOCK_UNSTABLE_GIT_TAG "${CMOCK_GIT_TAG}") |
| 93 | ++if(CMOCK_GIT_TAG STREQUAL "" OR CMOCK_UNSTABLE_GIT_TAG) |
| 94 | ++ message(WARNING "${CMAKE_PROJECT_NAME}: Declare CMOCK_GIT_TAG to stable version not: ${CMOCK_UNSTABLE_GIT_TAG}") |
| 95 | ++endif() |
| 96 | ++ |
| 97 | ++set(FETCHCONTENT_QUIET FALSE) |
| 98 | ++FetchContent_MakeAvailable(CMock) |
| 99 | ++ |
| 100 | ++message(STATUS "CMock Sources: ${cmock_SOURCE_DIR}") |
| 101 | ++message(STATUS "CMock Binaries: ${cmock_BINARY_DIR}") |
| 102 | +diff --git a/components/testframework/libs/testframework/CMakeLists.txt b/components/testframework/libs/testframework/CMakeLists.txt |
| 103 | +index 66da6a46fe..afd5b65d7b 100644 |
| 104 | +--- a/components/testframework/libs/testframework/CMakeLists.txt |
| 105 | ++++ b/components/testframework/libs/testframework/CMakeLists.txt |
| 106 | +@@ -103,14 +103,38 @@ if (NOT COMMAND ADD_UNITY_TEST) |
| 107 | + endif() |
| 108 | + endfunction(ADD_UNITY_TEST) |
| 109 | + |
| 110 | ++set(DEFAULT_THS-CMOCK_PATH libs/cmock) |
| 111 | ++if(EXISTS ${THS-CMOCK_LOCATION}) |
| 112 | ++ set(THS-CMOCK_PATH ${THS-CMOCK_LOCATION}) |
| 113 | ++else() |
| 114 | ++ set(THS-CMOCK_PATH ${DEFAULT_THS-CMOCK_PATH}) |
| 115 | ++endif() |
| 116 | ++if(EXISTS ${THS-CMOCK_PATH}) |
| 117 | ++ message(STATUS "Found ths-cmock: ${THS-CMOCK_PATH}") |
| 118 | ++else() |
| 119 | ++ message(STATUS "Did not find ths-cmock at ${THS-CMOCK_PATH}") |
| 120 | ++endif() |
| 121 | ++ |
| 122 | ++set(DEFAULT_THS-UNITY_PATH libs/cmock/vendor/unity) |
| 123 | ++if(EXISTS ${THS-UNITY_LOCATION}) |
| 124 | ++ set(THS-UNITY_PATH ${THS-UNITY_LOCATION}) |
| 125 | ++else() |
| 126 | ++ set(THS-UNITY_PATH ${DEFAULT_THS-UNITY_PATH}) |
| 127 | ++endif() |
| 128 | ++if(EXISTS ${THS-UNITY_PATH}) |
| 129 | ++ message(STATUS "Found ths-unity: ${THS-UNITY_PATH}") |
| 130 | ++else() |
| 131 | ++ message(STATUS "Did not find ths-unity at ${THS-UNITY_PATH}") |
| 132 | ++endif() |
| 133 | ++ |
| 134 | + # compile the unity version bundled along with cmock sources. |
| 135 | +- add_library(unity ../cmock/vendor/unity/src/unity.c) |
| 136 | +- target_include_directories(unity PUBLIC ../cmock/vendor/unity/src) |
| 137 | ++ add_library(unity ${THS-UNITY_PATH}/src/unity.c) |
| 138 | ++ target_include_directories(unity PUBLIC ${THS-UNITY_PATH}/src) |
| 139 | + target_compile_options(unity PRIVATE "-fPIC") |
| 140 | + |
| 141 | + # Build the cmock library and link the above compiled unity with the cmock library |
| 142 | +- add_library(cmock2 STATIC ../cmock/src/cmock.c) |
| 143 | +- target_include_directories(cmock2 PUBLIC ../cmock/src) |
| 144 | ++ add_library(cmock2 STATIC ${THS-CMOCK_PATH}/src/cmock.c) |
| 145 | ++ target_include_directories(cmock2 PUBLIC ${THS-CMOCK_PATH}/src) |
| 146 | + target_link_libraries(cmock2 PUBLIC unity) |
| 147 | + target_compile_options(cmock2 PRIVATE "-fPIC") |
| 148 | + |
| 149 | +diff --git a/components/testframework/target_add_unittest.cmake b/components/testframework/target_add_unittest.cmake |
| 150 | +index 7129c27dc6..267393ea7d 100644 |
| 151 | +--- a/components/testframework/target_add_unittest.cmake |
| 152 | ++++ b/components/testframework/target_add_unittest.cmake |
| 153 | +@@ -24,24 +24,20 @@ function(generate_unity_runner test_runner test_file) |
| 154 | + endif() |
| 155 | + if(EXISTS ${THS-UNITY_LOCATION}) |
| 156 | + set(UNITY_DIR ${THS-UNITY_LOCATION}) |
| 157 | +- add_custom_command( |
| 158 | +- OUTPUT ${TEST_RUNNER} |
| 159 | +- DEPENDS ${TEST_FILE} |
| 160 | +- COMMAND |
| 161 | +- ${UNITY2_RUBY_EXECUTABLE} ${UNITY_DIR}/auto/generate_test_runner.rb |
| 162 | +- ${ZWAVE_UNITY_CONFIG} ${TEST_FILE} ${TEST_RUNNER} |
| 163 | +- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) |
| 164 | + else() |
| 165 | + set(UNITY_DIR "${DIR_OF_TARGET_ADD_UNIT_TEST}/libs/cmock/vendor/unity") |
| 166 | +- add_custom_command( |
| 167 | +- OUTPUT ${TEST_RUNNER} |
| 168 | +- DEPENDS ${TEST_FILE} |
| 169 | +- COMMAND |
| 170 | +- ${UNITY2_RUBY_EXECUTABLE} ${UNITY_DIR}/auto/generate_test_runner.rb |
| 171 | +- ${DIR_OF_TARGET_ADD_UNIT_TEST}/zwave_unity_config.yml ${TEST_FILE} |
| 172 | +- ${TEST_RUNNER} |
| 173 | +- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) |
| 174 | + endif() |
| 175 | ++ if(NOT DEFINED ${UNIFY_UNITY_RUNNER_CONFIG}) |
| 176 | ++ set(UNIFY_UNITY_RUNNER_CONFIG ${DIR_OF_TARGET_ADD_UNIT_TEST}/zwave_unity_config.yml) |
| 177 | ++ endif() |
| 178 | ++ add_custom_command( |
| 179 | ++ OUTPUT ${TEST_RUNNER} |
| 180 | ++ DEPENDS ${TEST_FILE} ${UNIFY_UNITY_RUNNER_CONFIG} |
| 181 | ++ COMMAND |
| 182 | ++ ${UNITY2_RUBY_EXECUTABLE} ${UNITY_DIR}/auto/generate_test_runner.rb |
| 183 | ++ ${UNIFY_UNITY_RUNNER_CONFIG} |
| 184 | ++ ${TEST_FILE} ${TEST_RUNNER} |
| 185 | ++ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) |
| 186 | + endfunction() |
| 187 | + |
| 188 | + # This function creates unity2 test executables. It uses the provided target to setup and import configuration |
| 189 | +-- |
| 190 | +2.39.5 |
| 191 | + |
0 commit comments