Skip to content

make: defer expansion of OPENROAD_*_CMD - #4596

Open
gtkiku wants to merge 2 commits into
The-OpenROAD-Project:masterfrom
gtkiku:defer-expand
Open

gtkiku wants to merge 2 commits into
The-OpenROAD-Project:masterfrom
gtkiku:defer-expand

Conversation

@gtkiku

@gtkiku gtkiku commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

With a single $, the variables are immediately expanded in $(eval), which means modifications done in util/utils.mk, such as prefixing with gdb or valgrind, don't take effect. Use $$ instead, which causes the expansion to happen when the open_*/gui_*/web_* rules are actually triggered instead of when they're generated.

With a single $, the variables are immediately expanded in $(eval),
which means modifications done in util/utils.mk, such as prefixing with
gdb or valgrind, don't take effect. Use $$ instead, which causes the
expansion to happen when the `open_*/gui_*/web_*` rules are actually
triggered instead of when they're generated.

Signed-off-by: ctkiku <kim.kuparinen@tuni.fi>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the OPEN_GUI macro in flow/Makefile to escape the OPENROAD command variables with double dollar signs ($$). The reviewer notes that due to the double expansion mechanism of $(eval $(call ...)) in GNU Make, using $$ is insufficient because the variables are defined later in util/utils.mk. To properly defer expansion until recipe execution, the reviewer recommends escaping the variables with four dollar signs ($$$$).

Comment thread flow/Makefile
Comment on lines 359 to +364
open_$(1):
$(2)=$(RESULTS_DIR)/$(1) $(OPENROAD_NO_EXIT_CMD) $(SCRIPTS_DIR)/open.tcl
$(2)=$(RESULTS_DIR)/$(1) $$(OPENROAD_NO_EXIT_CMD) $(SCRIPTS_DIR)/open.tcl
gui_$(1):
$(2)=$(RESULTS_DIR)/$(1) $(OPENROAD_GUI_CMD) $(SCRIPTS_DIR)/open.tcl
$(2)=$(RESULTS_DIR)/$(1) $$(OPENROAD_GUI_CMD) $(SCRIPTS_DIR)/open.tcl
web_$(1):
$(2)=$(RESULTS_DIR)/$(1) $(OPENROAD_WEB_CMD) $(SCRIPTS_DIR)/open.tcl
$(2)=$(RESULTS_DIR)/$(1) $$(OPENROAD_WEB_CMD) $(SCRIPTS_DIR)/open.tcl

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In GNU Make, when using $(eval $(call ...)), there is a double expansion process:

  1. $(call ...) expands the macro, which turns $$ into $.
  2. $(eval ...) expands its argument again before parsing it as makefile syntax.

Because of this double expansion, using $$ (e.g., $$(OPENROAD_NO_EXIT_CMD)) results in $(OPENROAD_NO_EXIT_CMD) being passed to eval, which then immediately expands it at eval time (when the foreach loops are evaluated at lines 876-878). Since util/utils.mk is included at line 927 (after the foreach loops), any modifications to these commands in util/utils.mk still won't take effect.

To truly defer the expansion until the recipe is executed (when the rules are triggered), you need to escape the $ twice by using $$$$ (four dollar signs). This way, call expands $$$$ to $$, eval expands $$ to $, and the final parsed recipe contains $(OPENROAD_NO_EXIT_CMD), which is expanded at execution time.

open_$(1):
	$(2)=$(RESULTS_DIR)/$(1) $$$$(OPENROAD_NO_EXIT_CMD) $(SCRIPTS_DIR)/open.tcl
gui_$(1):
	$(2)=$(RESULTS_DIR)/$(1) $$$$(OPENROAD_GUI_CMD) $(SCRIPTS_DIR)/open.tcl
web_$(1):
	$(2)=$(RESULTS_DIR)/$(1) $$$$(OPENROAD_WEB_CMD) $(SCRIPTS_DIR)/open.tcl

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work, as it delays expansion to the shell, so something like echo $(VAR) which tries to launch a subshell for $(VAR) and fails to find a command literally named VAR.

The commit message could maybe be more explicit that the expansion is delayed to the last possible moment still within Make, I guess?

@openroad-ci

openroad-ci commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

🔍 QoR check

Metrics reflect the PR merge build — i.e. what will land on the target branch.

Commit fee8656 · Jenkins build #10 · Baseline: build · View build on dashboard

62 design(s) checked — 0 with regression(s), 2 without a comparable baseline.

Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
@maliberty

Copy link
Copy Markdown
Member

Claude's suggestion seems like a better solution:

  1. The underlying cause is still there (flow/util/utils.mk:206). The gdb/valgrind wrapping happens after the rules are generated. Moving those two ifdef blocks into flow/scripts/variables.mk, right after export OPENROAD_EXE ?=, would fix it for every rule without needing $$. As it stands, the next generated rule that uses the command will lose the wrapper again.

@gtkiku

gtkiku commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

That re-defines OPENROAD_EXE within variables.mk, which is apparently a no-no for bazel-orfs, as per 72eca88.

Is gdb/valgrind expected to be used under bazel-orfs? @oharboe

More generally, why does it matter where a given variable was defined? Seems atypical.

@gtkiku

gtkiku commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

In fact, why does OPENROAD_EXE have to be the path to a binary anyway? If a user has a specific wrapper tool, like perf, wouldn't it make more sense to just have an OPENROAD_CMD or something and let the user decide what to do?

@oharboe

oharboe commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks for asking before moving it! bazel-orfs doesn't use the GDB/VALGRIND switches: it passes OPENROAD_EXE through the environment and debugs with its own binary in a plain make tree, so it only needs that environment value to reach sub-makes untouched when neither switch is set.

I tried the move on nangate45/gcd with a fake openroad that logs its argv, through test-unset-and-make-do-2_1_floorplan. With GDB/VALGRIND unset, OPENROAD_EXE keeps origin environment at the top level and in the sub-make, and the argv is the same as on master.

Why origin mattered in 72eca88: UNSET_AND_MAKE clears every file-origin variable before a sub-make, and := turned the environment value into a file one, so the sub-make fell back to tools/install/OpenROAD/bin/openroad. Since 2669d02, get_variables excludes OPENROAD_EXE and OPENROAD_*_CMD by name, so that no longer happens whatever the origin. Removing it from that list brings the fallback back.

Two things I noticed along the way, already on master: under GDB=1 the sub-make runs gdb --args gdb --args openroad (and valgrind -q valgrind -q under VALGRIND), because the exported wrapped value is wrapped again. And with the move, OPENROAD_IS_VALID would be computed on the wrapped value, so test -x gdb --args … fails at the top level.

@gtkiku

gtkiku commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the clarifications. I still don't quite understand why the variables have to be unset, seems a bit counter-intuitive to first export variables, only to be hidden later. The docs in docs/contrib/MakefileContract.md don't seem to elaborate.

Two things I noticed along the way, already on master: under GDB=1 the sub-make runs gdb --args gdb --args openroad (and valgrind -q valgrind -q under VALGRIND), because the exported wrapped value is wrapped again. And with the move, OPENROAD_IS_VALID would be computed on the wrapped value, so test -x gdb --args … fails at the top level.

Would it make more sense to just remove OPENROAD_IS_VALID and YOSYS_IS_VALID? Seems they are only used in check-openroad and check-yosys respectively, and so replacing*_IS_VALID with running something like eval $(OPENROAD_EXE) --version in check-* would allow arbitrary user-specified commands in OPENROAD_EXE/YOSYS_EXE.

GDB and VALGRIND can still be kept around for backwards-compatibility if required, although the double-expansion would imply they're a bit broken. Seems to work if run via do-2_1_floorplan etc, which I expect is the most common usage, but still. They only wrap OPENROAD_EXE anyway, what if I'm interested in wrapping yosys or some other tool?

@oharboe

oharboe commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

I wonder if we can simply delete the special casing and use FOO_EXE overriding?

@gtkiku

gtkiku commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

I wonder if we can simply delete the special casing and use FOO_EXE overriding?

Sorry, what is this referring to? "FOO_EXE overriding" as I read it could be removing GDB/VALGRIND and letting the user override the default *_EXE values on the command line, or letting GDB/VALGRIND override the default value later. Or something else?

@oharboe

oharboe commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Sorry, what is this referring to?

The first reading. Delete GDB/VALGRIND, and have the user put the wrapper in the variable on the command line:

make OPENROAD_EXE="gdb --args openroad" ...
make OPENROAD_EXE="valgrind openroad" ...
make OPENROAD_EXE="perf record -g -- openroad" ...

That is the OPENROAD_CMD-style freedom you asked for, through the variable ORFS already has. It works for yosys too (YOSYS_EXE="valgrind yosys"), which covers your question about wrapping other tools.

Your analysis found what stood in the way. With OPENROAD_IS_VALID's test -x, make all refused a wrapped command, and GDB=1 only got past the check because it rewrote the variable after the check had run. Two more spots took the value as a single path: the version query in genMetrics.py and the --openroad argument to gen_memories.py. Your #4044 had already fixed flow.sh.

The switches themselves date from 2021, when the binary had no variable of its own. OPENROAD_EXE ?= arrived two months after GDB (#53), and VALGRIND (#205) came after that.

I've put this together in #4864 as a draft. It checks the first word of the command with command -v (close to your eval $(OPENROAD_EXE) --version idea, without starting a debugger in a check), splits the value with shlex in Python, deletes the two ifdef blocks, fixes command-line values with quotes on their way into sub-makes (so -ex 'thread apply all bt' works there), and adds a test with a stub openroad and a stub wrapper. The test checks that the wrapper runs once at the top level, once through a sub-make, and in the open_/gui_/web_ rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants