From b7e2d4296ae0a4c656153075195bf7d6a51c41af Mon Sep 17 00:00:00 2001 From: didas72 Date: Wed, 19 Nov 2025 20:32:51 +0000 Subject: [PATCH] Added weakening of handlers --- mx_convert/mx_convert.py | 32 +++++++++++++++++++++++++++++++- pyproject.toml | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/mx_convert/mx_convert.py b/mx_convert/mx_convert.py index 084f3c0..060245b 100644 --- a/mx_convert/mx_convert.py +++ b/mx_convert/mx_convert.py @@ -3,6 +3,7 @@ from shutil import rmtree from sys import argv from argparse import ArgumentParser, Namespace import tomllib as toml +import re MX_DISCARD_PATHS = [ "cmake", @@ -27,6 +28,7 @@ CMAKE_EXCLUDE_SOURCES = [ ] CMAKE_TARGET = "" # Set from config ASSUME_YES = False +WEAK_HANDLERS = True def mx_cleanup(path: Path): @@ -105,7 +107,24 @@ def root_cmakelists(path: Path): # Save final result with open(lists_path, "w") as of: of.write("\n".join(lines)+"\n") - pass + +def weaken_handlers_file(path: Path): + with open(path, "r") as _if: + content = _if.read() + + myre = re.compile(r"void ([A-Z][A-Za-z]+)_Handler\(void\)") + replaced = myre.sub(r"__attribute__\(\(weak\)\) void \1_Handler\(void\)", content) + + with open(path, "w") as of: + of.write(replaced) + +def weaken_handlers(path: Path): + root = path / "Core/Src" + + for child in root.iterdir(): + if any(child.match(ext) for ext in SOURCE_EXTS) and \ + not any(child.match(exclude) for exclude in CMAKE_EXCLUDE_SOURCES): + weaken_handlers_file(child) def load_config(path: Path): if not path.exists(): @@ -126,6 +145,7 @@ def overlay_config(config: dict): global CMAKE_TARGET global CMAKE_EXCLUDE_SOURCES global ASSUME_YES + global WEAK_HANDLERS if "discard" in config and config["discard"]: if not isinstance(config["discard"], list): @@ -157,6 +177,11 @@ def overlay_config(config: dict): print("assume-yes must be a bool") exit(1) ASSUME_YES |= config["assume-yes"] + if "weak-handlers" in config and config["weak-handlers"]: + if not isinstance(config["weak-handlers"], bool): + print("weak-handlers must be a bool") + exit(1) + WEAK_HANDLERS &= config["weak-handlers"] def print_config(): print("MX_DISCARD_PATHS", MX_DISCARD_PATHS) @@ -165,6 +190,7 @@ def print_config(): print("CMAKE_EXCLUDE_SOURCES", CMAKE_EXCLUDE_SOURCES) print("CMAKE_TARGET", CMAKE_TARGET) print("ASSUME_YES", ASSUME_YES) + print("WEAK_HANDLERS", WEAK_HANDLERS) def main2(ns: Namespace): if not ns.path.is_dir(): @@ -182,12 +208,16 @@ def main2(ns: Namespace): recurse_cmakelists(ns.path) root_cmakelists(ns.path) + if WEAK_HANDLERS: + weaken_handlers(ns.path) + def main(): parser = ArgumentParser(prog="MX Convert") parser.add_argument("-c", "--config", action="store", type=Path, default="mx_convert.toml", help="The config file to read. If not specified, defaults to 'mx_convert.toml'.") parser.add_argument("-d", "--discard", action="append", help="Add a file to the discard list (delete if found).") parser.add_argument("-H", "--header", action="append", help="Add a pattern to the headers list, single wildcards allowed. Includes *.h by default.") parser.add_argument("-s", "--source", action="append", help="Add a pattern to the sources list, single wildcards allowed. Includes *.c and *.s by default.") + parser.add_argument("-S", "--strong-handlers", action="store_false", help="Disable automatic adding of __weak__ attribute to handler functions.") parser.add_argument("-t", "--target", action="store", default="cubemx", help="The CMake target to generate references to. Defaults to 'cubemx'.") parser.add_argument("-x", "--exclude", action="append", help="Add a file to the source exclusion list, single wildcards allowed.") parser.add_argument("-y", "--assume-yes", action="store_true", help="Assume yes on all queries, useful for scripts.") diff --git a/pyproject.toml b/pyproject.toml index 6152460..18864e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "mx_convert" -version = "0.2.4" +version = "0.3.0" description = "A Cube MX projct conversion helper" authors = [{ name = "Didas72" }] dependencies = []