1""" 2Utility to autogenerate generic HAL headers. 3 4Usage:: 5 6 python3 gd32headers.py [-p /path/to/HAL] [-o /path/to/output_dir] 7 8Copyright (c) 2022 Teslabs Engineering S.L. 9 10SPDX-License-Identifier: Apache-2.0 11""" 12 13import argparse 14from pathlib import Path 15import re 16import shutil 17 18 19REPO_ROOT = Path(__file__).absolute().parents[1] 20"""Repository root (used for input/output default folders).""" 21 22 23def main(hal_path, output): 24 """Entry point. 25 26 Args: 27 hal_path: HAL repository path. 28 output: Output directory. 29 """ 30 31 # obtain all available APIs for each series 32 apis = dict() 33 for entry in sorted(hal_path.iterdir()): 34 if not entry.is_dir() or not entry.name.startswith("gd32"): 35 continue 36 37 # obtain series API headers 38 series_headers = entry / "standard_peripheral" / "include" 39 for header in series_headers.iterdir(): 40 m = re.match(r"^gd32[a-z0-9]+_([a-z0-9]+)\.h$", header.name) 41 if not m: 42 continue 43 44 api = m.group(1) 45 46 # ignore libopt (not an API) 47 if api == "libopt": 48 continue 49 50 if api not in apis: 51 apis[api] = list() 52 apis[api].append(entry.name) 53 54 if output.exists(): 55 shutil.rmtree(output) 56 output.mkdir(parents=True) 57 58 for api, all_series in apis.items(): 59 header_file = output / ("gd32_" + api + ".h") 60 with open(header_file, "w") as f: 61 f.write("/*\n") 62 f.write(" * NOTE: Autogenerated file using gd32headers.py\n") 63 f.write(" *\n") 64 f.write(" * SPDX-License-Identifier: Apache-2.0\n") 65 f.write(" */\n\n") 66 67 for i, series in enumerate(all_series): 68 f.write("#if" if i == 0 else "#elif") 69 f.write(f" defined(CONFIG_SOC_SERIES_{series.upper()})\n") 70 f.write(f"#include <{series}_{api}.h>\n") 71 f.write("#endif\n") 72 73 74if __name__ == "__main__": 75 parser = argparse.ArgumentParser() 76 parser.add_argument( 77 "-p", 78 "--hal-path", 79 type=Path, 80 default=REPO_ROOT, 81 help="Path to the HAL repository root", 82 ) 83 parser.add_argument( 84 "-o", 85 "--output", 86 type=Path, 87 default=REPO_ROOT / "common_include", 88 help="Output directory", 89 ) 90 args = parser.parse_args() 91 92 main(args.hal_path, args.output) 93