Skip to content

Commit 1442445

Browse files
oharboeclaude
andcommitted
flow: a quoted argument in OPENROAD_EXE or YOSYS_EXE stays whole
flow.sh ran the stage through $RUN_CMD ... $OPENROAD_CMD by plain word splitting, and synth.sh did the same with YOSYS_EXE. A wrapper with a quoted argument of its own, the FAQ's gdb -ex 'thread apply all bt' --args openroad, then got the quotes and the pieces: -ex, 'thread, apply, all, bt'. The make recipes did not have the problem, since the recipe shell parses the command. Both scripts now let the shell parse the command line, as a recipe does. genMetrics.py asked OPENROAD_EXE, wrapper included, for the version. Through gdb the banner was read as the version and the commit; through perf record the query overwrote perf.data. It now asks the binary alone, the last word of OPENROAD_EXE, and the FAQ says that the binary is the last word. Tests: flow.sh and synth.sh each hand a stub wrapper a quoted argument and check its argv; both fail on the scripts as they were. genMetrics's openroad_binary() is checked for a path, an unset variable and the FAQ's three wrappers. An unused import goes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 6a8e1c9 commit 1442445

8 files changed

Lines changed: 131 additions & 11 deletions

File tree

‎docs/user/FAQS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ See how to update using the Metrics [guide](../contrib/Metrics.md).
5353
Put the tool in front of the binary in `OPENROAD_EXE`. Every rule runs
5454
`OPENROAD_EXE` as a command, sub-makes included, so it reaches every
5555
stage and the `open_*`, `gui_*` and `web_*` targets. Below, `openroad`
56-
stands for the binary the flow would run otherwise.
56+
stands for the binary the flow would run otherwise. The binary is the
57+
last word of `OPENROAD_EXE`: the flow asks it alone for its version when
58+
it writes the metrics, so the tool in front is not run for that.
5759

