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-12-01 00:28:28 +00:00
|
|
|
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
|
2020-07-20 16:52:23 +00:00
|
|
|
|
2020-10-28 16:50:23 +00:00
|
|
|
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
|
2021-01-27 12:27:48 +00:00
|
|
|
include(tools/cmake/set_linker_script.cmake)
|
2020-08-04 18:23:25 +00:00
|
|
|
|
2020-10-26 16:13:36 +00:00
|
|
|
add_library(mbed-core INTERFACE)
|
2020-08-25 11:40:20 +00:00
|
|
|
|
2020-10-26 16:13:36 +00:00
|
|
|
add_library(mbed-os INTERFACE)
|
|
|
|
|
|
|
|
target_link_libraries(mbed-os
|
2020-10-28 16:53:46 +00:00
|
|
|
INTERFACE
|
|
|
|
mbed-rtos
|
|
|
|
mbed-core
|
2020-10-26 16:13:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
add_library(mbed-baremetal INTERFACE)
|
|
|
|
|
|
|
|
target_link_libraries(mbed-baremetal
|
|
|
|
INTERFACE
|
|
|
|
mbed-core
|
|
|
|
)
|
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 "")
|
2021-01-27 12:26:54 +00:00
|
|
|
endif()
|
2020-09-15 13:37:07 +00:00
|
|
|
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-10-28 16:53:46 +00:00
|
|
|
mbed_set_cpu_core_definitions(mbed-core)
|
|
|
|
if(${MBED_TOOLCHAIN_FILE_USED})
|
|
|
|
mbed_set_profile_options(mbed-core ${MBED_TOOLCHAIN})
|
|
|
|
mbed_set_c_lib(mbed-core ${MBED_C_LIB})
|
|
|
|
mbed_set_printf_lib(mbed-core ${MBED_PRINTF_LIB})
|
2021-02-10 10:30:11 +00:00
|
|
|
|
|
|
|
target_compile_features(mbed-core
|
|
|
|
INTERFACE
|
|
|
|
c_std_11
|
|
|
|
cxx_std_14
|
|
|
|
)
|
|
|
|
|
2020-10-28 16:53:46 +00:00
|
|
|
endif()
|
|
|
|
|
2020-10-22 16:00:21 +00:00
|
|
|
target_compile_definitions(mbed-core
|
2020-10-26 16:13:36 +00:00
|
|
|
INTERFACE
|
2020-08-06 15:41:48 +00:00
|
|
|
${MBED_TARGET_DEFINITIONS}
|
|
|
|
${MBED_CONFIG_DEFINITIONS}
|
|
|
|
)
|
2020-07-30 11:51:46 +00:00
|
|
|
|
2020-10-28 16:50:23 +00:00
|
|
|
# Add compile definitions for backward compatibility with the toolchain
|
|
|
|
# supported. New source files should instead check for __GNUC__ and __clang__
|
2021-01-27 12:26:54 +00:00
|
|
|
# for the GCC_ARM and ARM toolchains respectively.
|
2020-10-28 16:50:23 +00:00
|
|
|
if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
|
|
|
|
target_compile_definitions(mbed-core
|
|
|
|
INTERFACE
|
|
|
|
TOOLCHAIN_GCC_ARM
|
|
|
|
TOOLCHAIN_GCC
|
|
|
|
)
|
|
|
|
elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
|
|
|
|
target_compile_definitions(mbed-core
|
|
|
|
INTERFACE
|
|
|
|
TOOLCHAIN_ARM
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2020-07-20 16:52:23 +00:00
|
|
|
# Include mbed.h and config from generate folder
|
2020-10-22 16:00:21 +00:00
|
|
|
target_include_directories(mbed-core
|
2020-10-26 16:13:36 +00:00
|
|
|
INTERFACE
|
2020-08-06 15:41:48 +00:00
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}
|
|
|
|
)
|
2020-07-20 16:52:23 +00:00
|
|
|
|
2020-10-16 13:21:21 +00:00
|
|
|
# These targets are made visible here so their source files which
|
|
|
|
# are spread in different directories can be referenced and can be linked against
|
|
|
|
# by libraries that depend on them.
|
|
|
|
# TODO CMake: Should the source files be moved?
|
2020-10-22 16:00:21 +00:00
|
|
|
add_library(mbed-device_key INTERFACE)
|
2020-10-16 13:21:21 +00:00
|
|
|
add_library(mbed-rtos INTERFACE)
|
2020-10-12 11:35:19 +00:00
|
|
|
|
2020-07-20 16:52:23 +00:00
|
|
|
add_subdirectory(cmsis)
|
|
|
|
add_subdirectory(drivers)
|
|
|
|
add_subdirectory(hal)
|
|
|
|
add_subdirectory(platform)
|
|
|
|
add_subdirectory(rtos)
|
|
|
|
add_subdirectory(targets)
|
2020-07-30 11:51:46 +00:00
|
|
|
|
2020-10-12 11:35:19 +00:00
|
|
|
# The directories below contain optional target libraries
|
|
|
|
add_subdirectory(events EXCLUDE_FROM_ALL)
|
|
|
|
add_subdirectory(connectivity EXCLUDE_FROM_ALL)
|
|
|
|
add_subdirectory(storage EXCLUDE_FROM_ALL)
|
|
|
|
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
|
2020-11-13 15:49:24 +00:00
|
|
|
add_subdirectory(drivers/usb EXCLUDE_FROM_ALL)
|
2020-10-12 11:35:19 +00:00
|
|
|
add_subdirectory(features EXCLUDE_FROM_ALL)
|
2020-10-16 13:21:21 +00:00
|
|
|
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
|
|
|
|
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)
|
2020-07-30 11:51:46 +00:00
|
|
|
|
2021-01-27 12:27:48 +00:00
|
|
|
# This is a temporary workaround to prevent the build from failing for MBED_TARGETS that
|
|
|
|
# haven't been converted to build system targets yet.
|
|
|
|
# The refactored MBED_TARGETS set the linker script and forward it to the build system as a
|
|
|
|
# usage requirement. The 'old' mechanism was to set the linker script on the top level mbed-core
|
|
|
|
# target. This was needed because MBED_TARGETS were not registered as buildsystem targets,
|
|
|
|
# preventing CMake from working its usage requirements magic and forcing us to set the linker
|
|
|
|
# script globally.
|
|
|
|
#
|
2021-02-10 12:15:48 +00:00
|
|
|
# Ensure the words that make up the Mbed target name are separated with a hyphen, lowercase, and with the `mbed-` prefix.
|
2021-02-09 15:17:31 +00:00
|
|
|
string(TOLOWER ${MBED_TARGET} MBED_TARGET_CONVERTED)
|
|
|
|
string(REPLACE "_" "-" MBED_TARGET_CONVERTED ${MBED_TARGET_CONVERTED})
|
|
|
|
string(PREPEND MBED_TARGET_CONVERTED "mbed-")
|
|
|
|
|
2021-01-27 12:27:48 +00:00
|
|
|
# TODO: Remove when all MBED_TARGETS have been converted to build system targets.
|
2021-02-09 15:17:31 +00:00
|
|
|
if(TARGET ${MBED_TARGET_CONVERTED})
|
|
|
|
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
|
2021-01-27 12:27:48 +00:00
|
|
|
else()
|
|
|
|
get_property(LINKER_SCRIPT GLOBAL PROPERTY MBED_TARGET_LINKER_FILE)
|
|
|
|
mbed_set_linker_script(mbed-core ${LINKER_SCRIPT})
|
|
|
|
endif()
|
|
|
|
|
2020-08-10 09:42:22 +00:00
|
|
|
#
|
|
|
|
# Configures the application
|
2021-02-10 19:37:40 +00:00
|
|
|
# Note, this function will be removed in the next revisions
|
2020-08-10 09:42:22 +00:00
|
|
|
#
|
2020-08-12 12:11:16 +00:00
|
|
|
function(mbed_configure_app_target target)
|
2021-02-10 11:07:09 +00:00
|
|
|
# We need to generate a "response file" to pass to the C preprocessor because of path length
|
|
|
|
# limitations on Windows. We set the response file and bind the path to a global property here.
|
|
|
|
# We query this global property when we set the linker script for the MBED_TARGET being built.
|
|
|
|
#
|
|
|
|
# TODO: Remove this and find a more idiomatic way of passing compile definitions to CPP without
|
|
|
|
# using global properties.
|
|
|
|
mbed_generate_options_for_linker(${target} LINKER_PREPROCESS_DEFINITIONS)
|
|
|
|
set_property(GLOBAL PROPERTY COMPILE_DEFS_RESPONSE_FILE ${LINKER_PREPROCESS_DEFINITIONS})
|
2021-02-10 19:37:40 +00:00
|
|
|
|
|
|
|
# Gcc Arm requires memap to be set with app name, equally to ARMClang
|
|
|
|
# TODO: move this to toolchain and set properly
|
|
|
|
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
|
|
|
|
message(${target})
|
|
|
|
target_link_options(mbed-core
|
|
|
|
INTERFACE
|
2021-02-11 21:49:07 +00:00
|
|
|
"-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map"
|
2021-02-10 19:37:40 +00:00
|
|
|
)
|
|
|
|
endif()
|
2020-08-10 09:42:22 +00:00
|
|
|
endfunction()
|
|
|
|
|
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)
|
|
|
|
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
|
|
|
|
set(CMAKE_POST_BUILD_COMMAND
|
2021-02-11 21:49:07 +00:00
|
|
|
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
|
|
|
|
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${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
|
2021-02-11 21:49:07 +00:00
|
|
|
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
|
|
|
|
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${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
|
|
|
|
)
|
CMake: Add post build operation support
A CMake custom target, mbed-post-build, is added as a dependency of the
application CMake target if a Mbed target adds a CMake custom target
named mbed-post-build-bin. mbed-post-build-bin is added as a dependency
of mbed-post-build. mbed-post-build-bin depends on the application binary.
This is done so a CMake custom command that executes post-build can be added.
The Python scripts that implement the operations have been modified to add
CLI entry points so they can be called from CMake. Dependency on the old
tool has been removed on those scripts by passing them exactly what they
require instead of passing old tool Python objects. A consequence of that
was to slightly amend how the old tool calls some of those Python modules.
Support has only been added for Mbed targets that currently have a requirement
for post build operations. This includes: LPC1114, LPC1768, ARCH_PRO, LPC54114,
LPC546XX, FF_LPC546XX, CY8CKIT064B0S2_4343W, CYTFM_064B0S2_4343W, CYSBSYSKIT_01
The following targets are not supported as TFM support is not yet included:
ARM_MUSCA_B1, ARM_MUSCA_B1_NS, ARM_MUSCA_S1, ARM_MUSCA_S1_NS.
2021-01-26 13:52:29 +00:00
|
|
|
|
|
|
|
if(TARGET mbed-post-build-bin)
|
|
|
|
add_custom_target(mbed-post-build
|
|
|
|
ALL
|
|
|
|
DEPENDS
|
|
|
|
mbed-post-build-bin
|
|
|
|
)
|
|
|
|
endif()
|
2020-07-30 15:42:41 +00:00
|
|
|
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)
|
|
|
|
add_custom_command(
|
|
|
|
TARGET
|
|
|
|
${target}
|
|
|
|
POST_BUILD
|
2021-02-11 21:49:07 +00:00
|
|
|
COMMAND ${Python3_EXECUTABLE} ${MBED_PATH}/tools/memap.py -t ${MBED_TOOLCHAIN} ${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
|
2020-10-08 17:39:13 +00:00
|
|
|
WORKING_DIRECTORY
|
2021-02-11 21:49:07 +00:00
|
|
|
${CMAKE_CURRENT_BINARY_DIR}
|
2020-10-08 17:39:13 +00:00
|
|
|
COMMENT
|
|
|
|
"Displaying memory map for ${target}"
|
|
|
|
)
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
#
|
2020-11-06 17:16:42 +00:00
|
|
|
# Validate selected application profile.
|
2020-10-08 17:39:13 +00:00
|
|
|
#
|
2020-11-06 17:16:42 +00:00
|
|
|
function(mbed_validate_application_profile target)
|
|
|
|
get_target_property(app_link_libraries ${target} LINK_LIBRARIES)
|
|
|
|
string(FIND "${app_link_libraries}" "mbed-baremetal" string_found_position)
|
|
|
|
if(${string_found_position} GREATER_EQUAL 0)
|
|
|
|
if(NOT "bare-metal" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
|
|
|
|
message(FATAL_ERROR
|
|
|
|
"Use full profile as baremetal profile is not supported for this Mbed target")
|
|
|
|
endif()
|
|
|
|
elseif(NOT "full" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
|
|
|
|
message(FATAL_ERROR
|
|
|
|
"The full profile is not supported for this Mbed target")
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set post build operations
|
|
|
|
#
|
|
|
|
function(mbed_set_post_build target)
|
|
|
|
mbed_validate_application_profile(${target})
|
2020-10-08 17:39:13 +00:00
|
|
|
mbed_generate_bin_hex(${target})
|
2021-01-28 03:39:53 +00:00
|
|
|
|
|
|
|
if(HAVE_MEMAP_DEPS)
|
|
|
|
mbed_generate_map_file(${target})
|
|
|
|
endif()
|
2020-10-08 17:39:13 +00:00
|
|
|
endfunction()
|
2020-10-28 15:25:16 +00:00
|
|
|
|
|
|
|
# Ninja requires to be forced for response files
|
|
|
|
if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
|
|
|
|
# known issue ARMClang and Ninja with response files for windows
|
|
|
|
# https://gitlab.kitware.com/cmake/cmake/-/issues/21093
|
|
|
|
if(NOT (CMAKE_HOST_SYSTEM_NAME MATCHES "Windows" AND CMAKE_CXX_COMPILER_ID MATCHES "ARMClang"))
|
|
|
|
set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1 CACHE INTERNAL "")
|
|
|
|
endif()
|
|
|
|
endif()
|