Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2017, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #This file defines project-specific overrides to cmake default behaviour for: |
| 9 | # * compiler detection |
| 10 | # * target system detection |
| 11 | cmake_minimum_required(VERSION 3.3) #IN_LIST was introduced in 3.3 |
| 12 | cmake_policy(SET CMP0057 NEW) |
| 13 | |
| 14 | Include(CMakeParseArguments) |
| 15 | |
| 16 | #The CMAKE_SYSTEM_XXX settings make cmake to stop applying some target system specific settings. |
| 17 | #Tell cmake we are compiling for ARM chips. |
| 18 | set (CMAKE_SYSTEM_PROCESSOR "arm" CACHE INTERNAL "Set target processor type to force cmake include some of our scripts." FORCE) |
| 19 | #Tell cmake this is an "Embedded" system |
| 20 | set(CMAKE_SYSTEM_NAME "Embedded" CACHE INTERNAL "Set target system name to force cmake include some of our scripts." FORCE) |
| 21 | |
| 22 | #Stop built in CMakeDetermine<lang>.cmake scripts to run. |
| 23 | set (CMAKE_CXX_COMPILER_ID_RUN 1) |
| 24 | #Stop cmake run compiler tests. |
| 25 | set (CMAKE_CXX_COMPILER_FORCED true) |
| 26 | #Stop built in CMakeDetermine<lang>.cmake scripts to run. |
| 27 | set (CMAKE_C_COMPILER_ID_RUN 1) |
| 28 | #Stop cmake run compiler tests. |
| 29 | set (CMAKE_C_COMPILER_FORCED true) |
| 30 | |
| 31 | #This macro is used to enforce the ARM project structure. |
| 32 | #Inputs: (This macro uses some "global" variables) |
| 33 | # global variable PROJ_CONFIG - a global configuration file to be used by all projects. |
| 34 | # It overrides value of CONFIG parameter. |
| 35 | # CONFIG - the configuration file which shall be used |
| 36 | #Examples |
| 37 | # To use global project config: |
| 38 | # embedded_project_start() |
| 39 | # To use config file relative to the top level CmakeLists.txt: |
| 40 | # embedded_project_start(./ConfigDefault.cmake) |
| 41 | # To use config file relative to the CmakeLists.txt file where this macro is used: |
| 42 | # embedded_project_start(${CMAKE_CURRENT_LIST_DIR}/ConfigDefault.cmake) |
| 43 | macro(embedded_project_start) |
| 44 | #Default project configuration file |
| 45 | if (DEFINED PROJ_CONFIG) #Take the global setting as default value |
| 46 | set(_PROJ_CONFIG ${PROJ_CONFIG}) |
| 47 | endif() |
| 48 | |
| 49 | set( _OPTIONS_ARGS ) #No option (on/off) arguments |
| 50 | set( _ONE_VALUE_ARGS CONFIG) #Single option arguments (e.g. PROJ_NAME "bubu_project") |
| 51 | set( _MULTI_VALUE_ARGS ) #One list argument (e.g. LANGUAGES C ASM CXX) |
| 52 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 53 | |
| 54 | #Cehck passed parameters |
| 55 | if(NOT _MY_PARAMS_CONFIG) |
| 56 | if(NOT DEFINED _PROJ_CONFIG) |
| 57 | set(_PROJ_CONFIG "./ConfigDefault.cmake") |
| 58 | message(STATUS "embedded_project_start: no project configuration file defined, falling back to default.") |
| 59 | endif() |
| 60 | elseif(NOT DEFINED PROJ_CONFIG) |
| 61 | set(_PROJ_CONFIG ${_MY_PARAMS_CONFIG}) |
| 62 | endif() |
| 63 | |
| 64 | get_filename_component(_ABS_PROJ_CONFIG ${_PROJ_CONFIG} ABSOLUTE) |
| 65 | message( STATUS "embedded_project_start: using project specific config file (PROJ_CONFIG = ${_ABS_PROJ_CONFIG})") |
| 66 | include("${_PROJ_CONFIG}") |
| 67 | endmacro() |
| 68 | |
| 69 | #Override CMake default behaviour |
| 70 | macro(embedded_project_fixup) |
| 71 | get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES) |
| 72 | if("CXX" IN_LIST languages) |
| 73 | include(Common/CompilerDetermineCXX) |
| 74 | #since all CMake "built in" scripts already executed, we need fo fix up some things here. |
| 75 | embedded_fixup_build_type_vars(CXX) |
| 76 | endif() |
| 77 | if("C" IN_LIST languages) |
| 78 | include(Common/CompilerDetermineC) |
| 79 | embedded_fixup_build_type_vars(C) |
| 80 | endif() |
| 81 | |
| 82 | #Merge CPU and configuration specific compiler and linker flags. |
| 83 | foreach(LNG ${languages}) |
| 84 | #Apply CPU specific and configuration specific compile flags. |
| 85 | if(NOT CMAKE_${LNG}_FLAGS MATCHES ".*${CMAKE_${LNG}_FLAGS_CPU}.*") |
| 86 | set(CMAKE_${LNG}_FLAGS "${CMAKE_${LNG}_FLAGS} ${CMAKE_${LNG}_FLAGS_CPU}") |
| 87 | endif() |
| 88 | #Fix output file extension. |
| 89 | set (CMAKE_EXECUTABLE_SUFFIX_${LNG} ".axf") |
| 90 | unset(ALL_SRC_${LNG}) |
| 91 | unset(ALL_SRC_${LNG}_S) |
| 92 | unset(ALL_SRC_${LNG}_NS) |
| 93 | endforeach() |
| 94 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_LINK_FLAGS_CPU}") |
| 95 | endmacro() |
| 96 | |
| 97 | #Allow project specific script to do configuration after all targets are specified; it has to be done for each target defined |
| 98 | macro (embedded_project_end TARGET) |
| 99 | get_property(_MAC DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY MACROS) |
| 100 | if ("embedded_project_build_config_apply" IN_LIST _MAC) |
| 101 | message(WARNING "embedded_project_end(): macro embedded_project_build_config_apply() is defined. Your build config file may be out-dated.") |
| 102 | endif() |
| 103 | |
| 104 | #Apply compile flags. |
| 105 | _embedded_apply_compile_flags(${TARGET}) |
| 106 | #Apply macro definitions |
| 107 | _embedded_apply_compile_defines(${TARGET}) |
| 108 | #Apply include paths |
| 109 | _embedded_apply_include_directories(${TARGET}) |
| 110 | #Apply linker flags. |
| 111 | _embedded_apply_link_flags(${TARGET}) |
| 112 | #If target is executable, apply linker command file setting. |
| 113 | get_property(_TGT_TYPE TARGET ${TARGET} PROPERTY TYPE) |
| 114 | if(_TGT_TYPE STREQUAL "EXECUTABLE") |
| 115 | _embedded_apply_linker_cmd_file_setting(${TARGET}) |
| 116 | endif() |
| 117 | endmacro() |
| 118 | |
| 119 | macro(embedded_fixup_build_type_vars LANG) |
| 120 | #since all CMake "built in" scripts already executed, we need fo fix up some things here. |
| 121 | set (CMAKE_${LANG}_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG_INIT}" CACHE STRING "Flags used by the compiler during debug builds." FORCE) |
| 122 | set (CMAKE_${LANG}_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL_INIT}" CACHE STRING "Flags used by the compiler during release builds for minimum size." FORCE) |
| 123 | set (CMAKE_${LANG}_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_INIT}" CACHE STRING "Flags used by the compiler during release builds." FORCE) |
| 124 | set (CMAKE_${LANG}_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "Flags used by the compiler during release builds with debug info." FORCE) |
| 125 | endmacro() |
| 126 | |
| 127 | |
| 128 | #Convert a CMake list to a string |
| 129 | # |
| 130 | #Examples: |
| 131 | # list_to_string(my_string 1 2 3 4) |
| 132 | # list_to_string(my_string ${CMAKE_C_FLAGS}) |
| 133 | # |
| 134 | #INPUTS: |
| 135 | # RES - (mandatory) - name of variable to put result in |
| 136 | # The list to be converted. |
| 137 | # |
| 138 | #OUTPUTS |
| 139 | # List items concatenated to a string. |
| 140 | # |
| 141 | function(list_to_string RES) |
| 142 | foreach(_ITEM ${ARGN}) |
| 143 | set(_RES "${_RES} ${_ITEM}") |
| 144 | endforeach() |
| 145 | set(${RES} "${_RES}" PARENT_SCOPE) |
| 146 | endfunction() |
| 147 | |
| 148 | #Ensure current generator is supported. |
| 149 | # |
| 150 | # This function takes a list of supported generators (e.g. "Unix Makefiles") and |
| 151 | # exits with fatal error is the current generator is not on the list. |
| 152 | #Examples: |
| 153 | # assert_generator_is("MSYS Makefiles" "MinGW Makefiles") |
| 154 | # |
| 155 | #INPUTS: |
| 156 | # The list of supported generators. |
| 157 | # |
| 158 | #OUTPUTS |
| 159 | # n/a |
| 160 | # |
| 161 | function(assert_generator_is) |
| 162 | if (NOT CMAKE_GENERATOR IN_LIST ARGN) |
| 163 | message(FATAL_ERROR "assert_generator_is(): Generator '${CMAKE_GENERATOR}' is not on the list of supported generators.") |
| 164 | endif() |
| 165 | endfunction() |
| 166 | |
| 167 | #Specify an include path for the compiler |
| 168 | # |
| 169 | # Specify a global include directory for all non external targets in the current |
| 170 | # build. |
| 171 | # The parameter ABSOLUTE can be set to true to ask CMake to convert the PATH to |
| 172 | # absolute. This gives better looking command line arguments, and also helps |
| 173 | # removing duplicates. |
| 174 | # |
| 175 | #Examples: |
| 176 | # embedded_include_directories(PATH "C:/fo/bar/include") |
| 177 | # embedded_include_directories(PATH "C:/fo/bar/include" ABSOLUTE) |
| 178 | # |
| 179 | #INPUTS: |
| 180 | # PATH - (mandatory) - the include path to add |
| 181 | # ABSOLUTE - (optional) - whether the path shall be converted to absolute |
| 182 | # |
| 183 | #OUTPUTS |
| 184 | # Modified list of include directories. |
| 185 | # |
| 186 | function (embedded_include_directories) |
| 187 | #Parse our arguments |
| 188 | set( _OPTIONS_ARGS ABSOLUTE) #Option (on/off) arguments (e.g. IGNORE_CASE) |
| 189 | set( _ONE_VALUE_ARGS PATH) #Single option arguments (e.g. PATH "./foo/bar") |
| 190 | set( _MULTI_VALUE_ARGS ) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 191 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 192 | |
| 193 | #Check mandatory parameters |
| 194 | if(NOT _MY_PARAMS_PATH) |
| 195 | failure("embedded_include_directories(): Missing PATH parameter!") |
| 196 | endif() |
| 197 | |
| 198 | if(DEFINED _MY_PARAMS_ABSOLUTE AND ${_MY_PARAMS_ABSOLUTE}) |
| 199 | get_filename_component(_MY_PARAMS_PATH ${_MY_PARAMS_PATH} ABSOLUTE) |
| 200 | endif() |
| 201 | |
| 202 | include_directories(${_MY_PARAMS_PATH}) |
| 203 | endfunction() |
| 204 | |
| 205 | #Return the language of a source file. |
| 206 | # |
| 207 | # Language is either specified by the LANGUAGE property of the source file or |
| 208 | # determined based on the extension of the file. |
| 209 | # Search is limited for languages enabled in the current project. |
| 210 | # |
| 211 | #Examples: |
| 212 | # To get language of "foo/bar.c" written into _LNG: |
| 213 | # embedded_get_source_language("foo/bar.c" _LNG) |
| 214 | # |
| 215 | #INPUTS: |
| 216 | # FILE - (mandatory) - The file to determine language of. |
| 217 | # RES - (mandatory) - Name of the variable to write the result into. |
| 218 | # |
| 219 | #OUTPUTS |
| 220 | # ${RES} - language string (e.g. "C", "CXX", "ASM"). empty string for files |
| 221 | # with no language. |
| 222 | # |
| 223 | function(embedded_get_source_language FILE RES) |
| 224 | if (ARGN) |
| 225 | message(FATAL_ERROR "embedded_get_source_language(): too many parameters passed.") |
| 226 | endif() |
| 227 | #If language property is set, use that. |
| 228 | get_property(_LNG SOURCE ${_SRC} PROPERTY LANGUAGE) |
| 229 | if (NOT ${_LNG} STREQUAL "") |
| 230 | set(${RES} ${_LNG} PARENT_SCOPE) |
| 231 | else() |
| 232 | #Set empty return value. |
| 233 | set(${RES} "" PARENT_SCOPE) |
| 234 | #Property not set, use extension of the file to determine |
| 235 | #language. |
| 236 | string(REGEX MATCH "[^.]*$" _EXT "${_SRC}") |
| 237 | #Get list of enabled languages. |
| 238 | get_property(_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) |
| 239 | foreach(_LNG ${_LANGUAGES}) |
| 240 | #See if the extension is contained in the list of file extensions |
| 241 | #of this language. |
| 242 | if(_EXT IN_LIST CMAKE_${_LNG}_SOURCE_FILE_EXTENSIONS) |
| 243 | set(${RES} ${_LNG} PARENT_SCOPE) |
| 244 | break() |
| 245 | endif() |
| 246 | endforeach() |
| 247 | endif() |
| 248 | endfunction() |
| 249 | |
| 250 | #Set compilation flags for the specified target. |
| 251 | # |
| 252 | # Store compilation flags for the specified target and language pair. Flags are |
| 253 | # stored in a global property and will |
| 254 | # be applied to source files in the current directory and sub-directories. |
| 255 | # Property name must follow a specific scheme (see outputs). |
| 256 | # See: _embedded_apply_compile_flags() |
| 257 | # |
| 258 | #Examples: |
| 259 | # embedded_set_target_compile_flags(my_app C "-fchar-unsigned") |
| 260 | # embedded_set_target_compile_flags(my_lib CXX "-fchar-unsigned") |
| 261 | # embedded_set_target_compile_flags(my_app ASM "-fchar-unsigned") |
| 262 | # |
| 263 | #INPUTS: |
| 264 | # TARGET - (mandatory) - The target to apply settings to. |
| 265 | # LANGUAGE - (mandatory) - Programming language of source files settings shall |
| 266 | # be applied to. |
| 267 | # FLAGS - (mandatory) - List with the compiler flags. |
| 268 | # APPEND - (optional) - True if FLAGS shall be appended. |
| 269 | # |
| 270 | #OUTPUTS |
| 271 | # Directory property EMBEDDED_COMPILE_FLAGS_TTT_LLL is set, where TTT is the |
| 272 | # target name, and LLL is the language. |
| 273 | # |
| 274 | function (embedded_set_target_compile_flags) |
| 275 | set( _OPTIONS_ARGS APPEND) #Option (on/off) arguments (e.g. IGNORE_CASE) |
| 276 | set( _ONE_VALUE_ARGS TARGET LANGUAGE) #Single option arguments (e.g. PATH "./foo/bar") |
| 277 | set( _MULTI_VALUE_ARGS FLAGS) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 278 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 279 | |
| 280 | if (NOT DEFINED _MY_PARAMS_TARGET) |
| 281 | message(FATAL_ERROR "embedded_set_target_compile_flags(): mandatory parameter 'TARGET' missing.") |
| 282 | endif() |
| 283 | |
| 284 | if (NOT DEFINED _MY_PARAMS_LANGUAGE) |
| 285 | message(FATAL_ERROR "embedded_set_target_compile_flags(): mandatory parameter 'LANGUAGE' missing.") |
| 286 | endif() |
| 287 | |
| 288 | if (NOT DEFINED _MY_PARAMS_FLAGS) |
| 289 | message(FATAL_ERROR "embedded_set_target_compile_flags(): mandatory parameter 'FLAGS' missing.") |
| 290 | endif() |
| 291 | |
| 292 | if (_MY_PARAMS_APPEND) |
| 293 | set_property(GLOBAL APPEND PROPERTY EMBEDDED_COMPILE_FLAGS_${_MY_PARAMS_TARGET}_${_MY_PARAMS_LANGUAGE} ${_MY_PARAMS_FLAGS}) |
| 294 | else() |
| 295 | set_property(GLOBAL PROPERTY EMBEDDED_COMPILE_FLAGS_${_MY_PARAMS_TARGET}_${_MY_PARAMS_LANGUAGE} ${_MY_PARAMS_FLAGS}) |
| 296 | endif() |
| 297 | endfunction() |
| 298 | |
| 299 | #Apply compilation flag settings for the specified target. |
| 300 | # |
| 301 | # Compilation flags stored in a global property and are applied to source files. |
| 302 | # Note: |
| 303 | # - Directory property name must follow a specific scheme. |
| 304 | # - This is an internal function. |
| 305 | # - This function only supports make and ninja generators. |
| 306 | # |
| 307 | # See: embedded_set_target_compile_flags() |
| 308 | # |
| 309 | #Examples: |
| 310 | # _embedded_apply_compile_flags(my_app) |
| 311 | # |
| 312 | #INPUTS: |
| 313 | # TARGET - (mandatory) - The target to apply settings to. |
| 314 | # Directory property - (optional) - Flags to apply. |
| 315 | # |
| 316 | #OUTPUTS |
| 317 | # n/a |
| 318 | # |
| 319 | function(_embedded_apply_compile_flags TARGET) |
| 320 | #Check if the parameter is a target. |
| 321 | if(NOT TARGET ${TARGET}) |
| 322 | message(FATAL_ERROR "_embedded_apply_compile_flags(): target '${TARGET}' is not defined.") |
| 323 | endif() |
| 324 | #Get list of enabled languages. |
| 325 | get_property(_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) |
| 326 | foreach(_LNG ${_LANGUAGES}) |
| 327 | #Get the flags for this language. |
| 328 | get_property(_FLAGS GLOBAL PROPERTY EMBEDDED_COMPILE_FLAGS_${TARGET}_${_LNG}) |
| 329 | |
| 330 | #The generator expression below is only supported by the make and ninja |
| 331 | #generators. |
| 332 | assert_generator_is(_GENERATORS_OK "MSYS Makefiles" "MinGW Makefiles" "NMake Makefiles" "NMake Makefiles JOM" "Unix Makefiles" "Watcom WMake" "Ninja") |
| 333 | target_compile_options(${TARGET} PRIVATE $<$<COMPILE_LANGUAGE:${_LNG}>:${_FLAGS}>) |
| 334 | endforeach() |
| 335 | endfunction() |
| 336 | |
| 337 | #Set compilation defines for the specified target. |
| 338 | # |
| 339 | # Store compilation defines for the specified target and language pair. Macros |
| 340 | # are stored in a global property and will |
| 341 | # be applied to source files in the current directory and sub-directories. |
| 342 | # Property name must follow a specific scheme (see outputs). |
| 343 | # See: _embedded_apply_compile_defines() |
| 344 | # |
| 345 | #Examples: |
| 346 | # embedded_set_target_compile_defines(my_app C "FOO BAR=1") |
| 347 | # embedded_set_target_compile_defines(my_lib CXX "FOO BAR=1") |
| 348 | # embedded_set_target_compile_defines(my_app ASM "FOO BAR=1") |
| 349 | # |
| 350 | #INPUTS: |
| 351 | # TARGET - (mandatory) - The target to apply settings to. |
| 352 | # LANGUAGE - (mandatory) - Programming language of source files settings shall |
| 353 | # be applied to. |
| 354 | # DEFINES - (mandatory) - List with the compiler flags. |
| 355 | # APPEND - (optional) - Present if FLAGS shall be appended. |
| 356 | # |
| 357 | #OUTPUTS |
| 358 | # Directory property EMBEDDED_COMPILE_DEFINES_TTT_LLL is set, where TTT is the |
| 359 | # target name, and LLL is the language. |
| 360 | # |
| 361 | function (embedded_set_target_compile_defines) |
| 362 | set( _OPTIONS_ARGS APPEND) #Option (on/off) arguments (e.g. IGNORE_CASE) |
| 363 | set( _ONE_VALUE_ARGS TARGET LANGUAGE) #Single option arguments (e.g. PATH "./foo/bar") |
| 364 | set( _MULTI_VALUE_ARGS DEFINES) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 365 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 366 | |
| 367 | if (NOT DEFINED _MY_PARAMS_TARGET) |
| 368 | message(FATAL_ERROR "embedded_set_target_compile_defines(): mandatory parameter 'TARGET' missing.") |
| 369 | endif() |
| 370 | |
| 371 | if (NOT DEFINED _MY_PARAMS_LANGUAGE) |
| 372 | message(FATAL_ERROR "embedded_set_target_compile_defines(): mandatory parameter 'LANGUAGE' missing.") |
| 373 | endif() |
| 374 | |
| 375 | if (NOT DEFINED _MY_PARAMS_DEFINES) |
| 376 | message(FATAL_ERROR "embedded_set_target_compile_defines(): mandatory parameter 'DEFINES' missing.") |
| 377 | endif() |
| 378 | |
| 379 | if (_MY_PARAMS_APPEND) |
| 380 | set_property(GLOBAL APPEND PROPERTY EMBEDDED_COMPILE_DEFINES_${_MY_PARAMS_TARGET}_${_MY_PARAMS_LANGUAGE} ${_MY_PARAMS_DEFINES}) |
| 381 | else() |
| 382 | set_property(GLOBAL PROPERTY EMBEDDED_COMPILE_DEFINES_${_MY_PARAMS_TARGET}_${_MY_PARAMS_LANGUAGE} ${_MY_PARAMS_DEFINES}) |
| 383 | endif() |
| 384 | endfunction() |
| 385 | |
| 386 | #Apply compilation defines for the specified target. |
| 387 | # |
| 388 | # Macro definitions are stored in a global property and are applied to |
| 389 | # source files. |
| 390 | # |
| 391 | # Note: |
| 392 | # - Directory property name must follow a specific scheme. |
| 393 | # - This is an internal function. |
| 394 | # - This function only supports make and ninja generators. |
| 395 | # |
| 396 | # See: embedded_set_target_compile_defines() |
| 397 | # |
| 398 | #Examples: |
| 399 | # _embedded_apply_compile_defines(my_app) |
| 400 | # |
| 401 | #INPUTS: |
| 402 | # TARGET - (mandatory) - The target to apply settings to. |
| 403 | # Directory property - (optional) - Flags to apply. |
| 404 | # |
| 405 | #OUTPUTS |
| 406 | # n/a |
| 407 | # |
| 408 | function(_embedded_apply_compile_defines TARGET) |
| 409 | #Check if the parameter is a target. |
| 410 | if(NOT TARGET ${TARGET}) |
| 411 | message(FATAL_ERROR "_embedded_apply_compile_defines(): target '${TARGET}' is not defined.") |
| 412 | endif() |
| 413 | #Get list of enabled languages. |
| 414 | get_property(_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) |
| 415 | foreach(_LNG ${_LANGUAGES}) |
| 416 | #Get the flags for this language. |
| 417 | get_property(_FLAGS GLOBAL PROPERTY EMBEDDED_COMPILE_DEFINES_${TARGET}_${_LNG}) |
| 418 | #The generator expression below is only supported by the make and ninja |
| 419 | #generators. |
| 420 | assert_generator_is(_GENERATORS_OK "MSYS Makefiles" "MinGW Makefiles" "NMake Makefiles" "NMake Makefiles JOM" "Unix Makefiles" "Watcom WMake" "Ninja") |
| 421 | target_compile_definitions(${TARGET} PRIVATE $<$<COMPILE_LANGUAGE:${_LNG}>:${_FLAGS}>) |
| 422 | endforeach() |
| 423 | endfunction() |
| 424 | |
| 425 | #Specify an include path for the compiler affecting a specific build target (all |
| 426 | # languages). |
| 427 | # |
| 428 | # Store include paths for the specified target. PATH is stored in a global |
| 429 | # property. The property name must follow a specific scheme (see outputs). |
| 430 | # See: _embedded_apply_include_directories() |
| 431 | # |
| 432 | #Examples: |
| 433 | # embedded_target_include_directories(TARGET foo PATH "C:/fo/bar/include") |
| 434 | # embedded_target_include_directories(TARGET foo PATH "C:/fo/bar/include" APPEND) |
| 435 | # embedded_target_include_directories(TARGET foo PATH "C:/fo/bar/include" ABSOLUTE) |
| 436 | # |
| 437 | #INPUTS: |
| 438 | # ABSOLUTE - (optional)- whether the path shall be converted to absolute |
| 439 | # APPEND - (optional) - if set append path to existing values |
| 440 | # PATH - (mandatory) - the include path to add |
| 441 | # TARGET - (mandatory) - name of target to apply settings to |
| 442 | # |
| 443 | #OUTPUTS |
| 444 | # Directory property EMBEDDED_COMPILE_INCLUDES_TTT is set, where TTT is |
| 445 | # the target name. |
| 446 | # |
| 447 | function (embedded_target_include_directories) |
| 448 | #Parse our arguments |
| 449 | set( _OPTIONS_ARGS ABSOLUTE APPEND) #Option (on/off) arguments (e.g. IGNORE_CASE) |
| 450 | set( _ONE_VALUE_ARGS PATH TARGET) #Single option arguments (e.g. PATH "./foo/bar") |
| 451 | set( _MULTI_VALUE_ARGS ) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 452 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 453 | |
| 454 | #Check mandatory parameters |
| 455 | if(NOT _MY_PARAMS_PATH) |
| 456 | failure("embedded_target_include_directories(): Missing PATH parameter!") |
| 457 | endif() |
| 458 | |
| 459 | if(NOT _MY_PARAMS_TARGET) |
| 460 | failure("embedded_target_include_directories(): Missing TARGET parameter!") |
| 461 | endif() |
| 462 | |
| 463 | if(DEFINED _MY_PARAMS_ABSOLUTE AND ${_MY_PARAMS_ABSOLUTE}) |
| 464 | get_filename_component(_MY_PARAMS_PATH ${_MY_PARAMS_PATH} ABSOLUTE) |
| 465 | endif() |
| 466 | |
| 467 | if (_MY_PARAMS_APPEND) |
| 468 | set_property(GLOBAL APPEND PROPERTY EMBEDDED_COMPILE_INCLUDES_${_MY_PARAMS_TARGET} ${_MY_PARAMS_PATH}) |
| 469 | else() |
| 470 | set_property(GLOBAL PROPERTY EMBEDDED_COMPILE_INCLUDES_${_MY_PARAMS_TARGET} ${_MY_PARAMS_PATH}) |
| 471 | endif() |
| 472 | endfunction() |
| 473 | |
| 474 | #Apply include path settings for the specified target. |
| 475 | # |
| 476 | # Include paths are stored in a global property and are applied to source files. |
| 477 | # Note: |
| 478 | # - Directory property name must follow a specific scheme. |
| 479 | # - This is an internal function. |
| 480 | # |
| 481 | # See: embedded_target_include_directories() |
| 482 | # |
| 483 | #Examples: |
| 484 | # _embedded_apply_include_directories(my_app) |
| 485 | # |
| 486 | #INPUTS: |
| 487 | # TARGET - (mandatory) - The target to apply settings to. |
| 488 | # Directory property - (optional) - Flags to apply. |
| 489 | # |
| 490 | #OUTPUTS |
| 491 | # n/a |
| 492 | # |
| 493 | function(_embedded_apply_include_directories TARGET) |
| 494 | #Check if the parameter is a target. |
| 495 | if(NOT TARGET ${TARGET}) |
| 496 | message(FATAL_ERROR "_embedded_apply_include_directories(): target '${TARGET}' is not defined.") |
| 497 | endif() |
| 498 | #Get the flags for this language. |
| 499 | get_property(_FLAGS GLOBAL PROPERTY EMBEDDED_COMPILE_INCLUDES_${TARGET}) |
| 500 | #If we have flags to apply for this language. |
| 501 | if (NOT _FLAGS STREQUAL "") |
| 502 | target_include_directories(${TARGET} PRIVATE ${_FLAGS}) |
| 503 | endif() |
| 504 | endfunction() |
| 505 | |
| 506 | #Set linker flags for the specified target. |
| 507 | # |
| 508 | # Store linker flags for the specified target in a global property. |
| 509 | # See: _embedded_apply_link_flags() |
| 510 | # |
| 511 | #Examples: |
| 512 | # embedded_set_target_link_flags(my_app "-M my_map_file.map") |
| 513 | # |
| 514 | #INPUTS: |
| 515 | # TARGET - (mandatory) - The target to apply settings to. |
| 516 | # FLAGS - (mandatory) - List with the compiler flags. |
| 517 | # APPEND - (optional) - True if FLAGS shall be appended. |
| 518 | # |
| 519 | #OUTPUTS |
| 520 | # Directory property EMBEDDED_LINKER_FLAGS_TTT is set, where TTT is the |
| 521 | # target name. |
| 522 | # |
| 523 | function(embedded_set_target_link_flags) |
| 524 | set( _OPTIONS_ARGS APPEND) #Option (on/off) arguments (e.g. IGNORE_CASE) |
| 525 | set( _ONE_VALUE_ARGS TARGET) #Single option arguments (e.g. PATH "./foo/bar") |
| 526 | set( _MULTI_VALUE_ARGS FLAGS) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 527 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 528 | |
| 529 | if (NOT DEFINED _MY_PARAMS_TARGET) |
| 530 | message(FATAL_ERROR "embedded_set_target_link_flags(): mandatory parameter 'TARGET' missing.") |
| 531 | endif() |
| 532 | |
| 533 | if (NOT DEFINED _MY_PARAMS_FLAGS) |
| 534 | message(FATAL_ERROR "embedded_set_target_link_flags(): mandatory parameter 'FLAGS' missing.") |
| 535 | endif() |
| 536 | |
| 537 | if (_MY_PARAMS_APPEND) |
| 538 | set_property(GLOBAL APPEND PROPERTY EMBEDDED_LINKER_FLAGS_${_MY_PARAMS_TARGET} ${_MY_PARAMS_FLAGS}) |
| 539 | else() |
| 540 | set_property(GLOBAL PROPERTY EMBEDDED_LINKER_FLAGS_${_MY_PARAMS_TARGET} ${_MY_PARAMS_FLAGS}) |
| 541 | endif() |
| 542 | endfunction() |
| 543 | |
| 544 | #Apply linker flags for the specified target. |
| 545 | # |
| 546 | # Linker flags stored in a global property are applied. |
| 547 | # |
| 548 | # Note: |
| 549 | # - Directory property name must follow a specific scheme. |
| 550 | # - This is an internal function. |
| 551 | # |
| 552 | # See: embedded_set_target_link_flags() |
| 553 | # |
| 554 | #Examples: |
| 555 | # _embedded_apply_link_flags(my_app) |
| 556 | # |
| 557 | #INPUTS: |
| 558 | # TARGET - (mandatory) - The target to apply settings to. |
| 559 | # Directory property - (optional) - Flags to apply. |
| 560 | # |
| 561 | #OUTPUTS |
| 562 | # n/a |
| 563 | # |
| 564 | function(_embedded_apply_link_flags TARGET) |
| 565 | #Check if the parameter is a target. |
| 566 | if(NOT TARGET ${TARGET}) |
| 567 | message(FATAL_ERROR "_embedded_apply_link_flags(): target '${TARGET}' is not defined.") |
| 568 | endif() |
| 569 | #Get the stored flags. |
| 570 | get_property(_FLAGS GLOBAL PROPERTY EMBEDDED_LINKER_FLAGS_${TARGET}) |
| 571 | #Apply flags if defined. |
| 572 | if (NOT _FLAGS STREQUAL "") |
| 573 | list_to_string(_STR_FLAGS ${_FLAGS}) |
| 574 | set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS ${_STR_FLAGS}) |
| 575 | endif() |
| 576 | endfunction() |
| 577 | |
| 578 | #Set linker command file for the specified target. |
| 579 | # |
| 580 | # Store path to linker command file for the specified target in a global |
| 581 | # property. |
| 582 | # |
| 583 | # See: _embedded_apply_linker_cmd_file_setting() |
| 584 | # |
| 585 | #Examples: |
| 586 | # embedded_set_target_linker_file(my_app "foo/my_linker_cmd.sct") |
| 587 | # |
| 588 | #INPUTS: |
| 589 | # TARGET - (mandatory) - The target to apply settings to. |
| 590 | # PATH - (mandatory) - Path to linker script. |
| 591 | # |
| 592 | #OUTPUTS |
| 593 | # Directory property EMBEDDED_LINKER_CMD_FILE_TTT is set, where TTT is the |
| 594 | # target name. |
| 595 | # |
| 596 | function(embedded_set_target_linker_file) |
| 597 | set( _OPTIONS_ARGS ) #Option (on/off) arguments (e.g. IGNORE_CASE) |
| 598 | set( _ONE_VALUE_ARGS TARGET PATH) #Single option arguments (e.g. PATH "./foo/bar") |
| 599 | set( _MULTI_VALUE_ARGS ) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 600 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 601 | |
| 602 | if (NOT DEFINED _MY_PARAMS_TARGET) |
| 603 | message(FATAL_ERROR "embedded_set_target_linker_file(): mandatory parameter 'TARGET' missing.") |
| 604 | endif() |
| 605 | |
| 606 | if (NOT DEFINED _MY_PARAMS_PATH) |
| 607 | message(FATAL_ERROR "embedded_set_target_linker_file(): mandatory parameter 'PATH' missing.") |
| 608 | endif() |
| 609 | |
| 610 | set_property(GLOBAL PROPERTY EMBEDDED_LINKER_CMD_FILE_${_MY_PARAMS_TARGET} ${_MY_PARAMS_PATH}) |
| 611 | endfunction() |
| 612 | |
| 613 | #Apply linker linker command file setting for the specified target. |
| 614 | # |
| 615 | # Path to linker command file stored in a global property is applied. |
| 616 | # |
| 617 | # Note: |
| 618 | # - Directory property name must follow a specific scheme. |
| 619 | # - This is an internal function. |
| 620 | # |
| 621 | # See: embedded_set_target_linker_file() |
| 622 | # |
| 623 | #Examples: |
| 624 | # _embedded_apply_linker_cmd_file_setting(my_app) |
| 625 | # |
| 626 | #INPUTS: |
| 627 | # TARGET - (mandatory) - The target to apply settings to. |
| 628 | # Directory property - (optional) - Flags to apply. |
| 629 | # |
| 630 | #OUTPUTS |
| 631 | # n/a |
| 632 | # |
| 633 | function(_embedded_apply_linker_cmd_file_setting TARGET) |
| 634 | #Check if the parameter is a target. |
| 635 | if(NOT TARGET ${TARGET}) |
| 636 | message(FATAL_ERROR "_embedded_apply_linker_cmd_file_setting(): target '${TARGET}' is not defined.") |
| 637 | endif() |
| 638 | #Check if target is an executable. |
| 639 | get_property(_TGT_TYPE TARGET ${TARGET} PROPERTY TYPE) |
| 640 | if(NOT _TGT_TYPE STREQUAL "EXECUTABLE") |
| 641 | message(FATAL_ERROR "_embedded_apply_linker_cmd_file_setting(): target '${TARGET}' is not an executable.") |
| 642 | endif() |
| 643 | |
| 644 | #Check if executable has a linker command file set. |
| 645 | get_property(_LINKER_CMD_FILE GLOBAL PROPERTY EMBEDDED_LINKER_CMD_FILE_${TARGET} SET) |
| 646 | if (NOT _LINKER_CMD_FILE) |
| 647 | message(FATAL_ERROR "_embedded_apply_linker_cmd_file_setting(): Please set linker command file for target '${TARGET}' using embedded_set_target_linker_file().") |
| 648 | endif() |
| 649 | #Get the path to the linker command file. |
| 650 | get_property(_LINKER_CMD_FILE GLOBAL PROPERTY EMBEDDED_LINKER_CMD_FILE_${TARGET}) |
| 651 | #Set the path |
| 652 | compiler_set_linkercmdfile(${TARGET} ${_LINKER_CMD_FILE}) |
| 653 | endfunction() |