Tools: make create_OEM_board.py: handle .inc files

This commit is contained in:
Peter Barker 2024-08-14 18:38:31 +10:00 committed by Peter Barker
parent 669cb6563b
commit 7037164d08
1 changed files with 50 additions and 14 deletions

View File

@ -13,20 +13,56 @@ import subprocess
import pathlib
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../libraries/AP_HAL_ChibiOS/hwdef/scripts'))
import chibios_hwdef # noqa
class OEMCreate:
def __init__(self, oem_board_name, board_name):
self.oem_board_name = oem_board_name
self.board_name = board_name
def run(self):
hwdef_dir = "libraries/AP_HAL_ChibiOS/hwdef"
oem_board_dirpath = f"{hwdef_dir}/{oem_board_name}"
if os.path.exists(oem_board_dirpath):
raise ValueError(f"{oem_board_dirpath} already exists") # FIXME exception type
hwdef_include_relpath = None
for extension in "dat", "inc":
tmp = f"../{board_name}/hwdef.{extension}"
tmp_norm = os.path.normpath(f"{oem_board_dirpath}/{tmp}")
if os.path.exists(tmp_norm):
hwdef_include_relpath = tmp
hwdef_include_normpath = tmp_norm
break
hwdef_content = f"include {hwdef_include_relpath}\n"
ch = chibios_hwdef.ChibiOSHWDef(hwdef_include_normpath)
use_bootloader_from_board = ch.get_config('USE_BOOTLOADER_FROM_BOARD', default=None, required=False)
if use_bootloader_from_board is None:
hwdef_content += "\n"
hwdef_content += f"USE_BOOTLOADER_FROM_BOARD {board_name}\n"
subprocess.run(["mkdir", oem_board_dirpath])
# create files and add reference to originals
new_hwdef = pathlib.Path(oem_board_dirpath, "hwdef.dat")
if hwdef_include_relpath is None:
raise ValueError(f"Could not find .inc or .dat for {board_name}")
new_hwdef.write_text(hwdef_content)
if os.path.exists(f"{hwdef_dir}/{board_name}/defaults.parm"):
path = pathlib.Path(f"{hwdef_dir}/{oem_board_name}/defaults.parm")
path.write_text(f"@include ../{board_name}/defaults.parm\n") # noqa
board_name = sys.argv[1]
oem_board_name = sys.argv[2]
# directory creation
if not os.path.exists("libraries/AP_HAL_ChibiOS/hwdef/%s" % oem_board_name):
subprocess.run(["mkdir", "libraries/AP_HAL_ChibiOS/hwdef/%s" % oem_board_name])
# create files and add reference to originals
f = open("libraries/AP_HAL_ChibiOS/hwdef/%s/hwdef.dat" % oem_board_name, "x")
f.write("include ../%s/hwdef.dat\n" % board_name)
f.close()
f = open("libraries/AP_HAL_ChibiOS/hwdef/%s/hwdef-bl.dat" % oem_board_name, "x")
f.write("include ../%s/hwdef-bl.dat\n" % board_name)
f.close()
if os.path.exists("libraries/AP_HAL_ChibiOS/hwdef/%s/defaults.parm" % board_name):
path = pathlib.Path(f"libraries/AP_HAL_ChibiOS/hwdef/{oem_board_name}/defaults.parm")
path.write_text(f"@include ../{board_name}/defaults.parm\n") # noqa
oemcreate = OEMCreate(board_name, oem_board_name)
oemcreate.run()