Skip to content

refactor: split .h files into proper .c + .h compilation units#2374

Open
Islam0953 wants to merge 16 commits intocommaai:masterfrom
Islam0953:refactor/source-header-structure
Open

refactor: split .h files into proper .c + .h compilation units#2374
Islam0953 wants to merge 16 commits intocommaai:masterfrom
Islam0953:refactor/source-header-structure

Conversation

@Islam0953
Copy link
Copy Markdown

Summary

  • Split monolithic .h files (that contained function definitions) into proper .c + .h pairs across drivers, STM32H7 platform, and board-level code
  • Updated SConscript to compile the new .c source files for all targets (panda, body, jungle) with correct main-only vs shared source separation
  • Added minimal, targeted #include statements to each new .c file instead of pulling in the mega-header chain, avoiding multiple-definition linker errors
  • Made shared utility functions (get_ts_elapsed, GPIO helpers) static inline to allow inclusion from multiple compilation units

This refactoring improves code organization by separating declarations from definitions, enabling proper incremental compilation and reducing coupling between modules.

Test plan

  • Full scons build passes cleanly for all 6 targets (panda_h7, body_h7, panda_jungle_h7 — both main and bootstub)
  • No compiler warnings (-Wall -Wextra -Werror enabled)
  • Hardware testing on panda device to verify runtime behavior unchanged

Islam0953 and others added 16 commits March 23, 2026 11:22
Move function implementations from simple_watchdog.h, fan.h,
clock_source.h, bootkick.h, registers.h, and interrupts.h into
corresponding .c files. Headers now contain only declarations via
drivers.h.
Move function implementations from harness.h, uart.h, spi.h,
can_common.h, and fdcan.h into corresponding .c files.
Move all USB implementation from usb.h into usb.c. The header
retains only the include of drivers.h.
Move implementations from clock.h, peripherals.h, lluart.h, llspi.h,
llfan.h, llflash.h, lladc.h, llfdcan.h, llusb.h, and sound.h into
corresponding .c files.
Move implementations from early_init.h, libc.h, flasher.h,
can_comms.h, main_comms.h, provision.h, and crc.h into
corresponding .c files.
Add all new .c files to the build system. Shared sources are compiled
separately for bootstub (with -DBOOTSTUB) and main targets using
unique object file names to avoid conflicts.
Move global variable definitions from main_definitions.h into main.c,
jungle/main.c, and body/main.c. Move bootstub variable definitions
from bootstub_declarations.h into bootstub.c. The _definitions.h
files now only contain extern declarations and type stubs.
Each new .c file now includes the minimal set of headers it needs
(stdint.h, stm32h7xx.h, drivers.h, etc.) instead of the mega-header
config.h which caused multiple-definition linker errors.

Key changes:
- Add STM32 type includes to all split .c files
- Add forward declarations for functions from unsplit .h files
- Add extern declarations for shared variables (can queues, uart rings,
  safety variables, gitversion, etc.)
- Update SConscript with per-target source lists to avoid compiling
  panda-specific files for body/jungle builds
- Make gpio.h functions static inline for multi-TU inclusion
- Make get_ts_elapsed static inline in utils.h
- Fix sound_stop_dac visibility (non-static, declared in sound.h)
- Add sys.h include to drivers.h for ENTER_CRITICAL/EXIT_CRITICAL
- Move jungle board_declarations.h globals to extern declarations
- Add interrupts_enabled extern to sys.h, remove static from critical.h
- Add NUM_INTERRUPTS, harness_configuration typedef, pwm declarations,
  can_clear, and can queue externs to drivers.h
- Add CAN queue definitions (can_rx_q, can_tx1_q, can_tx2_q, can_tx3_q)
  to tests/libpanda/panda.c since they are now defined in can_common.c
  and not available when building the test shared library
- Move global variable definitions after all #include directives in
  board/main.c to fix MISRA C:2012 Rule 20.1 violations
- Remove duplicate typedef harness_configuration from drivers.h
  (keep in main_declarations.h) to fix MISRA C:2012 Rule 5.6
- Remove duplicate extern sound_output_level from main_declarations.h
  (keep in sound.h) to fix MISRA C:2012 Rule 8.5
- Move BOOTSTUB uart_ring_som_debug definition after all includes in
  stm32h7_config.h and remove duplicate extern to fix MISRA Rules 20.1/8.5
Move the typedef to the single location where it's actually used
(struct board definition), eliminating MISRA C:2012 Rule 5.6 duplicates
across main_declarations.h, bootstub_declarations.h, and panda.c.
Add inline cppcheck-suppress comments for false positives caused by
splitting code into separate .c files (cppcheck single-TU analysis
can't see cross-file usage):
- misra-c2012-2.3/2.4: typedefs/tags used in .c files (fdcan.c, simple_watchdog.c)
- misra-c2012-2.4: health_t tag used in main_comms.c
- misra-c2012-8.7: functions called from separate .c files (uart.c, interrupts.c)
- misra-c2012-21.2: restore memset/memcpy/memcmp suppressions in libc.h
- misra-c2012-8.7: suppress for opendbc safety.h (external library)
- Change mutation target from llfdcan.h (now empty) to gpio.h
  (still contains function code visible to cppcheck)
- Use header_files for random pattern mutations since cppcheck
  only analyzes main.c and its includes, not standalone .c files
After the .h/.c split, CAN functions (can_pop, can_push, comms_can_read,
etc.) moved from headers to .c files with STM32 dependencies. The test
library (libpanda.so) can't include those .c files directly, so provide
the implementations inline in panda.c.

Also update MISRA mutation test to target gpio.h (llfdcan.h was emptied
by the refactoring) and restrict random mutations to header files only
(cppcheck single-TU analysis doesn't see standalone .c files).
…t lib

- Define bus_config[PANDA_CAN_CNT] (was extern from drivers.h, defined
  in can_common.c which is not part of the test library)
- Implement refresh_can_tx_slots_available (was only forward-declared)
- These resolve undefined symbol errors when loading libpanda.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants