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