Skip to content

Commit e57d71b

Browse files
committed
make: immediately expand deferred vars, v2
Previous approach apparently caused some externally-defined variables to become overwritten, and modifications made in 72eca88 effectively reverted back to the old behaviour of spawning a huge number of shells for each `make` invocation. Try to cut that number down again by requiring that the runtime environment provide appropriate values for PYTHON_EXE, OPENROAD_EXE, OPENSTA_EXE and YOSYS_EXE, otherwise use ORFS-built tools. Signed-off-by: ctkiku <kim.kuparinen@tuni.fi>
1 parent 25b6429 commit e57d71b

2 files changed

Lines changed: 44 additions & 37 deletions

File tree

‎flake.nix‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
pkgs.python3Packages.pyyaml
3737
pkgs.python3Packages.yamlfix
3838
];
39+
OPENROAD_EXE = "${openroad.packages.${system}.default}/bin/openroad";
40+
OPENSTA_EXE = "${openroad.packages.${system}.default}/bin/sta";
41+
YOSYS_EXE = "${yosys.packages.${system}.default}/bin/yosys";
42+
PYTHON_EXE = "${pkgs.python3}/bin/python3";
3943
};
4044
}
4145
);

‎flow/scripts/variables.mk‎

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -71,40 +71,50 @@ export NUM_CORES
7171
#-------------------------------------------------------------------------------
7272
# setup all commands used within this flow
7373
#
74-
# HERE BE DRAGONS: use bare `export VAR`, never `export VAR := $(VAR)`.
74+
# Nix and Bazel (or whatever else environment) should explicitly set the
75+
# variables OPENROAD_EXE, OPENSTA_EXE, YOSYS_EXE and PYTHON_EXE to point to
76+
# installed tools. If those are not defined, tools built by ORFS are used and
77+
# referred to by absolute paths.
7578
#
76-
# `export VAR := $(VAR)` rebinds the variable to origin "file", which makes
77-
# get_variables (below) include it in UNSET_VARIABLES_NAMES. UNSET_AND_MAKE
78-
# then unsets it before the sub-make runs, so any `?=` fallback here fires
79-
# with the wrong value in the sub-make (e.g. the in-tree tools/install path
80-
# that does not exist in a Bazel sandbox). A bare `export` preserves the
81-
# original origin (environment when bazel-orfs supplies it, file when the
82-
# local default fills in), so the value survives UNSET_VARS.
83-
PYTHON_EXE ?= $(shell command -v python3)
79+
# Avoid exporting deferred shell commands, for example something like
80+
#
81+
# export PYTHON_EXE ?= $(shell command -v python3)
82+
#
83+
# causes Make to start up a new shell whenever PYTHON_EXE is expanded. An
84+
# annoying side-effect of this is that whenever a shell is launched, all
85+
# exported variables are expanded, which causes PYTHON_EXE to launch a shell,
86+
# which causes other exported variables to be expanded, and if those launch
87+
# shells, PYTHON_EXE is re-expanded which launches a shell, and so on.
88+
#
89+
# This is not an infinite loop, as there are rules in place to stop expansion
90+
# under certain circumstances, but the effect is nonetheless cumulative.
91+
# Previous versions of this file caused ~28k shell instances to be spawned for
92+
# `make DESIGN_CONFIG=... clean_all`, which could be reduced to 41.
93+
#
94+
# The 'obvious' solution here is to just run each shell command once by forcing
95+
# an immediate expansion with `:=`, something like
96+
#
97+
# PYTHON_EXE ?= $(shell command -v python3)
98+
# export PYTHON_EXE := $(PYTHON_EXE)
99+
#
100+
# but this changes 'where' PYTHON_EXE was defined, command line/environment
101+
# versus in a file, which messes with `get_variables` and causes command
102+
# line/environment variables to not be propagated to submakes.
103+
#
104+
# Python is not built by ORFS, and as such its absolute path has to be fetched
105+
# from the environment. Use `ifeq ($(origin PYTHON_EXE), undefined)` to check if
106+
# the variable hasn't been defined yet and run a single shell instance to fetch
107+
# the absolute path if so. The other tools should've been built by ORFS, and can
108+
# use a pre-specified path, which we can just use ?= for.
109+
ifeq ($(origin PYTHON_EXE), undefined)
110+
PYTHON_EXE := $(shell command -v python3)
111+
endif
84112
export PYTHON_EXE
85113

86114
export RUN_CMD = $(PYTHON_EXE) $(FLOW_HOME)/scripts/run_command.py
87115

88-
# The following determine the executable location for each tool used by this flow.
89-
# Priority is given to
90-
# 1 user explicit set with variable in Makefile or command line, for instance setting OPENROAD_EXE
91-
# 2 either
92-
# 2.1 if in Nix shell: openroad, yosys from the environment
93-
# 2.2 ORFS compiled tools: openroad, yosys
94-
ifneq (${IN_NIX_SHELL},)
95-
OPENROAD_EXE ?= $(shell command -v openroad)
96-
else
97-
OPENROAD_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/openroad)
98-
endif
99-
ifneq (${IN_NIX_SHELL},)
100-
OPENSTA_EXE ?= $(shell command -v sta)
101-
else
102-
OPENSTA_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/sta)
103-
endif
104-
105-
# See dragons comment near PYTHON_EXE: bare `export`, not `export VAR := $(VAR)`.
106-
export OPENROAD_EXE
107-
export OPENSTA_EXE
116+
export OPENROAD_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/openroad)
117+
export OPENSTA_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/sta)
108118

109119
OPENROAD_IS_VALID := $(if $(OPENROAD_EXE),$(shell test -x $(OPENROAD_EXE) && echo "true"),)
110120

@@ -114,14 +124,7 @@ export OPENROAD_NO_EXIT_CMD = $(OPENROAD_EXE) $(OPENROAD_ARGS)
114124
export OPENROAD_GUI_CMD = $(OPENROAD_EXE) -gui -threads $(NUM_CORES) $(OR_ARGS)
115125
export OPENROAD_WEB_CMD = $(OPENROAD_EXE) -web -threads $(NUM_CORES) $(OR_ARGS)
116126

117-
ifneq (${IN_NIX_SHELL},)
118-
YOSYS_EXE ?= $(shell command -v yosys)
119-
else
120-
YOSYS_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/yosys/bin/yosys)
121-
endif
122-
123-
# See dragons comment near PYTHON_EXE: bare `export`, not `export VAR := $(VAR)`.
124-
export YOSYS_EXE
127+
export YOSYS_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/yosys/bin/yosys)
125128

126129
YOSYS_IS_VALID := $(if $(YOSYS_EXE),$(shell test -x $(YOSYS_EXE) && echo "true"),)
127130

0 commit comments

Comments
 (0)