-
Notifications
You must be signed in to change notification settings - Fork 8.2k
cmake: modules: Update zephyr_code_relocate API to support relocating libraries #50791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36325f4
5eebb6b
33fced9
48aecc2
88c5c87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -468,6 +468,8 @@ endfunction() | |
|
|
||
| # | ||
| # zephyr_library versions of normal CMake target_<func> functions | ||
| # Note, paths passed to this function must be relative in order | ||
| # to support the library relocation feature of zephyr_code_relocate | ||
| # | ||
| function(zephyr_library_sources source) | ||
| target_sources(${ZEPHYR_CURRENT_LIBRARY} PRIVATE ${source} ${ARGN}) | ||
|
|
@@ -1283,22 +1285,85 @@ endfunction(zephyr_linker_sources) | |
|
|
||
|
|
||
| # Helper function for CONFIG_CODE_DATA_RELOCATION | ||
| # Call this function with 2 arguments file and then memory location. | ||
| # One optional [NOCOPY] flag can be used. | ||
| function(zephyr_code_relocate file location) | ||
| # This function may either be invoked with a list of files, or a library | ||
| # name to relocate. | ||
| # | ||
| # The FILES directive will relocate a list of files (wildcards supported) | ||
| # This directive will relocate file1. and file2.c to SRAM: | ||
| # zephyr_code_relocate(FILES file1.c file2.c LOCATION SRAM) | ||
| # Note, files can also be passed as a comma separated list to support using | ||
| # cmake generator arguments | ||
| # | ||
| # The LIBRARY directive will relocate a library | ||
| # This directive will relocate the target my_lib to SRAM: | ||
| # zephyr_code_relocate(LIBRARY my_lib SRAM) | ||
| # | ||
| # The following optional arguments are supported: | ||
| # - NOCOPY: this flag indicates that the file data does not need to be copied | ||
| # at boot time (For example, for flash XIP). | ||
| # - PHDR [program_header]: add program header. Used on Xtensa platforms. | ||
| function(zephyr_code_relocate) | ||
| set(options NOCOPY) | ||
| cmake_parse_arguments(CODE_REL "${options}" "" "" ${ARGN}) | ||
| if(NOT IS_ABSOLUTE ${file}) | ||
| set(file ${CMAKE_CURRENT_SOURCE_DIR}/${file}) | ||
| set(single_args LIBRARY LOCATION PHDR) | ||
| set(multi_args FILES) | ||
| cmake_parse_arguments(CODE_REL "${options}" "${single_args}" | ||
| "${multi_args}" ${ARGN}) | ||
| # Argument validation | ||
| if(CODE_REL_UNPARSED_ARGUMENTS) | ||
| message(FATAL_ERROR "zephyr_code_relocate(${ARGV0} ...) " | ||
| "given unknown arguments: ${CODE_REL_UNPARSED_ARGUMENTS}") | ||
| endif() | ||
| if((NOT CODE_REL_FILES) AND (NOT CODE_REL_LIBRARY)) | ||
| message(FATAL_ERROR | ||
| "zephyr_code_relocate() requires either FILES or LIBRARY be provided") | ||
| endif() | ||
| if(CODE_REL_FILES AND CODE_REL_LIBRARY) | ||
| message(FATAL_ERROR "zephyr_code_relocate() only accepts " | ||
| "one argument between FILES and LIBRARY") | ||
| endif() | ||
| if(NOT CODE_REL_LOCATION) | ||
| message(FATAL_ERROR "zephyr_code_relocate() requires a LOCATION argument") | ||
| endif() | ||
| if(CODE_REL_LIBRARY) | ||
| # Use cmake generator expression to convert library to file list | ||
| set(genex_src_dir "$<TARGET_PROPERTY:${CODE_REL_LIBRARY},SOURCE_DIR>") | ||
| set(genex_src_list "$<TARGET_PROPERTY:${CODE_REL_LIBRARY},SOURCES>") | ||
| set(file_list | ||
| "${genex_src_dir}/$<JOIN:${genex_src_list},$<SEMICOLON>${genex_src_dir}/>") | ||
| else() | ||
| # Check if CODE_REL_FILES is a generator expression, if so leave it | ||
| # untouched. | ||
| string(GENEX_STRIP "${CODE_REL_FILES}" no_genex) | ||
| if(CODE_REL_FILES STREQUAL no_genex) | ||
| # no generator expression in CODE_REL_FILES, check if list of files | ||
| # is absolute | ||
| foreach(file ${CODE_REL_FILES}) | ||
| if(NOT IS_ABSOLUTE ${file}) | ||
| set(file ${CMAKE_CURRENT_SOURCE_DIR}/${file}) | ||
| endif() | ||
| list(APPEND file_list ${file}) | ||
| endforeach() | ||
| else() | ||
| # Generator expression is present in file list. Leave the list untouched. | ||
| set(file_list ${CODE_REL_FILES}) | ||
|
||
| endif() | ||
| endif() | ||
| if(NOT CODE_REL_NOCOPY) | ||
| set(copy_flag COPY) | ||
| else() | ||
| set(copy_flag NOCOPY) | ||
| endif() | ||
| if(CODE_REL_PHDR) | ||
| set(CODE_REL_LOCATION "${CODE_REL_LOCATION}\ :${CODE_REL_PHDR}") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly why is an escaped space needed here ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PHDR directive was previously paired with the relocation location, as you can see in this diff: https://github.com/zephyrproject-rtos/zephyr/pull/50791/files#diff-7087c4d7b0a6595b501520d5eb10ed5ecb01b3540831e3a141a6a662632ac940R20. This directive makes sure that the PHDR is still paired with the code location, so it can be parsed by |
||
| endif() | ||
| # We use the "|" character to separate code relocation directives instead | ||
| # of using CMake lists. This way, the ";" character can be reserved for | ||
| # generator expression file lists. | ||
| get_property(code_rel_str TARGET code_data_relocation_target | ||
| PROPERTY COMPILE_DEFINITIONS) | ||
| set_property(TARGET code_data_relocation_target | ||
| APPEND PROPERTY COMPILE_DEFINITIONS | ||
| "${location}:${copy_flag}:${file}") | ||
| PROPERTY COMPILE_DEFINITIONS | ||
| "${code_rel_str}|${CODE_REL_LOCATION}:${copy_flag}:${file_list}") | ||
| endfunction() | ||
|
|
||
| # Usage: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -54,14 +54,13 @@ for data copy operations from ROM to required memory type. | |||||
| * Inside the ``CMakeLists.txt`` file in the project, mention | ||||||
| all the files that need relocation. | ||||||
|
|
||||||
| ``zephyr_code_relocate(src/*.c SRAM2)`` | ||||||
| ``zephyr_code_relocate(FILES src/*.c LOCATION SRAM2)`` | ||||||
|
|
||||||
| Where the first argument is the file/files and the second | ||||||
| argument is the memory where it must be placed. | ||||||
|
|
||||||
| .. note:: | ||||||
|
|
||||||
| The file argument supports limited regular expressions. | ||||||
| function zephyr_code_relocate() can be called as many times as required. | ||||||
| This step has to be performed before calling find_package(Zephyr ...) | ||||||
| in the application's CMakeLists.txt. | ||||||
|
|
@@ -77,21 +76,30 @@ This section shows additional configuration options that can be set in | |||||
|
|
||||||
| .. code-block:: none | ||||||
|
|
||||||
| zephyr_code_relocate(src/file1.c SRAM2) | ||||||
| zephyr_code_relocate(src/file2.c.c SRAM) | ||||||
| zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2) | ||||||
| zephyr_code_relocate(FILES src/file2.c LOCATION SRAM) | ||||||
|
|
||||||
| * if the memory type is appended with _DATA, _TEXT, _RODATA or _BSS, only the | ||||||
| selected memory is placed in the required memory region. | ||||||
| for example: | ||||||
|
|
||||||
| .. code-block:: none | ||||||
|
|
||||||
| zephyr_code_relocate(src/file1.c SRAM2_DATA) | ||||||
| zephyr_code_relocate(src/file2.c.c SRAM2_TEXT) | ||||||
| zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2_DATA) | ||||||
| zephyr_code_relocate(FILES src/file2.c LOCATION SRAM2_TEXT) | ||||||
|
|
||||||
| * Multiple regions can also be appended together such as: SRAM2_DATA_BSS. | ||||||
| This will place data and bss inside SRAM2. | ||||||
|
|
||||||
| * Multiple files can be passed to the FILES argument, or CMake generator | ||||||
| expressions can be used to relocate a comma-separated list of files | ||||||
|
||||||
| The file argument supports limited regular expressions. |
we don't need to inform users of an approach that is sub-optimal (incremental build wise) and harder to debug.
and when at it, maybe remove this line as well:
zephyr/doc/kernel/code-relocation.rst
Line 66 in a084be3
| This step has to be performed before the inclusion of boilerplate.cmake. |
boilerplate.cmake directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this note in the latest push, so that regular expressions are no longer documented.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as described in other comment, then I believe this example will not work unfortunately, because the python relocation script will expect the files to be comma separated,
But let's wait with adjusting this until the final approach has been decided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clever way of testing for genex in a variable.
I would just have used
if("${argument}" MATCHES ".*\\$\\<.*\\>.*")to test for any genex, but this code is much more readable 👍