Skip to content

Commit 22d926f

Browse files
authored
Merge pull request #13566 from ARMmbed/feature-cmake
CMake: Add initial support
2 parents c29a2e2 + 073ae1d commit 22d926f

File tree

346 files changed

+8622
-70
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

346 files changed

+8622
-70
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,8 @@ DELIVERY/
100100

101101
# Directory used to clone and build TF-M
102102
features/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_IGNORE/
103+
104+
# CMake
105+
CMakeCache.txt
106+
cmake_install.cmake
107+
CMakeFiles/

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ env:
2626

2727
cache:
2828
pip: true
29+
ccache: true
2930
directories:
3031
- ${HOME}/.cache/deps
31-
- ${HOME}/.ccache
32-
3332

3433
before_install:
3534
- source tools/test/travis-ci/functions.sh
3635

36+
addons:
37+
apt:
38+
packages:
39+
- ninja-build
3740

3841
matrix:
3942
include:
@@ -106,7 +109,6 @@ matrix:
106109
':!*tests/*' ':!*targets/*' ':!*TARGET_*' ':!*unsupported/*' \
107110
':!*events/tests/*' ':!*drivers/tests/*'
108111
109-
110112
### Docs Tests ###
111113
- &docs-vm
112114
stage: "Docs"

CMakeLists.txt

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This is the boilerplate for Mbed OS
5+
6+
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
7+
8+
# Using relative paths behavior
9+
if(POLICY CMP0076)
10+
cmake_policy(SET CMP0076 NEW)
11+
endif()
12+
13+
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
14+
include(${MBED_PATH}/tools/cmake/core.cmake)
15+
16+
add_library(mbed-core INTERFACE)
17+
18+
add_library(mbed-os INTERFACE)
19+
20+
target_link_libraries(mbed-os
21+
INTERFACE
22+
mbed-rtos
23+
mbed-core
24+
)
25+
26+
add_library(mbed-baremetal INTERFACE)
27+
28+
target_link_libraries(mbed-baremetal
29+
INTERFACE
30+
mbed-core
31+
)
32+
33+
# Validate selected C library type
34+
# The C library type selected has to match the library that the target can support
35+
if(${MBED_C_LIB} STREQUAL "small")
36+
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
37+
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
38+
message(WARNING
39+
"We noticed that target.c_lib is set to `${MBED_C_LIB}`."
40+
" As the ${MBED_TARGET} target does not support a small C library for the ${MBED_TOOLCHAIN} toolchain,"
41+
" we are using the standard C library instead."
42+
)
43+
set(MBED_C_LIB "std" CACHE STRING "")
44+
endif()
45+
endif()
46+
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
47+
message(FATAL_ERROR
48+
"Invalid `target.c_lib` ('${MBED_C_LIB}') for '${MBED_TARGET}' target."
49+
"\nPossible value(s): ${MBED_TARGET_SUPPORTED_C_LIBS}"
50+
)
51+
endif()
52+
53+
# Validate selected printf library
54+
set(MBED_PRINTF_LIB_TYPES std minimal-printf)
55+
if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES)
56+
message(FATAL_ERROR
57+
"Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}"
58+
)
59+
endif()
60+
61+
mbed_set_cpu_core_definitions(mbed-core)
62+
if(${MBED_TOOLCHAIN_FILE_USED})
63+
mbed_set_cpu_core_options(mbed-core ${MBED_TOOLCHAIN})
64+
mbed_set_toolchain_options(mbed-core)
65+
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
66+
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
67+
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
68+
endif()
69+
70+
71+
set(MBED_TARGET_LABELS
72+
${MBED_TARGET_LABELS} CACHE INTERNAL ""
73+
)
74+
75+
target_compile_definitions(mbed-core
76+
INTERFACE
77+
${MBED_TARGET_DEFINITIONS}
78+
${MBED_CONFIG_DEFINITIONS}
79+
)
80+
81+
# Add compile definitions for backward compatibility with the toolchain
82+
# supported. New source files should instead check for __GNUC__ and __clang__
83+
# for the GCC_ARM and ARM toolchains respectively.
84+
if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
85+
target_compile_definitions(mbed-core
86+
INTERFACE
87+
TOOLCHAIN_GCC_ARM
88+
TOOLCHAIN_GCC
89+
)
90+
elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
91+
target_compile_definitions(mbed-core
92+
INTERFACE
93+
TOOLCHAIN_ARM
94+
)
95+
endif()
96+
97+
# Include mbed.h and config from generate folder
98+
target_include_directories(mbed-core
99+
INTERFACE
100+
${CMAKE_CURRENT_SOURCE_DIR}
101+
)
102+
103+
# These targets are made visible here so their source files which
104+
# are spread in different directories can be referenced and can be linked against
105+
# by libraries that depend on them.
106+
# TODO CMake: Should the source files be moved?
107+
add_library(mbed-device_key INTERFACE)
108+
add_library(mbed-rtos INTERFACE)
109+
110+
add_subdirectory(cmsis)
111+
add_subdirectory(drivers)
112+
add_subdirectory(hal)
113+
add_subdirectory(platform)
114+
add_subdirectory(rtos)
115+
add_subdirectory(targets)
116+
117+
# The directories below contain optional target libraries
118+
add_subdirectory(events EXCLUDE_FROM_ALL)
119+
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
120+
add_subdirectory(storage EXCLUDE_FROM_ALL)
121+
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
122+
add_subdirectory(drivers/source/usb EXCLUDE_FROM_ALL)
123+
add_subdirectory(features EXCLUDE_FROM_ALL)
124+
add_subdirectory(platform/FEATURE_EXPERIMENTAL_API EXCLUDE_FROM_ALL)
125+
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
126+
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)
127+
128+
#
129+
# Configures the application
130+
#
131+
function(mbed_configure_app_target target)
132+
mbed_set_language_standard(${target})
133+
endfunction()
134+
135+
#
136+
# Specifies linker script used for linking `target`.
137+
#
138+
function(mbed_set_mbed_target_linker_script target)
139+
get_property(mbed_target_linker_script GLOBAL PROPERTY MBED_TARGET_LINKER_FILE)
140+
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
141+
mbed_generate_gcc_options_for_linker(${target} _linker_preprocess_definitions)
142+
set(CMAKE_PRE_BUILD_COMMAND
143+
COMMAND "arm-none-eabi-cpp" ${_linker_preprocess_definitions} -x assembler-with-cpp -E -Wp,-P
144+
${mbed_target_linker_script} -o ${CMAKE_BINARY_DIR}/${target}.link_script.ld
145+
146+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
147+
BYPRODUCTS "${CMAKE_BINARY_DIR}/${target}.link_script.ld"
148+
)
149+
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
150+
set(CMAKE_PRE_BUILD_COMMAND COMMAND "")
151+
target_link_options(mbed-core
152+
INTERFACE
153+
"--scatter=${mbed_target_linker_script}"
154+
)
155+
endif()
156+
add_custom_command(
157+
TARGET
158+
${target}
159+
PRE_LINK
160+
${CMAKE_PRE_BUILD_COMMAND}
161+
COMMENT
162+
"Link line:"
163+
VERBATIM
164+
)
165+
endfunction()
166+
167+
#
168+
# Converts output file of `target` to binary file and to Intel HEX file.
169+
#
170+
function(mbed_generate_bin_hex target)
171+
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
172+
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
173+
set(CMAKE_POST_BUILD_COMMAND
174+
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_BINARY_DIR}/${target}.bin
175+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_BINARY_DIR}/${target}.bin"
176+
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_BINARY_DIR}/${target}.hex
177+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_BINARY_DIR}/${target}.hex"
178+
)
179+
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
180+
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
181+
set(CMAKE_POST_BUILD_COMMAND
182+
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
183+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_BINARY_DIR}/${target}.bin"
184+
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
185+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_BINARY_DIR}/${target}.hex"
186+
)
187+
endif()
188+
add_custom_command(
189+
TARGET
190+
${target}
191+
POST_BUILD
192+
${CMAKE_POST_BUILD_COMMAND}
193+
COMMENT
194+
"executable:"
195+
VERBATIM
196+
)
197+
endfunction()
198+
199+
#
200+
# Parse toolchain generated map file of `target` and display a readable table format.
201+
#
202+
function(mbed_generate_map_file target)
203+
find_package (Python3)
204+
add_custom_command(
205+
TARGET
206+
${target}
207+
POST_BUILD
208+
COMMAND ${Python3_EXECUTABLE} ${MBED_PATH}/tools/memap.py -t ${MBED_TOOLCHAIN} ${CMAKE_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
209+
WORKING_DIRECTORY
210+
${CMAKE_BINARY_DIR}
211+
COMMENT
212+
"Displaying memory map for ${target}"
213+
)
214+
endfunction()
215+
216+
#
217+
# Validate selected application profile.
218+
#
219+
function(mbed_validate_application_profile target)
220+
get_target_property(app_link_libraries ${target} LINK_LIBRARIES)
221+
string(FIND "${app_link_libraries}" "mbed-baremetal" string_found_position)
222+
if(${string_found_position} GREATER_EQUAL 0)
223+
if(NOT "bare-metal" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
224+
message(FATAL_ERROR
225+
"Use full profile as baremetal profile is not supported for this Mbed target")
226+
endif()
227+
elseif(NOT "full" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
228+
message(FATAL_ERROR
229+
"The full profile is not supported for this Mbed target")
230+
endif()
231+
endfunction()
232+
233+
#
234+
# Set post build operations
235+
#
236+
function(mbed_set_post_build target)
237+
mbed_validate_application_profile(${target})
238+
mbed_generate_bin_hex(${target})
239+
mbed_generate_map_file(${target})
240+
endfunction()
241+
242+
# Ninja requires to be forced for response files
243+
if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
244+
# known issue ARMClang and Ninja with response files for windows
245+
# https://gitlab.kitware.com/cmake/cmake/-/issues/21093
246+
if(NOT (CMAKE_HOST_SYSTEM_NAME MATCHES "Windows" AND CMAKE_CXX_COMPILER_ID MATCHES "ARMClang"))
247+
set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1 CACHE INTERNAL "")
248+
endif()
249+
endif()

cmsis/CMSIS_5/CMSIS/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if("CORTEX_A" IN_LIST MBED_TARGET_LABELS)
5+
add_subdirectory(TARGET_CORTEX_A)
6+
elseif("CORTEX_M" IN_LIST MBED_TARGET_LABELS)
7+
add_subdirectory(TARGET_CORTEX_M)
8+
endif()
9+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_subdirectory(RTX)
5+
6+
target_include_directories(mbed-rtos
7+
INTERFACE
8+
Include
9+
)
10+
11+
target_sources(mbed-rtos
12+
INTERFACE
13+
Source/os_systick.c
14+
Source/os_tick_ptim.c
15+
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
function(_mbed_get_cortex_m_exception_handlers toolchain_dir)
5+
foreach(key ${MBED_TARGET_LABELS})
6+
if(${key} STREQUAL CORTEX_A)
7+
set(STARTUP_RTX_FILE TARGET_CORTEX_A/irq_ca.S)
8+
elseif(${key} STREQUAL M0)
9+
set(STARTUP_RTX_FILE TARGET_M0/irq_cm0.S)
10+
elseif(${key} STREQUAL M0P)
11+
set(STARTUP_RTX_FILE TARGET_M0P/irq_cm0.S)
12+
elseif(${key} STREQUAL M23)
13+
set(STARTUP_RTX_FILE TARGET_M23/irq_armv8mbl.S)
14+
elseif(${key} STREQUAL M3)
15+
set(STARTUP_RTX_FILE TARGET_M3/irq_cm3.S)
16+
elseif(${key} STREQUAL M33)
17+
set(STARTUP_RTX_FILE TARGET_M33/irq_armv8mml.S)
18+
elseif(${key} STREQUAL RTOS_M4_M7)
19+
set(STARTUP_RTX_FILE TARGET_RTOS_M4_M7/irq_cm4f.S)
20+
endif()
21+
22+
target_sources(mbed-rtos
23+
INTERFACE
24+
Source/${toolchain_dir}/${STARTUP_RTX_FILE}
25+
)
26+
endforeach()
27+
endfunction()
28+
29+
function(_mbed_get_cortex_a_exception_handlers)
30+
foreach(key ${MBED_TARGET_LABELS})
31+
if(${key} STREQUAL CORTEX_A)
32+
target_sources(mbed-rtos INTERFACE Config/TARGET_CORTEX_A/handlers.c)
33+
endif()
34+
endforeach()
35+
endfunction()
36+
37+
_mbed_get_cortex_a_exception_handlers()
38+
39+
if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
40+
_mbed_get_cortex_m_exception_handlers(TOOLCHAIN_GCC)
41+
elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
42+
_mbed_get_cortex_m_exception_handlers(TOOLCHAIN_ARM)
43+
elseif(${MBED_TOOLCHAIN} STREQUAL "IAR")
44+
_mbed_get_cortex_m_exception_handlers(TOOLCHAIN_IAR)
45+
endif()
46+
47+
target_include_directories(mbed-rtos
48+
INTERFACE
49+
Config
50+
Include
51+
Include1
52+
Source
53+
)
54+
55+
target_sources(mbed-rtos
56+
INTERFACE
57+
Config/RTX_Config.c
58+
59+
Library/cmsis_os1.c
60+
61+
Source/rtx_delay.c
62+
Source/rtx_evflags.c
63+
Source/rtx_evr.c
64+
Source/rtx_kernel.c
65+
Source/rtx_lib.c
66+
Source/rtx_memory.c
67+
Source/rtx_mempool.c
68+
Source/rtx_msgqueue.c
69+
Source/rtx_mutex.c
70+
Source/rtx_semaphore.c
71+
Source/rtx_system.c
72+
Source/rtx_thread.c
73+
Source/rtx_timer.c
74+
)

0 commit comments

Comments
 (0)