pico-sdk/tools/extract_cmake_functions.py
William Vinnicombe 677667dc0d Handle newlines elsewhere - just remove #
Also print warnings for functions missing descriptions
2025-04-15 16:29:16 +01:00

70 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#
# Script to scan the Raspberry Pi Pico SDK tree searching for CMake functions
# Outputs a tab separated file of the function:
# name signature description
#
# Usage:
#
# tools/extract_cmake_functions.py <root of repo> [output file]
#
# If not specified, output file will be `pico_cmake_functions.tsv`
import os
import sys
import re
import csv
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
scandir = sys.argv[1]
outfile = sys.argv[2] if len(sys.argv) > 2 else 'pico_cmake_functions.tsv'
CMAKE_FUNCTION_RE = re.compile(r'^#(.*)((\n\s*#.*)*)\nfunction\(([^\s]*)', re.MULTILINE)
CMAKE_PICO_FUNCTIONS_RE = re.compile(r'^\s*function\((pico_[^\s]*)', re.MULTILINE)
all_functions = {}
# Scan all CMakeLists.txt and .cmake files in the specific path, recursively.
for dirpath, dirnames, filenames in os.walk(scandir):
for filename in filenames:
group = os.path.basename(dirpath)
file_ext = os.path.splitext(filename)[1]
if filename == 'CMakeLists.txt' or file_ext == '.cmake':
file_path = os.path.join(dirpath, filename)
with open(file_path, encoding="ISO-8859-1") as fh:
text = fh.read()
for match in CMAKE_FUNCTION_RE.finditer(text):
name = match.group(4)
signature = match.group(1).strip()
description = match.group(2)
description = description.replace('#', '').strip()
if signature.startswith(name):
all_functions[name] = (signature, description, group)
for match in CMAKE_PICO_FUNCTIONS_RE.finditer(text):
name = match.group(1)
if name not in all_functions:
print(f"Warning: {name} function has no description")
with open(outfile, 'w', newline='') as csvfile:
fieldnames = ('name', 'signature', 'description', 'group')
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore', dialect='excel-tab')
writer.writeheader()
for name, (signature, description, group) in sorted(all_functions.items(), key=lambda x: all_functions[x[0]][2]):
writer.writerow({'name': name, 'signature': signature, 'description': description, 'group': group})