TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, IAR Systems AB. All rights reserved. |
| 3 | # Copyright (c) 2020, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | #------------------------------------------------------------------------------- |
| 8 | |
| 9 | cmake_minimum_required(VERSION 3.14) |
| 10 | |
Raef Coles | 6e8f83d | 2020-09-28 10:44:06 +0100 | [diff] [blame] | 11 | set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES) |
| 12 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 13 | SET(CMAKE_SYSTEM_NAME Generic) |
| 14 | # This setting is overridden in ${TFM_PLATFORM}/preload.cmake. It can be set to |
| 15 | # any value here. |
| 16 | set(CMAKE_SYSTEM_PROCESSOR cortex-m23) |
| 17 | |
| 18 | set(CMAKE_C_COMPILER iccarm) |
| 19 | set(CMAKE_ASM_COMPILER iasmarm) |
| 20 | |
| 21 | set(COMPILER_CMSE_FLAG --cmse) |
| 22 | set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out= ) |
| 23 | |
| 24 | # This variable name is a bit of a misnomer. The file it is set to is included |
| 25 | # at a particular step in the compiler initialisation. It is used here to |
| 26 | # configure the extensions for object files. Despite the name, it also works |
| 27 | # with the Ninja generator. |
| 28 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake) |
| 29 | |
| 30 | # Cmake makes it _really_ hard to dynamically set the cmake_system_processor |
| 31 | # (used for setting mcpu flags etc). Instead we load |
| 32 | # platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro |
| 33 | # to reload the compiler autoconfig. Note that it can't be loaded in this file |
| 34 | # as cmake does not allow the use of command-line defined variables in toolchain |
| 35 | # files and the path is platform dependent. |
| 36 | macro(_compiler_reload) |
| 37 | if(${TFM_SYSTEM_PROCESSOR} STREQUAL "cortex-m0plus") |
| 38 | set(CMAKE_SYSTEM_PROCESSOR Cortex-M0+) |
| 39 | else() |
| 40 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
| 41 | endif() |
| 42 | set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE}) |
| 43 | |
| 44 | if (DEFINED TFM_SYSTEM_FP) |
| 45 | if(TFM_SYSTEM_FP) |
| 46 | # TODO Whether a system requires these extensions appears to depend |
| 47 | # on the system in question, with no real rule. Since adding .fp |
| 48 | # will cause compile failures on systems that already have fp |
| 49 | # enabled, this is commented out for now to avoid those failures. In |
| 50 | # future, better handling should be implemented. |
| 51 | # string(APPEND CMAKE_SYSTEM_PROCESSOR ".fp") |
| 52 | endif() |
| 53 | endif() |
| 54 | |
| 55 | if (DEFINED TFM_SYSTEM_DSP) |
| 56 | if(TFM_SYSTEM_DSP) |
| 57 | # TODO Whether a system requires these extensions appears to depend |
| 58 | # on the system in question, with no real rule. Since adding .dsp |
| 59 | # will cause compile failures on systems that already have dsp |
| 60 | # enabled, this is commented out for now to avoid those failures. In |
| 61 | # future, better handling should be implemented. |
| 62 | # string(APPEND CMAKE_SYSTEM_PROCESSOR ".dsp") |
| 63 | else() |
| 64 | string(APPEND CMAKE_SYSTEM_PROCESSOR ".no_dsp") |
| 65 | endif() |
| 66 | endif() |
| 67 | |
| 68 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") |
| 69 | set_property(DIRECTORY PROPERTY LINK_OPTIONS "") |
| 70 | add_compile_options( |
| 71 | $<$<COMPILE_LANGUAGE:C,CXX>:-e> |
| 72 | $<$<COMPILE_LANGUAGE:C,CXX>:--dlib_config=full> |
| 73 | $<$<COMPILE_LANGUAGE:C,CXX>:--vla> |
| 74 | $<$<COMPILE_LANGUAGE:C,CXX>:--silent> |
| 75 | $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF> |
| 76 | $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES> |
| 77 | $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084> |
| 78 | ) |
| 79 | add_link_options( |
| 80 | --silent |
| 81 | --semihosting |
| 82 | --redirect __write=__write_buffered |
| 83 | ) |
| 84 | |
| 85 | unset(CMAKE_C_FLAGS_INIT) |
| 86 | unset(CMAKE_C_LINK_FLAGS) |
| 87 | unset(CMAKE_ASM_FLAGS_INIT) |
| 88 | unset(CMAKE_ASM_LINK_FLAGS) |
| 89 | |
| 90 | set(CMAKE_C_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 91 | set(CMAKE_ASM_FLAGS_INIT "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 92 | set(CMAKE_C_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 93 | set(CMAKE_ASM_LINK_FLAGS "--cpu ${CMAKE_SYSTEM_PROCESSOR}") |
| 94 | |
| 95 | set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) |
| 96 | set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) |
| 97 | |
| 98 | endmacro() |
| 99 | |
| 100 | # Behaviour for handling scatter files is so wildly divergent between compilers |
| 101 | # that this macro is required. |
| 102 | macro(target_add_scatter_file target) |
| 103 | target_link_options(${target} |
| 104 | PRIVATE |
| 105 | --config $<TARGET_OBJECTS:${target}_scatter> |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 106 | ) |
| 107 | add_dependencies(${target} |
| 108 | ${target}_scatter |
| 109 | ) |
| 110 | |
| 111 | add_library(${target}_scatter OBJECT) |
| 112 | foreach(scatter_file ${ARGN}) |
| 113 | target_sources(${target}_scatter |
| 114 | PRIVATE |
| 115 | ${scatter_file} |
| 116 | ) |
| 117 | # Cmake cannot use generator expressions in the |
| 118 | # set_source_file_properties command, so instead we just parse the regex |
| 119 | # for the filename and set the property on all files, regardless of if |
| 120 | # the generator expression would evaluate to true or not. |
TTornblom | af19ae9 | 2020-09-29 13:26:29 +0200 | [diff] [blame^] | 121 | string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 122 | set_source_files_properties(${SCATTER_FILE_PATH} |
| 123 | PROPERTIES |
| 124 | LANGUAGE C |
| 125 | ) |
| 126 | endforeach() |
| 127 | |
TTornblom | 18b3bf0 | 2020-09-03 17:42:11 +0200 | [diff] [blame] | 128 | target_link_libraries(${target}_scatter |
| 129 | platform_region_defs |
| 130 | psa_interface |
| 131 | tfm_partition_defs |
| 132 | ) |
| 133 | |
| 134 | target_compile_options(${target}_scatter |
| 135 | PRIVATE |
| 136 | --preprocess=sn $<TARGET_OBJECTS:${target}_scatter> |
| 137 | ) |
| 138 | endmacro() |
| 139 | |
| 140 | macro(add_convert_to_bin_target target) |
| 141 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
| 142 | |
| 143 | add_custom_target(${target}_bin |
| 144 | SOURCES ${bin_dir}/${target}.bin |
| 145 | ) |
| 146 | add_custom_command(OUTPUT ${bin_dir}/${target}.bin |
| 147 | DEPENDS ${target} |
| 148 | COMMAND ielftool |
| 149 | --silent |
| 150 | --bin $<TARGET_FILE:${target}> |
| 151 | ${bin_dir}/${target}.bin |
| 152 | ) |
| 153 | |
| 154 | add_custom_target(${target}_elf |
| 155 | SOURCES ${bin_dir}/${target}.elf |
| 156 | ) |
| 157 | add_custom_command(OUTPUT ${bin_dir}/${target}.elf |
| 158 | DEPENDS ${target} |
| 159 | COMMAND ielftool |
| 160 | --silent |
| 161 | $<TARGET_FILE:${target}> |
| 162 | ${bin_dir}/${target}.elf |
| 163 | ) |
| 164 | |
| 165 | add_custom_target(${target}_hex |
| 166 | SOURCES ${bin_dir}/${target}.hex |
| 167 | ) |
| 168 | add_custom_command(OUTPUT ${bin_dir}/${target}.hex |
| 169 | DEPENDS ${target} |
| 170 | COMMAND ielftool |
| 171 | --silent |
| 172 | --ihex $<TARGET_FILE:${target}> |
| 173 | ${bin_dir}/${target}.hex |
| 174 | ) |
| 175 | |
| 176 | add_custom_target(${target}_binaries |
| 177 | ALL |
| 178 | DEPENDS ${target}_bin |
| 179 | DEPENDS ${target}_elf |
| 180 | DEPENDS ${target}_hex |
| 181 | ) |
| 182 | endmacro() |