diff --git a/mx_convert/mx_convert.py b/mx_convert/mx_convert.py index ca3363b..59f9afd 100644 --- a/mx_convert/mx_convert.py +++ b/mx_convert/mx_convert.py @@ -25,7 +25,7 @@ HEADER_EXTS = [ CMAKE_EXCLUDE_SOURCES = [ "*_template.c" ] -CMAKE_TARGET = "" # Set from argv +CMAKE_TARGET = "" # Set from config ASSUME_YES = False @@ -80,15 +80,35 @@ def recurse_cmakelists(path: Path): lines.append(")") lines += ["add_subdirectory(%s)"%(directory.name) for directory in dirs] - # Write CMakeLists - lists_path = path / "CMakeLists.txt" - with open(lists_path, "w") as of: - of.write("\n".join(lines)+"\n") + # Write CMakeLists if not empty + if len(lines) != 0: + lists_path = path / "CMakeLists.txt" + with open(lists_path, "w") as of: + of.write("\n".join(lines)+"\n") # Recurse for directory in dirs: recurse_cmakelists(directory) +def root_cmakelists(path: Path): + lines: list[str] = [] + lists_path = path / "CMakeLists.txt" + + # Define custom libraries + lines.append("add_library(%s STATIC)"%(CMAKE_TARGET)) + lines.append("target_compile_definitions(%s PRIVATE ${COMPILER_DEFINES})"%(CMAKE_TARGET)) + lines.append("target_compile_options(${EXECUTABLE} PRIVATE \"-Os -ffunction-sections -fdata-sections -g -lto\")") + lines.append("target_link_options(${EXECUTABLE} PRIVATE \"-T ${LINKER_SCRIPT} --specs=nano.specs -Wl,--gc-sections\")") + + # Read previously generated contents + with open(lists_path, "r") as _if: + lines = _if.readlines() + + # Save final result + with open(lists_path, "w") as of: + of.write("\n".join(lines)+"\n") + pass + def load_config(path: Path): if not path.exists(): return @@ -162,6 +182,7 @@ def main2(ns: Namespace): mx_cleanup(ns.path) recurse_cmakelists(ns.path) + root_cmakelists(ns.path) def main(): parser = ArgumentParser(prog="MX Convert") @@ -169,7 +190,7 @@ def main(): 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("-s", "--source", action="append", help="Add a pattern to the sources list, single wildcards allowed. Includes *.c and *.s by default.") - parser.add_argument("-t", "--target", action="store", default="${EXECUTABLE}", help="The CMake target to generate references to. Defaults to '${EXECUTABLE}'.") + 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.") parser.add_argument("--show-cfg", action="store_true", help="Show configs that would be used to run and exit successfully.")