forked from stm32duino/Arduino_Core_STM32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: easy_setup prompts board codenames
Using a fuzzy find after parsing boards.txt. This has required refactoring the board parser code in a dedicated modules, with minor impacts in the files that depended on it.
- Loading branch information
Showing
5 changed files
with
168 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import pathlib | ||
import argparse | ||
|
||
from jinja2 import Environment, FileSystemLoader | ||
|
||
from parse_boards import parse_file | ||
|
||
|
||
def get_fpconf(config) : | ||
fpu = (config.build.get("fpu") or "-mfpu=").rsplit("=", 1)[1] | ||
abi = (config.build.get("float-abi") or "-mfloat-abi=").rsplit("=", 1)[1] | ||
return f"{fpu}-{abi}" | ||
|
||
def boardstxt_filter(key) : | ||
# Remove menu entry labels | ||
# In our data model, they conflict with the actual configuration they are associated to | ||
# i.e. Nucleo_144.menu.pnum.NUCLEO_F207ZG would be both a string ("Nucleo F207ZG") | ||
# and a dict (.build.variant_h=..., .upload.maximum_size=...) | ||
|
||
|
||
if key[0] == "menu" : | ||
# menu.xserial=U(S)ART support | ||
return True | ||
if len(key) == 4 and key[1] == "menu": | ||
# Nucleo_144.menu.pnum.NUCLEO_F207ZG=Nucleo F207ZG | ||
# Midatronics.menu.upload_method.MassStorage=Mass Storage | ||
return True | ||
|
||
# Remove upload_method also, that's out of our scope and requires more logic | ||
if len(key) >= 3 and key[2] == "upload_method" : | ||
return True | ||
|
||
return False | ||
|
||
def platformtxt_filter(key) : | ||
# reject everything but build.** | ||
# and also build.info (that's specific to the build system, we'll hard-code it) | ||
# we don't need anything else from platform.txt | ||
# +additional stuff might confuse later parts of the script | ||
# e.g.: | ||
|
||
# compiler.warning_flags=-w | ||
# compiler.warning_flags.none=-w | ||
# compiler.warning_flags.default= | ||
# compiler.warning_flags.more=-Wall | ||
# compiler.warning_flags.all=-Wall -Wextra | ||
|
||
if key[0] == "build" and key[1] != "info" : | ||
return False | ||
return True | ||
|
||
def regenerate_template(config, infile, outfile) : | ||
j2_env = Environment( | ||
loader=FileSystemLoader(str(infile.parent)), trim_blocks=True, lstrip_blocks=True | ||
) | ||
cmake_template = j2_env.get_template(infile.name) | ||
|
||
with open(outfile, "w") as out : | ||
out.write(cmake_template.render( | ||
allcfg = config, | ||
)) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-b", "--boards", type=pathlib.Path, required=True, help="path to boards.txt") | ||
parser.add_argument("-p", "--platform", type=pathlib.Path, required=True, help="path to platform.txt") | ||
parser.add_argument("-t", "--template", type=pathlib.Path, required=True, help="path to the jinja template") | ||
parser.add_argument("-o", "--outfile", type=pathlib.Path, required=True, help="path to the cmake database to generate") | ||
|
||
shargs = parser.parse_args() | ||
|
||
platformtxt_cfg = parse_file(shargs.platform, reject=platformtxt_filter) | ||
platformtxt_cfg = {"build":platformtxt_cfg["build"]} # whitelist what we need | ||
|
||
boardstxt_cfg = parse_file(shargs.boards, reject=boardstxt_filter) | ||
del boardstxt_cfg["menu"] # blacklist what we don't need | ||
|
||
# these are optional features to be picked out by the user | ||
BOARD_FEATURES = [ | ||
"enable_virtio", | ||
"enable_usb", | ||
"usb_speed", | ||
"xSerial", | ||
] | ||
|
||
allboards = dict() | ||
|
||
for fam, famcfg in boardstxt_cfg.items() : | ||
famcfg.set_default_entries(platformtxt_cfg) | ||
|
||
inherit_fam = famcfg.copy() | ||
# shallow copy; we don't want to impact famcfg so we have to copy before edit/del | ||
inherit_fam["menu"] = inherit_fam["menu"].copy() | ||
# del what you iterate over (otherwise you get infinite nesting) | ||
del inherit_fam["menu"]["pnum"] | ||
|
||
for board, boardcfg in famcfg.menu.pnum.items() : | ||
boardcfg.set_default_entries(inherit_fam) | ||
|
||
inherit_board = boardcfg.copy() | ||
del inherit_board["menu"] | ||
|
||
board_feature_names = tuple(boardcfg["menu"].keys()) | ||
|
||
for fname in board_feature_names : | ||
for label, labelcfg in boardcfg["menu"][fname].items() : | ||
labelcfg.set_default_entries(inherit_board) | ||
labelcfg.evaluate_entries() | ||
|
||
# base config won't manage all the board features, we thus have to mask them out | ||
for feat in BOARD_FEATURES : | ||
boardcfg.build[feat] = "" | ||
|
||
boardcfg.evaluate_entries() | ||
|
||
boardcfg["_fpconf"] = get_fpconf(boardcfg) | ||
allboards[board] = boardcfg | ||
|
||
regenerate_template(allboards, shargs.template, shargs.outfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters