2020-07-23 13:13:14 +00:00
|
|
|
# Copyright (c) 2020 ARM Limited. All rights reserved.
|
2020-07-20 16:52:23 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-07-23 19:23:56 +00:00
|
|
|
# This is the boilerplate for Mbed OS
|
2020-07-20 16:52:23 +00:00
|
|
|
|
2020-09-04 07:25:48 +00:00
|
|
|
cmake_minimum_required(VERSION 3.18.2 FATAL_ERROR)
|
2020-07-20 16:52:23 +00:00
|
|
|
|
|
|
|
# Using relative paths behavior
|
|
|
|
if(POLICY CMP0076)
|
|
|
|
cmake_policy(SET CMP0076 NEW)
|
|
|
|
endif()
|
|
|
|
|
2020-07-30 11:51:46 +00:00
|
|
|
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
|
2020-08-04 18:23:25 +00:00
|
|
|
|
2020-08-25 11:40:20 +00:00
|
|
|
# Set default toolchain file
|
|
|
|
if(NOT CMAKE_TOOLCHAIN_FILE)
|
2020-09-14 20:38:00 +00:00
|
|
|
set(CMAKE_TOOLCHAIN_FILE "${MBED_ROOT}/tools/cmake/toolchain.cmake" CACHE INTERNAL "")
|
2020-08-25 11:40:20 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# Toolchain setup
|
2020-09-14 20:38:00 +00:00
|
|
|
include(${MBED_ROOT}/tools/cmake/toolchains/${MBED_TOOLCHAIN}.cmake)
|
2020-08-25 11:40:20 +00:00
|
|
|
enable_language(C CXX ASM)
|
|
|
|
|
2020-09-14 20:38:00 +00:00
|
|
|
include(${MBED_ROOT}/tools/cmake/core.cmake)
|
|
|
|
include(${MBED_ROOT}/tools/cmake/util.cmake)
|
|
|
|
include(${MBED_ROOT}/tools/cmake/profile.cmake)
|
2020-08-04 18:23:25 +00:00
|
|
|
|
2020-08-25 11:40:20 +00:00
|
|
|
# Create Mbed OS library
|
|
|
|
add_library(mbed-os OBJECT)
|
|
|
|
|
2020-09-15 13:37:07 +00:00
|
|
|
# Validate selected C library type
|
|
|
|
# The C library type selected has to match the library that the target can support
|
|
|
|
if(${MBED_C_LIB} STREQUAL "small")
|
|
|
|
if(NOT "small" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
|
|
|
|
if("std" IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
|
|
|
|
message(WARNING
|
|
|
|
"We noticed that target.c_lib is set to `${MBED_C_LIB}`."
|
|
|
|
" As the ${MBED_TARGET} target does not support a small C library for the ${MBED_TOOLCHAIN} toolchain,"
|
|
|
|
" we are using the standard C library instead."
|
|
|
|
)
|
|
|
|
set(MBED_C_LIB "std" CACHE STRING "")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
|
|
|
|
message(FATAL_ERROR
|
|
|
|
"Invalid `target.c_lib` ('${MBED_C_LIB}') for '${MBED_TARGET}' target."
|
|
|
|
"\nPossible value(s): ${MBED_TARGET_SUPPORTED_C_LIBS}"
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2020-09-15 11:18:00 +00:00
|
|
|
# Validate selected printf library
|
|
|
|
set(MBED_PRINTF_LIB_TYPES std minimal-printf)
|
|
|
|
if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES)
|
|
|
|
message(FATAL_ERROR
|
|
|
|
"Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}"
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2020-08-25 11:40:20 +00:00
|
|
|
mbed_set_cpu_core_options(mbed-os ${MBED_TOOLCHAIN})
|
2020-08-04 18:34:31 +00:00
|
|
|
mbed_set_toolchain_options(mbed-os)
|
2020-09-15 13:37:07 +00:00
|
|
|
mbed_set_c_lib(mbed-os ${MBED_C_LIB})
|
2020-09-15 11:18:00 +00:00
|
|
|
mbed_set_printf_lib(mbed-os ${MBED_PRINTF_LIB})
|
2020-08-10 09:42:22 +00:00
|
|
|
mbed_set_language_standard(mbed-os)
|
2020-08-04 18:38:17 +00:00
|
|
|
mbed_set_profile_options(mbed-os ${MBED_TOOLCHAIN})
|
2020-08-04 18:23:25 +00:00
|
|
|
|
2020-08-06 15:41:48 +00:00
|
|
|
set_target_properties(mbed-os
|
|
|
|
PROPERTIES
|
|
|
|
MBED_TARGET_LABELS "${MBED_TARGET_LABELS}"
|
|
|
|
)
|
|
|
|
|
|
|
|
target_compile_definitions(mbed-os
|
|
|
|
PUBLIC
|
|
|
|
${MBED_TARGET_DEFINITIONS}
|
|
|
|
${MBED_CONFIG_DEFINITIONS}
|
|
|
|
)
|
2020-07-30 11:51:46 +00:00
|
|
|
|
2020-07-20 16:52:23 +00:00
|
|
|
# Include mbed.h and config from generate folder
|
2020-08-06 15:41:48 +00:00
|
|
|
target_include_directories(mbed-os
|
|
|
|
PUBLIC
|
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}
|
|
|
|
)
|
2020-07-20 16:52:23 +00:00
|
|
|
|
|
|
|
# Default build
|
|
|
|
add_subdirectory(cmsis)
|
|
|
|
add_subdirectory(components)
|
|
|
|
add_subdirectory(connectivity)
|
|
|
|
add_subdirectory(drivers)
|
|
|
|
add_subdirectory(events)
|
|
|
|
add_subdirectory(features)
|
|
|
|
add_subdirectory(hal)
|
|
|
|
add_subdirectory(platform)
|
|
|
|
add_subdirectory(rtos)
|
|
|
|
add_subdirectory(storage)
|
|
|
|
add_subdirectory(targets)
|
2020-07-30 11:51:46 +00:00
|
|
|
|
|
|
|
|
2020-08-10 09:42:22 +00:00
|
|
|
#
|
|
|
|
# Configures the application
|
|
|
|
#
|
2020-08-12 12:11:16 +00:00
|
|
|
function(mbed_configure_app_target target)
|
2020-08-10 09:42:22 +00:00
|
|
|
mbed_set_language_standard(${target})
|
|
|
|
endfunction()
|
|
|
|
|
2020-07-30 15:42:41 +00:00
|
|
|
#
|
|
|
|
# Specifies linker script used for linking `target`.
|
|
|
|
#
|
2020-08-12 12:11:16 +00:00
|
|
|
function(mbed_set_mbed_target_linker_script target)
|
2020-09-08 15:19:04 +00:00
|
|
|
get_property(mbed_target_linker_script GLOBAL PROPERTY MBED_TARGET_LINKER_FILE)
|
2020-07-30 15:42:41 +00:00
|
|
|
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
|
2020-09-11 18:10:50 +00:00
|
|
|
mbed_generate_gcc_options_for_linker(${target} _linker_preprocess_definitions _linker_preprocess_options)
|
2020-07-30 15:42:41 +00:00
|
|
|
set(CMAKE_PRE_BUILD_COMMAND
|
|
|
|
COMMAND "arm-none-eabi-cpp" -E -P
|
2020-09-08 15:19:04 +00:00
|
|
|
${_linker_preprocess_options} ${_linker_preprocess_definitions}
|
|
|
|
${mbed_target_linker_script} -o ${CMAKE_BINARY_DIR}/${target}.link_script.ld
|
2020-07-30 15:42:41 +00:00
|
|
|
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
|
|
BYPRODUCTS "${CMAKE_BINARY_DIR}/${target}.link_script.ld"
|
|
|
|
)
|
|
|
|
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
|
|
|
|
set(CMAKE_PRE_BUILD_COMMAND COMMAND "")
|
|
|
|
target_link_options(mbed-os
|
|
|
|
PUBLIC
|
2020-09-08 15:19:04 +00:00
|
|
|
"--scatter=${mbed_target_linker_script}"
|
2020-07-30 15:42:41 +00:00
|
|
|
)
|
|
|
|
endif()
|
|
|
|
add_custom_command(
|
|
|
|
TARGET
|
|
|
|
${target}
|
|
|
|
PRE_LINK
|
|
|
|
${CMAKE_PRE_BUILD_COMMAND}
|
|
|
|
COMMENT
|
|
|
|
"Link line:"
|
|
|
|
VERBATIM
|
2020-07-30 11:51:46 +00:00
|
|
|
)
|
2020-07-30 15:42:41 +00:00
|
|
|
endfunction()
|
2020-07-30 11:51:46 +00:00
|
|
|
|
2020-07-30 15:42:41 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Converts output file of `target` to binary file and to Intel HEX file.
|
|
|
|
#
|
2020-08-12 12:11:16 +00:00
|
|
|
function(mbed_generate_bin_hex target)
|
2020-07-30 15:42:41 +00:00
|
|
|
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
|
|
|
|
# TODO: @mbed-os-tools This post-build commands should get details from target + profile.
|
|
|
|
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
|
|
|
|
set(CMAKE_POST_BUILD_COMMAND
|
|
|
|
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> $<TARGET_FILE:${target}>.bin
|
2020-08-10 10:44:45 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${target}>.bin"
|
2020-07-30 15:42:41 +00:00
|
|
|
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> $<TARGET_FILE:${target}>.hex
|
2020-08-10 10:44:45 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${target}>.hex"
|
2020-07-30 15:42:41 +00:00
|
|
|
)
|
|
|
|
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
|
|
|
|
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
|
|
|
|
set(CMAKE_POST_BUILD_COMMAND
|
|
|
|
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o $<TARGET_FILE:${target}>.bin $<TARGET_FILE:${target}>
|
2020-08-10 10:44:45 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${target}>.bin"
|
2020-07-30 15:42:41 +00:00
|
|
|
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o $<TARGET_FILE:${target}>.hex $<TARGET_FILE:${target}>
|
2020-08-10 10:44:45 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${target}>.hex"
|
2020-07-30 15:42:41 +00:00
|
|
|
)
|
|
|
|
endif()
|
|
|
|
add_custom_command(
|
|
|
|
TARGET
|
|
|
|
${target}
|
|
|
|
POST_BUILD
|
|
|
|
${CMAKE_POST_BUILD_COMMAND}
|
|
|
|
COMMENT
|
|
|
|
"executable:"
|
|
|
|
VERBATIM
|
|
|
|
)
|
|
|
|
endfunction()
|
2020-10-08 17:39:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Parse toolchain generated map file of `target` and display a readable table format.
|
|
|
|
#
|
|
|
|
function(mbed_generate_map_file target)
|
|
|
|
find_package (Python3)
|
|
|
|
add_custom_command(
|
|
|
|
TARGET
|
|
|
|
${target}
|
|
|
|
POST_BUILD
|
|
|
|
COMMAND ${Python3_EXECUTABLE} ${MBED_ROOT}/tools/memap.py -t ${MBED_TOOLCHAIN} -d 4 ${CMAKE_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
|
|
|
|
WORKING_DIRECTORY
|
|
|
|
${CMAKE_BINARY_DIR}
|
|
|
|
COMMENT
|
|
|
|
"Displaying memory map for ${target}"
|
|
|
|
)
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate executables
|
|
|
|
#
|
|
|
|
function(mbed_generate_executable target)
|
|
|
|
mbed_generate_bin_hex(${target})
|
|
|
|
mbed_generate_map_file(${target})
|
|
|
|
endfunction()
|