Skip to content
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

[RFC][PoC] Zephyr PreProcessor (ZPP) framework #82990

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

pdgendt
Copy link
Collaborator

@pdgendt pdgendt commented Dec 14, 2024

Introduction

This is a proof-of-concept, namely the Zephyr Pre-Processor (ZPP) framework, an alternative to the syscall parsing for gathering function and struct information.

Problem description

The https://github.com/zephyrproject-rtos/zephyr/blob/main/scripts/build/parse_syscalls.py script has a few drawbacks that this PR tries to improve upon.

  • It's currently (ab)used to find __subsystem and __net_socket "tags" but this is a hidden implementation detail
  • It operates on header/source files before the C pre-processer so we can't rely on macro expansion
  • It generates more boiler plate than is needed

Proposed change

Run the C pre-processor, in the pre-build stage, on files that are part of the build and have the -DZPP compile definition (opt-in), and parse the output for annotated functions or structs.
Gather the information in a formatted (json) file that can be fed into other build steps.

Detailed RFC

In the aforementioned C pre-processor run, the compile definition -D__ZPP__ is added which enables the C23 annotations (not part of the regular build), for example:

#ifdef __ZPP__
#define __zpp(x) [[zephyr::x]]
#else
#define __zpp(...)
#endif

#define __syscall __zpp(func("syscall", __FILE__, __LINE__))
#define __subsystem __zpp(struct("__subsystem"))

The newly introduced scripts/zpp.py scans for these annotations and can output the structured data needed for the next build steps.

[
  {
    "attr": "func",
    "args": [
      "syscall",
      "WEST_TOPDIR/zephyr/include/zephyr/kernel.h",
      537
    ],
    "data": {
      "type": "int",
      "name": "k_thread_join",
      "args": "struct k_thread *thread, k_timeout_t timeout"
    }
  },
  {
    "attr": "struct",
    "args": [
      "__subsystem"
    ],
    "data": {
      "name": "gpio_driver_api"
    }
  }
]

Proposed change (Detailed)

  • Convert the __subsystem tags with ZPP (part of the PR already)
  • Convert the __net_socket tags with ZPP
  • Convert the __syscall tags with ZPP
  • Introduce __section_iterable tags for structs to allow creating linker sections from source files, without the need for additional linker scripts (cleanup all common-rom.ld and common-rom.cmake files)

Dependencies

Affects downstream usage of the syscall mechanism.

Concerns and Unresolved Questions

  • Portability for different toolchains
  • Opt-in vs opt-out
  • Generated headers need include guards when running ZPP (part of the PR already)

Alternatives

Don't do this

@pdgendt pdgendt added the RFC Request For Comments: want input from the community label Dec 14, 2024
Generated headers/sources are not available when the Zephyr PreProcessor
runs, add guards.

Signed-off-by: Pieter De Gendt <[email protected]>
@pdgendt pdgendt force-pushed the poc-zpp branch 5 times, most recently from a6d9d38 to eedde91 Compare December 14, 2024 15:11
Add a PoC script to act as a pre-processor to capture functions and
structs that require special attention.

Signed-off-by: Pieter De Gendt <[email protected]>
Update some directives to add a C23 annotation instead.

Signed-off-by: Pieter De Gendt <[email protected]>
Create a CMake target to build the annotations together with a depfile.

Signed-off-by: Pieter De Gendt <[email protected]>
Rework gen_iter_sections.py to use the annotations json file generated
by ZPP.

Signed-off-by: Pieter De Gendt <[email protected]>
TO BE REMOVED
For testing enable the Zephyr PreProcessor on all source files.

Signed-off-by: Pieter De Gendt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments: want input from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant