1# SPDX-License-Identifier: Apache-2.0
2# Copyright 2022 NXP
3
4cmake_minimum_required(VERSION 3.20.0)
5
6find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7project(code_relocation)
8
9FILE(GLOB app_sources src/*.c)
10target_sources(app PRIVATE ${app_sources})
11
12if (CONFIG_BOARD_QEMU_XTENSA)
13    set(RAM_PHDR PHDR sram0_phdr)
14    set(SRAM2_PHDR PHDR sram2_phdr)
15endif()
16
17# Code relocation feature
18zephyr_code_relocate(FILES src/test_file1.c ${SRAM2_PHDR} LOCATION SRAM2)
19
20zephyr_code_relocate(FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/test_file2.c ${RAM_PHDR} LOCATION RAM)
21
22# Add custom library that we can relocate code for
23add_subdirectory(test_lib)
24target_link_libraries(app PUBLIC test_lib)
25target_include_directories(app PUBLIC ${CMAKE_CURRENT_LIST_DIR}/test_lib)
26# Relocate library code
27zephyr_code_relocate(LIBRARY test_lib LOCATION SRAM2)
28
29# Test support for a simple generator expression to relocate two files
30set(reloc_files src/test_file4.c src/test_file5.c)
31set(genex_expr
32  ${CMAKE_CURRENT_LIST_DIR}/$<JOIN:${reloc_files},$<SEMICOLON>${CMAKE_CURRENT_LIST_DIR}/>)
33zephyr_code_relocate(FILES ${genex_expr} LOCATION SRAM2)
34
35zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_LITERAL)
36zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_TEXT)
37zephyr_code_relocate(FILES src/test_file3.c LOCATION RAM_DATA)
38zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_BSS)
39
40# Test NOKEEP support. Placing both KEEP and NOKEEP symbols in the same location
41# (this and test_file2.c in RAM) should work fine.
42zephyr_code_relocate(FILES ${ZEPHYR_BASE}/kernel/sem.c ${RAM_PHDR} LOCATION RAM NOKEEP)
43
44if (CONFIG_RELOCATE_TO_ITCM)
45zephyr_code_relocate(FILES ${ZEPHYR_BASE}/lib/libc/minimal/source/string/string.c
46	LOCATION ITCM_TEXT)
47endif()
48
49zephyr_linker_sources(SECTIONS custom-sections.ld)
50