Skip to contentSkip to conversation

build: reduce number of $(shell) execution on make 4.4

PATCH Superseded Anthony PERARD 4 participants 10 messages Jun 21, 2023 – Jun 22, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover build: reduce number of $(shell) execution on make 4.4

Patch series available in this git branch:
https://xenbits.xen.org/git-http/people/aperard/xen-unstable.git br.build-exported-shell-command-value-v1

With GNU make 4.4, the number of execution of the command present in $(shell ) in exported variables increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

From the annoncement:

https://lists.gnu.org/archive/html/info-gnu/2022-10/msg00008.html
> * WARNING: Backward-incompatibility!
> Previously makefile variables marked as export were not exported to commands
> started by the $(shell ...) function. Now, all exported variables are
> exported to $(shell ...). If this leads to recursion during expansion, then
> for backward-compatibility the value from the original environment is used.
> To detect this change search for 'shell-export' in the .FEATURES variable.

Also, there's a new paragraph in the GNU make manual, but it's a warning about exporting all variable, still it might be relevant to the new observed behavior:

https://www.gnu.org/software/make/manual/make.html#Variables_002fRecursion
> Adding a variable’s value to the environment requires it to be expanded. If > expanding a variable has side-effects (such as the info or eval or similar > functions) then these side-effects will be seen every time a command is > invoked.

The issue was reported on IRC by jandryuk.

So, I've locate a few obvious candidate to fix, maybe there's more $(shell) to change?

Anthony PERARD (3):
build: define ARCH and SRCARCH later
build: evaluate XEN_BUILD_* and XEN_DOMAIN on first use Config.mk: evaluate XEN_COMPILE_ARCH and XEN_OS on first use

 Config.mk    |  6 +++---
 xen/Makefile | 21 +++++++++++----------
 2 files changed, 14 insertions(+), 13 deletions(-)
Signature · 1 line
Anthony PERARD
#1 lore raw

PATCH 1/3 build: define ARCH and SRCARCH later

Defining ARCH and SRCARCH later in xen/Makefile allows to switch to immediate evaluation variable type.

ARCH and SRCARCH depends on value defined in Config.mk and aren't used TARGET_SUBARCH or TARGET_ARCH, and not before it's needed in a sub-make or a rule.

This will help reduce the number of times the shell rune is been run.

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

Also, make -d shows a lot of these:
Makefile:39: not recursively expanding SRCARCH to export to shell function Makefile:38: not recursively expanding ARCH to export to shell function

---
 xen/Makefile | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
xen/Makefile +7 -6
index e89fc461fc4b..9631e45cfb9b 100644
@@ -35,12 +35,6 @@ MAKEFLAGS += -rR
 
 EFI_MOUNTPOINT ?= $(BOOT_DIR)/efi
 
-ARCH=$(XEN_TARGET_ARCH)
-SRCARCH=$(shell echo $(ARCH) | \
-          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
-              -e s'/riscv.*/riscv/g')
-export ARCH SRCARCH
-
 # Allow someone to change their config file
 export KCONFIG_CONFIG ?= .config
 
@@ -241,6 +235,13 @@ include scripts/Kbuild.include
 include $(XEN_ROOT)/Config.mk
 
 # Set ARCH/SUBARCH appropriately.
