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