Added weakening of handlers

This commit is contained in:
2025-11-19 20:32:51 +00:00
parent 273d917bab
commit b7e2d4296a
2 changed files with 32 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ from shutil import rmtree
from sys import argv from sys import argv
from argparse import ArgumentParser, Namespace from argparse import ArgumentParser, Namespace
import tomllib as toml import tomllib as toml
import re
MX_DISCARD_PATHS = [ MX_DISCARD_PATHS = [
"cmake", "cmake",
@@ -27,6 +28,7 @@ CMAKE_EXCLUDE_SOURCES = [
] ]
CMAKE_TARGET = "" # Set from config CMAKE_TARGET = "" # Set from config
ASSUME_YES = False ASSUME_YES = False
WEAK_HANDLERS = True
def mx_cleanup(path: Path): def mx_cleanup(path: Path):
@@ -105,7 +107,24 @@ def root_cmakelists(path: Path):
# Save final result # Save final result
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")
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): def load_config(path: Path):
if not path.exists(): if not path.exists():
@@ -126,6 +145,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 WEAK_HANDLERS
if "discard" in config and config["discard"]: if "discard" in config and config["discard"]:
if not isinstance(config["discard"], list): if not isinstance(config["discard"], list):
@@ -157,6 +177,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 "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(): def print_config():
print("MX_DISCARD_PATHS", MX_DISCARD_PATHS) print("MX_DISCARD_PATHS", MX_DISCARD_PATHS)
@@ -165,6 +190,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("WEAK_HANDLERS", WEAK_HANDLERS)
def main2(ns: Namespace): def main2(ns: Namespace):
if not ns.path.is_dir(): if not ns.path.is_dir():
@@ -182,12 +208,16 @@ def main2(ns: Namespace):
recurse_cmakelists(ns.path) recurse_cmakelists(ns.path)
root_cmakelists(ns.path) root_cmakelists(ns.path)
if WEAK_HANDLERS:
weaken_handlers(ns.path)
def main(): 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("-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("-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_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("-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.2.4" version = "0.3.0"
description = "A Cube MX projct conversion helper" description = "A Cube MX projct conversion helper"
authors = [{ name = "Didas72" }] authors = [{ name = "Didas72" }]
dependencies = [] dependencies = []