Replaced weak handlers with external calls

This commit is contained in:
2025-11-21 15:14:38 +00:00
parent 9f531d04c1
commit 7419088a5c
2 changed files with 18 additions and 16 deletions

View File

@@ -24,7 +24,7 @@ CMAKE_EXCLUDE_SOURCES = [
] ]
CMAKE_TARGET = "" CMAKE_TARGET = ""
ASSUME_YES = False ASSUME_YES = False
STRONG_HANDLERS = False INJECT_HANDLERS = []
NO_MY_MAIN = False NO_MY_MAIN = False
@@ -105,23 +105,25 @@ def root_cmakelists(path: Path):
with open(lists_path, "w") as of: with open(lists_path, "w") as of:
of.write("\n".join(lines)+"\n") of.write("\n".join(lines)+"\n")
def weaken_handlers_file(path: Path): def inject_handlers_file(path: Path):
with open(path, "r") as _if: with open(path, "r") as _if:
content = _if.read() content = _if.read()
myre = re.compile(r"void ([A-Z][A-Za-z]+)_Handler\(void\)") patched = content
patched = myre.sub(r"__attribute__((weak)) void \1_Handler(void)", 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: with open(path, "w") as of:
of.write(patched) of.write(patched)
def weaken_handlers(path: Path): def inject_handlers(path: Path):
root = path / "Core/Src" root = path / "Core/Src"
for child in root.iterdir(): for child in root.iterdir():
if any(child.match(ext) for ext in SOURCE_EXTS) and \ if any(child.match(ext) for ext in SOURCE_EXTS) and \
not any(child.match(exclude) for exclude in CMAKE_EXCLUDE_SOURCES): 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): def call_my_main(path: Path):
main_c = path / "Core/Src/main.c" main_c = path / "Core/Src/main.c"
@@ -153,7 +155,7 @@ def overlay_config(config: dict):
global CMAKE_TARGET global CMAKE_TARGET
global CMAKE_EXCLUDE_SOURCES global CMAKE_EXCLUDE_SOURCES
global ASSUME_YES global ASSUME_YES
global STRONG_HANDLERS global INJECT_HANDLERS
global NO_MY_MAIN global NO_MY_MAIN
if "discard" in config and config["discard"]: if "discard" in config and config["discard"]:
@@ -186,11 +188,11 @@ def overlay_config(config: dict):
print("assume-yes must be a bool") print("assume-yes must be a bool")
exit(1) exit(1)
ASSUME_YES |= config["assume-yes"] ASSUME_YES |= config["assume-yes"]
if "strong-handlers" in config and config["strong-handlers"]: if "inject-handlers" in config and config["inject-handlers"]:
if not isinstance(config["strong-handlers"], bool): if not isinstance(config["inject-handlers"], list):
print("strong-handlers must be a bool") print("inject-handlers must be a list of strings")
exit(1) exit(1)
STRONG_HANDLERS |= config["strong-handlers"] INJECT_HANDLERS += config["inject-handlers"]
if "no-my-main" in config and config["no-my-main"]: if "no-my-main" in config and config["no-my-main"]:
if not isinstance(config["no-my-main"], bool): if not isinstance(config["no-my-main"], bool):
print("no-my-main must be a 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_EXCLUDE_SOURCES", CMAKE_EXCLUDE_SOURCES)
print("CMAKE_TARGET", CMAKE_TARGET) print("CMAKE_TARGET", CMAKE_TARGET)
print("ASSUME_YES", ASSUME_YES) print("ASSUME_YES", ASSUME_YES)
print("STRONG_HANDLERS", STRONG_HANDLERS) print("INJECT_HANDLERS", INJECT_HANDLERS)
print("NO_MY_MAIN", NO_MY_MAIN) print("NO_MY_MAIN", NO_MY_MAIN)
def main2(ns: Namespace): def main2(ns: Namespace):
@@ -223,8 +225,8 @@ def main2(ns: Namespace):
recurse_cmakelists(ns.path) recurse_cmakelists(ns.path)
root_cmakelists(ns.path) root_cmakelists(ns.path)
if not STRONG_HANDLERS: if not INJECT_HANDLERS:
weaken_handlers(ns.path) inject_handlers(ns.path)
if not NO_MY_MAIN: if not NO_MY_MAIN:
call_my_main(ns.path) call_my_main(ns.path)
@@ -233,10 +235,10 @@ def main():
parser = ArgumentParser(prog="MX Convert") 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("-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("-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("-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("-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", "--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("-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("-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.") parser.add_argument("-y", "--assume-yes", action="store_true", help="Assume yes on all queries, useful for scripts.")

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "mx_convert" name = "mx_convert"
version = "0.4.1" version = "0.5.0"
description = "A Cube MX projct conversion helper" description = "A Cube MX projct conversion helper"
authors = [{ name = "Didas72" }] authors = [{ name = "Didas72" }]
dependencies = [] dependencies = []