|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import logging |
| 4 | +import shlex |
| 5 | +from src.openroad_mcp.server.orfs.orfs_tools import ORFS |
| 6 | + |
| 7 | + |
| 8 | +def _should_skip_gui() -> bool: |
| 9 | + """Check if GUI commands should be skipped based on environment variable.""" |
| 10 | + return os.getenv("DISABLE_GUI", "false").lower() in ("true", "1", "yes") |
| 11 | + |
| 12 | + |
| 13 | +class ORFSBase(ORFS): |
| 14 | + def _get_platforms_impl(self) -> str: |
| 15 | + """Internal implementation of get_platforms""" |
| 16 | + # TODO: scrape platforms instead of serving only default sky130 |
| 17 | + assert ORFS.server is not None |
| 18 | + ORFS.server.platform = "sky130hd" |
| 19 | + return ORFS.server.platform |
| 20 | + |
| 21 | + def _get_designs_impl(self) -> str: |
| 22 | + """Internal implementation of get_designs""" |
| 23 | + # TODO: scrape designs instead of default riscv |
| 24 | + assert ORFS.server is not None |
| 25 | + ORFS.server.design = "riscv32i" |
| 26 | + return ORFS.server.design |
| 27 | + |
| 28 | + def _check_configuration(self) -> None: |
| 29 | + assert ORFS.server is not None |
| 30 | + if not ORFS.server.platform: |
| 31 | + ORFS.server._get_platforms_impl() |
| 32 | + logging.info(ORFS.server.platform) |
| 33 | + |
| 34 | + if not ORFS.server.design: |
| 35 | + ORFS.server._get_designs_impl() |
| 36 | + logging.info(ORFS.server.design) |
| 37 | + |
| 38 | + def _command(self, cmd: str) -> None: |
| 39 | + assert ORFS.server is not None |
| 40 | + working = os.getcwd() |
| 41 | + os.chdir(ORFS.server.flow_dir) |
| 42 | + |
| 43 | + make = f"make DESIGN_CONFIG={ORFS.server.flow_dir}/designs/{ORFS.server.platform}/{ORFS.server.design}/config.mk" |
| 44 | + logging.info(cmd) |
| 45 | + build_command = f"{make} {cmd}" |
| 46 | + ORFS.server._run_command(build_command) |
| 47 | + |
| 48 | + os.chdir(working) |
| 49 | + |
| 50 | + def _run_command(self, cmd: str) -> None: |
| 51 | + assert ORFS.server is not None |
| 52 | + logging.info("start command") |
| 53 | + |
| 54 | + process = subprocess.Popen( |
| 55 | + shlex.split(cmd), |
| 56 | + stdout=subprocess.PIPE, |
| 57 | + stderr=subprocess.STDOUT, |
| 58 | + bufsize=1, # Line-buffered |
| 59 | + universal_newlines=True, # Text mode |
| 60 | + env=ORFS.server.env, |
| 61 | + ) |
| 62 | + |
| 63 | + if process.stdout: |
| 64 | + for line in process.stdout: |
| 65 | + logging.info(line.rstrip()) |
| 66 | + |
| 67 | + process.wait() |
| 68 | + if process.returncode != 0: |
| 69 | + logging.error(f"Command exited with return code {process.returncode}") |
| 70 | + raise subprocess.CalledProcessError(process.returncode, cmd) |
| 71 | + |
| 72 | + ### mcp tool section ### |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + @ORFS.mcp.tool |
| 76 | + def get_platforms() -> str: |
| 77 | + """call get platforms to display possible platforms to run through flow""" |
| 78 | + assert ORFS.server is not None |
| 79 | + return ORFS.server._get_platforms_impl() |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + @ORFS.mcp.tool |
| 83 | + def get_designs() -> str: |
| 84 | + """call get designs to display possible designs to run through flow""" |
| 85 | + assert ORFS.server is not None |
| 86 | + return ORFS.server._get_designs_impl() |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + @ORFS.mcp.tool |
| 90 | + def make(cmd: str) -> str: |
| 91 | + """Execute a makefile target for OpenROAD-flow-scripts. |
| 92 | +
|
| 93 | + Common commands: |
| 94 | + - "clean" - Remove all build artifacts and start fresh |
| 95 | + - "synth" - Run synthesis |
| 96 | + - "place" - Run placement |
| 97 | + - "route" - Run routing |
| 98 | + - "final" - Generate final reports |
| 99 | +
|
| 100 | + Use this for any makefile target not covered by step/jump commands. |
| 101 | + """ |
| 102 | + assert ORFS.server is not None |
| 103 | + ORFS.server._check_configuration() |
| 104 | + ORFS.server._command(cmd) |
| 105 | + |
| 106 | + return f"finished {cmd}" |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + @ORFS.mcp.tool |
| 110 | + def get_stage_names() -> str: |
| 111 | + """get stage names for possible states this mcp server can be in the chip design pipeline""" |
| 112 | + assert ORFS.server is not None |
| 113 | + stage_names = [_.info() for _ in ORFS.server.stages.values()] |
| 114 | + logging.info(stage_names) # in server process |
| 115 | + # for chatbot output |
| 116 | + result = "" |
| 117 | + for _ in stage_names: |
| 118 | + result += f"{_}\n" |
| 119 | + return result |
| 120 | + |
| 121 | + @staticmethod |
| 122 | + @ORFS.mcp.tool |
| 123 | + def jump(stage: str) -> str: |
| 124 | + """Jump directly to a specific stage in the chip design pipeline. |
| 125 | +
|
| 126 | + Valid stage names (MUST use exact names): |
| 127 | + - "synth" - Synthesis |
| 128 | + - "floorplan" - Floorplan |
| 129 | + - "place" - Placement |
| 130 | + - "cts" - Clock Tree Synthesis |
| 131 | + - "route" - Routing |
| 132 | + - "final" - Final Report |
| 133 | +
|
| 134 | + Use get_stage_names() to see all available stages. |
| 135 | + """ |
| 136 | + assert ORFS.server is not None |
| 137 | + ORFS.server._check_configuration() |
| 138 | + |
| 139 | + stage_names = [_.info() for _ in ORFS.server.stages.values()] |
| 140 | + logging.info(stage_names) |
| 141 | + if stage in stage_names: |
| 142 | + logging.info(stage) |
| 143 | + ORFS.server.cur_stage = ORFS.server.stage_index[stage] |
| 144 | + |
| 145 | + ORFS.server._command(stage) |
| 146 | + |
| 147 | + # Open GUI if not disabled |
| 148 | + if not _should_skip_gui(): |
| 149 | + try: |
| 150 | + ORFS.server._command(f"gui_{stage}") |
| 151 | + except subprocess.CalledProcessError as e: |
| 152 | + logging.warning(f"GUI command failed: {e}") |
| 153 | + else: |
| 154 | + logging.info("Skipping GUI command (DISABLE_GUI=true)") |
| 155 | + |
| 156 | + return f"finished {stage}" |
| 157 | + else: |
| 158 | + logging.info("jump unsuccessful..") |
| 159 | + return f"aborted {stage}" |
| 160 | + |
| 161 | + @staticmethod |
| 162 | + @ORFS.mcp.tool |
| 163 | + def step() -> str: |
| 164 | + """Progress to the next stage in the chip design pipeline (synthesis -> floorplan -> placement -> CTS -> routing -> final report)""" |
| 165 | + assert ORFS.server is not None |
| 166 | + |
| 167 | + def make_keyword() -> str: |
| 168 | + assert ORFS.server is not None |
| 169 | + logging.info(ORFS.server.cur_stage) |
| 170 | + if ORFS.server.cur_stage <= len(ORFS.server.stages) - 2: |
| 171 | + ORFS.server.cur_stage += 1 |
| 172 | + else: |
| 173 | + logging.info("end of pipeline..") |
| 174 | + return ORFS.server.stages[ORFS.server.cur_stage].info() |
| 175 | + |
| 176 | + ORFS.server._check_configuration() |
| 177 | + |
| 178 | + command = make_keyword() |
| 179 | + ORFS.server._command(command) |
| 180 | + |
| 181 | + # Open GUI if not disabled |
| 182 | + if not _should_skip_gui(): |
| 183 | + try: |
| 184 | + ORFS.server._command(f"gui_{command}") |
| 185 | + except subprocess.CalledProcessError as e: |
| 186 | + logging.warning(f"GUI command failed: {e}") |
| 187 | + else: |
| 188 | + logging.info("Skipping GUI command (DISABLE_GUI=true)") |
| 189 | + |
| 190 | + return f"finished {command}" |
| 191 | + |
| 192 | + # TODO: scrape all makefile keywords and make into mcp tool |
| 193 | + @staticmethod |
| 194 | + def get_all_keywords() -> None: |
| 195 | + pass |
0 commit comments