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

@@ -36,7 +36,7 @@ All the relevant settings are under the 'Project Manager' tab and should be set
Setting | Value Setting | Value
-- | -- -- | --
Project > Do not generate the main() | SET Project > Do not generate the main() | UNSET
Project > Toolchain / IDE | 'CMake' Project > Toolchain / IDE | 'CMake'
Code Generator > STM32Cube MCU packages and embedded software packages | 'Copy only necessary library files' Code Generator > STM32Cube MCU packages and embedded software packages | 'Copy only necessary library files'
Code Generator > Generate peripheral initialization as a pair of '.c/.h' files per peripheral | SET Code Generator > Generate peripheral initialization as a pair of '.c/.h' files per peripheral | SET
@@ -66,3 +66,7 @@ After project creation of after modifying the IOC, code generation needs to be i
### The remaining work ### The remaining work
After code generation and convertion, a more manual part of project setup remains. MX Convert only prepares code for integration with CMake projects, the base CMake should be built around the cubemx folder, and it's setup is outside the scope of this tool and guide. After code generation and convertion, a more manual part of project setup remains. MX Convert only prepares code for integration with CMake projects, the base CMake should be built around the cubemx folder, and it's setup is outside the scope of this tool and guide.
### TODO
Explain `my_main` and FREERTOS use.

View File

@@ -10,10 +10,6 @@ MX_DISCARD_PATHS = [
".mxproject", ".mxproject",
"CMakePresets.json", "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 = [ SOURCE_EXTS = [
"*.c", "*.c",
@@ -26,9 +22,10 @@ HEADER_EXTS = [
CMAKE_EXCLUDE_SOURCES = [ CMAKE_EXCLUDE_SOURCES = [
"*_template.c" "*_template.c"
] ]
CMAKE_TARGET = "" # Set from config CMAKE_TARGET = ""
ASSUME_YES = False ASSUME_YES = False
WEAK_HANDLERS = True STRONG_HANDLERS = False
NO_MY_MAIN = False
def mx_cleanup(path: Path): def mx_cleanup(path: Path):
@@ -113,10 +110,10 @@ def weaken_handlers_file(path: Path):
content = _if.read() content = _if.read()
myre = re.compile(r"void ([A-Z][A-Za-z]+)_Handler\(void\)") 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: with open(path, "w") as of:
of.write(replaced) of.write(patched)
def weaken_handlers(path: Path): def weaken_handlers(path: Path):
root = path / "Core/Src" root = path / "Core/Src"
@@ -126,6 +123,17 @@ def weaken_handlers(path: Path):
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) 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): def load_config(path: Path):
if not path.exists(): if not path.exists():
return return
@@ -145,7 +153,8 @@ 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 global STRONG_HANDLERS
global NO_MY_MAIN
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):
@@ -177,11 +186,16 @@ 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 "strong-handlers" in config and config["strong-handlers"]:
if not isinstance(config["weak-handlers"], bool): if not isinstance(config["strong-handlers"], bool):
print("weak-handlers must be a bool") print("strong-handlers must be a bool")
exit(1) 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(): def print_config():
print("MX_DISCARD_PATHS", MX_DISCARD_PATHS) print("MX_DISCARD_PATHS", MX_DISCARD_PATHS)
@@ -190,7 +204,8 @@ 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) print("STRONG_HANDLERS", STRONG_HANDLERS)
print("NO_MY_MAIN", NO_MY_MAIN)
def main2(ns: Namespace): def main2(ns: Namespace):
if not ns.path.is_dir(): if not ns.path.is_dir():
@@ -208,16 +223,20 @@ def main2(ns: Namespace):
recurse_cmakelists(ns.path) recurse_cmakelists(ns.path)
root_cmakelists(ns.path) root_cmakelists(ns.path)
if WEAK_HANDLERS: if not STRONG_HANDLERS:
weaken_handlers(ns.path) weaken_handlers(ns.path)
if not NO_MY_MAIN:
call_my_main(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("-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_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("-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.3.2" version = "0.4.0"
description = "A Cube MX projct conversion helper" description = "A Cube MX projct conversion helper"
authors = [{ name = "Didas72" }] authors = [{ name = "Didas72" }]
dependencies = [] dependencies = []