Skip to content

Commit 04684c2

Browse files
cmake: modules: add zephyr_library_relocate, to relocate zephyr library
Add zephyr_library_relocate, a helper macro intended to allow entire Zephyr libraries to be relocated to a new memory region. This is implemented via a two step process, to ensure that sources added to a library after relocation is requested still are relocated: 1. zephyr_library_relocate adds a custom target property to the library with the relocation region and flags 2. the root level CMakeLists.txt captures this property while parsing all Zephyr libraries, and adds all library source files to the code relocation target Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent fd9e542 commit 04684c2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ set(PARSE_SYSCALLS_TARGET parse_syscalls_target)
9191

9292
define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
9393
set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) # BFD format
94+
define_property(GLOBAL PROPERTY PROPERTY_CODE_RELOCATION_TARGET
95+
BRIEF_DOCS "Code relocation target location" FULL_DOCS " ")
9496

9597
# "zephyr_interface" is a source-less library that encapsulates all the global
9698
# compiler options needed by all source files. All zephyr libraries,
@@ -815,6 +817,21 @@ foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
815817
list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
816818
continue()
817819
endif()
820+
# In order to allow zephyr_library_relocate to be called prior to adding
821+
# library sources, we must parse and generate relocation information
822+
# once all libraries have been defined
823+
get_property(relocation_target TARGET ${zephyr_lib} PROPERTY
824+
PROPERTY_CODE_RELOCATION_TARGET)
825+
if(NOT "${relocation_target}" STREQUAL "")
826+
get_property(lib_source_dir TARGET ${zephyr_lib} PROPERTY SOURCE_DIR)
827+
foreach(file ${source_list})
828+
if(NOT IS_ABSOLUTE ${file})
829+
set(file ${lib_source_dir}/${file})
830+
endif()
831+
set_property(TARGET code_data_relocation_target APPEND PROPERTY
832+
COMPILE_DEFINITIONS "${relocation_target}:${file}")
833+
endforeach()
834+
endif()
818835
endif()
819836

820837
# TODO: Could this become an INTERFACE property of zephyr_interface?

cmake/modules/extensions.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,20 @@ function(zephyr_linker_sources location)
12811281
endforeach()
12821282
endfunction(zephyr_linker_sources)
12831283

1284+
function(zephyr_library_relocate location)
1285+
set(options NOCOPY)
1286+
cmake_parse_arguments(CODE_REL "${options}" "" "" ${ARGN})
1287+
if(NOT CODE_REL_NOCOPY)
1288+
set(copy_flag COPY)
1289+
else()
1290+
set(copy_flag NOCOPY)
1291+
endif()
1292+
# Set current library relocation property. This will be picked up
1293+
# in the root CMakeLists.txt file to relocate all sources added to
1294+
# this library
1295+
set_property(TARGET ${ZEPHYR_CURRENT_LIBRARY}
1296+
PROPERTY PROPERTY_CODE_RELOCATION_TARGET "${location}:${copy_flag}")
1297+
endfunction()
12841298

12851299
# Helper function for CONFIG_CODE_DATA_RELOCATION
12861300
# Call this function with 2 arguments file and then memory location.

0 commit comments

Comments
 (0)