Skip to content

Commit 2afdf35

Browse files
committed
CMake: Update unittest CMake support
- Add a new MbedOS project in mbed os root CMake which can be used along with BUILD_TESTING conditional check for enabling the unittest build - Update UNITTEST CMake for setting the CMake configuration like c, cxx flags etc., - Add if CMAKE_CROSSCOMPILING conditional check wherever target configuration check and toolchain configuration to avoid such configuration gets included for unittest build.
1 parent 3aee3dd commit 2afdf35

File tree

6 files changed

+201
-328
lines changed

6 files changed

+201
-328
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ DELIVERY/
102102
CMakeCache.txt
103103
cmake_install.cmake
104104
CMakeFiles/
105+
cmake_build/
106+
Testing/

CMakeLists.txt

Lines changed: 105 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
# This is the boilerplate for Mbed OS
5+
project(MbedOS)
56

67
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
78

8-
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
9+
option(BUILD_TESTING "Run unit tests only." OFF)
10+
11+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
12+
include(CTest)
13+
endif()
14+
15+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
16+
add_subdirectory(UNITTESTS)
17+
set(MBED_BUILD_UNITTESTS ON CACHE BOOL "")
18+
endif()
19+
20+
if(${CMAKE_CROSSCOMPILING})
21+
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
22+
endif()
23+
924
include(tools/cmake/set_linker_script.cmake)
1025

1126
add_library(mbed-core INTERFACE)
@@ -26,80 +41,83 @@ target_link_libraries(mbed-baremetal
2641
)
2742
# Validate selected C library type
2843
# The C library type selected has to match the library that the target can support
29-
if(${MBED_C_LIB} STREQUAL "small")
30-
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
31-
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
32-
message(WARNING
33-
"We noticed that target.c_lib is set to `${MBED_C_LIB}`."
34-
" As the ${MBED_TARGET} target does not support a small C library for the ${MBED_TOOLCHAIN} toolchain,"
35-
" we are using the standard C library instead."
36-
)
37-
set(MBED_C_LIB "std" CACHE STRING "")
44+
if(${CMAKE_CROSSCOMPILING})
45+
if(${MBED_C_LIB} STREQUAL "small")
46+
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
47+
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
48+
message(WARNING
49+
"We noticed that target.c_lib is set to `${MBED_C_LIB}`."
50+
" As the ${MBED_TARGET} target does not support a small C library for the ${MBED_TOOLCHAIN} toolchain,"
51+
" we are using the standard C library instead."
52+
)
53+
set(MBED_C_LIB "std" CACHE STRING "")
54+
endif()
3855
endif()
56+
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
57+
message(FATAL_ERROR
58+
"Invalid `target.c_lib` ('${MBED_C_LIB}') for '${MBED_TARGET}' target."
59+
"\nPossible value(s): ${MBED_TARGET_SUPPORTED_C_LIBS}"
60+
)
3961
endif()
40-
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
41-
message(FATAL_ERROR
42-
"Invalid `target.c_lib` ('${MBED_C_LIB}') for '${MBED_TARGET}' target."
43-
"\nPossible value(s): ${MBED_TARGET_SUPPORTED_C_LIBS}"
44-
)
45-
endif()
46-
47-
# Validate selected printf library
48-
set(MBED_PRINTF_LIB_TYPES std minimal-printf)
49-
if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES)
50-
message(FATAL_ERROR
51-
"Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}"
52-
)
53-
endif()
5462

55-
mbed_set_cpu_core_definitions(mbed-core)
56-
if(${MBED_TOOLCHAIN_FILE_USED})
57-
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
58-
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
59-
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
60-
61-
target_compile_features(mbed-core
62-
INTERFACE
63-
c_std_11
64-
cxx_std_14
65-
)
66-
67-
endif()
68-
69-
target_compile_definitions(mbed-core
70-
INTERFACE
71-
${MBED_TARGET_DEFINITIONS}
72-
${MBED_CONFIG_DEFINITIONS}
73-
)
63+
# Validate selected printf library
64+
set(MBED_PRINTF_LIB_TYPES std minimal-printf)
65+
if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES)
66+
message(FATAL_ERROR
67+
"Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}"
68+
)
69+
endif()
70+
71+
mbed_set_cpu_core_definitions(mbed-core)
72+
if(${MBED_TOOLCHAIN_FILE_USED})
73+
message(STATUS ${MBED_TOOLCHAIN})
74+
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
75+
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
76+
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
77+
78+
target_compile_features(mbed-core
79+
INTERFACE
80+
c_std_11
81+
cxx_std_14
82+
)
83+
84+
endif()
7485

