From 1a2ff6dedaf2c75c4cbf7ae42e83d4fb6eabe594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Thu, 28 Nov 2019 16:57:04 +0100 Subject: [PATCH] cmake: Add sorting of linker snippets by key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows snippets to be placed in a predictable order into the linker script. This is useful for data that must be placed at a particular location. Signed-off-by: Øyvind Rønningstad --- cmake/extensions.cmake | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/cmake/extensions.cmake b/cmake/extensions.cmake index 8fa8792df22..ab7cb2ea332 100644 --- a/cmake/extensions.cmake +++ b/cmake/extensions.cmake @@ -863,7 +863,7 @@ function(zephyr_check_compiler_flag_hardcoded lang option check exists) endif() endfunction(zephyr_check_compiler_flag_hardcoded) -# zephyr_linker_sources( ) +# zephyr_linker_sources( [SORT_KEY ] ) # # is one or more .ld formatted files whose contents will be # copied/included verbatim into the given in the global linker.ld. @@ -878,6 +878,9 @@ endfunction(zephyr_check_compiler_flag_hardcoded) # RAM_SECTIONS Inside the RAMABLE_REGION GROUP. # SECTIONS Near the end of the file. Don't use this when linking into # RAMABLE_REGION, use RAM_SECTIONS instead. +# is an optional key to sort by inside of each location. The key must +# be alphanumeric, and the keys are sorted alphabetically. If no key is +# given, the key 'default' is used. Keys are case-sensitive. # # Use NOINIT, RWDATA, and RODATA unless they don't work for your use case. # @@ -945,7 +948,13 @@ function(zephyr_linker_sources location) message(fatal_error "Must choose valid location for linker snippet.") endif() - foreach(file IN ITEMS ${ARGN}) + cmake_parse_arguments(L "" "SORT_KEY" "" ${ARGN}) + set(SORT_KEY default) + if(DEFINED L_SORT_KEY) + set(SORT_KEY ${L_SORT_KEY}) + endif() + + foreach(file IN ITEMS ${L_UNPARSED_ARGUMENTS}) # Resolve path. if(IS_ABSOLUTE ${file}) set(path ${file}) @@ -957,11 +966,18 @@ function(zephyr_linker_sources location) message(FATAL_ERROR "zephyr_linker_sources() was called on a directory") endif() - # Append the file contents to the relevant destination file. - file(READ ${path} snippet) - file(RELATIVE_PATH relpath ${ZEPHYR_BASE} ${path}) - file(APPEND ${snippet_path} - "\n/* From \${ZEPHYR_BASE}/${relpath}: */\n" "${snippet}\n") + # Find the relative path to the linker file from the include folder. + file(RELATIVE_PATH relpath ${ZEPHYR_BASE}/include ${path}) + + # Create strings to be written into the file + set (include_str "/* Sort key: \"${SORT_KEY}\" */#include \"${relpath}\"") + + # Add new line to existing lines, sort them, and write them back. + file(STRINGS ${snippet_path} lines) # Get current lines (without newlines). + list(APPEND lines ${include_str}) + list(SORT lines) + string(REPLACE ";" "\n;" lines "${lines}") # Add newline to each line. + file(WRITE ${snippet_path} ${lines} "\n") endforeach() endfunction(zephyr_linker_sources)