Skip to content

Commit 10e85bf

Browse files
danieldegrassecarlescufi
authored andcommitted
cmake: update API for zephyr_code_relocate to support relocating libraries
Update API for zephyr_code_relocate to support cmake generator expressions, as well as relocating libraries. zephyr_code_relocate can now accept a target name to the LIBRARY argument, which will be converted into a set of source files from that target to relocate. Alternatively, files can be passed as a space separated list or CMake generator expression. This allows users more flexibility when relocating files. Glob matching functionality is still available, although the preferred method to do this would now be: file(GLOB relocate_sources "src/*.c") zephyr_code_relocate(FILES ${relocate_sources} LOCATION <location>) Note! This commit breaks support for zephyr_code_relocate until in tree usages of the API are updated to the new format. Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent ca14626 commit 10e85bf

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

cmake/modules/extensions.cmake

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ endfunction()
468468

469469
#
470470
# zephyr_library versions of normal CMake target_<func> functions
471+
# Note, paths passed to this function must be relative in order
472+
# to support the library relocation feature of zephyr_code_relocate
471473
#
472474
function(zephyr_library_sources source)
473475
target_sources(${ZEPHYR_CURRENT_LIBRARY} PRIVATE ${source} ${ARGN})
@@ -1283,22 +1285,85 @@ endfunction(zephyr_linker_sources)
12831285

12841286

12851287
# Helper function for CONFIG_CODE_DATA_RELOCATION
1286-
# Call this function with 2 arguments file and then memory location.
1287-
# One optional [NOCOPY] flag can be used.
1288-
function(zephyr_code_relocate file location)
1288+
# This function may either be invoked with a list of files, or a library
1289+
# name to relocate.
1290+
#
1291+
# The FILES directive will relocate a list of files (wildcards supported)
1292+
# This directive will relocate file1. and file2.c to SRAM:
1293+
# zephyr_code_relocate(FILES file1.c file2.c LOCATION SRAM)
1294+
# Note, files can also be passed as a comma separated list to support using
1295+
# cmake generator arguments
1296+
#
1297+
# The LIBRARY directive will relocate a library
1298+
# This directive will relocate the target my_lib to SRAM:
1299+
# zephyr_code_relocate(LIBRARY my_lib SRAM)
1300+
#
1301+
# The following optional arguments are supported:
1302+
# - NOCOPY: this flag indicates that the file data does not need to be copied
1303+
# at boot time (For example, for flash XIP).
1304+
# - PHDR [program_header]: add program header. Used on Xtensa platforms.
1305+
function(zephyr_code_relocate)
12891306
set(options NOCOPY)
1290-
cmake_parse_arguments(CODE_REL "${options}" "" "" ${ARGN})
1291-
if(NOT IS_ABSOLUTE ${file})
1292-
set(file ${CMAKE_CURRENT_SOURCE_DIR}/${file})
1307+
set(single_args LIBRARY LOCATION PHDR)
1308+
set(multi_args FILES)
1309+
cmake_parse_arguments(CODE_REL "${options}" "${single_args}"
1310+
"${multi_args}" ${ARGN})
1311+
# Argument validation
1312+
if(CODE_REL_UNPARSED_ARGUMENTS)
1313+
message(FATAL_ERROR "zephyr_code_relocate(${ARGV0} ...) "
1314+
"given unknown arguments: ${CODE_REL_UNPARSED_ARGUMENTS}")
1315+
endif()
1316+
if((NOT CODE_REL_FILES) AND (NOT CODE_REL_LIBRARY))
1317+
message(FATAL_ERROR
1318+
"zephyr_code_relocate() requires either FILES or LIBRARY be provided")
1319+
endif()
1320+
if(CODE_REL_FILES AND CODE_REL_LIBRARY)
1321+
message(FATAL_ERROR "zephyr_code_relocate() only accepts "
1322+
"one argument between FILES and LIBRARY")
1323+
endif()
1324+
if(NOT CODE_REL_LOCATION)
1325+
message(FATAL_ERROR "zephyr_code_relocate() requires a LOCATION argument")
1326+
endif()
1327+
if(CODE_REL_LIBRARY)
1328+
# Use cmake generator expression to convert library to file list
1329+
set(genex_src_dir "$<TARGET_PROPERTY:${CODE_REL_LIBRARY},SOURCE_DIR>")
1330+
set(genex_src_list "$<TARGET_PROPERTY:${CODE_REL_LIBRARY},SOURCES>")
1331+
set(file_list
1332+
"${genex_src_dir}/$<JOIN:${genex_src_list},$<SEMICOLON>${genex_src_dir}/>")
1333+
else()
1334+
# Check if CODE_REL_FILES is a generator expression, if so leave it
1335+
# untouched.
1336+
string(GENEX_STRIP "${CODE_REL_FILES}" no_genex)
1337+
if(CODE_REL_FILES STREQUAL no_genex)
1338+
# no generator expression in CODE_REL_FILES, check if list of files
1339+
# is absolute
1340+
foreach(file ${CODE_REL_FILES})
1341+
if(NOT IS_ABSOLUTE ${file})
1342+
set(file ${CMAKE_CURRENT_SOURCE_DIR}/${file})
1343+
endif()
1344+
list(APPEND file_list ${file})
1345+
endforeach()
1346+
else()
1347+
# Generator expression is present in file list. Leave the list untouched.
1348+
set(file_list ${CODE_REL_FILES})
1349+
endif()
12931350
endif()
12941351
if(NOT CODE_REL_NOCOPY)
12951352
set(copy_flag COPY)
12961353
else()
12971354
set(copy_flag NOCOPY)
12981355
endif()
1356+
if(CODE_REL_PHDR)
1357+
set(CODE_REL_LOCATION "${CODE_REL_LOCATION}\ :${CODE_REL_PHDR}")
1358+
endif()
1359+
# We use the "|" character to separate code relocation directives instead
1360+
# of using CMake lists. This way, the ";" character can be reserved for
1361+
# generator expression file lists.
1362+
get_property(code_rel_str TARGET code_data_relocation_target
1363+
PROPERTY COMPILE_DEFINITIONS)
12991364
set_property(TARGET code_data_relocation_target
1300-
APPEND PROPERTY COMPILE_DEFINITIONS
1301-
"${location}:${copy_flag}:${file}")
1365+
PROPERTY COMPILE_DEFINITIONS
1366+
"${code_rel_str}|${CODE_REL_LOCATION}:${copy_flag}:${file_list}")
13021367
endfunction()
13031368

13041369
# Usage:

0 commit comments

Comments
 (0)