|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | +# Copyright (c) 2026, The OpenROAD Authors |
| 4 | +# |
| 5 | +# Embed JS/CSS files as C++ raw string literals for the standalone |
| 6 | +# timing report HTML. Produces a .cpp file with const char* constants. |
| 7 | +# |
| 8 | +# Each JS file is wrapped in an IIFE to isolate file-private const/let |
| 9 | +# declarations. Exported symbols are forwarded to outer-scope vars. |
| 10 | + |
| 11 | +import argparse |
| 12 | +import re |
| 13 | + |
| 14 | + |
| 15 | +def process_js_file(content): |
| 16 | + """Process a single JS file for concatenation into a shared scope.""" |
| 17 | + # Remove import lines. |
| 18 | + content = re.sub(r"^import\s+.*;\s*$", "", content, flags=re.MULTILINE) |
| 19 | + |
| 20 | + # Find exported names and strip the export keyword. |
| 21 | + # Two patterns: |
| 22 | + # 1. export function/class/const Name ... |
| 23 | + # 2. export { InternalA as ExportedA, InternalB, ... } |
| 24 | + exported_names = [] |
| 25 | + |
| 26 | + # Pattern 1: export function/class/const Name |
| 27 | + def capture_export_decl(m): |
| 28 | + keyword = m.group(1) # function, class, or const |
| 29 | + name = m.group(2) |
| 30 | + exported_names.append(name) |
| 31 | + return keyword + " " + name |
| 32 | + |
| 33 | + content = re.sub( |
| 34 | + r"^export\s+(function|class|const)\s+(\w+)", |
| 35 | + capture_export_decl, |
| 36 | + content, |
| 37 | + flags=re.MULTILINE, |
| 38 | + ) |
| 39 | + |
| 40 | + # Pattern 2: export { InternalA as ExportedA, InternalB, ... } |
| 41 | + # Collects (internal_name, exported_name) pairs; removes the line; |
| 42 | + # will add alias assignments after the IIFE. |
| 43 | + renamed_exports = [] # (internal, exported) |
| 44 | + |
| 45 | + def capture_export_block(m): |
| 46 | + for item in m.group(1).split(","): |
| 47 | + item = item.strip() |
| 48 | + if not item: |
| 49 | + continue |
| 50 | + if " as " in item: |
| 51 | + internal, exported = item.split(" as ", 1) |
| 52 | + renamed_exports.append((internal.strip(), exported.strip())) |
| 53 | + else: |
| 54 | + renamed_exports.append((item, item)) |
| 55 | + return "" # remove the export block |
| 56 | + |
| 57 | + content = re.sub(r"export\s*\{([^}]+)\}", capture_export_block, content) |
| 58 | + |
| 59 | + content = content.strip() |
| 60 | + if not content: |
| 61 | + return "" |
| 62 | + |
| 63 | + # Merge both export lists into a unified set of outer-scope names. |
| 64 | + all_exported = list(exported_names) |
| 65 | + for _, exported in renamed_exports: |
| 66 | + if exported not in all_exported: |
| 67 | + all_exported.append(exported) |
| 68 | + |
| 69 | + if not all_exported: |
| 70 | + # No exports — wrap in a bare block for const/let isolation. |
| 71 | + return "{\n" + content + "\n}" |
| 72 | + |
| 73 | + # Wrap in an IIFE. Exported names get outer-scope `var` declarations. |
| 74 | + lines = [] |
| 75 | + lines.append("var " + ", ".join(all_exported) + ";") |
| 76 | + # IIFE returns an object with the exported names. |
| 77 | + # For pattern-1 exports, the name is the same inside and out. |
| 78 | + # For pattern-2 exports, we map internal -> exported. |
| 79 | + return_pairs = [] |
| 80 | + for name in exported_names: |
| 81 | + return_pairs.append(name + ": " + name) |
| 82 | + for internal, exported in renamed_exports: |
| 83 | + return_pairs.append(exported + ": " + internal) |
| 84 | + exports_obj = "{ " + ", ".join(return_pairs) + " }" |
| 85 | + lines.append("var __e = (function() {") |
| 86 | + lines.append(content) |
| 87 | + lines.append("return " + exports_obj + ";") |
| 88 | + lines.append("})();") |
| 89 | + # Assign from returned object to outer vars. |
| 90 | + for name in all_exported: |
| 91 | + lines.append(name + " = __e." + name + ";") |
| 92 | + return "\n".join(lines) |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + parser = argparse.ArgumentParser() |
| 97 | + parser.add_argument("--output", "-o", required=True) |
| 98 | + parser.add_argument("--css", required=True, help="style.css path") |
| 99 | + parser.add_argument( |
| 100 | + "--js", nargs="+", required=True, help="JS files in dependency order" |
| 101 | + ) |
| 102 | + args = parser.parse_args() |
| 103 | + |
| 104 | + # Read and process JS files. |
| 105 | + js_parts = [] |
| 106 | + for path in args.js: |
| 107 | + with open(path, encoding="utf-8") as f: |
| 108 | + content = f.read() |
| 109 | + js_parts.append(f'// ── {path.split("/")[-1]} ──') |
| 110 | + js_parts.append(process_js_file(content)) |
| 111 | + combined_js = "\n".join(js_parts) |
| 112 | + |
| 113 | + # Read CSS. |
| 114 | + with open(args.css, encoding="utf-8") as f: |
| 115 | + css_content = f.read() |
| 116 | + |
| 117 | + with open(args.output, "w", encoding="utf-8") as out: |
| 118 | + out.write("// Auto-generated — do not edit.\n") |
| 119 | + out.write("#include <string_view>\n") |
| 120 | + out.write("namespace web {\n") |
| 121 | + out.write('extern const std::string_view kReportCSS = R"__CSS__(\n') |
| 122 | + out.write(css_content) |
| 123 | + out.write(')__CSS__";\n\n') |
| 124 | + out.write('extern const std::string_view kReportJS = R"__JS__(\n') |
| 125 | + out.write(combined_js) |
| 126 | + out.write(')__JS__";\n') |
| 127 | + out.write("} // namespace web\n") |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments