|
| 1 | +"""Repository rule that parses config.mk files and generates a .bzl with design configs. |
| 2 | +
|
| 3 | +The generated .bzl file contains a DESIGNS dict that orfs_design() reads |
| 4 | +to create orfs_flow() targets with properly routed stage arguments. |
| 5 | +
|
| 6 | +Each config.mk is read via repository_ctx.read() so that Bazel watches |
| 7 | +them and re-fetches the repo when any config.mk changes. |
| 8 | +""" |
| 9 | + |
| 10 | +def _find_config_files(repository_ctx, designs_dir, platforms): |
| 11 | + """Walk designs_dir to find all config.mk files.""" |
| 12 | + configs = [] |
| 13 | + for platform in platforms: |
| 14 | + platform_dir = designs_dir + "/" + platform |
| 15 | + # List design directories under this platform |
| 16 | + result = repository_ctx.execute(["find", platform_dir, "-name", "config.mk", "-type", "f"]) |
| 17 | + if result.return_code == 0: |
| 18 | + for line in result.stdout.strip().split("\n"): |
| 19 | + if line: |
| 20 | + configs.append(line) |
| 21 | + return configs |
| 22 | + |
| 23 | +def _orfs_designs_impl(repository_ctx): |
| 24 | + parser_path = repository_ctx.path(repository_ctx.attr._parser) |
| 25 | + designs_path = repository_ctx.path(repository_ctx.attr.designs_dir) |
| 26 | + designs_dir = str(designs_path.dirname) |
| 27 | + |
| 28 | + python = repository_ctx.which("python3") or repository_ctx.which("python") |
| 29 | + if not python: |
| 30 | + fail("python3 not found in PATH") |
| 31 | + |
| 32 | + # Read every config.mk to register them as watched inputs. |
| 33 | + # When any config.mk changes, Bazel will re-fetch this repo. |
| 34 | + config_files = _find_config_files( |
| 35 | + repository_ctx, |
| 36 | + designs_dir, |
| 37 | + repository_ctx.attr.platforms, |
| 38 | + ) |
| 39 | + for config_file in config_files: |
| 40 | + repository_ctx.read(config_file) |
| 41 | + |
| 42 | + platforms_arg = ",".join(repository_ctx.attr.platforms) |
| 43 | + result = repository_ctx.execute( |
| 44 | + [python, str(parser_path), "--all", designs_dir, "--platforms", platforms_arg, "--json"], |
| 45 | + timeout = 120, |
| 46 | + ) |
| 47 | + |
| 48 | + if result.return_code != 0: |
| 49 | + fail("config_mk_parser.py failed (dir=%s):\nstdout: %s\nstderr: %s" % (designs_dir, result.stdout, result.stderr)) |
| 50 | + |
| 51 | + if not result.stdout or not result.stdout.strip(): |
| 52 | + fail("config_mk_parser.py produced no output (dir=%s, stderr=%s)" % (designs_dir, result.stderr)) |
| 53 | + |
| 54 | + configs = json.decode(result.stdout) |
| 55 | + |
| 56 | + # Build a dict keyed by "platform/design_nickname" |
| 57 | + designs = {} |
| 58 | + for config in configs: |
| 59 | + platform = config.get("platform", "") |
| 60 | + nickname = config.get("design_nickname", "") |
| 61 | + name = config.get("design_name", "") or nickname |
| 62 | + if not name or not platform or not nickname: |
| 63 | + continue |
| 64 | + |
| 65 | + key = "%s/%s" % (platform, nickname) |
| 66 | + designs[key] = { |
| 67 | + "name": name, |
| 68 | + "platform": platform, |
| 69 | + "verilog_files": config.get("verilog_files", []), |
| 70 | + "sources": config.get("sources", {}), |
| 71 | + "arguments": config.get("arguments", {}), |
| 72 | + "blocks": config.get("blocks", []), |
| 73 | + } |
| 74 | + |
| 75 | + # Also add block_configs as separate entries |
| 76 | + for block_config in config.get("block_configs", []): |
| 77 | + bp = block_config.get("platform", platform) |
| 78 | + bn = block_config.get("design_nickname", "") |
| 79 | + bname = block_config.get("design_name", "") or bn |
| 80 | + if not bname or not bn: |
| 81 | + continue |
| 82 | + bkey = "%s/%s" % (bp, bn) |
| 83 | + designs[bkey] = { |
| 84 | + "name": bname, |
| 85 | + "platform": bp, |
| 86 | + "verilog_files": block_config.get("verilog_files", []), |
| 87 | + "sources": block_config.get("sources", {}), |
| 88 | + "arguments": block_config.get("arguments", {}), |
| 89 | + "blocks": [], |
| 90 | + } |
| 91 | + |
| 92 | + # Generate designs.bzl |
| 93 | + bzl_content = 'DESIGNS = %s\n' % repr(designs) |
| 94 | + repository_ctx.file("designs.bzl", bzl_content) |
| 95 | + repository_ctx.file("BUILD.bazel", "") |
| 96 | + |
| 97 | +orfs_designs = repository_rule( |
| 98 | + implementation = _orfs_designs_impl, |
| 99 | + attrs = { |
| 100 | + "designs_dir": attr.label( |
| 101 | + mandatory = True, |
| 102 | + ), |
| 103 | + "platforms": attr.string_list( |
| 104 | + default = ["asap7", "sky130hd", "sky130hs", "nangate45", "ihp-sg13g2"], |
| 105 | + ), |
| 106 | + "module_name": attr.string( |
| 107 | + default = "orfs", |
| 108 | + ), |
| 109 | + "_parser": attr.label( |
| 110 | + default = Label("//bazel:config_mk_parser.py"), |
| 111 | + allow_single_file = True, |
| 112 | + ), |
| 113 | + }, |
| 114 | +) |
0 commit comments