Compare commits

...

2 Commits

2 changed files with 24 additions and 21 deletions

View File

@@ -3,7 +3,6 @@ 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",
@@ -74,7 +73,7 @@ def recurse_cmakelists(path: Path):
if has_headers: if has_headers:
lines.append("target_include_directories(%s PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})"%(CMAKE_TARGET)) lines.append("target_include_directories(%s PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})"%(CMAKE_TARGET))
if len(sources) != 0: if len(sources) != 0:
lines.append("target_sources(%s PUBLIC"%(CMAKE_TARGET)) lines.append("target_sources(%s PRIVATE"%(CMAKE_TARGET))
lines += ["\t%s"%(src.name) for src in sources] lines += ["\t%s"%(src.name) for src in sources]
lines.append(")") lines.append(")")
lines += ["add_subdirectory(%s)"%(directory.name) for directory in dirs] lines += ["add_subdirectory(%s)"%(directory.name) for directory in dirs]
@@ -92,10 +91,9 @@ def root_cmakelists(path: Path):
lists_path = path / "CMakeLists.txt" lists_path = path / "CMakeLists.txt"
# Define custom libraries # Define custom libraries
lines.append("add_library(%s STATIC)"%(CMAKE_TARGET)) lines.append("add_library(%s OBJECT)"%(CMAKE_TARGET))
lines.append("target_compile_definitions(%s PRIVATE ${COMPILER_DEFINES})"%(CMAKE_TARGET)) lines.append("target_compile_definitions(%s PRIVATE ${COMPILER_DEFINES})"%(CMAKE_TARGET))
lines.append("target_compile_options(%s PRIVATE -Os -ffunction-sections -fdata-sections -g -flto)"%(CMAKE_TARGET)) lines.append("target_compile_options(%s PRIVATE -Os -ffunction-sections -fdata-sections -g -flto)"%(CMAKE_TARGET))
lines.append("target_link_options(%s PRIVATE -T ${LINKER_SCRIPT} -Wl,--gc-sections -flto)"%(CMAKE_TARGET))
# Read previously generated contents # Read previously generated contents
with open(lists_path, "r") as _if: with open(lists_path, "r") as _if:
@@ -112,7 +110,9 @@ def inject_handlers_file(path: Path):
patched = content patched = content
for handler in INJECT_HANDLERS: for handler in INJECT_HANDLERS:
patched = patched.replace("void %s_Handler(void){"%(handler), "void %s_Handler(void){\nmy_%s();"%(handler, handler)) patched = patched \
.replace("void %sHandler(void)\n{\n /*"%(handler), "void %sHandler(void)\n{\n my_%s();\n /*"%(handler, handler)) \
.replace("/* USER CODE BEGIN PFP */\n\n", "/* USER CODE BEGIN PFP */\nvoid my_%s();\n"%(handler))
with open(path, "w") as of: with open(path, "w") as of:
of.write(patched) of.write(patched)
@@ -131,13 +131,14 @@ def call_my_main(path: Path):
with open(main_c, "r") as _if: with open(main_c, "r") as _if:
content = _if.read() 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();") patched = content.replace("/* USER CODE BEGIN PFP */\n\n", "/* USER CODE BEGIN PFP */\nvoid my_main();\n").replace("/* USER CODE BEGIN WHILE */\n while (1)", "/* USER CODE BEGIN WHILE */\n my_main();\n while (1)")
with open(main_c, "w") as of: with open(main_c, "w") as of:
of.write(patched) of.write(patched)
def load_config(path: Path): def load_config(path: Path):
if not path.exists(): if not path.exists():
print("WARN: No config file", path)
return return
if not path.is_file(): if not path.is_file():
print("Config '%s' is not a file."%(path)) print("Config '%s' is not a file."%(path))
@@ -183,21 +184,21 @@ def overlay_config(config: dict):
print("exclude must be a list of strings") print("exclude must be a list of strings")
exit(1) exit(1)
CMAKE_EXCLUDE_SOURCES = config["exclude"] CMAKE_EXCLUDE_SOURCES = config["exclude"]
if "assume-yes" in config and config["assume-yes"]: if "assume_yes" in config and config["assume_yes"]:
if not isinstance(config["assume-yes"], bool): if not isinstance(config["assume_yes"], bool):
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 "inject-handlers" in config and config["inject-handlers"]: if "inject_handlers" in config and config["inject_handlers"]:
if not isinstance(config["inject-handlers"], list): if not isinstance(config["inject_handlers"], list):
print("inject-handlers must be a list of strings") print("inject_handlers must be a list of strings")
exit(1) exit(1)
INJECT_HANDLERS += config["inject-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")
exit(1) exit(1)
NO_MY_MAIN |= config["no-my-main"] 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)
@@ -225,17 +226,19 @@ def main2(ns: Namespace):
recurse_cmakelists(ns.path) recurse_cmakelists(ns.path)
root_cmakelists(ns.path) root_cmakelists(ns.path)
if not INJECT_HANDLERS: if INJECT_HANDLERS:
inject_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)
print("Convert complete")
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("-i", "--inject-handlers", action="store", help="Add a call to your own handler functions in MX generated code.") parser.add_argument("-i", "--inject-handlers", action="append", help="Add a call to your own handler functions in MX generated code. Ex: for PendSV_Handler use '-i PendSV_'")
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.")

View File

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