Added my_main invocation

This commit is contained in:
2025-11-20 10:34:17 +00:00
parent 1fab64f837
commit e5800ee343
3 changed files with 41 additions and 18 deletions

View File

@@ -10,10 +10,6 @@ MX_DISCARD_PATHS = [
".mxproject",
"CMakePresets.json",
]
#MX_MOVE_PATHS = [
# ("Core/Inc/main.h", "Core/Inc/mx-main.h"), #TODO: Force rename in includes as well
# ("Core/Src/main.c", "Core/Src/mx-main.c"),
#]
SOURCE_EXTS = [
"*.c",
@@ -26,9 +22,10 @@ HEADER_EXTS = [
CMAKE_EXCLUDE_SOURCES = [
"*_template.c"
]
CMAKE_TARGET = "" # Set from config
CMAKE_TARGET = ""
ASSUME_YES = False
WEAK_HANDLERS = True
STRONG_HANDLERS = False
NO_MY_MAIN = False
def mx_cleanup(path: Path):
@@ -113,10 +110,10 @@ def weaken_handlers_file(path: Path):
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)
patched = myre.sub(r"__attribute__((weak)) void \1_Handler(void)", content)
with open(path, "w") as of:
of.write(replaced)
of.write(patched)
def weaken_handlers(path: Path):
root = path / "Core/Src"
@@ -126,6 +123,17 @@ def weaken_handlers(path: Path):
not any(child.match(exclude) for exclude in CMAKE_EXCLUDE_SOURCES):
weaken_handlers_file(child)
def call_my_main(path: Path):
main_c = path / "Core/Src/main.c"
with open(main_c, "r") as _if:
content = _if.read()
patched = content.replace("/* USER CODE BEGIN PFP */", "/* USER CODE BEGIN PFP */\nvoid my_main();").replace("/* USER CODE BEGIN WHILE */", "/* USER CODE BEGIN WHILE */\nmy_main();")
with open(main_c, "w") as of:
of.write(patched)
def load_config(path: Path):
if not path.exists():
return
@@ -145,7 +153,8 @@ def overlay_config(config: dict):
global CMAKE_TARGET
global CMAKE_EXCLUDE_SOURCES
global ASSUME_YES
global WEAK_HANDLERS
global STRONG_HANDLERS
global NO_MY_MAIN
if "discard" in config and config["discard"]:
if not isinstance(config["discard"], list):
@@ -177,11 +186,16 @@ 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")
if "strong-handlers" in config and config["strong-handlers"]:
if not isinstance(config["strong-handlers"], bool):
print("strong-handlers must be a bool")
exit(1)
WEAK_HANDLERS &= config["weak-handlers"]
STRONG_HANDLERS |= config["strong-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")
exit(1)
NO_MY_MAIN |= config["no-my-main"]
def print_config():
print("MX_DISCARD_PATHS", MX_DISCARD_PATHS)
@@ -190,7 +204,8 @@ 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)
print("STRONG_HANDLERS", STRONG_HANDLERS)
print("NO_MY_MAIN", NO_MY_MAIN)
def main2(ns: Namespace):
if not ns.path.is_dir():
@@ -208,16 +223,20 @@ def main2(ns: Namespace):
recurse_cmakelists(ns.path)
root_cmakelists(ns.path)
if WEAK_HANDLERS:
if not STRONG_HANDLERS:
weaken_handlers(ns.path)
if not NO_MY_MAIN:
call_my_main(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("-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_false", help="Disable automatic adding of __weak__ attribute to handler functions.")
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.")