mirror of
https://github.com/espressif/esp-idf.git
synced 2026-07-17 00:13:06 +03:00
Refactor the esp_err_to_name() system to decouple esp_common from higher-level components. Instead of a monolithic generated table, each component registers its error codes into a dedicated linker section (.esp_err_msg_table) via idf_define_esp_err_codes() in its CMakeLists.txt. New files: - tools/err_codes_extract.py: extract ESP_ERR_* defines from headers to CSV - tools/err_codes_to_c.py: generate C source placing entries into linker section - tools/err_codes_to_rst.py: generate RST documentation from error codes - tools/cmake/err_codes.cmake: CMake module providing idf_define_esp_err_codes() - components/esp_common/include/esp_err_codes.h: esp_err_msg_t typedef - components/esp_common/src/esp_err_to_name_new.c: new lookup using link-time array - tools/test_apps/build_system/err_codes_check/: CI test app Changes: - Remove all optional component dependencies from esp_common/CMakeLists.txt - Add .esp_err_msg_table section to all 5 linker scripts - Register error codes in 18 components via idf_define_esp_err_codes() - Add new scripts to .gitlab/ci/rules.yml build_check patterns - use new scripts to generate doc and add CI validation - Update esp_err.rst to add description of composable code registration
99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
# SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
# Generate RST documentation from extracted error codes.
|
|
# This script takes a CSV file (produced by err_codes_extract.py) and generates
|
|
# an RST file suitable for inclusion in Sphinx documentation.
|
|
#
|
|
# Usage:
|
|
# python err_codes_to_rst.py --csv errors.csv --output error_codes.inc
|
|
# python err_codes_to_rst.py --search-dirs components/ --output error_codes.inc
|
|
#
|
|
|
|
import argparse
|
|
import csv
|
|
import os
|
|
import sys
|
|
|
|
# Allow importing err_codes_extract from same directory
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from err_codes_extract import extract_all # noqa: E402
|
|
from err_codes_extract import search_headers # noqa: E402
|
|
|
|
|
|
def read_csv_entries(csv_path: str) -> list[tuple[str, int, str]]:
|
|
"""Read error codes from a CSV file."""
|
|
entries: list[tuple[str, int, str]] = []
|
|
with open(csv_path, encoding='utf-8') as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
name = row['name']
|
|
value = int(row['value'])
|
|
comment = row.get('comment', '')
|
|
entries.append((name, value, comment))
|
|
return entries
|
|
|
|
|
|
def generate_rst(entries: list[tuple[str, int, str]]) -> str:
|
|
"""Generate RST content from error code entries.
|
|
|
|
Output format matches the existing esp_err_defs.inc generated by
|
|
gen_esp_err_to_name.py so it can be used as a drop-in replacement
|
|
in the docs build (included via ``.. include-build-file::``).
|
|
"""
|
|
lines: list[str] = []
|
|
|
|
# Sort by value: negative values first (ascending), then non-negative (ascending)
|
|
sorted_entries = sorted(entries, key=lambda e: (e[1], e[0]))
|
|
|
|
for name, value, comment in sorted_entries:
|
|
line = f':c:macro:`{name}` '
|
|
if value > 0:
|
|
line += f'**(0x{value:x})**'
|
|
else:
|
|
line += f'({value:d})'
|
|
if comment:
|
|
line += f': {comment}'
|
|
lines.append(line)
|
|
lines.append('')
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description='Generate RST documentation of ESP error codes')
|
|
parser.add_argument('--csv', default=None, help='Input CSV file with error codes (from err_codes_extract.py)')
|
|
parser.add_argument(
|
|
'--search-dirs', nargs='*', default=[], help='Directories to search recursively for header files'
|
|
)
|
|
parser.add_argument('--base-path', default=None, help='Base path for computing relative paths')
|
|
parser.add_argument('--output', required=True, help='Output RST file path')
|
|
|
|
args = parser.parse_args()
|
|
|
|
base_path = args.base_path
|
|
if base_path is None:
|
|
base_path = os.environ.get('IDF_PATH', os.getcwd())
|
|
|
|
if args.csv:
|
|
entries = read_csv_entries(args.csv)
|
|
elif args.search_dirs:
|
|
headers = search_headers(args.search_dirs, base_path)
|
|
err_codes = extract_all(headers, base_path)
|
|
entries = [(ec.name, ec.value, ec.comment) for ec in err_codes if ec.value is not None]
|
|
else:
|
|
print('Error: Specify either --csv or --search-dirs.', file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
rst_content = generate_rst(entries)
|
|
|
|
os.makedirs(os.path.dirname(os.path.abspath(args.output)), exist_ok=True)
|
|
with open(args.output, 'w', encoding='utf-8') as f:
|
|
f.write(rst_content)
|
|
|
|
print(f'Generated {args.output} with {len(entries)} error codes')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|