cmake: Toolchain abstraction: Introduce toolchain_ld_relocation macro

No functional change expected.

This is motivated by the wish to abstract Zephyr's usage of toolchains,
permitting non-intrusive porting to other (commercial) toolchains.

Signed-off-by: Mark Ruvald Pedersen <mped@oticon.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 70e81d6..a4d8cfb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -750,25 +750,8 @@
 endif()
 
 if(CONFIG_CODE_DATA_RELOCATION)
-   set(MEM_RELOCATAION_LD "${PROJECT_BINARY_DIR}/include/generated/linker_relocate.ld")
-   set(MEM_RELOCATAION_CODE "${PROJECT_BINARY_DIR}/code_relocation.c")
-
-   add_custom_command(
-     OUTPUT ${MEM_RELOCATAION_CODE} ${MEM_RELOCATAION_LD}
-     COMMAND
-     ${PYTHON_EXECUTABLE}
-     ${ZEPHYR_BASE}/scripts/gen_relocate_app.py
-     $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
-     -d ${APPLICATION_BINARY_DIR}
-     -i '$<TARGET_PROPERTY:code_data_relocation_target,COMPILE_DEFINITIONS>'
-     -o ${MEM_RELOCATAION_LD}
-     -c ${MEM_RELOCATAION_CODE}
-     DEPENDS app kernel ${ZEPHYR_LIBS_PROPERTY}
-     )
-
-   add_library(code_relocation_source_lib  STATIC ${MEM_RELOCATAION_CODE})
-   target_link_libraries(code_relocation_source_lib zephyr_interface)
-
+  # @Intent: Linker script to relocate .text, data and .bss sections
+  toolchain_ld_relocation()
 endif()
 
 if(CONFIG_USERSPACE)
diff --git a/cmake/linker/ld/target.cmake b/cmake/linker/ld/target.cmake
index 0c412a4..b2fcce3 100644
--- a/cmake/linker/ld/target.cmake
+++ b/cmake/linker/ld/target.cmake
@@ -57,3 +57,4 @@
 include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_base.cmake)
 include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_baremetal.cmake)
 include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_cpp.cmake)
+include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_relocation.cmake)
diff --git a/cmake/linker/ld/target_relocation.cmake b/cmake/linker/ld/target_relocation.cmake
new file mode 100644
index 0000000..a2ce748
--- /dev/null
+++ b/cmake/linker/ld/target_relocation.cmake
@@ -0,0 +1,24 @@
+# SPDX-License-Identifier: Apache-2.0
+
+# See root CMakeLists.txt for description and expectations of these macros
+
+macro(toolchain_ld_relocation)
+  set(MEM_RELOCATAION_LD   "${PROJECT_BINARY_DIR}/include/generated/linker_relocate.ld")
+  set(MEM_RELOCATAION_CODE "${PROJECT_BINARY_DIR}/code_relocation.c")
+
+  add_custom_command(
+    OUTPUT ${MEM_RELOCATAION_CODE} ${MEM_RELOCATAION_LD}
+    COMMAND
+    ${PYTHON_EXECUTABLE}
+    ${ZEPHYR_BASE}/scripts/gen_relocate_app.py
+    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
+    -d ${APPLICATION_BINARY_DIR}
+    -i '$<TARGET_PROPERTY:code_data_relocation_target,COMPILE_DEFINITIONS>'
+    -o ${MEM_RELOCATAION_LD}
+    -c ${MEM_RELOCATAION_CODE}
+    DEPENDS app kernel ${ZEPHYR_LIBS_PROPERTY}
+    )
+
+  add_library(code_relocation_source_lib  STATIC ${MEM_RELOCATAION_CODE})
+  target_link_libraries(code_relocation_source_lib zephyr_interface)
+endmacro()