Skip to content

Commit

Permalink
scripts: build: Use annotations file for gen_iter_sections.py
Browse files Browse the repository at this point in the history
Rework gen_iter_sections.py to use the annotations json file generated
by ZPP.

Signed-off-by: Pieter De Gendt <[email protected]>
  • Loading branch information
pdgendt committed Dec 16, 2024
1 parent eb9ce84 commit b6a0540
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ set(ZEPHYR_CURRENT_CMAKE_DIR)
get_property(LIBC_LINK_LIBRARIES TARGET zephyr_interface PROPERTY LIBC_LINK_LIBRARIES)
zephyr_link_libraries(${LIBC_LINK_LIBRARIES})

set(annotations_json ${CMAKE_BINARY_DIR}/zephyr/annotations.json)
set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/zephyr/syscall_list.h)
set(edk_syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/edk/include/generated/zephyr/syscall_list.h)
set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
Expand Down Expand Up @@ -906,13 +907,14 @@ add_custom_command(
${PYTHON_EXECUTABLE}
${ZEPHYR_BASE}/scripts/build/gen_iter_sections.py
--alignment ${CONFIG_LINKER_ITERABLE_SUBALIGN}
--input ${struct_tags_json}
--input ${annotations_json}
--tag __subsystem
--ld-output ${DEVICE_API_LD_SECTIONS}
--cmake-output ${DEVICE_API_LINKER_SECTIONS_CMAKE}
--cmake-prefix "DEVICE_API"
DEPENDS
${ZEPHYR_BASE}/scripts/build/gen_iter_sections.py
${struct_tags_json}
${annotations_json}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

Expand Down
24 changes: 18 additions & 6 deletions scripts/build/gen_iter_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@
#
# SPDX-License-Identifier: Apache-2.0
"""
Script to generate iterable sections from JSON encoded dictionary containing lists of items.
Script to generate iterable sections from JSON encoded dictionary containing a list of annotations.
"""

import argparse
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parents[1]))
from zpp import Annotation, AnnotationType


def get_tagged_items(filepath: str, tag: str) -> list:
with open(filepath) as fp:
return json.load(fp)[tag]
annotations = [Annotation(**a) for a in json.load(fp)]

return [
a.data.get("name")
for a in annotations
if a.attr == AnnotationType.STRUCT and len(a.args) > 0 and a.args[0] == tag
]


def gen_ld(filepath: str, items: list, alignment: int):
Expand All @@ -22,7 +33,7 @@ def gen_ld(filepath: str, items: list, alignment: int):
fp.write(f"ITERABLE_SECTION_ROM({item}, {alignment})\n")


def gen_cmake(filepath: str, items: list, alignment: int):
def gen_cmake(filepath: str, items: list, alignment: int, prefix: str):
with open(filepath, "w") as fp:
for item in items:
fp.write(
Expand All @@ -38,8 +49,8 @@ def gen_cmake(filepath: str, items: list, alignment: int):
+ f'INPUT\\;._{item}.static.*\\;'
+ f'SYMBOLS\\;_{item}_list_start\\;_{item}_list_end}}")\n'
)
fp.write('set(DEVICE_API_SECTIONS "${sections}" CACHE INTERNAL "")\n')
fp.write('set(DEVICE_API_SECTION_SETTINGS "${section_settings}" CACHE INTERNAL "")\n')
fp.write(f'set({prefix}_SECTIONS "${{sections}}" CACHE INTERNAL "")\n')
fp.write(f'set({prefix}_SECTION_SETTINGS "${{section_settings}}" CACHE INTERNAL "")\n')


def parse_args() -> argparse.Namespace:
Expand All @@ -56,6 +67,7 @@ def parse_args() -> argparse.Namespace:
parser.add_argument(
"-c", "--cmake-output", required=True, help="Path to CMake linker script inclusion file"
)
parser.add_argument("-p", "--cmake-prefix", required=True, help="Section name prefix")

return parser.parse_args()

Expand All @@ -66,7 +78,7 @@ def main():
items = get_tagged_items(args.input, args.tag)

gen_ld(args.ld_output, items, args.alignment)
gen_cmake(args.cmake_output, items, args.alignment)
gen_cmake(args.cmake_output, items, args.alignment, args.cmake_prefix)


if __name__ == "__main__":
Expand Down

0 comments on commit b6a0540

Please sign in to comment.