From 7419088a5c0b1d151de797ce6adbd5594ddaab21 Mon Sep 17 00:00:00 2001 From: Didas72 Date: Fri, 21 Nov 2025 15:14:38 +0000 Subject: [PATCH] Replaced weak handlers with external calls --- mx_convert/mx_convert.py | 32 +++++++++++++++++--------------- pyproject.toml | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/mx_convert/mx_convert.py b/mx_convert/mx_convert.py index b298d35..935e186 100644 --- a/mx_convert/mx_convert.py +++ b/mx_convert/mx_convert.py @@ -24,7 +24,7 @@ CMAKE_EXCLUDE_SOURCES = [ ] CMAKE_TARGET = "" ASSUME_YES = False -STRONG_HANDLERS = False +INJECT_HANDLERS = [] NO_MY_MAIN = False @@ -105,23 +105,25 @@ def root_cmakelists(path: Path): with open(lists_path, "w") as of: of.write("\n".join(lines)+"\n") -def weaken_handlers_file(path: Path): +def inject_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\)") - patched = myre.sub(r"__attribute__((weak)) void \1_Handler(void)", content) + patched = content + + for handler in INJECT_HANDLERS: + patched = patched.replace("void %s_Handler(void){"%(handler), "void %s_Handler(void){\nmy_%s();"%(handler, handler)) with open(path, "w") as of: of.write(patched) -def weaken_handlers(path: Path): +def inject_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) + inject_handlers_file(child) def call_my_main(path: Path): main_c = path / "Core/Src/main.c" @@ -153,7 +155,7 @@ def overlay_config(config: dict): global CMAKE_TARGET global CMAKE_EXCLUDE_SOURCES global ASSUME_YES - global STRONG_HANDLERS + global INJECT_HANDLERS global NO_MY_MAIN if "discard" in config and config["discard"]: @@ -186,11 +188,11 @@ def overlay_config(config: dict): print("assume-yes must be a bool") exit(1) ASSUME_YES |= config["assume-yes"] - if "strong-handlers" in config and config["strong-handlers"]: - if not isinstance(config["strong-handlers"], bool): - print("strong-handlers must be a bool") + if "inject-handlers" in config and config["inject-handlers"]: + if not isinstance(config["inject-handlers"], list): + print("inject-handlers must be a list of strings") exit(1) - STRONG_HANDLERS |= config["strong-handlers"] + INJECT_HANDLERS += config["inject-handlers"] if "no-my-main" in config and config["no-my-main"]: if not isinstance(config["no-my-main"], bool): print("no-my-main must be a bool") @@ -204,7 +206,7 @@ def print_config(): print("CMAKE_EXCLUDE_SOURCES", CMAKE_EXCLUDE_SOURCES) print("CMAKE_TARGET", CMAKE_TARGET) print("ASSUME_YES", ASSUME_YES) - print("STRONG_HANDLERS", STRONG_HANDLERS) + print("INJECT_HANDLERS", INJECT_HANDLERS) print("NO_MY_MAIN", NO_MY_MAIN) def main2(ns: Namespace): @@ -223,8 +225,8 @@ def main2(ns: Namespace): recurse_cmakelists(ns.path) root_cmakelists(ns.path) - if not STRONG_HANDLERS: - weaken_handlers(ns.path) + if not INJECT_HANDLERS: + inject_handlers(ns.path) if not NO_MY_MAIN: call_my_main(ns.path) @@ -233,10 +235,10 @@ 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("-i", "--inject-handlers", action="store", help="Add a call to your own handler functions in MX generated code.") 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("-n", "--no-my-main", action="store_true", help="Disable added call to my_main inside the generated main function.") 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_true", 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 925b0d6..45b2027 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "mx_convert" -version = "0.4.1" +version = "0.5.0" description = "A Cube MX projct conversion helper" authors = [{ name = "Didas72" }] dependencies = []