Skip to content

Commit a88f43f

Browse files
committed
CMake: Update mbed os, unittest CMake and add CMAKE_CROSSCOMPILING guard
- 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 4ade0bd commit a88f43f

File tree

6 files changed

+193
-345
lines changed

6 files changed

+193
-345
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: 106 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
77

8-
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
9-
include(mbed_set_linker_script)
8+
if(${CMAKE_CROSSCOMPILING})
9+
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
10+
include(mbed_set_linker_script)
11+
endif()
1012

1113
project(mbed-os)
1214

@@ -15,6 +17,18 @@ list(APPEND CMAKE_MODULE_PATH
1517
"${mbed-os_SOURCE_DIR}/platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_TFM_LATEST/scripts;${mbed-os_SOURCE_DIR}/targets/TARGET_Cypress/scripts;${mbed-os_SOURCE_DIR}/targets/TARGET_NXP/scripts"
1618
)
1719

20+
option(BUILD_TESTING "Run unit tests only." OFF)
21+
22+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
23+
include(CTest)
24+
add_subdirectory(UNITTESTS)
25+
# Add MBED_TEST_MODE for backward compatibility with Greentea tests written for use with Mbed CLI 1
26+
target_compile_definitions(${PROJECT_NAME}
27+
PUBLIC
28+
MBED_TEST_MODE
29+
)
30+
endif()
31+
1832
add_library(mbed-core INTERFACE)
1933

2034
add_library(mbed-os INTERFACE)
@@ -33,88 +47,83 @@ target_link_libraries(mbed-baremetal
3347
)
3448
# Validate selected C library type
3549
# The C library type selected has to match the library that the target can support
36-
if(${MBED_C_LIB} STREQUAL "small")
37-
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
38-
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
39-
message(WARNING
40-
"We noticed that target.c_lib is set to `${MBED_C_LIB}`."
41-
" As the ${MBED_TARGET} target does not support a small C library for the ${MBED_TOOLCHAIN} toolchain,"
42-
" we are using the standard C library instead."
43-
)
44-
set(MBED_C_LIB "std" CACHE STRING "")
50+
if(${CMAKE_CROSSCOMPILING})
51+
if(${MBED_C_LIB} STREQUAL "small")
52+
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
53+
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
54+
message(WARNING
55+
"We noticed that target.c_lib is set to `${MBED_C_LIB}`."
56+
" As the ${MBED_TARGET} target does not support a small C library for the ${MBED_TOOLCHAIN} toolchain,"
57+
" we are using the standard C library instead."
58+
)
59+
set(MBED_C_LIB "std" CACHE STRING "")
60+
endif()
4561
endif()
62+
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
63+
message(FATAL_ERROR
64+
"Invalid `target.c_lib` ('${MBED_C_LIB}') for '${MBED_TARGET}' target."
65+
"\nPossible value(s): ${MBED_TARGET_SUPPORTED_C_LIBS}"
66+
)
4667
endif()
47-
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
48-
message(FATAL_ERROR
49-
"Invalid `target.c_lib` ('${MBED_C_LIB}') for '${MBED_TARGET}' target."
50-
"\nPossible value(s): ${MBED_TARGET_SUPPORTED_C_LIBS}"
51-
)
52-
endif()
53-
54-
# Validate selected printf library
55-
set(MBED_PRINTF_LIB_TYPES std minimal-printf)
56-
if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES)
57-
message(FATAL_ERROR
58-
"Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}"
59-
)
60-
endif()
61-
62-
mbed_set_cpu_core_definitions(mbed-core)
63-
if(${MBED_TOOLCHAIN_FILE_USED})
64-
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
65-
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
66-
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
67-
68-
target_compile_features(mbed-core
69-
INTERFACE
70-
c_std_11
71-
cxx_std_14
72-
)
73-
74-
endif()
7568

76-
target_compile_definitions(mbed-core
77-
INTERFACE
78-
${MBED_TARGET_DEFINITIONS}
79-
${MBED_CONFIG_DEFINITIONS}
80-
)
81-
82-
# Add MBED_TEST_MODE for backward compatibility with Greentea tests written for use with Mbed CLI 1
83-
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
84-
target_compile_definitions(${PROJECT_NAME}
85-
PUBLIC
86-
MBED_TEST_MODE
87-
)
88-
endif()
69+
# Validate selected printf library
70+
set(MBED_PRINTF_LIB_TYPES std minimal-printf)
71+
if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES)
72+
message(FATAL_ERROR
73+
"Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}"
74+
)
75+
endif()
76+
77+
mbed_set_cpu_core_definitions(mbed-core)
78+
if(${MBED_TOOLCHAIN_FILE_USED})
79+
message(STATUS ${MBED_TOOLCHAIN})
80+
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
81+
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
82+
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
83+
84+
target_compile_features(mbed-core
85+
INTERFACE
86+
c_std_11
87+
cxx_std_14
88+
)
89+
90+
endif()
8991

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

120129
# Include mbed.h and config from generate folder
@@ -135,24 +144,35 @@ add_subdirectory(drivers)
135144
add_subdirectory(hal)
136145
add_subdirectory(platform)
137146
add_subdirectory(rtos)
138-
add_subdirectory(targets)
147+
148+
if(${CMAKE_CROSSCOMPILING})
149+
add_subdirectory(targets)
150+
# The directories below contain optional target libraries
151+
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
152+
add_subdirectory(storage EXCLUDE_FROM_ALL)
153+
add_subdirectory(events EXCLUDE_FROM_ALL)
154+
else()
155+
add_subdirectory(connectivity)
156+
add_subdirectory(storage)
157+
add_subdirectory(events)
158+
endif()
139159

140160
# The directories below contain optional target libraries
141-
add_subdirectory(events EXCLUDE_FROM_ALL)
142-
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
143-
add_subdirectory(storage EXCLUDE_FROM_ALL)
144161
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
145162
add_subdirectory(drivers/usb EXCLUDE_FROM_ALL)
146163
add_subdirectory(features EXCLUDE_FROM_ALL)
147164
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
148165
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)
149166

150-
# Ensure the words that make up the Mbed target name are separated with a hyphen, lowercase, and with the `mbed-` prefix.
151-
string(TOLOWER ${MBED_TARGET} MBED_TARGET_CONVERTED)
152-
string(REPLACE "_" "-" MBED_TARGET_CONVERTED ${MBED_TARGET_CONVERTED})
153-
string(PREPEND MBED_TARGET_CONVERTED "mbed-")
154167

155-
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
168+
if(${CMAKE_CROSSCOMPILING})
169+
# Ensure the words that make up the Mbed target name are separated with a hyphen, lowercase, and with the `mbed-` prefix.
170+
string(TOLOWER ${MBED_TARGET} MBED_TARGET_CONVERTED)
171+
string(REPLACE "_" "-" MBED_TARGET_CONVERTED ${MBED_TARGET_CONVERTED})
172+
string(PREPEND MBED_TARGET_CONVERTED "mbed-")
173+
174+
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
175+
endif()
156176

157177
#
158178
# Converts output file of `target` to binary file and to Intel HEX file.

0 commit comments

Comments
 (0)