Files
esp-idf/tools/cmake/linker_script_preprocessor.cmake
Frantisek Hrbata aaca1e20ea feat(cmakev2): preprocess linker scripts with component includes
Resolve #include lines in linker script templates against the linked
component graph: idf_build_library appends every linked component's
INCLUDE_DIRS to the C preprocessor invocation for each .in linker
script, so a template can include any component header (e.g. a ULP
memory-layout template including soc/soc.h) without the owning
component having to know about it.

Also:
- Always pass -I<config_dir> so templates can include sdkconfig.h.
- Add a FLAGS option to target_linker_script for explicit preprocessor
  flags (e.g. -D__ASSEMBLER__ or ld-snippet include dirs); when given,
  FLAGS replaces the parent-dir==target include heuristic for that
  script.
- Add a MEMORY option to target_linker_script that emits the marked
  linker script as -T before all others, so section-placement scripts
  in one component can reference MEMORY regions and REGION_ALIASes
  declared by a memory-layout script in another component.
- Make C-preprocessor comment keeping (-C) part of the default flag set
  rather than hardcoded, so FLAGS can drop it. linker_script_preprocessor
  no longer forces -C; both the cmakev1 and cmakev2 preprocessors add it
  to their default CFLAGS (output unchanged for existing scripts). A ULP
  template that includes soc/soc.h omits -C so ld does not choke on the
  header's // comments.
- Store per-script metadata (generated output, flags) in MD5-keyed
  component properties instead of parallel lists.

Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
2026-07-15 09:14:35 +09:00

20 lines
1.1 KiB
CMake

# Split CFLAGS string into list for proper argument passing
# Uses UNIX_COMMAND method because NATIVE_COMMAND has issues on Windows
# where the first argument is parsed specially, causing problems with quoted paths
# Note: Paths are expected to be quoted in the CFLAGS variable
separate_arguments(CFLAGS_LIST UNIX_COMMAND "${CFLAGS}")
# Comment handling is left to the caller via CFLAGS. The default linker-script
# CFLAGS keep comments (-C) to preserve historical output. Callers whose
# templates include headers with C++-style "//" comments, which ld rejects,
# e.g. the ULP memory layout including soc/soc.h, omit -C in their flags.
execute_process(COMMAND "${CC}" "-P" "-x" "c" "-E" ${CFLAGS_LIST} "${SOURCE}"
RESULT_VARIABLE RET_CODE
OUTPUT_VARIABLE PREPROCESSED_LINKER_SCRIPT
ERROR_VARIABLE ERROR_VAR)
if(RET_CODE AND NOT RET_CODE EQUAL 0)
message(FATAL_ERROR "Can't generate ${TARGET}\nRET_CODE: ${RET_CODE}\nERROR_MESSAGE: ${ERROR_VAR}")
endif()
string(REPLACE "\\n" "\n" TEXT "${PREPROCESSED_LINKER_SCRIPT}")
file(WRITE "${TARGET}" "${TEXT}")