Raef Coles | 9ec67e6 | 2020-07-10 09:40:35 +0100 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | set(CMAKE_SYSTEM_NAME Generic) |
| 9 | set(CMAKE_SYSTEM_PROCESSOR arm) |
| 10 | |
| 11 | set(CMAKE_C_COMPILER arm-none-eabi-gcc) |
| 12 | set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) |
| 13 | |
| 14 | # Tell CMake not to try to link executables during its checks |
| 15 | set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) |
| 16 | |
| 17 | # This variable name is a bit of a misnomer. The file it is set to is included |
| 18 | # at a particular step in the compiler initialisation. It is used here to |
| 19 | # configure the extensions for object files. Despite the name, it also works |
| 20 | # with the Ninja generator. |
| 21 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake) |
| 22 | |
| 23 | # Cmake makes it _really_ hard to dynamically set the cmake_system_processor |
| 24 | # (used for setting mcpu flags etc). Instead we load |
| 25 | # platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro |
| 26 | # to reload the compiler autoconfig. Note that it can't be loaded in this file |
| 27 | # as cmake does not allow the use of command-line defined variables in toolchain |
| 28 | # files and the path is platform dependent. |
| 29 | macro(_compiler_reload) |
| 30 | set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR}) |
| 31 | set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE}) |
| 32 | |
| 33 | if (DEFINED TFM_SYSTEM_FP) |
| 34 | if(TFM_SYSTEM_FP) |
| 35 | # TODO Whether a system requires these extensions appears to depend |
| 36 | # on the system in question, with no real rule. Since adding +fp |
| 37 | # will cause compile failures on systems that already have fp |
| 38 | # enabled, this is commented out for now to avoid those failures. In |
| 39 | # future, better handling should be implemented. |
| 40 | # string(APPEND CMAKE_SYSTEM_PROCESSOR "+fp") |
| 41 | else() |
| 42 | string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp") |
| 43 | endif() |
| 44 | endif() |
| 45 | |
| 46 | if (DEFINED TFM_SYSTEM_DSP) |
| 47 | if(TFM_SYSTEM_DSP) |
| 48 | # TODO Whether a system requires these extensions appears to depend |
| 49 | # on the system in question, with no real rule. Since adding +dsp |
| 50 | # will cause compile failures on systems that already have dsp |
| 51 | # enabled, this is commented out for now to avoid those failures. In |
| 52 | # future, better handling should be implemented. |
| 53 | # string(APPEND CMAKE_SYSTEM_PROCESSOR "+dsp") |
| 54 | else() |
| 55 | string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp") |
| 56 | endif() |
| 57 | endif() |
| 58 | |
| 59 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") |
| 60 | set_property(DIRECTORY PROPERTY LINK_OPTIONS "") |
| 61 | add_compile_options( |
| 62 | --specs=nano.specs |
| 63 | -Wall |
| 64 | -Wno-format |
| 65 | -Wno-return-type |
| 66 | -Wno-unused-but-set-variable |
| 67 | -c |
| 68 | -fdata-sections |
| 69 | -ffunction-sections |
| 70 | -fno-builtin |
| 71 | -fshort-enums |
| 72 | -fshort-wchar |
| 73 | -funsigned-char |
| 74 | -mthumb |
| 75 | -nostdlib |
| 76 | -std=c99 |
| 77 | $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float> |
| 78 | ) |
| 79 | add_link_options( |
| 80 | --entry=Reset_Handler |
| 81 | --specs=nano.specs |
| 82 | LINKER:-check-sections |
| 83 | LINKER:-fatal-warnings |
| 84 | LINKER:--gc-sections |
| 85 | LINKER:--no-wchar-size-warning |
| 86 | LINKER:--print-memory-usage |
| 87 | ) |
| 88 | |
| 89 | unset(CMAKE_C_FLAGS_INIT) |
| 90 | unset(CMAKE_ASM_FLAGS_INIT) |
| 91 | |
| 92 | set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 93 | set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 94 | set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 95 | set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 96 | |
| 97 | set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT}) |
| 98 | set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) |
| 99 | endmacro() |
| 100 | |
| 101 | set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=) |
| 102 | set(COMPILER_CMSE_FLAG -mcmse) |
| 103 | |
| 104 | macro(target_add_scatter_file target) |
| 105 | target_link_options(${target} |
| 106 | PRIVATE |
| 107 | -T $<TARGET_OBJECTS:${target}_scatter> |
| 108 | ) |
| 109 | |
| 110 | add_dependencies(${target} |
| 111 | ${target}_scatter |
| 112 | ) |
| 113 | |
| 114 | add_library(${target}_scatter OBJECT) |
| 115 | foreach(scatter_file ${ARGN}) |
| 116 | target_sources(${target}_scatter |
| 117 | PRIVATE |
| 118 | ${scatter_file} |
| 119 | ) |
| 120 | # Cmake cannot use generator expressions in the |
| 121 | # set_source_file_properties command, so instead we just parse the regex |
| 122 | # for the filename and set the property on all files, regardless of if |
| 123 | # the generator expression would evaluate to true or not. |
| 124 | string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") |
| 125 | set_source_files_properties(${SCATTER_FILE_PATH} |
| 126 | PROPERTIES |
| 127 | LANGUAGE C |
| 128 | ) |
| 129 | endforeach() |
| 130 | |
| 131 | set_target_properties(${target}_scatter PROPERTIES |
| 132 | SUFFIX ".ld" |
| 133 | ) |
| 134 | |
| 135 | target_link_libraries(${target}_scatter |
| 136 | platform_region_defs |
| 137 | psa_interface |
| 138 | tfm_partition_defs |
| 139 | ) |
| 140 | |
| 141 | target_compile_options(${target}_scatter |
| 142 | PRIVATE |
| 143 | -E |
| 144 | -P |
| 145 | -xc |
| 146 | ) |
| 147 | endmacro() |
| 148 | |
| 149 | macro(add_convert_to_bin_target target) |
| 150 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
| 151 | |
| 152 | add_custom_target(${target}_bin |
| 153 | SOURCES ${bin_dir}/${target}.bin |
| 154 | ) |
| 155 | add_custom_command(OUTPUT ${bin_dir}/${target}.bin |
| 156 | DEPENDS ${target} |
| 157 | COMMAND ${CMAKE_OBJCOPY} |
| 158 | -O binary $<TARGET_FILE:${target}> |
| 159 | ${bin_dir}/${target}.bin |
| 160 | ) |
| 161 | |
| 162 | add_custom_target(${target}_elf |
| 163 | SOURCES ${bin_dir}/${target}.elf |
| 164 | ) |
| 165 | add_custom_command(OUTPUT ${bin_dir}/${target}.elf |
| 166 | DEPENDS ${target} |
| 167 | COMMAND ${CMAKE_OBJCOPY} |
| 168 | -O elf32-littlearm $<TARGET_FILE:${target}> |
| 169 | ${bin_dir}/${target}.elf |
| 170 | ) |
| 171 | |
| 172 | add_custom_target(${target}_hex |
| 173 | SOURCES ${bin_dir}/${target}.hex |
| 174 | ) |
| 175 | add_custom_command(OUTPUT ${bin_dir}/${target}.hex |
| 176 | DEPENDS ${target} |
| 177 | COMMAND ${CMAKE_OBJCOPY} |
| 178 | -O ihex $<TARGET_FILE:${target}> |
| 179 | ${bin_dir}/${target}.hex |
| 180 | ) |
| 181 | |
| 182 | add_custom_target(${target}_binaries |
| 183 | ALL |
| 184 | DEPENDS ${target}_bin |
| 185 | DEPENDS ${target}_elf |
| 186 | DEPENDS ${target}_hex |
| 187 | ) |
| 188 | endmacro() |