2021-02-17 17:28:47 +00:00
|
|
|
# Copyright (c) 2020-2021 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-04-12 12:21:28 +00:00
|
|
|
include(mbed_set_linker_script)
|
2020-08-04 18:23:25 +00:00
|
|
|
|
2021-04-08 11:59:53 +00:00
|
|
|
project(mbed-os)
|
|
|
|
|
2021-04-14 11:22:53 +00:00
|
|
|
# Add all paths to the list files within Mbed OS
|
|
|
|
list(APPEND CMAKE_MODULE_PATH
|
|
|
|
"${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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2021-02-11 15:59:19 +00:00
|
|
|
# We need to generate a "response file" to pass to the C preprocessor when we preprocess the linker
|
|
|
|
# script, because of path length limitations on Windows. We set the response file and bind the path
|
|
|
|
# to a global property here. The MBED_TARGET being built queries this global property when it sets
|
|
|
|
# the linker script.
|
|
|
|
#
|
|
|
|
# We must set this global property before the targets subdirectory is added to the project. This is
|
|
|
|
# required because the MBED_TARGET depends on the response file. If the path to the response file
|
|
|
|
# is not defined when the target requests it the config definitions will not be passed to CPP.
|
|
|
|
#
|
|
|
|
# TODO: Remove this and find a more idiomatic way of passing compile definitions to CPP without
|
|
|
|
# using response files or global properties.
|
|
|
|
mbed_generate_options_for_linker(mbed-core RESPONSE_FILE_PATH)
|
|
|
|
set_property(GLOBAL PROPERTY COMPILE_DEFS_RESPONSE_FILE ${RESPONSE_FILE_PATH})
|
|
|
|
|
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-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-02-15 14:11:09 +00:00
|
|
|
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
|
2021-01-27 12:27:48 +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)
|
2021-03-25 12:40:33 +00:00
|
|
|
if (MBED_TOOLCHAIN STREQUAL "GCC_ARM")
|
|
|
|
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
|
|
|
|
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
|
|
|
|
list(APPEND CMAKE_POST_BUILD_COMMAND
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
|
|
|
|
list(APPEND CMAKE_POST_BUILD_COMMAND
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
endif()
|
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)
|
2021-03-25 12:40:33 +00:00
|
|
|
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
|
|
|
|
list(APPEND CMAKE_POST_BUILD_COMMAND
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
|
|
|
|
list(APPEND CMAKE_POST_BUILD_COMMAND
|
2021-02-11 21:49:07 +00:00
|
|
|
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"
|
2021-03-25 12:40:33 +00:00
|
|
|
)
|
|
|
|
endif()
|
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
|
|
|
|
2021-02-15 17:39:27 +00:00
|
|
|
if(TARGET mbed-post-build-bin-${MBED_TARGET})
|
2021-03-03 18:11:45 +00:00
|
|
|
# Remove the .elf file to force regenerate the application binaries
|
|
|
|
# (including .bin and .hex). This ensures that the post-build script runs
|
|
|
|
# on a raw application instead of a previous build that already went
|
|
|
|
# through the post-build process once.
|
|
|
|
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf)
|
|
|
|
|
2021-03-03 18:26:11 +00:00
|
|
|
# Pass the application's name to the Mbed target's post build operation
|
|
|
|
set_target_properties(mbed-post-build-bin-${MBED_TARGET}
|
|
|
|
PROPERTIES
|
|
|
|
application ${target}
|
|
|
|
)
|
|
|
|
|
2021-02-19 11:52:57 +00:00
|
|
|
# The artefacts must be created before they can be further manipulated
|
|
|
|
add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target})
|
|
|
|
|
|
|
|
# Add a post-build hook to the top-level CMake target in the form of a
|
|
|
|
# CMake custom target. The hook depends on Mbed target specific
|
|
|
|
# post-build CMake target which has a custom command attached to it.
|
|
|
|
add_custom_target(mbed-post-build ALL DEPENDS mbed-post-build-bin-${MBED_TARGET})
|
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
|
|
|
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-04-08 12:38:09 +00:00
|
|
|
COMMAND ${Python3_EXECUTABLE} ${mbed-os_SOURCE_DIR}/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)
|
2021-02-26 12:38:37 +00:00
|
|
|
# The mapfile name includes the top-level target name and the
|
2021-02-17 17:28:47 +00:00
|
|
|
# executable suffix for all toolchains as CMake hardcodes the name of the
|
|
|
|
# diagnostic output file for some toolchains.
|
2021-02-26 12:38:37 +00:00
|
|
|
mbed_configure_memory_map(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map")
|
2020-11-06 17:16:42 +00:00
|
|
|
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()
|
2021-02-17 17:28:47 +00:00
|
|
|
|
|
|
|
# TODO: Remove once all example applications have removed it
|
|
|
|
function(mbed_configure_app_target target)
|
|
|
|
endfunction()
|