From 9f8229fbb28fccc1d3edd1b6e5637c08be96bca6 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nicolas@debian.org>
Date: Sun, 10 Mar 2024 20:18:52 +0100
Subject: [PATCH 08/11] scripts/*.mk: reduce the number of subprocesses

In architecture.mk, each unset variable was spawning a shell.

In buildapi.mk, each expansion was spawning a shell.
Default.mk and vendor.mk both expand DPKG_BUILD_API.

In buildflags.mk, each first expansion of an unset variable was
spawning a shell.
DPKG_EXPORT_BUILDFLAGS expands all variables.

In pkg-info.mk, each variable was spawning its own shell.

Vendors.mk was correct but use the same mechanism for consistency.
---
 scripts/mk/architecture.mk | 16 +++++++++++-----
 scripts/mk/buildapi.mk     |  6 +++++-
 scripts/mk/buildflags.mk   | 36 +++++++++++++++++-------------------
 scripts/mk/pkg-info.mk     | 33 +++++++++++++++++++++++----------
 scripts/mk/vendor.mk       |  8 ++++----
 5 files changed, 60 insertions(+), 39 deletions(-)

diff --git a/scripts/mk/architecture.mk b/scripts/mk/architecture.mk
index 1565903c4..50af265d4 100644
--- a/scripts/mk/architecture.mk
+++ b/scripts/mk/architecture.mk
@@ -1,16 +1,22 @@
 # This Makefile fragment (since dpkg 1.16.1) defines all the DEB_HOST_* and
 # DEB_BUILD_* variables that dpkg-architecture can return. Existing values
 # of those variables are preserved as per policy.
+# All variables are exported.
 
 ifndef /usr/share/dpkg/architecture.mk_included
 /usr/share/dpkg/architecture.mk_included = yes
 
-dpkg_lazy_eval ?= $$(or $$(value DPKG_CACHE_$(1)),$$(eval DPKG_CACHE_$(1) := $$(shell $(2)))$$(value DPKG_CACHE_$(1)))
-
-dpkg_architecture_setvar = export $(1) ?= $(call dpkg_lazy_eval,$(1),dpkg-architecture -q$(1))
-
+dpkg-architecture_vars = \
 $(foreach machine,BUILD HOST TARGET,\
   $(foreach var,ARCH ARCH_ABI ARCH_LIBC ARCH_OS ARCH_CPU ARCH_BITS ARCH_ENDIAN GNU_CPU GNU_SYSTEM GNU_TYPE MULTIARCH,\
-    $(eval $(call dpkg_architecture_setvar,DEB_$(machine)_$(var)))))
+    DEB_$(machine)_$(var)))
+
+# Dpkg-buildpackage sets all variables. Optimize this frequent case.
+ifneq (,$(strip $(foreach v,$(dpkg-architecture_vars),$(if $(value $(v)),,1))))
+  # $(info Running dpkg-architecture)
+  $(foreach line,$(subst =,?=,$(shell dpkg-architecture)),$(eval $(line)))
+endif
+
+export $(dpkg-architecture_vars)
 
 endif # ifndef /usr/share/dpkg/architecture.mk_included
diff --git a/scripts/mk/buildapi.mk b/scripts/mk/buildapi.mk
index cb68d91c2..0ab50af9c 100644
--- a/scripts/mk/buildapi.mk
+++ b/scripts/mk/buildapi.mk
@@ -4,7 +4,11 @@ ifndef /usr/share/dpkg/buildapi.mk_included
 /usr/share/dpkg/buildapi.mk_included = yes
 
 # Default API level when not set.
-DPKG_BUILD_API ?= $(shell dpkg-buildapi)
+ifndef DPKG_BUILD_API
+  dpkg_lazy ?= $(eval $(1) = $(2)$$($(1)))
+  dpkg_lazy_set ?= $(call dpkg_lazy,$(1),$$(eval $(1) := $(2)))
+  $(call dpkg_lazy_set,DPKG_BUILD_API,$$(shell dpkg-buildapi))
+endif
 
 # We could use only built-in GNU make functions, but that seems too much
 # complexity given no integer operators, given that we currently have to
diff --git a/scripts/mk/buildflags.mk b/scripts/mk/buildflags.mk
index 7d949bc04..838a0e0c4 100644
--- a/scripts/mk/buildflags.mk
+++ b/scripts/mk/buildflags.mk
@@ -37,8 +37,6 @@ ifndef /usr/share/dpkg/buildflags.mk_included
 # This list is kept in sync with the default set of flags returned
 # by dpkg-buildflags.
 
-dpkg_lazy_eval ?= $$(or $$(value DPKG_CACHE_$(1)),$$(eval DPKG_CACHE_$(1) := $$(shell $(2)))$$(value DPKG_CACHE_$(1)))
-
 DPKG_BUILDFLAGS_LIST := $(foreach var,\
   ASFLAGS \
   CFLAGS \
@@ -53,26 +51,26 @@ DPKG_BUILDFLAGS_LIST := $(foreach var,\
   OBJCXXFLAGS \
   ,$(var) $(var)_FOR_BUILD)
 
-define dpkg_buildflags_export_envvar
-  ifdef $(1)
-    DPKG_BUILDFLAGS_EXPORT_ENVVAR += $(1)="$$(value $(1))"
-  endif
-endef
-
-$(eval $(call dpkg_buildflags_export_envvar,DEB_BUILD_OPTIONS))
-$(eval $(call dpkg_buildflags_export_envvar,DEB_BUILD_MAINT_OPTIONS))
-$(eval $(call dpkg_buildflags_export_envvar,DEB_BUILD_PATH))
-$(foreach flag,$(DPKG_BUILDFLAGS_LIST),\
-  $(foreach operation,SET STRIP APPEND PREPEND,\
-    $(eval $(call dpkg_buildflags_export_envvar,DEB_$(flag)_MAINT_$(operation)))))
-
-dpkg_buildflags_setvar = $(1) = $(call dpkg_lazy_eval,$(1),$(DPKG_BUILDFLAGS_EXPORT_ENVVAR) dpkg-buildflags --get $(1))
-
-$(foreach flag,$(DPKG_BUILDFLAGS_LIST),\
-  $(eval $(call dpkg_buildflags_setvar,$(flag))))
+dpkg-buildflags_run = $(eval $(shell \
+  $(foreach exported,\
+    DEB_BUILD_OPTIONS\
+    DEB_BUILD_MAINT_OPTIONS\
+    DEB_BUILD_PATH\
+    $(foreach flag,$(DPKG_BUILDFLAGS_LIST),\
+      $(foreach operation,SET STRIP APPEND PREPEND,\
+        DEB_$(flag)_MAINT_$(operation)))\
+    ,$(if $(value $(exported))\
+      ,$(exported)="$(value $(exported))"))\
+  dpkg-buildflags | sed -r 's/([^=]*)(.*)/$$(eval \1:\2)/'))
 
 ifdef DPKG_EXPORT_BUILDFLAGS
+  # We need to compute the values right now.
+  $(dpkg-buildflags_run)
   export $(DPKG_BUILDFLAGS_LIST)
+else
+  dpkg_lazy ?= $(eval $(1) = $(2)$$($(1)))
+  $(foreach v,$(DPKG_BUILDFLAGS_LIST),\
+    $(call dpkg_lazy,$(v),$$(dpkg-buildflags_run)))
 endif
 
 endif # ifndef /usr/share/dpkg/buildflags.mk_included
diff --git a/scripts/mk/pkg-info.mk b/scripts/mk/pkg-info.mk
index 144c0d758..0eb5e8a4b 100644
--- a/scripts/mk/pkg-info.mk
+++ b/scripts/mk/pkg-info.mk
@@ -12,21 +12,34 @@
 #   SOURCE_DATE_EPOCH: source release date as seconds since the epoch, as
 #     specified by <https://reproducible-builds.org/specs/source-date-epoch/>
 #     (since dpkg 1.18.8).
+#     If it is undefined, the date of the latest changelog entry is used.
+#     In both cases, the value is exported.
 
 ifndef /usr/share/dpkg/pkg-info.mk_included
 /usr/share/dpkg/pkg-info.mk_included = yes
 
-dpkg_late_eval ?= $(or $(value DPKG_CACHE_$(1)),$(eval DPKG_CACHE_$(1) := $(shell $(2)))$(value DPKG_CACHE_$(1)))
-
-DEB_SOURCE = $(call dpkg_late_eval,DEB_SOURCE,dpkg-parsechangelog -SSource)
-DEB_VERSION = $(call dpkg_late_eval,DEB_VERSION,dpkg-parsechangelog -SVersion)
-DEB_VERSION_EPOCH_UPSTREAM = $(call dpkg_late_eval,DEB_VERSION_EPOCH_UPSTREAM,echo '$(DEB_VERSION)' | sed -e 's/-[^-]*$$//')
-DEB_VERSION_UPSTREAM_REVISION = $(call dpkg_late_eval,DEB_VERSION_UPSTREAM_REVISION,echo '$(DEB_VERSION)' | sed -e 's/^[0-9]*://')
-DEB_VERSION_UPSTREAM = $(call dpkg_late_eval,DEB_VERSION_UPSTREAM,echo '$(DEB_VERSION_EPOCH_UPSTREAM)' | sed -e 's/^[0-9]*://')
-DEB_DISTRIBUTION = $(call dpkg_late_eval,DEB_DISTRIBUTION,dpkg-parsechangelog -SDistribution)
-
-SOURCE_DATE_EPOCH ?= $(call dpkg_late_eval,SOURCE_DATE_EPOCH,dpkg-parsechangelog -STimestamp)
+dpkg-parsechangelog_run = $(eval $(shell dpkg-parsechangelog | sed -nr '\
+  s/^Distribution: (.*)/$$(eval DEB_DISTRIBUTION:=\1)/p;\
+  s/^Source: (.*)/$$(eval DEB_SOURCE:=\1)/p;\
+  s/^Version: ([0-9]*:)?([^-]*)((.*)-[^-]*)?$$/\
+    $$(eval DEB_VERSION:=\1\2\3)\
+    $$(eval DEB_VERSION_EPOCH_UPSTREAM:=\1\2\4)\
+    $$(eval DEB_VERSION_UPSTREAM_REVISION:=\2\3)\
+    $$(eval DEB_VERSION_UPSTREAM:=\2\4)/p;\
+  s/^Timestamp: (.*)/$$(eval SOURCE_DATE_EPOCH?=\1)/p'))
 
+ifdef SOURCE_DATE_EPOCH
+  dpkg_lazy ?= $(eval $(1) = $(2)$$($(1)))
+  $(call dpkg_lazy,DEB_DISTRIBUTION,$$(dpkg-parsechangelog_run))
+  $(call dpkg_lazy,DEB_SOURCE,$$(dpkg-parsechangelog_run))
+  $(call dpkg_lazy,DEB_VERSION,$$(dpkg-parsechangelog_run))
+  $(call dpkg_lazy,DEB_VERSION_EPOCH_UPSTREAM,$$(dpkg-parsechangelog_run))
+  $(call dpkg_lazy,DEB_VERSION_UPSTREAM,$$(dpkg-parsechangelog_run))
+  $(call dpkg_lazy,DEB_UPSTREAM_REVISION,$$(dpkg-parsechangelog_run))
+else
+  # We need to compute the values right now.
+  $(dpkg-parsechangelog_run)
+endif
 export SOURCE_DATE_EPOCH
 
 endif # ifndef /usr/share/dpkg/pkg-info.mk_included
diff --git a/scripts/mk/vendor.mk b/scripts/mk/vendor.mk
index afd86b014..5e2ad50ce 100644
--- a/scripts/mk/vendor.mk
+++ b/scripts/mk/vendor.mk
@@ -42,10 +42,10 @@ ifndef dpkg_datadir
 endif
 include $(dpkg_datadir)/buildapi.mk
 
-dpkg_late_eval ?= $(or $(value DPKG_CACHE_$(1)),$(eval DPKG_CACHE_$(1) := $(shell $(2)))$(value DPKG_CACHE_$(1)))
-
-DEB_VENDOR = $(call dpkg_late_eval,DEB_VENDOR,dpkg-vendor --query Vendor)
-DEB_PARENT_VENDOR = $(call dpkg_late_eval,DEB_PARENT_VENDOR,dpkg-vendor --query Parent)
+dpkg_lazy ?= $(eval $(1) = $(2)$$($(1)))
+dpkg_lazy_set ?= $(call dpkg_lazy,$(1),$$(eval $(1) := $(2)))
+$(call dpkg_lazy_set,DEB_VENDOR,$$(shell dpkg-vendor --query Vendor))
+$(call dpkg_lazy_set,DEB_PARENT_VENDOR,$$(shell dpkg-vendor --query Parent))
 
 dpkg_vendor_derives_from_v0 = dpkg-vendor --derives-from $(1) && echo yes || echo no
 dpkg_vendor_derives_from_v1 = $(shell $(dpkg_vendor_derives_from_v0))
-- 
2.39.2

