Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | cmake_minimum_required(VERSION 3.15) |
| 8 | |
| 9 | SET(CMAKE_SYSTEM_NAME Generic) |
| 10 | |
| 11 | set(CMAKE_C_COMPILER armclang) |
| 12 | set(CMAKE_ASM_COMPILER armasm) |
| 13 | |
| 14 | set(LINKER_VENEER_OUTPUT_FLAG --import_cmse_lib_out=) |
| 15 | set(COMPILER_CMSE_FLAG $<$<COMPILE_LANGUAGE:C>:-mcmse>) |
| 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}/set_extensions.cmake) |
| 22 | |
| 23 | macro(arm_toolchain_reset_compiler_flags) |
| 24 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") |
| 25 | string(REGEX REPLACE "\\+nodsp" ".no_dsp" CMAKE_ASM_CPU_FLAG "${CMAKE_SYSTEM_PROCESSOR}") |
| 26 | |
| 27 | add_compile_options( |
| 28 | $<$<COMPILE_LANGUAGE:C>:-Wno-ignored-optimization-argument> |
| 29 | $<$<COMPILE_LANGUAGE:C>:-Wno-unused-command-line-argument> |
| 30 | $<$<COMPILE_LANGUAGE:C>:-Wall> |
| 31 | # Don't error when the MBEDTLS_NULL_ENTROPY warning is shown |
| 32 | $<$<COMPILE_LANGUAGE:C>:-Wno-error=cpp> |
| 33 | $<$<COMPILE_LANGUAGE:C>:-c> |
| 34 | $<$<COMPILE_LANGUAGE:C>:-fdata-sections> |
| 35 | $<$<COMPILE_LANGUAGE:C>:-ffunction-sections> |
| 36 | $<$<COMPILE_LANGUAGE:C>:-fno-builtin> |
| 37 | $<$<COMPILE_LANGUAGE:C>:-fshort-enums> |
| 38 | $<$<COMPILE_LANGUAGE:C>:-fshort-wchar> |
| 39 | $<$<COMPILE_LANGUAGE:C>:-funsigned-char> |
| 40 | $<$<COMPILE_LANGUAGE:C>:-masm=auto> |
| 41 | $<$<COMPILE_LANGUAGE:C>:-nostdlib> |
| 42 | $<$<COMPILE_LANGUAGE:C>:-std=c99> |
| 43 | $<$<AND:$<COMPILE_LANGUAGE:C>,$<NOT:$<BOOL:${ARM_SYSTEM_FP}>>>:-mfpu=none> |
| 44 | $<$<AND:$<COMPILE_LANGUAGE:ASM>,$<NOT:$<BOOL:${ARM_SYSTEM_FP}>>>:--fpu=none> |
| 45 | $<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_ASM_CPU_FLAG}> |
| 46 | ) |
| 47 | endmacro() |
| 48 | |
| 49 | macro(arm_toolchain_reset_linker_flags) |
| 50 | set_property(DIRECTORY PROPERTY LINK_OPTIONS "") |
| 51 | |
| 52 | add_link_options( |
| 53 | --info=summarysizes,sizes,totals,unused,veneers |
| 54 | --strict |
| 55 | --symbols |
| 56 | --xref |
| 57 | # Suppress link warnings that are consistant (and therefore hopefully |
| 58 | # harmless) |
| 59 | # https://developer.arm.com/documentation/100074/0608/linker-errors-and-warnings/list-of-the-armlink-error-and-warning-messages |
| 60 | # Empty region description |
| 61 | --diag_suppress=6312 |
| 62 | # Ns section matches pattern |
| 63 | --diag_suppress=6314 |
| 64 | # Duplicate input files |
| 65 | --diag_suppress=6304 |
| 66 | $<$<NOT:$<BOOL:${ARM_SYSTEM_FP}>>:--fpu=softvfp> |
| 67 | ) |
| 68 | endmacro() |
| 69 | |
| 70 | macro(arm_toolchain_set_processor_arch) |
| 71 | set(CMAKE_SYSTEM_PROCESSOR ${ARM_SYSTEM_PROCESSOR}) |
| 72 | set(CMAKE_SYSTEM_ARCHITECTURE ${ARM_SYSTEM_ARCHITECTURE}) |
| 73 | |
| 74 | set(CMAKE_C_COMPILER_TARGET arm-${CROSS_COMPILE}) |
| 75 | set(CMAKE_ASM_COMPILER_TARGET arm-${CROSS_COMPILE}) |
| 76 | |
| 77 | if (DEFINED ARM_SYSTEM_DSP) |
| 78 | if(NOT ARM_SYSTEM_DSP) |
| 79 | string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp") |
| 80 | endif() |
| 81 | |
| 82 | # Cmake's ARMClang support has several issues with compiler validation. To |
| 83 | # avoid these, we set the list of supported -mcpu and -march variables to |
| 84 | # the ones we intend to use so that the validation will never fail. |
| 85 | include(Compiler/ARMClang) |
| 86 | set(CMAKE_C_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR}) |
| 87 | set(CMAKE_C_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_PROCESSOR}) |
| 88 | set(CMAKE_ASM_COMPILER_PROCESSOR_LIST ${CMAKE_SYSTEM_PROCESSOR}) |
| 89 | set(CMAKE_ASM_COMPILER_ARCH_LIST ${CMAKE_SYSTEM_PROCESSOR}) |
| 90 | endif() |
| 91 | endmacro() |
| 92 | |
| 93 | macro(arm_toolchain_reload_compiler) |
| 94 | arm_toolchain_set_processor_arch() |
| 95 | arm_toolchain_reset_compiler_flags() |
| 96 | arm_toolchain_reset_linker_flags() |
| 97 | |
| 98 | unset(CMAKE_C_FLAGS_INIT) |
| 99 | unset(CMAKE_C_LINK_FLAGS) |
| 100 | unset(CMAKE_ASM_FLAGS_INIT) |
| 101 | unset(CMAKE_ASM_LINK_FLAGS) |
| 102 | unset(__mcpu_flag_set) |
| 103 | unset(__march_flag_set) |
| 104 | |
| 105 | include(Compiler/ARMClang) |
| 106 | __compiler_armclang(C) |
| 107 | include(Compiler/ARMCC) |
| 108 | __compiler_armcc(ASM) |
| 109 | |
| 110 | # Cmake's armclang support will set either mcpu or march, but march gives |
| 111 | # better code size so we manually set it. |
| 112 | set(CMAKE_C_FLAGS "-march=${CMAKE_SYSTEM_ARCHITECTURE}") |
| 113 | set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT}) |
| 114 | |
| 115 | set(CMAKE_C_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 116 | set(CMAKE_ASM_LINK_FLAGS "--cpu=${CMAKE_SYSTEM_PROCESSOR}") |
| 117 | # But armlink doesn't support this +dsp syntax |
| 118 | string(REGEX REPLACE "\\+nodsp" "" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}") |
| 119 | string(REGEX REPLACE "\\+nodsp" "" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}") |
| 120 | # And uses different syntax for +nofp |
| 121 | string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS}") |
| 122 | string(REGEX REPLACE "\\+nofp" ".no_fp" CMAKE_ASM_LINK_FLAGS "${CMAKE_ASM_LINK_FLAGS}") |
| 123 | |
| 124 | # Workaround for issues with --depend-single-line with armasm and Ninja |
| 125 | if (CMAKE_GENERATOR STREQUAL "Ninja") |
| 126 | set( CMAKE_DEPFILE_FLAGS_ASM "--depend=<OBJECT>.d") |
| 127 | endif() |
| 128 | |
| 129 | set(CMAKE_C_FLAGS_MINSIZEREL "-Oz -DNDEBUG") |
| 130 | endmacro() |
| 131 | |
| 132 | # Configure environment for the compiler setup run by cmake at the first |
| 133 | # `project` call in <tfm_root>/CMakeLists.txt. After this mandatory setup is |
| 134 | # done, all further compiler setup is done via arm_toolchain_reload_compiler() |
| 135 | arm_toolchain_set_processor_arch() |
| 136 | arm_toolchain_reset_compiler_flags() |
| 137 | arm_toolchain_reset_linker_flags() |
| 138 | |
| 139 | # Behaviour for handling scatter files is so wildly divergent between compilers |
| 140 | # that this macro is required. |
| 141 | macro(target_add_scatter_file target) |
| 142 | target_link_options(${target} |
| 143 | PRIVATE |
| 144 | --scatter=$<TARGET_OBJECTS:${target}_scatter> |
| 145 | ) |
| 146 | |
| 147 | add_dependencies(${target} |
| 148 | ${target}_scatter |
| 149 | ) |
| 150 | |
| 151 | add_library(${target}_scatter OBJECT) |
| 152 | foreach(scatter_file ${ARGN}) |
| 153 | target_sources(${target}_scatter |
| 154 | PRIVATE |
| 155 | ${scatter_file} |
| 156 | ) |
| 157 | # Cmake cannot use generator expressions in the |
| 158 | # set_source_file_properties command, so instead we just parse the regex |
| 159 | # for the filename and set the property on all files, regardless of if |
| 160 | # the generator expression would evaluate to true or not. |
| 161 | string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}") |
| 162 | set_source_files_properties(${SCATTER_FILE_PATH} |
| 163 | PROPERTIES |
| 164 | LANGUAGE C |
| 165 | ) |
| 166 | endforeach() |
| 167 | |
| 168 | target_link_libraries(${target}_scatter |
| 169 | platform_region_defs |
| 170 | ) |
| 171 | |
| 172 | target_compile_options(${target}_scatter |
| 173 | PRIVATE |
| 174 | -E |
| 175 | -xc |
| 176 | ) |
| 177 | endmacro() |
| 178 | |
| 179 | macro(add_convert_to_bin_target target) |
| 180 | get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY) |
| 181 | |
| 182 | add_custom_target(${target}_bin |
| 183 | SOURCES ${bin_dir}/${target}.bin |
| 184 | ) |
| 185 | add_custom_command(OUTPUT ${bin_dir}/${target}.bin |
| 186 | DEPENDS ${target} |
| 187 | COMMAND fromelf |
| 188 | --bincombined $<TARGET_FILE:${target}> |
| 189 | --output=${bin_dir}/${target}.bin |
| 190 | ) |
| 191 | |
| 192 | add_custom_target(${target}_elf |
| 193 | SOURCES ${bin_dir}/${target}.elf |
| 194 | ) |
| 195 | add_custom_command(OUTPUT ${bin_dir}/${target}.elf |
| 196 | DEPENDS ${target} |
| 197 | COMMAND fromelf |
| 198 | --elf $<TARGET_FILE:${target}> |
| 199 | --output=${bin_dir}/${target}.elf |
| 200 | ) |
| 201 | |
| 202 | add_custom_target(${target}_hex |
| 203 | SOURCES ${bin_dir}/${target}.hex |
| 204 | ) |
| 205 | add_custom_command(OUTPUT ${bin_dir}/${target}.hex |
| 206 | DEPENDS ${target} |
| 207 | COMMAND fromelf |
| 208 | --i32combined $<TARGET_FILE:${target}> |
| 209 | --output=${bin_dir}/${target}.hex |
| 210 | ) |
| 211 | |
| 212 | add_custom_target(${target}_binaries |
| 213 | ALL |
| 214 | DEPENDS ${target}_bin |
| 215 | DEPENDS ${target}_elf |
| 216 | DEPENDS ${target}_hex |
| 217 | ) |
| 218 | endmacro() |