Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Mate Toth-Pal | 8f17a71 | 2020-03-02 16:22:19 +0100 | [diff] [blame] | 2 | # Copyright (c) 2017-2020, Arm Limited. All rights reserved. |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #This file contains settings to specify how GNUARM shall be used |
| 9 | |
| 10 | function(check_gnuarm_input_vars MY_VERSION) |
| 11 | #Specify where gnuarm is |
| 12 | if (NOT DEFINED GNUARM_PATH) |
| 13 | message(FATAL_ERROR "Please set GNUARM_PATH to the root directory of the gnuarm installation. e.g. set(GNUARM_PATH \"C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/\"") |
| 14 | endif() |
| 15 | |
| 16 | STRING(REGEX REPLACE "([0-9]+).([0-9]+).*" "\\1.\\2" _MY_MAJOR_MINOR "${MY_VERSION}") |
| 17 | STRING(REGEX REPLACE "([0-9]+).([0-9]+).*" "\\1.\\2" _GNUARM_MAJOR_MINOR "${GNUARM_VER}") |
| 18 | |
| 19 | #Check gnuarm version. |
| 20 | if (NOT "${_MY_MAJOR_MINOR}" VERSION_EQUAL "${_GNUARM_MAJOR_MINOR}") |
| 21 | message(FATAL_ERROR "GNUARM version (GNUARM_VER=${GNUARM_VER}) does not match ${MY_VERSION}") |
| 22 | endif() |
| 23 | |
Antonio de Angelis | f1f7ebd | 2018-11-23 23:11:41 +0000 | [diff] [blame] | 24 | if (NOT DEFINED ARM_CPU_ARCHITECTURE AND NOT DEFINED ARM_CPU_TYPE) |
| 25 | message(FATAL_ERROR "ARM_CPU_TYPE and ARM_CPU_ARCHITECTURE is not defined! Please include the CPU specific config file before this one.") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 26 | endif() |
| 27 | |
| 28 | endfunction() |
| 29 | |
| 30 | message(STATUS "Using gcc compiler package v${GNUARM_VER} from ${GNUARM_PATH}") |
| 31 | |
| 32 | |
| 33 | #Tell cmake which compiler we use |
| 34 | if (EXISTS "c:/") |
Kumar Gala | 7946bdd | 2020-04-16 16:29:49 -0500 | [diff] [blame] | 35 | set (CMAKE_C_COMPILER "${GNUARM_PATH}/bin/${GNUARM_PREFIX}-gcc.exe") |
| 36 | set (CMAKE_CXX_COMPILER "${GNUARM_PATH}/bin/${GNUARM_PREFIX}-g++.exe") |
| 37 | set (CMAKE_ASM_COMPILER "${GNUARM_PATH}/bin/${GNUARM_PREFIX}-gcc.exe") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 38 | else() |
Kumar Gala | 7946bdd | 2020-04-16 16:29:49 -0500 | [diff] [blame] | 39 | set (CMAKE_C_COMPILER "${GNUARM_PATH}/bin/${GNUARM_PREFIX}-gcc") |
| 40 | set (CMAKE_CXX_COMPILER "${GNUARM_PATH}/bin/${GNUARM_PREFIX}-g++") |
| 41 | set (CMAKE_ASM_COMPILER "${GNUARM_PATH}/bin/${GNUARM_PREFIX}-gcc") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 42 | endif() |
| 43 | |
| 44 | if("CXX" IN_LIST languages) |
| 45 | set(CMAKE_CXX_COMPILER_ID "GNUARM" CACHE INTERNAL "CXX compiler ID" FORCE) |
| 46 | include(Compiler/GNUARM-CXX) |
| 47 | endif() |
| 48 | |
| 49 | if("C" IN_LIST languages) |
| 50 | set(CMAKE_C_COMPILER_ID "GNUARM" CACHE INTERNAL "C compiler ID" FORCE) |
| 51 | include(Compiler/GNUARM-C) |
| 52 | endif() |
| 53 | |
| 54 | if("ASM" IN_LIST languages) |
| 55 | set(CMAKE_C_COMPILER_ID "GNUARM" CACHE INTERNAL "ASM compiler ID" FORCE) |
| 56 | include(Compiler/GNUARM-ASM) |
| 57 | endif() |
| 58 | |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 59 | function(compiler_get_preinclude_option_string INCLUDE RES) |
| 60 | set(${RES} "-include ${INCLUDE}" PARENT_SCOPE) |
| 61 | endfunction() |
| 62 | |
| 63 | function(compiler_set_preinclude_file) |
| 64 | #Option (on/off) arguments. |
| 65 | set( _OPTIONS_ARGS GLOBAL) |
| 66 | #Single option arguments. |
| 67 | set( _ONE_VALUE_ARGS INCLUDE) |
| 68 | #List arguments |
| 69 | set( _MULTI_VALUE_ARGS TARGETS FILES) |
| 70 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 71 | if(NOT DEFINED _MY_PARAMS) |
| 72 | message(FATAL_ERROR "compiler_set_preinclude_file: missing mandatory parameter INCLUDE.") |
| 73 | endif() |
| 74 | compiler_get_preinclude_option_string(${INCLUDE} _OPTION_STRING) |
| 75 | #If include is to be set globally, we ignore TARGETS and FILES |
| 76 | if(_MY_PARAMS_GLOBAL) |
| 77 | set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY COMPILE_OPTIONS "${_OPTION_STRING}") |
| 78 | else() |
| 79 | #If GLOBAL was not passed, then either TARGETS or FILES must be present |
| 80 | if(NOT DEFINED _MY_PARAM_TARGETS AND NOT DEFINED _MY_PARAM_FILES) |
| 81 | message(FATAL_ERROR "compiler_set_preinclude_file: missing mandatory parameter. Either TARGETS and/or FILES must be specified.") |
| 82 | endif() |
| 83 | #Iterate over targets. Note: call embedded_set_target_compile_flags to |
| 84 | #allow the target to be defined after this function call. This helps |
| 85 | #modularisation |
| 86 | foreach(_TGT IN_LISTS _MY_PARAM_TARGETS) |
Mate Toth-Pal | 8f17a71 | 2020-03-02 16:22:19 +0100 | [diff] [blame] | 87 | embedded_set_target_compile_flags(TARGET ${_TGT} LANGUAGE "C" APPEND FLAGS "${_OPTION_STRING}") |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 88 | endforeach() |
| 89 | #Iterate over files |
| 90 | foreach(_FILE IN_LISTS _MY_PARAM_FILES) |
| 91 | set_property(FILE ${_FILE} APPEND PROPERTY COMPILE_OPTIONS "${_OPTION_STRING}") |
| 92 | endforeach() |
| 93 | endif() |
| 94 | endfunction() |
| 95 | |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 96 | function(compiler_set_linkercmdfile) |
| 97 | set( _OPTIONS_ARGS ) #Option (on/off) arguments. |
| 98 | set( _ONE_VALUE_ARGS TARGET PATH) #Single option arguments. |
| 99 | set( _MULTI_VALUE_ARGS DEFINES INCLUDES) #List arguments |
| 100 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 101 | |
| 102 | #Check passed parameters |
| 103 | if(NOT _MY_PARAMS_TARGET) |
| 104 | message(FATAL_ERROR "compiler_set_linkercmdfile: mandatory parameter 'TARGET' is missing.") |
| 105 | endif() |
| 106 | if (NOT TARGET ${_MY_PARAMS_TARGET}) |
| 107 | message(FATAL_ERROR "compiler_set_linkercmdfile: value of parameter 'TARGET' is invalid.") |
| 108 | endif() |
| 109 | |
| 110 | if(NOT _MY_PARAMS_PATH) |
| 111 | message(FATAL_ERROR "compiler_set_linkercmdfile: mandatory parameter 'PATH' is missing.") |
| 112 | endif() |
| 113 | set(_FILE_PATH ${_MY_PARAMS_PATH}) |
| 114 | |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 115 | #Create additional target if linker script needs to be pre-processed. |
| 116 | if (_MY_PARAMS_DEFINES OR _MY_PARAMS_INCLUDES) |
| 117 | #Name of pre-processed linker script file. |
Tamas Ban | 6032c3b | 2018-07-17 22:11:20 +0100 | [diff] [blame] | 118 | set(FINAL_LD_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_MY_PARAMS_TARGET}.ld.i") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 119 | #Name of the target doing the pre-processing |
| 120 | set(LD_PP_TARGET_NAME "${_MY_PARAMS_TARGET}_ldpp") |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 121 | compiler_preprocess_file(SRC ${_MY_PARAMS_PATH} |
| 122 | DST ${FINAL_LD_FILE_NAME} |
| 123 | TARGET_PREFIX ${_MY_PARAMS_TARGET} |
| 124 | BEFORE_TARGET ${_MY_PARAMS_TARGET} |
| 125 | DEFINES ${_MY_PARAMS_DEFINES} |
| 126 | INCLUDES ${_MY_PARAMS_INCLUDES}) |
| 127 | |
| 128 | #Tell cmake to delete the intermediate linker script when the clean rule is executed. |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 129 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 130 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${FINAL_LD_FILE_NAME}") |
| 131 | #Set the path to linker script point to the intermediate file. |
| 132 | set(_FILE_PATH ${FINAL_LD_FILE_NAME}) |
| 133 | endif() |
| 134 | |
| 135 | #Note: the space before the option is important! |
| 136 | set_property(TARGET ${_MY_PARAMS_TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -T${_FILE_PATH}") |
| 137 | set_property(TARGET ${_MY_PARAMS_TARGET} APPEND PROPERTY LINK_DEPENDS ${_FILE_PATH}) |
| 138 | #Tell cmake .map files shall be removed when project is cleaned (make clean) |
| 139 | get_filename_component(_TARGET_BASE_NAME ${_MY_PARAMS_TARGET} NAME_WE) |
| 140 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 141 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${_TARGET_BASE_NAME}.map") |
| 142 | endfunction() |
| 143 | |
| 144 | function(compiler_set_cmse_output TARGET FILE_PATH) |
| 145 | #Note: the space before the option is important! |
| 146 | set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--cmse-implib,--out-implib=${FILE_PATH}") |
| 147 | #Tell cmake cmse output is a generated object file. |
| 148 | SET_SOURCE_FILES_PROPERTIES("${FILE_PATH}" PROPERTIES EXTERNAL_OBJECT true GENERATED true) |
Kevin Townsend | 99c0a96 | 2020-06-02 12:50:12 +0200 | [diff] [blame] | 149 | add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo "" BYPRODUCTS ${FILE_PATH}) |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 150 | #Tell cmake cmse output shall be removed by clean target. |
| 151 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 152 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${FILE_PATH}") |
| 153 | endfunction() |
| 154 | |
| 155 | function(compiler_merge_library) |
| 156 | set( _OPTIONS_ARGS ) #Option (on/off) arguments. |
| 157 | set( _ONE_VALUE_ARGS DEST) #Single option arguments. |
| 158 | set( _MULTI_VALUE_ARGS LIBS) #List arguments |
| 159 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 160 | |
| 161 | #Check passed parameters |
| 162 | if(NOT _MY_PARAMS_DEST) |
| 163 | message(FATAL_ERROR "compiler_merge_library: no destination target specified. Please see the DEST parameter.") |
| 164 | endif() |
| 165 | #Check if destination is a library |
| 166 | get_target_property(_tmp ${_MY_PARAMS_DEST} TYPE) |
| 167 | if(NOT "${_tmp}" STREQUAL "STATIC_LIBRARY") |
| 168 | message(FATAL_ERROR "compiler_merge_library: parameter DEST must be a static library target.") |
| 169 | endif() |
| 170 | set(_DEST ${_MY_PARAMS_DEST}) |
| 171 | |
| 172 | if(NOT _MY_PARAMS_LIBS) |
| 173 | message(FATAL_ERROR "compiler_merge_library: no source libraries specified. Please see the LIBS parameter.") |
| 174 | endif() |
| 175 | set(_LIBS ${_MY_PARAMS_LIBS}) |
| 176 | |
| 177 | ##Find the cmake script doing the merge. |
| 178 | find_file(_MERGE_SCRIPT "GNUArMerge.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH) |
| 179 | |
| 180 | #Now add a custom command for each source library to our custom target to |
| 181 | #merge into the destination. |
| 182 | foreach(SRC_LIB ${_LIBS}) |
| 183 | get_filename_component(_SRC_LIB_NAME "${SRC_LIB}" NAME) |
| 184 | add_custom_command(TARGET ${_DEST} POST_BUILD |
| 185 | COMMAND ${CMAKE_COMMAND} -DCMAKE_AR=${CMAKE_AR} -DSOURCE=${SRC_LIB} -DDESTINATION=$<TARGET_FILE:${_DEST}> -P ${_MERGE_SCRIPT} |
| 186 | COMMENT "\t\tmerging objects from ${_SRC_LIB_NAME}") |
| 187 | endforeach() |
| 188 | endfunction() |
| 189 | |
| 190 | function(compiler_generate_binary_output TARGET) |
| 191 | add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_GNUARM_OBJCOPY} ARGS -O binary $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin) |
| 192 | endfunction() |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 193 | |
Øyvind Rønningstad | 762415c | 2020-04-02 15:34:50 +0200 | [diff] [blame] | 194 | function(compiler_generate_hex_output TARGET) |
| 195 | add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_GNUARM_OBJCOPY} ARGS -O ihex $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.hex) |
| 196 | endfunction() |
| 197 | |
| 198 | function(compiler_generate_elf_output TARGET) |
| 199 | add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_GNUARM_OBJCOPY} ARGS -O elf32-little $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.elf) |
| 200 | endfunction() |
| 201 | |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 202 | # Function for creating a new target that preprocesses a .c file |
| 203 | #INPUTS: |
| 204 | # SRC - (mandatory) - file to be preprocessed |
| 205 | # DST - (mandatory) - output file for the preprocessing |
| 206 | # TARGET_PREFIX - (optional) - prefix for the target that this function creates and which manages the preprocessing |
| 207 | # BEFORE_TARGET - (optional) - target which is dependent on the preprocessing target in the below function |
| 208 | # DEFINES - (optional) - additional command line switches from macro definitions for preprocessing |
| 209 | # INCLUDES - (optional) - additional command line switches from include paths for preprocessing |
| 210 | function(compiler_preprocess_file) |
| 211 | #Option (on/off) arguments. |
| 212 | set( _OPTIONS_ARGS) |
| 213 | #Single option arguments. |
| 214 | set( _ONE_VALUE_ARGS SRC DST TARGET_PREFIX BEFORE_TARGET) |
| 215 | #List arguments |
| 216 | set( _MULTI_VALUE_ARGS DEFINES INCLUDES) |
| 217 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 218 | |
| 219 | #Check passed parameters |
| 220 | if(NOT DEFINED _MY_PARAMS_SRC) |
| 221 | message(FATAL_ERROR "compiler_preprocess_file: mandatory parameter 'SRC' is missing.") |
| 222 | endif() |
| 223 | |
| 224 | if(NOT DEFINED _MY_PARAMS_DST) |
| 225 | message(FATAL_ERROR "compiler_preprocess_file: mandatory parameter 'DST' is missing.") |
| 226 | endif() |
| 227 | |
| 228 | if(DEFINED _MY_PARAMS_BEFORE_TARGET) |
| 229 | if(NOT TARGET ${_MY_PARAMS_BEFORE_TARGET}) |
| 230 | message(FATAL_ERROR "compiler_preprocess_file: optional parameter 'BEFORE_TARGET' is not target.") |
| 231 | endif() |
| 232 | endif() |
| 233 | |
| 234 | #Compose additional command line switches from macro definitions. |
| 235 | set(_FLAGS "") |
| 236 | if (_MY_PARAMS_DEFINES) |
| 237 | foreach(_DEFINE IN LISTS _MY_PARAMS_DEFINES) |
| 238 | list(APPEND _FLAGS "-D${_DEFINE}") |
| 239 | endforeach() |
| 240 | endif() |
| 241 | |
| 242 | #Compose additional command line switches from include paths. |
| 243 | if (_MY_PARAMS_INCLUDES) |
| 244 | foreach(_INCLUDE IN LISTS _MY_PARAMS_INCLUDES) |
| 245 | list(APPEND _FLAGS "-I${_INCLUDE}") |
| 246 | endforeach() |
| 247 | endif() |
| 248 | |
| 249 | #The compiler flag might contain leading spaces which can fail the preprocess operation, these are removed |
| 250 | STRING(STRIP ${CMAKE_C_FLAGS_CPU} _MY_TEMP_CMAKE_C_FLAGS_CPU) |
| 251 | #If a string contains spaces, then it is inserted amongst quotation marks. Furthermore the compiler fails if it is |
| 252 | #called with multiple switches included in one quotation mark. If the extra spaces are replaced by semicolons, |
| 253 | #then the insertion will be advantageous for the compiler. |
| 254 | STRING(REPLACE " " ";" _MY_TEMP2_CMAKE_C_FLAGS_CPU ${_MY_TEMP_CMAKE_C_FLAGS_CPU}) |
| 255 | set(_LOCAL_CMAKE_C_FLAGS_CPU "") |
| 256 | foreach(_C_FLAG IN LISTS _MY_TEMP2_CMAKE_C_FLAGS_CPU) |
| 257 | list(APPEND _LOCAL_CMAKE_C_FLAGS_CPU "${_C_FLAG}") |
| 258 | endforeach() |
| 259 | |
| 260 | add_custom_command(OUTPUT ${_MY_PARAMS_DST} |
| 261 | COMMAND ${CMAKE_C_COMPILER} ${_LOCAL_CMAKE_C_FLAGS_CPU} -E -P -xc ${_FLAGS} ${_MY_PARAMS_SRC} -o ${_MY_PARAMS_DST} |
| 262 | DEPENDS ${_MY_PARAMS_SRC} |
| 263 | COMMENT "Preprocess the ${_MY_PARAMS_SRC} file" |
| 264 | ) |
| 265 | |
| 266 | set(_MY_TARGET_PREFIX "") |
| 267 | if(TARGET ${_MY_PARAMS_TARGET_PREFIX}) |
| 268 | set(_MY_TARGET_PREFIX "${_MY_PARAMS_TARGET_PREFIX}") |
| 269 | endif() |
| 270 | #The preprocessing related target name is obtained by indexing the file's name that is to be preprocessed |
| 271 | get_filename_component(_MY_FILENAME_TO_BE_INDEXED ${_MY_PARAMS_SRC} NAME_WE) |
| 272 | foreach(_SUFFIX RANGE 1 100) |
| 273 | if (NOT TARGET ${_MY_TARGET_PREFIX}_pp_${_MY_FILENAME_TO_BE_INDEXED}_${_SUFFIX}) |
| 274 | set(_PREPROCESS_TARGET_NAME "${_MY_TARGET_PREFIX}_pp_${_MY_FILENAME_TO_BE_INDEXED}_${_SUFFIX}") |
| 275 | break() |
| 276 | endif() |
| 277 | if (_SUFFIX EQUAL 100) |
| 278 | message(FATAL_ERROR "You have called 'compiler_preprocess_file' too many times (${_SUFFIX} function calls).") |
| 279 | endif() |
| 280 | endforeach() |
| 281 | |
| 282 | #Make the original target depend on the new one. |
| 283 | if(TARGET ${_MY_PARAMS_BEFORE_TARGET}) |
| 284 | add_custom_target(${_PREPROCESS_TARGET_NAME} DEPENDS ${_MY_PARAMS_DST}) |
| 285 | add_dependencies(${_MY_PARAMS_BEFORE_TARGET} ${_PREPROCESS_TARGET_NAME}) |
| 286 | else() |
| 287 | add_custom_target(${_PREPROCESS_TARGET_NAME} ALL DEPENDS ${_MY_PARAMS_DST}) |
| 288 | endif() |
| 289 | endfunction() |