Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Gyorgy Szing | 1e4a5da | 2018-05-29 15:11:28 +0200 | [diff] [blame] | 2 | # Copyright (c) 2017-2019, Arm Limited. All rights reserved. |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #This file contains settings to specify how ARMCLANG shall be used |
| 9 | |
| 10 | function(check_armclang_input_vars MY_VERSION) |
| 11 | #Specify where armclang is |
| 12 | if (NOT DEFINED ARMCLANG_PATH) |
| 13 | message(FATAL_ERROR "Please set ARMCLANG_PATH to the root directory of the armclang installation. e.g. set(ARMCLANG_PATH \"C:/Program Files/ARMCompiler${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" _ARMCLANG_MAJOR_MINOR "${ARMCLANG_VER}") |
| 18 | |
| 19 | #Check armclang version. |
| 20 | if (NOT "${_MY_MAJOR_MINOR}" VERSION_EQUAL "${_ARMCLANG_MAJOR_MINOR}") |
| 21 | message(FATAL_ERROR "ARMClang version (ARMCLANG_VER=${ARMCLANG_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.") |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 26 | endif() |
| 27 | |
| 28 | endfunction() |
| 29 | |
| 30 | message(STATUS "Using armclang compiler package v${ARMCLANG_VER} from ${ARMCLANG_PATH}") |
| 31 | |
| 32 | |
| 33 | #Tell cmake which compiler we use |
| 34 | if (EXISTS "c:/") |
| 35 | set (CMAKE_C_COMPILER "${ARMCLANG_PATH}/bin/armclang.exe") |
| 36 | set (CMAKE_CXX_COMPILER "${ARMCLANG_PATH}/bin/armclang.exe") |
| 37 | set (CMAKE_ASM_COMPILER "${ARMCLANG_PATH}/bin/armasm.exe") |
| 38 | else() |
| 39 | set (CMAKE_C_COMPILER "${ARMCLANG_PATH}/bin/armclang") |
| 40 | set (CMAKE_CXX_COMPILER "${ARMCLANG_PATH}/bin/armclang") |
| 41 | set (CMAKE_ASM_COMPILER "${ARMCLANG_PATH}/bin/armasm") |
| 42 | endif() |
| 43 | |
| 44 | if("CXX" IN_LIST languages) |
| 45 | set(CMAKE_CXX_COMPILER_ID "ARMCLANG" CACHE INTERNAL "CXX compiler ID" FORCE) |
| 46 | include(Compiler/ARMClang-CXX) |
| 47 | endif() |
| 48 | |
| 49 | if("C" IN_LIST languages) |
| 50 | set(CMAKE_C_COMPILER_ID "ARMCLANG" CACHE INTERNAL "C compiler ID" FORCE) |
| 51 | include(Compiler/ARMClang-C) |
| 52 | endif() |
| 53 | |
| 54 | if("ASM" IN_LIST languages) |
| 55 | set(CMAKE_C_COMPILER_ID "ARMCLANG" CACHE INTERNAL "ASM compiler ID" FORCE) |
| 56 | include(Compiler/ARMClang-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) |
| 87 | embedded_set_target_compile_flags(TARGET ${_TGT} LANGUAGE "C" FLAGS "${_OPTION_STRING}") |
| 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 | |
| 115 | #Compose additional command line switches from macro definitions. |
| 116 | set(_FLAGS "") |
| 117 | if (_MY_PARAMS_DEFINES) |
| 118 | foreach(_DEFINE IN LISTS _MY_PARAMS_DEFINES) |
| 119 | string(APPEND _FLAGS " --predefine=\"-D${_DEFINE}\"") |
| 120 | endforeach() |
| 121 | endif() |
| 122 | #Compose additional command line switches from include paths. |
| 123 | if (_MY_PARAMS_INCLUDES) |
| 124 | foreach(_INCLUDE_P IN LISTS _MY_PARAMS_INCLUDES) |
Gabor Kertesz | fffaafb | 2018-06-27 17:13:13 +0200 | [diff] [blame] | 125 | string(APPEND _FLAGS " --predefine=\"-I${_INCLUDE_P}\"") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 126 | endforeach() |
| 127 | endif() |
| 128 | |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 129 | #Note: the space before the option is important! |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 130 | set_property(TARGET ${_MY_PARAMS_TARGET} APPEND_STRING PROPERTY LINK_FLAGS " ${_FLAGS} --scatter=${_FILE_PATH}") |
| 131 | set_property(TARGET ${_MY_PARAMS_TARGET} APPEND PROPERTY LINK_DEPENDS ${_FILE_PATH}) |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 132 | #Tell cmake .map files shall be removed when project is cleaned (make clean) |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 133 | get_filename_component(_TARGET_BASE_NAME ${_MY_PARAMS_TARGET} NAME_WE) |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 134 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 135 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${_TARGET_BASE_NAME}.map") |
| 136 | endfunction() |
| 137 | |
| 138 | function(compiler_set_cmse_output TARGET FILE_PATH) |
| 139 | #Note: the space before the option is important! |
| 140 | set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " --import_cmse_lib_out=${FILE_PATH}") |
| 141 | #Tell cmake cmse output is a generated object file. |
| 142 | SET_SOURCE_FILES_PROPERTIES("${FILE_PATH}" PROPERTIES EXTERNAL_OBJECT true GENERATED true) |
| 143 | #Tell cmake cmse output shall be removed by clean target. |
| 144 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 145 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${FILE_PATH}") |
| 146 | endfunction() |
| 147 | |
| 148 | function(compiler_merge_library) |
| 149 | set( _OPTIONS_ARGS ) #Option (on/off) arguments. |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 150 | set( _ONE_VALUE_ARGS DEST) #Single option arguments. |
| 151 | set( _MULTI_VALUE_ARGS LIBS) #List arguments |
| 152 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 153 | |
| 154 | #Check passed parameters |
| 155 | if(NOT _MY_PARAMS_DEST) |
| 156 | message(FATAL_ERROR "embedded_merge_library: no destination library target specified.") |
| 157 | endif() |
| 158 | #Check if destination is a target |
| 159 | if(NOT TARGET ${_MY_PARAMS_DEST}) |
| 160 | message(FATAL_ERROR "embedded_merge_library: parameter DEST must be a target already defined.") |
| 161 | endif() |
| 162 | #Check if destination is a library |
| 163 | get_target_property(_tmp ${_MY_PARAMS_DEST} TYPE) |
| 164 | if(NOT "${_tmp}" STREQUAL "STATIC_LIBRARY") |
| 165 | message(FATAL_ERROR "embedded_merge_library: parameter DEST must be a static library target.") |
| 166 | endif() |
| 167 | |
| 168 | #Check list if libraries to be merged |
| 169 | if(NOT _MY_PARAMS_LIBS) |
| 170 | message(FATAL_ERROR "embedded_merge_library: no source libraries specified. Please see the LIBS parameter.") |
| 171 | endif() |
| 172 | |
| 173 | #Mark each library file as a generated external object. This is needed to |
| 174 | #avoid error because CMake has no info how these can be built. |
| 175 | SET_SOURCE_FILES_PROPERTIES( |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 176 | ${_MY_PARAMS_LIBS} |
| 177 | PROPERTIES |
| 178 | EXTERNAL_OBJECT true |
| 179 | GENERATED true) |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 180 | |
| 181 | #Add additional input to target |
| 182 | target_sources(${_MY_PARAMS_DEST} PRIVATE ${_MY_PARAMS_LIBS}) |
| 183 | endfunction() |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 184 | |
| 185 | function(compiler_generate_binary_output TARGET) |
| 186 | add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_ARMCCLANG_FROMELF} ARGS --bincombined --output=$<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin $<TARGET_FILE:${TARGET}>) |
| 187 | endfunction() |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 188 | |
| 189 | # Function for creating a new target that preprocesses a .c file |
| 190 | #INPUTS: |
| 191 | # SRC - (mandatory) - file to be preprocessed |
| 192 | # DST - (mandatory) - output file for the preprocessing |
| 193 | # TARGET_PREFIX - (optional) - prefix for the target that this function creates and which manages the preprocessing |
| 194 | # BEFORE_TARGET - (optional) - target which is dependent on the preprocessing target in the below function |
David Vincze | a3e84c7 | 2019-06-27 16:33:08 +0200 | [diff] [blame] | 195 | # DEFINES - (optional) - additional command line switches from macro definitions for preprocessing |
| 196 | # INCLUDES - (optional) - additional command line switches from include paths for preprocessing |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 197 | function(compiler_preprocess_file) |
| 198 | #Option (on/off) arguments. |
| 199 | set( _OPTIONS_ARGS) |
| 200 | #Single option arguments. |
| 201 | set( _ONE_VALUE_ARGS SRC DST TARGET_PREFIX BEFORE_TARGET) |
| 202 | #List arguments |
David Vincze | a3e84c7 | 2019-06-27 16:33:08 +0200 | [diff] [blame] | 203 | set( _MULTI_VALUE_ARGS DEFINES INCLUDES) |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 204 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 205 | |
| 206 | #Check passed parameters |
| 207 | if(NOT DEFINED _MY_PARAMS_SRC) |
| 208 | message(FATAL_ERROR "compiler_preprocess_file: mandatory parameter 'SRC' is missing.") |
| 209 | endif() |
| 210 | |
| 211 | if(NOT DEFINED _MY_PARAMS_DST) |
| 212 | message(FATAL_ERROR "compiler_preprocess_file: mandatory parameter 'DST' is missing.") |
| 213 | endif() |
| 214 | |
| 215 | if(DEFINED _MY_PARAMS_BEFORE_TARGET) |
| 216 | if(NOT TARGET ${_MY_PARAMS_BEFORE_TARGET}) |
| 217 | message(FATAL_ERROR "compiler_preprocess_file: optional parameter 'BEFORE_TARGET' is not target.") |
| 218 | endif() |
| 219 | endif() |
| 220 | |
David Vincze | a3e84c7 | 2019-06-27 16:33:08 +0200 | [diff] [blame] | 221 | #Compose additional command line switches from macro definitions. |
| 222 | set(_FLAGS "") |
| 223 | if (_MY_PARAMS_DEFINES) |
| 224 | foreach(_DEFINE IN LISTS _MY_PARAMS_DEFINES) |
| 225 | list(APPEND _FLAGS "-D${_DEFINE}") |
| 226 | endforeach() |
| 227 | endif() |
| 228 | |
| 229 | #Compose additional command line switches from include paths. |
| 230 | if (_MY_PARAMS_INCLUDES) |
| 231 | foreach(_INCLUDE IN LISTS _MY_PARAMS_INCLUDES) |
| 232 | list(APPEND _FLAGS "-I${_INCLUDE}") |
| 233 | endforeach() |
| 234 | endif() |
| 235 | |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 236 | #The compiler flag might contain leading spaces which can fail the preprocess operation, these are removed |
| 237 | STRING(STRIP ${CMAKE_C_FLAGS_CPU} _MY_TEMP_CMAKE_C_FLAGS_CPU) |
| 238 | #If a string contains spaces, then it is inserted amongst quotation marks. Furthermore the compiler fails if it is |
| 239 | #called with multiple switches included in one quotation mark. If the extra spaces are replaced by semicolons, |
| 240 | #then the insertion will be advantageous for the compiler. |
| 241 | STRING(REPLACE " " ";" _MY_TEMP2_CMAKE_C_FLAGS_CPU ${_MY_TEMP_CMAKE_C_FLAGS_CPU}) |
| 242 | set(_LOCAL_CMAKE_C_FLAGS_CPU "") |
| 243 | foreach(_C_FLAG IN LISTS _MY_TEMP2_CMAKE_C_FLAGS_CPU) |
| 244 | list(APPEND _LOCAL_CMAKE_C_FLAGS_CPU "${_C_FLAG}") |
| 245 | endforeach() |
| 246 | |
| 247 | add_custom_command(OUTPUT ${_MY_PARAMS_DST} |
David Vincze | a3e84c7 | 2019-06-27 16:33:08 +0200 | [diff] [blame] | 248 | COMMAND ${CMAKE_C_COMPILER} ${_LOCAL_CMAKE_C_FLAGS_CPU} -E -P -xc ${_FLAGS} ${_MY_PARAMS_SRC} -o ${_MY_PARAMS_DST} |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 249 | DEPENDS ${_MY_PARAMS_SRC} |
| 250 | COMMENT "Preprocess the ${_MY_PARAMS_SRC} file" |
| 251 | ) |
| 252 | |
| 253 | set(_MY_TARGET_PREFIX "") |
| 254 | if(TARGET ${_MY_PARAMS_TARGET_PREFIX}) |
| 255 | set(_MY_TARGET_PREFIX "${_MY_PARAMS_TARGET_PREFIX}") |
| 256 | endif() |
| 257 | #The preprocessing related target name is obtained by indexing the file's name that is to be preprocessed |
| 258 | get_filename_component(_MY_FILENAME_TO_BE_INDEXED ${_MY_PARAMS_SRC} NAME_WE) |
| 259 | foreach(_SUFFIX RANGE 1 100) |
| 260 | if (NOT TARGET ${_MY_TARGET_PREFIX}_pp_${_MY_FILENAME_TO_BE_INDEXED}_${_SUFFIX}) |
| 261 | set(_PREPROCESS_TARGET_NAME "${_MY_TARGET_PREFIX}_pp_${_MY_FILENAME_TO_BE_INDEXED}_${_SUFFIX}") |
| 262 | break() |
| 263 | endif() |
| 264 | if (_SUFFIX EQUAL 100) |
| 265 | message(FATAL_ERROR "You have called 'compiler_preprocess_file' too many times (${_SUFFIX} function calls).") |
| 266 | endif() |
| 267 | endforeach() |
| 268 | |
| 269 | #Make the original target depend on the new one. |
| 270 | if(TARGET ${_MY_PARAMS_BEFORE_TARGET}) |
| 271 | add_custom_target(${_PREPROCESS_TARGET_NAME} DEPENDS ${_MY_PARAMS_DST}) |
| 272 | add_dependencies(${_MY_PARAMS_BEFORE_TARGET} ${_PREPROCESS_TARGET_NAME}) |
| 273 | else() |
| 274 | add_custom_target(${_PREPROCESS_TARGET_NAME} ALL DEPENDS ${_MY_PARAMS_DST}) |
| 275 | endif() |
| 276 | endfunction() |