Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame^] | 1 | # |
| 2 | # SPDX-License-Identifier: BSD-3-Clause |
| 3 | # SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | # |
| 5 | |
| 6 | #[=======================================================================[.rst: |
| 7 | ArmTargetLinkerScript |
| 8 | --------------------- |
| 9 | |
| 10 | .. default-domain:: cmake |
| 11 | |
| 12 | .. command:: arm_target_linker_script |
| 13 | |
| 14 | Set the linker script for a target. |
| 15 | |
| 16 | .. code:: cmake |
| 17 | |
| 18 | arm_target_linker_script(<target> <script>) |
| 19 | |
| 20 | Sets the linker script of the target ``<target>`` to the script ``<script>``, |
| 21 | which is first preprocessed with the C preprocessor. |
| 22 | |
| 23 | Properties for the linker script target may be set on `<target>-lds`. |
| 24 | |
| 25 | Example usage: |
| 26 | |
| 27 | .. code:: cmake |
| 28 | |
| 29 | add_executable(my-executable "main.c") |
| 30 | |
| 31 | arm_target_linker_script(my-executable "linker.lds") |
| 32 | |
| 33 | set_target_properties(my-executable-lds |
| 34 | PROPERTIES COMPILE_DEFINITIONS "__LINKER__") |
| 35 | |
| 36 | .. note:: |
| 37 | |
| 38 | When preprocessing, the linker script given to this macro automatically |
| 39 | inherits :variable:`CMAKE_C_FLAGS <variable:CMAKE_<LANG>_FLAGS>` and |
| 40 | :variable:`CMAKE_C_FLAGS_<CONFIG> <variable:CMAKE_<LANG>_FLAGS_<CONFIG>>`. |
| 41 | |
| 42 | It also inherits the following properties from the target ``<target>``: |
| 43 | |
| 44 | - :prop_tgt:`COMPILE_OPTIONS <prop_tgt:COMPILE_OPTIONS>` |
| 45 | - :prop_tgt:`COMPILE_DEFINITIONS <prop_tgt:COMPILE_DEFINITIONS>` |
| 46 | - :prop_tgt:`INCLUDE_DIRECTORIES <prop_tgt:INCLUDE_DIRECTORIES>` |
| 47 | #]=======================================================================] |
| 48 | |
| 49 | include_guard() |
| 50 | |
| 51 | include(ArmPreprocessSource) |
| 52 | |
| 53 | macro(arm_target_linker_script target script) |
| 54 | set(subtarget "${target}-lds") |
| 55 | |
| 56 | # |
| 57 | # Preprocess the linker script before doing anything else. |
| 58 | # |
| 59 | |
| 60 | arm_preprocess_source(${subtarget} "${script}") |
| 61 | |
| 62 | set_target_properties(${subtarget} |
| 63 | PROPERTIES |
| 64 | COMPILE_OPTIONS |
| 65 | "$<TARGET_PROPERTY:${target},COMPILE_OPTIONS>" |
| 66 | COMPILE_DEFINITIONS |
| 67 | "$<TARGET_PROPERTY:${target},COMPILE_DEFINITIONS>" |
| 68 | INCLUDE_DIRECTORIES |
| 69 | "$<TARGET_PROPERTY:${target},INCLUDE_DIRECTORIES>") |
| 70 | |
| 71 | # |
| 72 | # Add the linker script to the dependencies of the target. |
| 73 | # |
| 74 | |
| 75 | add_dependencies(${target} ${subtarget}) |
| 76 | |
| 77 | set(location "$<TARGET_PROPERTY:${subtarget},LOCATION_$<CONFIG>>") |
| 78 | |
| 79 | set_target_properties(${target} |
| 80 | PROPERTIES INTERFACE_LINK_DEPENDS "${location}") |
| 81 | |
| 82 | if(CMAKE_C_COMPILER_ID STREQUAL "ARMClang") |
| 83 | target_link_options(${target} |
| 84 | PUBLIC "LINKER:--scatter" "LINKER:${location}") |
| 85 | else() |
| 86 | target_link_options(${target} |
| 87 | PUBLIC "LINKER:-T" "LINKER:${location}") |
| 88 | endif() |
| 89 | endmacro() |