5860
```shell
5961
# a backtrace when OpenROAD crashes, its own exit code otherwise

‎flow/scripts/flow.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ rm -f "$RESULTS_DIR/$1-failed.odb" "$RESULTS_DIR/$1-failed.sdc"
2121
eval "$OPENROAD_EXE $OPENROAD_ARGS -exit \"$SCRIPTS_DIR/noop.tcl\"" \
2222
>"$LOG_DIR/$1.tmp.log" 2>&1
2323

24-
$RUN_CMD --log "$(realpath "$LOG_DIR/$1.tmp.log")" --append --tee -- \
25-
$OPENROAD_CMD -no_splash "$SCRIPTS_DIR/run_step.tcl" -metrics "$LOG_DIR/$1.json"
24+
# OPENROAD_CMD is a command line, quotes included: a wrapper in
25+
# OPENROAD_EXE can have a quoted argument of its own, as in
26+
# gdb -ex 'thread apply all bt' --args openroad. The shell parses it,
27+
# as it does in a make recipe; plain word splitting would hand the
28+
# wrapper the quotes and the pieces.
29+
log="$(realpath "$LOG_DIR/$1.tmp.log")"
30+
eval "$RUN_CMD --log \"\$log\" --append --tee -- $OPENROAD_CMD" \
31+
'-no_splash "$SCRIPTS_DIR/run_step.tcl" -metrics "$LOG_DIR/$1.json"'
2632
)
2733

2834
# Log the hash for this step. The summary "make elapsed" in "make finish",

‎flow/scripts/synth.sh‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
set -u -eo pipefail
33
mkdir -p $RESULTS_DIR $LOG_DIR $REPORTS_DIR $OBJECTS_DIR
44
touch $2
5-
$YOSYS_EXE -V > $(realpath $2)
6-
$PYTHON_EXE "$SCRIPTS_DIR/run_command.py" --log "$(realpath $2)" --append --tee -- \
7-
$YOSYS_EXE $YOSYS_FLAGS -c $1
5+
# YOSYS_EXE is a command line, quotes included: a wrapper in it can have
6+
# a quoted argument of its own. The shell parses it, as it does in a make
7+
# recipe.
8+
eval "$YOSYS_EXE -V" > "$(realpath $2)"
9+
eval '$PYTHON_EXE "$SCRIPTS_DIR/run_command.py" --log "$(realpath $2)" --append --tee --' \
10+
"$YOSYS_EXE $YOSYS_FLAGS" '-c "$1"'
811

912
# Log result hash, mirroring flow.sh's epilogue for OpenROAD stages.
1013
# genElapsedTime.py understands .odb/.rtlil/.v so it hashes the yosys

‎flow/scripts/variables.mk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export OPENSTA_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/sta)
130130
# OPENROAD_EXE and YOSYS_EXE are commands, not only paths: a wrapper can
131131
# come first, as in OPENROAD_EXE="gdb --args openroad" or
132132
# OPENROAD_EXE="valgrind openroad". The check is on the program the command
133-
# starts with, a path or a name on PATH.
133+
# starts with, a path or a name on PATH. The binary is the last word.
134134
OPENROAD_IS_VALID := $(if $(OPENROAD_EXE),$(shell command -v $(firstword $(OPENROAD_EXE)) >/dev/null && echo "true"),)
135135

136136
export OPENROAD_ARGS = -no_init -threads $(NUM_CORES) $(OR_ARGS)

‎flow/test/test_flow_sh.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ def test_run_cmd_runs_the_stage(self):
9595
self.assertIn("Elapsed time:", log)
9696
self.assert_summary(log)
9797

98+
def test_quoted_wrapper_argument_stays_one_argument(self):
99+
# OPENROAD_EXE="gdb -ex 'thread apply all bt' --args openroad": the
100+
# quoted argument reaches the wrapper whole, not split with its quotes.
101+
argv_log = os.path.join(self.tmp_dir.name, "argv.log")
102+
wrapper = os.path.join(self.tmp_dir.name, "wrapper")
103+
with open(wrapper, "w") as f:
104+
f.write("#!/usr/bin/env bash\n" 'printf "%s\\n" "$@" > "$ARGV_LOG"\n')
105+
os.chmod(wrapper, 0o755)
106+
self.env["ARGV_LOG"] = argv_log
107+
self.env["OPENROAD_EXE"] = "true"
108+
self.env["OPENROAD_CMD"] = (
109+
f"{wrapper} -ex 'thread apply all bt' --args openroad -exit"
110+
)
111+
112+
self.run_stage()
113+
114+
with open(argv_log) as f:
115+
argv = f.read().splitlines()
116+
self.assertEqual(
117+
argv[:5],
118+
["-ex", "thread apply all bt", "--args", "openroad", "-exit"],
119+
argv,
120+
)
121+
self.assertEqual(argv[5], "-no_splash", argv)
122+
self.assertEqual(argv[7], "-metrics", argv)
123+
98124

99125
if __name__ == "__main__":
100126
unittest.main()

‎flow/test/test_openroad_exe.py‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import os
1616
import subprocess
17+
import sys
1718
import tempfile
18-
import textwrap
1919
import unittest
2020

2121
FLOW_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
@@ -150,6 +150,46 @@ def test_generated_rules_run_the_command(self):
150150
self.assertIn(flag, call.split())
151151
self.assertTrue(call.endswith("open.tcl"), call)
152152

153+
def test_synth_sh_keeps_a_quoted_wrapper_argument_whole(self):
154+
# YOSYS_EXE="wrapper -ex 'two words' yosys": synth.sh hands the
155+
# wrapper the quoted argument as one argument, both times it runs it.
156+
tmp = self.tmp_dir.name
157+
wrapper = self.script(
158+
"argv", 'printf "%s|" "$@" >> "$ARGV_LOG"\necho >> "$ARGV_LOG"\n'
159+
)
160+
scripts = os.path.join(FLOW_DIR, "scripts")
161+
env = dict(
162+
self.env,
163+
RESULTS_DIR=os.path.join(tmp, "results"),
164+
LOG_DIR=os.path.join(tmp, "logs"),
165+
REPORTS_DIR=os.path.join(tmp, "reports"),
166+
OBJECTS_DIR=os.path.join(tmp, "objects"),
167+
SCRIPTS_DIR=scripts,
168+
UTILS_DIR=os.path.join(FLOW_DIR, "util"),
169+
PYTHON_EXE=sys.executable,
170+
YOSYS_EXE=f"{wrapper} -ex 'two words' yosys",
171+
YOSYS_FLAGS="-v 3",
172+
)
173+
result = subprocess.run(
174+
[
175+
"bash",
176+
os.path.join(scripts, "synth.sh"),
177+
"synth.tcl",
178+
os.path.join(tmp, "logs", "1_2_yosys.log"),
179+
],
180+
cwd=tmp,
181+
env=env,
182+
capture_output=True,
183+
text=True,
184+
)
185+
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
186+
with open(self.log) as f:
187+
calls = f.read().splitlines()
188+
self.assertEqual(
189+
calls,
190+
["-ex|two words|yosys|-V|", "-ex|two words|yosys|-v|3|-c|synth.tcl|"],
191+
)
192+
153193
def test_gdb_and_valgrind_are_not_switches(self):
154194
# a debugger or valgrind goes in OPENROAD_EXE; GDB and VALGRIND
155195
# set to anything leave the command as it is

‎flow/util/genMetrics.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ def merge_jsons(root_path, output, files):
215215
output.update(data)
216216

217217

218+
def openroad_binary():
219+
"""The binary OPENROAD_EXE runs: its last word.
220+
221+
OPENROAD_EXE is a command, and a wrapper such as gdb, valgrind or perf
222+
comes first in it. The version is asked of the binary alone: through
223+
the wrapper, gdb's banner would be read as the version, and perf
224+
record would overwrite perf.data with a profile of the query."""
225+
return shlex.split(os.environ.get("OPENROAD_EXE", "openroad"))[-1]
226+
227+
218228
def extract_metrics(
219229
cwd, platform, design, flow_variant, output, hier_json, logPath, rptPath, resultPath
220230
):
@@ -223,9 +233,7 @@ def extract_metrics(
223233
metrics_dict = defaultdict(dict)
224234
metrics_dict["run__flow__generate_date"] = datetime.now().strftime("%Y-%m-%d %H:%M")
225235
metrics_dict["run__flow__metrics_version"] = "Metrics_2.1.2"
226-
# OPENROAD_EXE is a command: a wrapper such as valgrind can come first
227-
openroad = shlex.split(os.environ.get("OPENROAD_EXE", "openroad"))
228-
cmdOutput = check_output(openroad + ["-version"])
236+
cmdOutput = check_output([openroad_binary(), "-version"])
229237
cmdFields = [x.decode("utf-8") for x in cmdOutput.split()]
230238
metrics_dict["run__flow__openroad_version"] = str(cmdFields[0])
231239
if len(cmdFields) > 1:

‎flow/util/genMetrics_test.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@
1111
import genMetrics
1212

1313

14+
class OpenroadBinaryTests(unittest.TestCase):
15+
"""The version is asked of the binary, not of a wrapper in front of
16+
it: gdb's banner is not a version, and perf record would overwrite
17+
perf.data with a profile of the query."""
18+
19+
def binary(self, openroad_exe):
20+
saved = os.environ.get("OPENROAD_EXE")
21+
try:
22+
if openroad_exe is None:
23+
os.environ.pop("OPENROAD_EXE", None)
24+
else:
25+
os.environ["OPENROAD_EXE"] = openroad_exe
26+
return genMetrics.openroad_binary()
27+
finally:
28+
if saved is None:
29+
os.environ.pop("OPENROAD_EXE", None)
30+
else:
31+
os.environ["OPENROAD_EXE"] = saved
32+
33+
def test_a_path_is_the_binary(self):
34+
self.assertEqual(self.binary("/opt/or/bin/openroad"), "/opt/or/bin/openroad")
35+
36+
def test_unset_is_openroad_on_path(self):
37+
self.assertEqual(self.binary(None), "openroad")
38+
39+
def test_the_binary_is_the_last_word_after_a_wrapper(self):
40+
for wrapped in (
41+
"valgrind /opt/or/bin/openroad",
42+
"perf record -g -- /opt/or/bin/openroad",
43+
"gdb -batch -ex run -ex 'thread apply all bt' --args /opt/or/bin/openroad",
44+
):
45+
with self.subTest(wrapped):
46+
self.assertEqual(self.binary(wrapped), "/opt/or/bin/openroad")
47+
48+
1449
class GitHeadCommitTests(unittest.TestCase):
1550
"""Tests git_head_commit()'s fallback behavior so commit metadata
1651
extraction degrades gracefully instead of raising."""

0 commit comments

Comments
 (0)