+
+ARCH := $(XEN_TARGET_ARCH)
+SRCARCH := $(shell echo $(ARCH) | \
+          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
+              -e s'/riscv.*/riscv/g')
+export ARCH SRCARCH
+
 export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
 export TARGET_ARCH     := $(shell echo $(XEN_TARGET_ARCH) | \
                             sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
Signature · 1 line
Anthony PERARD
#2 lore raw

Re: [XEN PATCH 1/3] build: define ARCH and SRCARCH later

Quoted 46 lines · Anthony PERARD

Defining ARCH and SRCARCH later in xen/Makefile allows to switch to immediate evaluation variable type.

ARCH and SRCARCH depends on value defined in Config.mk and aren't used TARGET_SUBARCH or TARGET_ARCH, and not before it's needed in a sub-make or a rule.

This will help reduce the number of times the shell rune is been run.

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

Also, make -d shows a lot of these:
Makefile:39: not recursively expanding SRCARCH to export to shell function Makefile:38: not recursively expanding ARCH to export to shell function

---
 xen/Makefile | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
xen/Makefile +0 -6
index e89fc461fc4b..9631e45cfb9b 100644
@@ -35,12 +35,6 @@ MAKEFLAGS += -rR
 
 EFI_MOUNTPOINT ?= $(BOOT_DIR)/efi
 
-ARCH=$(XEN_TARGET_ARCH)
-SRCARCH=$(shell echo $(ARCH) | \
-          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
-              -e s'/riscv.*/riscv/g')
-export ARCH SRCARCH
-
 # Allow someone to change their config file
 export KCONFIG_CONFIG ?= .config
 
@@ -241,6 +235,13 @@ include scripts/Kbuild.include
 include $(XEN_ROOT)/Config.mk
 
 # Set ARCH/SUBARCH appropriately.

xen/Makefile include scripts/Kbuild.include · line 247

Quoted 10 lines
xen/Makefile +7 -0
+
+ARCH := $(XEN_TARGET_ARCH)
+SRCARCH := $(shell echo $(ARCH) | \
+          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
+              -e s'/riscv.*/riscv/g')
+export ARCH SRCARCH
+
 export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
 export TARGET_ARCH     := $(shell echo $(XEN_TARGET_ARCH) | \
                             sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \

The change looks plausible to fix this issue, but could we take the opportunity to dedup the sed expression into a $(call src-arch ...) or so ?

Except, given that ARCH := $(XEN_TARGET_ARCH) now, doesn't that mean SRCARCH is always TARGET_ARCH ?

Can't we simplify this to just be plain aliases?

~Andrew

#3 lore raw

Re: [XEN PATCH 1/3] build: define ARCH and SRCARCH later

Quoted 61 lines · Andrew Cooper
Quoted 46 lines · Anthony PERARD

Defining ARCH and SRCARCH later in xen/Makefile allows to switch to immediate evaluation variable type.

ARCH and SRCARCH depends on value defined in Config.mk and aren't used TARGET_SUBARCH or TARGET_ARCH, and not before it's needed in a sub-make or a rule.

This will help reduce the number of times the shell rune is been run.

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

Also, make -d shows a lot of these:
Makefile:39: not recursively expanding SRCARCH to export to shell function Makefile:38: not recursively expanding ARCH to export to shell function

---
 xen/Makefile | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
xen/Makefile +0 -6
index e89fc461fc4b..9631e45cfb9b 100644
@@ -35,12 +35,6 @@ MAKEFLAGS += -rR
 
 EFI_MOUNTPOINT ?= $(BOOT_DIR)/efi
 
-ARCH=$(XEN_TARGET_ARCH)
-SRCARCH=$(shell echo $(ARCH) | \
-          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
-              -e s'/riscv.*/riscv/g')
-export ARCH SRCARCH
-
 # Allow someone to change their config file
 export KCONFIG_CONFIG ?= .config
 
@@ -241,6 +235,13 @@ include scripts/Kbuild.include
 include $(XEN_ROOT)/Config.mk
 
 # Set ARCH/SUBARCH appropriately.

xen/Makefile include scripts/Kbuild.include · line 247

Quoted 10 lines
xen/Makefile +7 -0
+
+ARCH := $(XEN_TARGET_ARCH)
+SRCARCH := $(shell echo $(ARCH) | \
+          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
+              -e s'/riscv.*/riscv/g')
+export ARCH SRCARCH
+
 export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
 export TARGET_ARCH     := $(shell echo $(XEN_TARGET_ARCH) | \
                             sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \

The change looks plausible to fix this issue, but could we take the opportunity to dedup the sed expression into a $(call src-arch ...) or so ?

Quoted 4 lines

Except, given that ARCH := $(XEN_TARGET_ARCH) now, doesn't that mean SRCARCH is always TARGET_ARCH ?

Can't we simplify this to just be plain aliases?

Or, putting it differently, do we actually need both TARGET_* values when they match other (exported) variables anyway?

Jan

#4 lore raw

Re: [XEN PATCH 1/3] build: define ARCH and SRCARCH later

Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper

xen/Makefile include scripts/Kbuild.include · line 247

Quoted 10 lines · Anthony PERARD
xen/Makefile +7 -0
+
+ARCH := $(XEN_TARGET_ARCH)
+SRCARCH := $(shell echo $(ARCH) | \
+          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
+              -e s'/riscv.*/riscv/g')
+export ARCH SRCARCH
+
 export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
 export TARGET_ARCH     := $(shell echo $(XEN_TARGET_ARCH) | \
                             sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \

The change looks plausible to fix this issue, but could we take the opportunity to dedup the sed expression into a $(call src-arch ...) or so ?

Quoted 7 lines
Quoted 4 lines

Except, given that ARCH := $(XEN_TARGET_ARCH) now, doesn't that mean SRCARCH is always TARGET_ARCH ?

Can't we simplify this to just be plain aliases?

Or, putting it differently, do we actually need both TARGET_* values when they match other (exported) variables anyway?

Sounds good to me, I can remove both TARGET_* variables.

Thanks,

Signature · 1 line
Anthony PERARD
Tested-by on v1 1/3
#5 lore raw

Re: [XEN PATCH 1/3] build: define ARCH and SRCARCH later

In reply to Anthony PERARD
Quoted 16 lines · Anthony PERARD

Defining ARCH and SRCARCH later in xen/Makefile allows to switch to immediate evaluation variable type.

ARCH and SRCARCH depends on value defined in Config.mk and aren't used TARGET_SUBARCH or TARGET_ARCH, and not before it's needed in a sub-make or a rule.

This will help reduce the number of times the shell rune is been run.

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

Quoted 6 lines

Also, make -d shows a lot of these:
Makefile:39: not recursively expanding SRCARCH to export to shell function Makefile:38: not recursively expanding ARCH to export to shell function

Things are back to normal speed - Thanks a lot!

-Jason

Tested-by for the whole series, FYI.
#6 lore raw

Re: [XEN PATCH 1/3] build: define ARCH and SRCARCH later

Quoted 19 lines · Jason Andryuk
Quoted 16 lines · Anthony PERARD

Defining ARCH and SRCARCH later in xen/Makefile allows to switch to immediate evaluation variable type.

ARCH and SRCARCH depends on value defined in Config.mk and aren't used TARGET_SUBARCH or TARGET_ARCH, and not before it's needed in a sub-make or a rule.

This will help reduce the number of times the shell rune is been run.

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

Quoted 8 lines
Quoted 6 lines

Also, make -d shows a lot of these:
Makefile:39: not recursively expanding SRCARCH to export to shell function Makefile:38: not recursively expanding ARCH to export to shell function

Tested-by for the whole series, FYI.

Thanks,
Jason

#7 lore raw

Re: [XEN PATCH 1/3] build: define ARCH and SRCARCH later

In reply to Anthony PERARD

xen/Makefile include scripts/Kbuild.include · line 242

Quoted 9 lines · Anthony PERARD
xen/Makefile +5 -0
@@ -241,6 +235,13 @@ include scripts/Kbuild.include
 include $(XEN_ROOT)/Config.mk
 
 # Set ARCH/SUBARCH appropriately.
+
+ARCH := $(XEN_TARGET_ARCH)
+SRCARCH := $(shell echo $(ARCH) | \
+          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
+              -e s'/riscv.*/riscv/g')

Could you take the opportunity and get quoting consistent in this construct, while you move it?

Jan

#8 lore raw

PATCH 2/3 build: evaluate XEN_BUILD_* and XEN_DOMAIN on first use

In reply to Anthony PERARD

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

Also, make -d shows a lot of these:
Makefile:15: not recursively expanding XEN_BUILD_DATE to export to shell function Makefile:16: not recursively expanding XEN_BUILD_TIME to export to shell function Makefile:17: not recursively expanding XEN_BUILD_HOST to export to shell function Makefile:14: not recursively expanding XEN_DOMAIN to export to shell function

So, to avoid having these command been run more than necessery, we will use a construct to evaluate on first use.

---
 xen/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/Makefile +4 -4
index 9631e45cfb9b..b58c2a7f4539 100644
@@ -11,10 +11,10 @@ export XEN_FULLVERSION   = $(XEN_VERSION).$(XEN_SUBVERSION)$(XEN_EXTRAVERSION)
 -include xen-version
 
 export XEN_WHOAMI	?= $(USER)
-export XEN_DOMAIN	?= $(shell ([ -x /bin/dnsdomainname ] && /bin/dnsdomainname) || ([ -x /bin/domainname ] && /bin/domainname || echo [unknown]))
-export XEN_BUILD_DATE	?= $(shell LC_ALL=C date)
-export XEN_BUILD_TIME	?= $(shell LC_ALL=C date +%T)
-export XEN_BUILD_HOST	?= $(shell hostname)
+export XEN_DOMAIN	?= $(eval XEN_DOMAIN := $(shell ([ -x /bin/dnsdomainname ] && /bin/dnsdomainname) || ([ -x /bin/domainname ] && /bin/domainname || echo [unknown])))$(XEN_DOMAIN)
+export XEN_BUILD_DATE	?= $(eval XEN_BUILD_DATE := $(shell LC_ALL=C date))$(XEN_BUILD_DATE)
+export XEN_BUILD_TIME	?= $(eval XEN_BUILD_TIME := $(shell LC_ALL=C date +%T))$(XEN_BUILD_TIME)
+export XEN_BUILD_HOST	?= $(eval XEN_BUILD_HOST := $(shell hostname))$(XEN_BUILD_HOST)
 
 # Best effort attempt to find a python interpreter, defaulting to Python 3 if
 # available.  Fall back to just `python` if `which` is nowhere to be found.
Signature · 1 line
Anthony PERARD
#9 lore raw

PATCH 3/3 Config.mk: evaluate XEN_COMPILE_ARCH and XEN_OS on first use

In reply to Anthony PERARD

With GNU make 4.4, the number of execution of the command present in these $(shell ) increased greatly. This is probably because as of make 4.4, exported variable are also added to the environment of $(shell ) construct.

So, to avoid having these command been run more than necessery, we will use a construct to evaluate on first use.

---
 Config.mk | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Config.mk +3 -3
index c529b1ba19cd..5fbdbc4500d2 100644
@@ -19,13 +19,13 @@ or       = $(if $(strip $(1)),$(1),$(if $(strip $(2)),$(2),$(if $(strip $(3)),$(
 
 -include $(XEN_ROOT)/.config
 
-XEN_COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/ \
+XEN_COMPILE_ARCH    ?= $(eval XEN_COMPILE_ARCH := $(shell uname -m | sed -e s/i.86/x86_32/ \
                          -e s/i86pc/x86_32/ -e s/amd64/x86_64/ \
                          -e s/armv7.*/arm32/ -e s/armv8.*/arm64/ \
-                         -e s/aarch64/arm64/)
+                         -e s/aarch64/arm64/))$(XEN_COMPILE_ARCH)
 
 XEN_TARGET_ARCH     ?= $(XEN_COMPILE_ARCH)
-XEN_OS              ?= $(shell uname -s)
+XEN_OS              ?= $(eval XEN_OS := $(shell uname -s))$(XEN_OS)
 
 CONFIG_$(XEN_OS) := y
 
Signature · 1 line
Anthony PERARD