Skip to content

Commit f838d67

Browse files
committed
tests: build: cmake: Use Upstream cmock
Not this is not latest version but it aligned to the version used at zwa, may unify-core align to it and synchronize all projects accordingly, or reconsider the archtecture. cmake: Adjust cmock config Relate-to: SiliconLabsSoftware/z-wave-protocol-controller#60 Bug-SiliconLabs: SWPROT-8953 Relate-to: SiliconLabsSoftware/z-wave-engine-application-layer#6 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent ce3b93c commit f838d67

File tree

4 files changed

+100
-19
lines changed

4 files changed

+100
-19
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ include(cmake/include/package-helper.cmake)
4343
include(cmake/include/uic_helper.cmake)
4444

4545
if(BUILD_TESTING)
46+
include(cmake/modules/FindCMock.cmake)
47+
set(THS-CMOCK_LOCATION "${cmock_SOURCE_DIR}")
48+
set(THS-UNITY_LOCATION "${cmock_SOURCE_DIR}/vendor/unity")
4649
include(components/testframework/target_add_unittest.cmake)
4750
endif()
4851

cmake/modules/FindCMock.cmake

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SPDX-FileCopyrightText: Silicon Laboratories Inc. <https://www.silabs.com/>
2+
# SPDX-License-Identifier: Zlib
3+
#
4+
# This recipe allows to download CMock
5+
# It can be used by projects which are depending on it
6+
# Feel free to copy this (up to date) file everywhere it is needed
7+
8+
include(FetchContent)
9+
10+
if(NOT DEFINED CMOCK_GIT_REPOSITORY)
11+
if(DEFINED ENV{CMOCK_GIT_REPOSITORY})
12+
set(CMOCK_GIT_REPOSITORY $ENV{CMOCK_GIT_REPOSITORY})
13+
endif()
14+
endif()
15+
if("${CMOCK_GIT_REPOSITORY}" STREQUAL "")
16+
set(CMOCK_GIT_REPOSITORY "https://github.com/ThrowTheSwitch/CMock")
17+
endif()
18+
19+
if(NOT DEFINED CMOCK_GIT_TAG)
20+
if(DEFINED ENV{CMOCK_GIT_TAG})
21+
set(CMOCK_GIT_TAG $ENV{CMOCK_GIT_TAG})
22+
else()
23+
set(CMOCK_GIT_TAG "v2.5.3")
24+
endif()
25+
endif()
26+
27+
file(GLOB CMOCK_PATCHES
28+
LIST_DIRECTORIES false
29+
${PROJECT_SOURCE_DIR}/patches/cmock/*.patch
30+
)
31+
32+
find_package(Git)
33+
FetchContent_Declare(
34+
CMock
35+
GIT_REPOSITORY ${CMOCK_GIT_REPOSITORY}
36+
GIT_TAG ${CMOCK_GIT_TAG}
37+
GIT_SUBMODULES_RECURSE True
38+
GIT_SHALLOW 1
39+
40+
# Prevent "fatal: unable to auto-detect email address"
41+
GIT_CONFIG user.email=nobody@${CMAKE_PROJECT_NAME}.localhost
42+
43+
PATCH_COMMAND ${GIT_EXECUTABLE}
44+
-C <SOURCE_DIR> am --ignore-whitespace
45+
"${CMOCK_PATCHES}"
46+
)
47+
48+
message(STATUS "${CMAKE_PROJECT_NAME}: Depends: ${CMOCK_GIT_REPOSITORY}#${CMOCK_GIT_TAG}")
49+
string(REGEX MATCH ".*/?main/?.*" CMOCK_UNSTABLE_GIT_TAG "${CMOCK_GIT_TAG}")
50+
if(CMOCK_GIT_TAG STREQUAL "" OR CMOCK_UNSTABLE_GIT_TAG)
51+
message(WARNING "${CMAKE_PROJECT_NAME}: Declare CMOCK_GIT_TAG to stable version not: ${CMOCK_UNSTABLE_GIT_TAG}")
52+
endif()
53+
54+
set(FETCHCONTENT_QUIET FALSE)
55+
FetchContent_MakeAvailable(CMock)
56+
57+
message(STATUS "CMock Sources: ${cmock_SOURCE_DIR}")
58+
message(STATUS "CMock Binaries: ${cmock_BINARY_DIR}")

components/testframework/libs/testframework/CMakeLists.txt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,38 @@ if (NOT COMMAND ADD_UNITY_TEST)
103103
endif()
104104
endfunction(ADD_UNITY_TEST)
105105

106+
set(DEFAULT_THS-CMOCK_PATH libs/cmock)
107+
if(EXISTS ${THS-CMOCK_LOCATION})
108+
set(THS-CMOCK_PATH ${THS-CMOCK_LOCATION})
109+
else()
110+
set(THS-CMOCK_PATH ${DEFAULT_THS-CMOCK_PATH})
111+
endif()
112+
if(EXISTS ${THS-CMOCK_PATH})
113+
message(STATUS "Found ths-cmock: ${THS-CMOCK_PATH}")
114+
else()
115+
message(STATUS "Did not find ths-cmock at ${THS-CMOCK_PATH}")
116+
endif()
117+
118+
set(DEFAULT_THS-UNITY_PATH libs/cmock/vendor/unity)
119+
if(EXISTS ${THS-UNITY_LOCATION})
120+
set(THS-UNITY_PATH ${THS-UNITY_LOCATION})
121+
else()
122+
set(THS-UNITY_PATH ${DEFAULT_THS-UNITY_PATH})
123+
endif()
124+
if(EXISTS ${THS-UNITY_PATH})
125+
message(STATUS "Found ths-unity: ${THS-UNITY_PATH}")
126+
else()
127+
message(STATUS "Did not find ths-unity at ${THS-UNITY_PATH}")
128+
endif()
129+
106130
# compile the unity version bundled along with cmock sources.
107-
add_library(unity ../cmock/vendor/unity/src/unity.c)
108-
target_include_directories(unity PUBLIC ../cmock/vendor/unity/src)
131+
add_library(unity ${THS-UNITY_PATH}/src/unity.c)
132+
target_include_directories(unity PUBLIC ${THS-UNITY_PATH}/src)
109133
target_compile_options(unity PRIVATE "-fPIC")
110134

111135
# Build the cmock library and link the above compiled unity with the cmock library
112-
add_library(cmock2 STATIC ../cmock/src/cmock.c)
113-
target_include_directories(cmock2 PUBLIC ../cmock/src)
136+
add_library(cmock2 STATIC ${THS-CMOCK_PATH}/src/cmock.c)
137+
target_include_directories(cmock2 PUBLIC ${THS-CMOCK_PATH}/src)
114138
target_link_libraries(cmock2 PUBLIC unity)
115139
target_compile_options(cmock2 PRIVATE "-fPIC")
116140

components/testframework/target_add_unittest.cmake

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,20 @@ function(generate_unity_runner test_runner test_file)
2424
endif()
2525
if(EXISTS ${THS-UNITY_LOCATION})
2626
set(UNITY_DIR ${THS-UNITY_LOCATION})
27-
add_custom_command(
28-
OUTPUT ${TEST_RUNNER}
29-
DEPENDS ${TEST_FILE}
30-
COMMAND
31-
${UNITY2_RUBY_EXECUTABLE} ${UNITY_DIR}/auto/generate_test_runner.rb
32-
${ZWAVE_UNITY_CONFIG} ${TEST_FILE} ${TEST_RUNNER}
33-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
3427
else()
3528
set(UNITY_DIR "${DIR_OF_TARGET_ADD_UNIT_TEST}/libs/cmock/vendor/unity")
36-
add_custom_command(
37-
OUTPUT ${TEST_RUNNER}
38-
DEPENDS ${TEST_FILE}
39-
COMMAND
40-
${UNITY2_RUBY_EXECUTABLE} ${UNITY_DIR}/auto/generate_test_runner.rb
41-
${DIR_OF_TARGET_ADD_UNIT_TEST}/zwave_unity_config.yml ${TEST_FILE}
42-
${TEST_RUNNER}
43-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
4429
endif()
30+
if(NOT DEFINED ${UNIFY_UNITY_RUNNER_CONFIG})
31+
set(UNIFY_UNITY_RUNNER_CONFIG ${DIR_OF_TARGET_ADD_UNIT_TEST}/zwave_unity_config.yml)
32+
endif()
33+
add_custom_command(
34+
OUTPUT ${TEST_RUNNER}
35+
DEPENDS ${TEST_FILE} ${UNIFY_UNITY_RUNNER_CONFIG}
36+
COMMAND
37+
${UNITY2_RUBY_EXECUTABLE} ${UNITY_DIR}/auto/generate_test_runner.rb
38+
${UNIFY_UNITY_RUNNER_CONFIG}
39+
${TEST_FILE} ${TEST_RUNNER}
40+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
4541
endfunction()
4642

4743
# This function creates unity2 test executables. It uses the provided target to setup and import configuration

0 commit comments

Comments
 (0)