|
| 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() |
0 commit comments