75-
# We need to generate a "response file" to pass to the C preprocessor when we preprocess the linker
76-
# script, because of path length limitations on Windows. We set the response file and bind the path
77-
# to a global property here. The MBED_TARGET being built queries this global property when it sets
78-
# the linker script.
79-
#
80-
# We must set this global property before the targets subdirectory is added to the project. This is
81-
# required because the MBED_TARGET depends on the response file. If the path to the response file
82-
# is not defined when the target requests it the config definitions will not be passed to CPP.
83-
#
84-
# TODO: Remove this and find a more idiomatic way of passing compile definitions to CPP without
85-
# using response files or global properties.
86-
mbed_generate_options_for_linker(mbed-core RESPONSE_FILE_PATH)
87-
set_property(GLOBAL PROPERTY COMPILE_DEFS_RESPONSE_FILE ${RESPONSE_FILE_PATH})
88-
89-
# Add compile definitions for backward compatibility with the toolchain
90-
# supported. New source files should instead check for __GNUC__ and __clang__
91-
# for the GCC_ARM and ARM toolchains respectively.
92-
if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
9386
target_compile_definitions(mbed-core
9487
INTERFACE
95-
TOOLCHAIN_GCC_ARM
96-
TOOLCHAIN_GCC
97-
)
98-
elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
99-
target_compile_definitions(mbed-core
100-
INTERFACE
101-
TOOLCHAIN_ARM
88+
${MBED_TARGET_DEFINITIONS}
89+
${MBED_CONFIG_DEFINITIONS}
10290
)
91+
92+
# We need to generate a "response file" to pass to the C preprocessor when we preprocess the linker
93+
# script, because of path length limitations on Windows. We set the response file and bind the path
94+
# to a global property here. The MBED_TARGET being built queries this global property when it sets
95+
# the linker script.
96+
#
97+
# We must set this global property before the targets subdirectory is added to the project. This is
98+
# required because the MBED_TARGET depends on the response file. If the path to the response file
99+
# is not defined when the target requests it the config definitions will not be passed to CPP.
100+
#
101+
# TODO: Remove this and find a more idiomatic way of passing compile definitions to CPP without
102+
# using response files or global properties.
103+
mbed_generate_options_for_linker(mbed-core RESPONSE_FILE_PATH)
104+
set_property(GLOBAL PROPERTY COMPILE_DEFS_RESPONSE_FILE ${RESPONSE_FILE_PATH})
105+
106+
# Add compile definitions for backward compatibility with the toolchain
107+
# supported. New source files should instead check for __GNUC__ and __clang__
108+
# for the GCC_ARM and ARM toolchains respectively.
109+
if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
110+
target_compile_definitions(mbed-core
111+
INTERFACE
112+
TOOLCHAIN_GCC_ARM
113+
TOOLCHAIN_GCC
114+
)
115+
elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
116+
target_compile_definitions(mbed-core
117+
INTERFACE
118+
TOOLCHAIN_ARM
119+
)
120+
endif()
103121
endif()
104122

105123
# Include mbed.h and config from generate folder
@@ -122,22 +140,33 @@ add_subdirectory(platform)
122140
add_subdirectory(rtos)
123141
add_subdirectory(targets)
124142

143+
if(${MBED_BUILD_UNITTESTS})
144+
add_subdirectory(connectivity)
145+
add_subdirectory(storage)
146+
add_subdirectory(events)
147+
else()
148+
# The directories below contain optional target libraries
149+
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
150+
add_subdirectory(storage EXCLUDE_FROM_ALL)
151+
add_subdirectory(events EXCLUDE_FROM_ALL)
152+
endif()
153+
125154
# The directories below contain optional target libraries
126-
add_subdirectory(events EXCLUDE_FROM_ALL)
127-
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
128-
add_subdirectory(storage EXCLUDE_FROM_ALL)
129155
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
130156
add_subdirectory(drivers/usb EXCLUDE_FROM_ALL)
131157
add_subdirectory(features EXCLUDE_FROM_ALL)
132158
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
133159
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)
134160

135-
# Ensure the words that make up the Mbed target name are separated with a hyphen, lowercase, and with the `mbed-` prefix.
136-
string(TOLOWER ${MBED_TARGET} MBED_TARGET_CONVERTED)
137-
string(REPLACE "_" "-" MBED_TARGET_CONVERTED ${MBED_TARGET_CONVERTED})
138-
string(PREPEND MBED_TARGET_CONVERTED "mbed-")
139161

140-
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
162+
if(${CMAKE_CROSSCOMPILING})
163+
# Ensure the words that make up the Mbed target name are separated with a hyphen, lowercase, and with the `mbed-` prefix.
164+
string(TOLOWER ${MBED_TARGET} MBED_TARGET_CONVERTED)
165+
string(REPLACE "_" "-" MBED_TARGET_CONVERTED ${MBED_TARGET_CONVERTED})
166+
string(PREPEND MBED_TARGET_CONVERTED "mbed-")
167+
168+
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
169+
endif()
141170

142171
#
143172
# Converts output file of `target` to binary file and to Intel HEX file.

0 commit comments

Comments
 (0)