Package: dpkg-dev
Followup-For: Bug #872381

Hello.

Please consider this new patch queue instead of the old or untested
ones.  With this one applied on 279c6ccb, the package builds and
passes all tests.

* scripts/mk: only use ASCII characters
  Cosmetic independent suggestion.

* scripts/mk: protect files against double inclusion
  The variables are renamed as you have recommended.
  The test is fixed (ifdef fails on a defined but empty variable).

* scripts/mk: stop hard-coding dpkg_datadir
  Already discussed.

* scripts/mk/buildopts.mk: search once for parallel= in DEB_BUILD_OPTIONS

> > [...DEB_BUILD_OPTION_PARALLEL empty instead of undefined
> >     when parallel= is missing...]
> [kind of an API change].

I have changed my patch and updated the comment.
However..
The policy only describes 'parallel=N' when N is a positive integer.
I think we should assume that the option is either missing or valid.
For me, 'parallel=' is as incorrect as 'parallel=foo'.

> I think it might perhaps make more sense to fallback to setting it
> to 1 if it's missing, but I need to ponder about possible
> consequences/fallout, etc.

I doubt any sensible default exist.
* 1 is safe/produces readable logs and $max_available_processors is fast.
* the policy/debhelper/... have found no one-size-fits-all solution.

* scripts/buildflags.mk: add missing GCJFLAGS
  Fixes a bug.

* scripts/buildflags.mk: generate the _FOR_BUILD variant of each variable
* scripts/buildflags.mk: sort the flag list
  These changes hopefully prevent new missing flags in the future (the
  output of dpkg-buildflags is sorted).

* scripts/*.mk: reduce the number of subprocesses
* scripts/t: use loops instead of repetitions, check exports and overrides
  * all four combinations of existing/new scripts/mk/*.mk pass the
    existing/new tests in scripts/t/mk/*.mk.
  * comparing the time taken by tests gives a rough idea of the speed
    gain
    architecture.mk 30 times faster (probably no gain under dpkg-buildpackage)
    buildflags.mk   20 times faster
    pkg-info.mk      4 times faster
    buildtools.mk        20% faster

Guillem Jover
> I've left this one out for now. I'm not entirely satisfied with the
> sed usage here. If we keep using sed, then I think it needs to be
> set via a SED variable, substituted from the value found at

In which context do you expect GNU Make but a non recent sed?
Should I rewrite the regular expressions without -r/-E?

> configure time. But then, I've been pondering whether we can have
> better export formats, that might make the sed usage not
> necessary. I started with a make-eval export mode for buildflags,
> but perhaps it would be better a more generic formatting mode where
> the caller can specify how the output should look like, akin
> «dpkg-query --showformat». Will ponder about this.

A generic format would be more maintainable in the long term.
Something like that would be convenient for the makefiles.

dpkg-architecture --print-format='${Dollar}(eval export ${key} ?= ${value})'
dpkg-buildflags --print-format='${Dollar}(eval ${key}:=${value})'
dpkg-parsechangelog --print-format='${Dollar}(eval DEB_SOURCE:=${Source}) 
${Dollar}(eval export SOURCE_DATE_EPOCH?=${Timestamp}) ..'
dpkg-vendor --print-format'${Dollar}(eval DEB_VENDOR:=${Vendor}) ${Dollar}(eval 
DEB_PARENT_VENDOR:=${Parent})'

* scripts/buildtools.mk: style suggestions
  This arguably improves the readability, and fixes a minor issue
  ($(findstring nostrip,...) unwantedly matches arduinostrip).

* scripts/t/mk/buildflags.mk: fix test of _MAINT_APPEND when TEST_ is empty
  This fixes a minor issue. During a test with
  DEB_BUILD_OPTIONS=noopt, TEST_CXXFLAGS was empty and caused the test
  of DEB_CXXFLAGS_MAINT_APPEND to fail because the correct result is
  not a concatenation, Make strips a space.  This issue can also be
  seen with 1.22.5.
>From 37f1089c450fca16d06d586cf390a05642af25f0 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Mon, 4 Mar 2024 13:23:56 +0100
Subject: [PATCH 01/11] scripts/mk: only use ASCII characters

The policy recommends english, so french parenthesis must be replaced.
More generally, prudence recommends ASCII in Make scripts.
---
 scripts/mk/buildtools.mk | 2 +-
 scripts/mk/vendor.mk     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/scripts/mk/buildtools.mk b/scripts/mk/buildtools.mk
index 933fdcfaa..7c6732210 100644
--- a/scripts/mk/buildtools.mk
+++ b/scripts/mk/buildtools.mk
@@ -20,7 +20,7 @@
 #   QMAKE: Qt build system generator (since dpkg 1.20.0).
 #
 # All the above variables have a counterpart variable for the build tool,
-# as in CC → CC_FOR_BUILD.
+# as in CC -> CC_FOR_BUILD.
 #
 # The variables are not exported by default. This can be changed by
 # defining DPKG_EXPORT_BUILDTOOLS.
diff --git a/scripts/mk/vendor.mk b/scripts/mk/vendor.mk
index f3241a57b..8bdaa235a 100644
--- a/scripts/mk/vendor.mk
+++ b/scripts/mk/vendor.mk
@@ -1,8 +1,8 @@
 # This Makefile fragment (since dpkg 1.16.1) defines the following
 # vendor-related variables:
 #
-#   DEB_VENDOR: output of «dpkg-vendor --query Vendor».
-#   DEB_PARENT_VENDOR: output of «dpkg-vendor --query Parent» (can be empty).
+#   DEB_VENDOR: output of "dpkg-vendor --query Vendor".
+#   DEB_PARENT_VENDOR: output of "dpkg-vendor --query Parent" (can be empty).
 #
 # This Makefile fragment also defines a set of "dpkg_vendor_derives_from"
 # macros that can be used to verify if the current vendor derives from
-- 
2.39.2

>From c5c0e7a90eb37d75b8bbca3cac412df1cb1ab911 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Mon, 29 Jul 2019 14:38:32 +0200
Subject: [PATCH 02/11] scripts/mk: protect files against double inclusion

Architecture.mk is included by default.mk the by buildtools.mk.
Buildapi.mk is included by default.mk and vendor.mk.
---
 scripts/mk/architecture.mk | 5 +++++
 scripts/mk/buildapi.mk     | 5 +++++
 scripts/mk/buildflags.mk   | 6 ++++++
 scripts/mk/buildopts.mk    | 5 +++++
 scripts/mk/buildtools.mk   | 5 +++++
 scripts/mk/default.mk      | 5 +++++
 scripts/mk/pkg-info.mk     | 5 +++++
 scripts/mk/vendor.mk       | 5 +++++
 8 files changed, 41 insertions(+)

diff --git a/scripts/mk/architecture.mk b/scripts/mk/architecture.mk
index c11cada16..1565903c4 100644
--- a/scripts/mk/architecture.mk
+++ b/scripts/mk/architecture.mk
@@ -2,6 +2,9 @@
 # DEB_BUILD_* variables that dpkg-architecture can return. Existing values
 # of those variables are preserved as per policy.
 
+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))
@@ -9,3 +12,5 @@ dpkg_architecture_setvar = export $(1) ?= $(call dpkg_lazy_eval,$(1),dpkg-archit
 $(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)))))
+
+endif # ifndef /usr/share/dpkg/architecture.mk_included
diff --git a/scripts/mk/buildapi.mk b/scripts/mk/buildapi.mk
index 668e325c8..cb68d91c2 100644
--- a/scripts/mk/buildapi.mk
+++ b/scripts/mk/buildapi.mk
@@ -1,5 +1,8 @@
 # This Makefile fragment (since dpkg 1.22.0) handles the build API.
 
+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)
 
@@ -7,3 +10,5 @@ DPKG_BUILD_API ?= $(shell dpkg-buildapi)
 # complexity given no integer operators, given that we currently have to
 # fetch the build API level anyway.
 dpkg_build_api_ge = $(shell test "$(DPKG_BUILD_API)" -ge "$(1)" && echo yes)
+
+endif # ifndef /usr/share/dpkg/buildapi.mk_included
diff --git a/scripts/mk/buildflags.mk b/scripts/mk/buildflags.mk
index 4b8a3d8c4..0fb9f8e91 100644
--- a/scripts/mk/buildflags.mk
+++ b/scripts/mk/buildflags.mk
@@ -28,6 +28,10 @@
 # You can also export them in the environment by setting
 # DPKG_EXPORT_BUILDFLAGS to a non-empty value.
 #
+
+ifndef /usr/share/dpkg/buildflags.mk_included
+/usr/share/dpkg/buildflags.mk_included = yes
+
 # This list is kept in sync with the default set of flags returned
 # by dpkg-buildflags.
 
@@ -77,3 +81,5 @@ $(foreach flag,$(DPKG_BUILDFLAGS_LIST),\
 ifdef DPKG_EXPORT_BUILDFLAGS
   export $(DPKG_BUILDFLAGS_LIST)
 endif
+
+endif # ifndef /usr/share/dpkg/buildflags.mk_included
diff --git a/scripts/mk/buildopts.mk b/scripts/mk/buildopts.mk
index c95777719..2aa65f5d0 100644
--- a/scripts/mk/buildopts.mk
+++ b/scripts/mk/buildopts.mk
@@ -5,6 +5,11 @@
 #
 #   DEB_BUILD_OPTION_PARALLEL: the argument for the parallel=N option.
 
+ifndef /usr/share/dpkg/buildopts.mk_included
+/usr/share/dpkg/buildopts.mk_included = yes
+
 ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
   DEB_BUILD_OPTION_PARALLEL = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
 endif
+
+endif # ifndef /usr/share/dpkg/buildopts.mk_included
diff --git a/scripts/mk/buildtools.mk b/scripts/mk/buildtools.mk
index 7c6732210..bbf4e799c 100644
--- a/scripts/mk/buildtools.mk
+++ b/scripts/mk/buildtools.mk
@@ -25,6 +25,9 @@
 # The variables are not exported by default. This can be changed by
 # defining DPKG_EXPORT_BUILDTOOLS.
 
+ifndef /usr/share/dpkg/buildtools.mk_included
+/usr/share/dpkg/buildtools.mk_included = yes
+
 dpkg_datadir = $(srcdir)/mk
 include $(dpkg_datadir)/architecture.mk
 
@@ -74,3 +77,5 @@ $(eval $(call dpkg_buildtool_setvar,AR,ar))
 $(eval $(call dpkg_buildtool_setvar,RANLIB,ranlib))
 $(eval $(call dpkg_buildtool_setvar,PKG_CONFIG,pkgconf))
 $(eval $(call dpkg_buildtool_setvar,QMAKE,qmake))
+
+endif # ifndef /usr/share/dpkg/buildtools.mk_included
diff --git a/scripts/mk/default.mk b/scripts/mk/default.mk
index 0b2fd4aca..60c85df2a 100644
--- a/scripts/mk/default.mk
+++ b/scripts/mk/default.mk
@@ -1,6 +1,9 @@
 # This Makefile fragment (since dpkg 1.16.1) includes all the Makefile
 # fragments that define variables that can be useful within debian/rules.
 
+ifndef /usr/share/dpkg/default.mk_included
+/usr/share/dpkg/default.mk_included = yes
+
 dpkg_datadir = $(srcdir)/mk
 include $(dpkg_datadir)/architecture.mk
 include $(dpkg_datadir)/buildapi.mk
@@ -11,3 +14,5 @@ include $(dpkg_datadir)/buildflags.mk
 include $(dpkg_datadir)/buildopts.mk
 include $(dpkg_datadir)/pkg-info.mk
 include $(dpkg_datadir)/vendor.mk
+
+endif # /usr/share/dpkg/default.mk_included
diff --git a/scripts/mk/pkg-info.mk b/scripts/mk/pkg-info.mk
index bccde2317..144c0d758 100644
--- a/scripts/mk/pkg-info.mk
+++ b/scripts/mk/pkg-info.mk
@@ -13,6 +13,9 @@
 #     specified by <https://reproducible-builds.org/specs/source-date-epoch/>
 #     (since dpkg 1.18.8).
 
+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)
@@ -25,3 +28,5 @@ DEB_DISTRIBUTION = $(call dpkg_late_eval,DEB_DISTRIBUTION,dpkg-parsechangelog -S
 SOURCE_DATE_EPOCH ?= $(call dpkg_late_eval,SOURCE_DATE_EPOCH,dpkg-parsechangelog -STimestamp)
 
 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 8bdaa235a..03082dc7d 100644
--- a/scripts/mk/vendor.mk
+++ b/scripts/mk/vendor.mk
@@ -33,6 +33,9 @@
 #     ...
 #   endif
 
+ifndef /usr/share/dpkg/vendor.mk_included
+/usr/share/dpkg/vendor.mk_included = yes
+
 dpkg_datadir = $(srcdir)/mk
 include $(dpkg_datadir)/buildapi.mk
 
@@ -49,3 +52,5 @@ dpkg_vendor_derives_from ?= $(dpkg_vendor_derives_from_v1)
 else
 dpkg_vendor_derives_from ?= $(dpkg_vendor_derives_from_v0)
 endif
+
+endif # ifndef /usr/share/dpkg/vendor.mk_included
-- 
2.39.2

>From f2bfba30836b85a4e06d06762bedc22638c64def Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Mon, 4 Mar 2024 11:58:53 +0100
Subject: [PATCH 03/11] scripts/mk: stop hard-coding dpkg_datadir

This path differ during tests and after installation.  Instead of
rewriting the file with a hardcoded path, compute it within Make.
---
 build-aux/subst.am       |  6 ------
 scripts/mk/Makefile.am   | 21 ---------------------
 scripts/mk/buildtools.mk |  5 ++++-
 scripts/mk/default.mk    |  3 ++-
 scripts/mk/vendor.mk     |  5 ++++-
 5 files changed, 10 insertions(+), 30 deletions(-)

diff --git a/build-aux/subst.am b/build-aux/subst.am
index 5515930d0..167a71257 100644
--- a/build-aux/subst.am
+++ b/build-aux/subst.am
@@ -39,9 +39,3 @@ SUFFIXES += .pl
 	@test -d `dirname $@` || $(MKDIR_P) `dirname $@`
 	$(do_perl_subst) <$< >$@
 	$(AM_V_at) chmod +x $@
-
-# Makefile support.
-
-do_make_subst = $(AM_V_GEN) $(SED) \
-	-e "s:dpkg_datadir[[:space:]]*=[[:space:]]*[^[:space:]]*:dpkg_datadir = $(pkgdatadir):" \
-	# EOL
diff --git a/scripts/mk/Makefile.am b/scripts/mk/Makefile.am
index 257ba5252..6e85e17b9 100644
--- a/scripts/mk/Makefile.am
+++ b/scripts/mk/Makefile.am
@@ -10,24 +10,3 @@ dist_pkgdata_DATA = \
 	pkg-info.mk \
 	vendor.mk \
 	# EOL
-
-SUFFIXES =
-
-include $(top_srcdir)/build-aux/subst.am
-
-# Ideally we'd use '$(SED) -i', but unfortunately that's not portable.
-install-data-hook:
-	$(do_make_subst) <$(DESTDIR)$(pkgdatadir)/default.mk \
-	                 >$(DESTDIR)$(pkgdatadir)/default.mk.new
-	mv $(DESTDIR)$(pkgdatadir)/default.mk.new \
-	   $(DESTDIR)$(pkgdatadir)/default.mk
-
-	$(do_make_subst) <$(DESTDIR)$(pkgdatadir)/buildtools.mk \
-	                 >$(DESTDIR)$(pkgdatadir)/buildtools.mk.new
-	mv $(DESTDIR)$(pkgdatadir)/buildtools.mk.new \
-	   $(DESTDIR)$(pkgdatadir)/buildtools.mk
-
-	$(do_make_subst) <$(DESTDIR)$(pkgdatadir)/vendor.mk \
-			 >$(DESTDIR)$(pkgdatadir)/vendor.mk.new
-	mv $(DESTDIR)$(pkgdatadir)/vendor.mk.new \
-	   $(DESTDIR)$(pkgdatadir)/vendor.mk
diff --git a/scripts/mk/buildtools.mk b/scripts/mk/buildtools.mk
index bbf4e799c..d70f546f5 100644
--- a/scripts/mk/buildtools.mk
+++ b/scripts/mk/buildtools.mk
@@ -28,7 +28,10 @@
 ifndef /usr/share/dpkg/buildtools.mk_included
 /usr/share/dpkg/buildtools.mk_included = yes
 
-dpkg_datadir = $(srcdir)/mk
+# Default.mk computes dpkg_datadir, other .mk files may use it again.
+ifndef dpkg_datadir
+  dpkg_datadir := $(patsubst %/buildtools.mk,%,$(lastword $(MAKEFILE_LIST)))
+endif
 include $(dpkg_datadir)/architecture.mk
 
 # We set the TOOL_FOR_BUILD variables to the specified value, and the TOOL
diff --git a/scripts/mk/default.mk b/scripts/mk/default.mk
index 60c85df2a..f2a1c9bf1 100644
--- a/scripts/mk/default.mk
+++ b/scripts/mk/default.mk
@@ -4,7 +4,8 @@
 ifndef /usr/share/dpkg/default.mk_included
 /usr/share/dpkg/default.mk_included = yes
 
-dpkg_datadir = $(srcdir)/mk
+dpkg_datadir := $(patsubst %/default.mk,%,$(lastword $(MAKEFILE_LIST)))
+
 include $(dpkg_datadir)/architecture.mk
 include $(dpkg_datadir)/buildapi.mk
 ifeq ($(call dpkg_build_api_ge,1),yes)
diff --git a/scripts/mk/vendor.mk b/scripts/mk/vendor.mk
index 03082dc7d..afd86b014 100644
--- a/scripts/mk/vendor.mk
+++ b/scripts/mk/vendor.mk
@@ -36,7 +36,10 @@
 ifndef /usr/share/dpkg/vendor.mk_included
 /usr/share/dpkg/vendor.mk_included = yes
 
-dpkg_datadir = $(srcdir)/mk
+# Default.mk computes dpkg_datadir, other .mk files may use it again.
+ifndef dpkg_datadir
+  dpkg_datadir := $(patsubst %/vendor.mk,%,$(lastword $(MAKEFILE_LIST)))
+endif
 include $(dpkg_datadir)/buildapi.mk
 
 dpkg_late_eval ?= $(or $(value DPKG_CACHE_$(1)),$(eval DPKG_CACHE_$(1) := $(shell $(2)))$(value DPKG_CACHE_$(1)))
-- 
2.39.2

>From 5484868d661acf8772ff867608225bd26d9b7ae4 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Mon, 1 Nov 2021 10:08:08 +0100
Subject: [PATCH 04/11] scripts/mk/buildopts.mk: search once for parrallel= in
 DEB_BUILD_OPTIONS

Previously each expansion of DEB_BUILD_OPTION_PARALLEL was parsing
DEB_BUILD_OPTIONS.
---
 scripts/mk/buildopts.mk | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/scripts/mk/buildopts.mk b/scripts/mk/buildopts.mk
index 2aa65f5d0..50ffc5ba6 100644
--- a/scripts/mk/buildopts.mk
+++ b/scripts/mk/buildopts.mk
@@ -4,12 +4,16 @@
 # Defines the following variables:
 #
 #   DEB_BUILD_OPTION_PARALLEL: the argument for the parallel=N option.
+#     $(DEB_BUILD_OPTIONS)                "parallel=2"  "parallel="  ""
+#     $(DEB_BUILD_OPTION_PARALLEL)        "2"           ""           unset
+#     $(DEB_BUILD_OPTION_PARALLEL:%=-j%)  "-j2"         ""           ""
 
 ifndef /usr/share/dpkg/buildopts.mk_included
 /usr/share/dpkg/buildopts.mk_included = yes
 
-ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
-  DEB_BUILD_OPTION_PARALLEL = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
+dpkg-buildopts_parallel := $(filter parallel=%,$(DEB_BUILD_OPTIONS))
+ifdef dpkg-buildopts_parallel
+  DEB_BUILD_OPTION_PARALLEL = $(patsubst parallel=%,%,$(dpkg-buildopts_parallel))
 endif
 
 endif # ifndef /usr/share/dpkg/buildopts.mk_included
-- 
2.39.2

>From cdda57e966a8ebd1f884ce33e5c6ef408a51636d Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Sat, 9 Mar 2024 17:21:53 +0100
Subject: [PATCH 05/11] scripts/buildflags.mk: generate the _FOR_BUILD variant
 of each variable

---
 scripts/mk/buildflags.mk | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/scripts/mk/buildflags.mk b/scripts/mk/buildflags.mk
index 0fb9f8e91..d3ce7aba3 100644
--- a/scripts/mk/buildflags.mk
+++ b/scripts/mk/buildflags.mk
@@ -37,28 +37,18 @@ ifndef /usr/share/dpkg/buildflags.mk_included
 
 dpkg_lazy_eval ?= $$(or $$(value DPKG_CACHE_$(1)),$$(eval DPKG_CACHE_$(1) := $$(shell $(2)))$$(value DPKG_CACHE_$(1)))
 
-DPKG_BUILDFLAGS_LIST = \
+DPKG_BUILDFLAGS_LIST := $(foreach var,\
   ASFLAGS \
-  ASFLAGS_FOR_BUILD \
   CFLAGS \
-  CFLAGS_FOR_BUILD \
   CPPFLAGS \
-  CPPFLAGS_FOR_BUILD \
   CXXFLAGS \
-  CXXFLAGS_FOR_BUILD \
   OBJCFLAGS \
-  OBJCFLAGS_FOR_BUILD \
   OBJCXXFLAGS \
-  OBJCXXFLAGS_FOR_BUILD \
   DFLAGS \
-  DFLAGS_FOR_BUILD \
   FFLAGS \
-  FFLAGS_FOR_BUILD \
   FCFLAGS \
-  FCFLAGS_FOR_BUILD \
   LDFLAGS \
-  LDFLAGS_FOR_BUILD \
-  # EOL
+  ,$(var) $(var)_FOR_BUILD)
 
 define dpkg_buildflags_export_envvar
   ifdef $(1)
-- 
2.39.2

>From de56dbe7d873fa825f10399d3cedd418f4f58cc7 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Fri, 8 Mar 2024 00:40:50 +0100
Subject: [PATCH 06/11] scripts/buildflags.mk: add missing GCJFLAGS

---
 scripts/mk/buildflags.mk | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/scripts/mk/buildflags.mk b/scripts/mk/buildflags.mk
index d3ce7aba3..700d2a1f7 100644
--- a/scripts/mk/buildflags.mk
+++ b/scripts/mk/buildflags.mk
@@ -10,6 +10,7 @@
 #   DFLAGS: flags for the host D compiler.
 #   FFLAGS: flags for the host Fortran 77 compiler.
 #   FCFLAGS: flags for the host Fortran 9x compiler.
+#   GCJFLAGS: flags for the host java compiler.
 #   LDFLAGS: flags for the host linker.
 #
 # And the following variables for the build tools (since dpkg 1.22.1):
@@ -23,6 +24,7 @@
 #   DFLAGS_FOR_BUILD: flags for the build D compiler.
 #   FFLAGS_FOR_BUILD: flags for the build Fortran 77 compiler.
 #   FCFLAGS_FOR_BUILD: flags for the build Fortran 9x compiler.
+#   GCJFLAGS_FOR_BUILD: flags for the host java compiler.
 #   LDFLAGS_FOR_BUILD: flags for the build linker.
 #
 # You can also export them in the environment by setting
@@ -47,6 +49,7 @@ DPKG_BUILDFLAGS_LIST := $(foreach var,\
   DFLAGS \
   FFLAGS \
   FCFLAGS \
+  GCJFLAGS \
   LDFLAGS \
   ,$(var) $(var)_FOR_BUILD)
 
-- 
2.39.2

>From 936c3ea007b8a222c186696442bac4ad5e052d38 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Sun, 10 Mar 2024 18:12:31 +0100
Subject: [PATCH 07/11] scripts/buildflags.mk: sort the flag list

in order to ease the comparison with the output of dpkg-buildflags.
---
 scripts/mk/buildflags.mk | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/scripts/mk/buildflags.mk b/scripts/mk/buildflags.mk
index 700d2a1f7..7d949bc04 100644
--- a/scripts/mk/buildflags.mk
+++ b/scripts/mk/buildflags.mk
@@ -5,13 +5,13 @@
 #   CFLAGS: flags for the host C compiler.
 #   CPPFLAGS: flags for the host C preprocessor.
 #   CXXFLAGS: flags for the host C++ compiler.
-#   OBJCFLAGS: flags for the host Objective C compiler.
-#   OBJCXXFLAGS: flags for the host Objective C++ compiler.
 #   DFLAGS: flags for the host D compiler.
-#   FFLAGS: flags for the host Fortran 77 compiler.
 #   FCFLAGS: flags for the host Fortran 9x compiler.
+#   FFLAGS: flags for the host Fortran 77 compiler.
 #   GCJFLAGS: flags for the host java compiler.
 #   LDFLAGS: flags for the host linker.
+#   OBJCFLAGS: flags for the host Objective C compiler.
+#   OBJCXXFLAGS: flags for the host Objective C++ compiler.
 #
 # And the following variables for the build tools (since dpkg 1.22.1):
 #
@@ -19,13 +19,13 @@
 #   CFLAGS_FOR_BUILD: flags for the build C compiler.
 #   CPPFLAGS_FOR_BUILD: flags for the build C preprocessor.
 #   CXXFLAGS_FOR_BUILD: flags for the build C++ compiler.
-#   OBJCFLAGS_FOR_BUILD: flags for the build Objective C compiler.
-#   OBJCXXFLAGS_FOR_BUILD: flags for the build Objective C++ compiler.
 #   DFLAGS_FOR_BUILD: flags for the build D compiler.
-#   FFLAGS_FOR_BUILD: flags for the build Fortran 77 compiler.
 #   FCFLAGS_FOR_BUILD: flags for the build Fortran 9x compiler.
+#   FFLAGS_FOR_BUILD: flags for the build Fortran 77 compiler.
 #   GCJFLAGS_FOR_BUILD: flags for the host java compiler.
 #   LDFLAGS_FOR_BUILD: flags for the build linker.
+#   OBJCFLAGS_FOR_BUILD: flags for the build Objective C compiler.
+#   OBJCXXFLAGS_FOR_BUILD: flags for the build Objective C++ compiler.
 #
 # You can also export them in the environment by setting
 # DPKG_EXPORT_BUILDFLAGS to a non-empty value.
@@ -44,13 +44,13 @@ DPKG_BUILDFLAGS_LIST := $(foreach var,\
   CFLAGS \
   CPPFLAGS \
   CXXFLAGS \
-  OBJCFLAGS \
-  OBJCXXFLAGS \
   DFLAGS \
-  FFLAGS \
   FCFLAGS \
+  FFLAGS \
   GCJFLAGS \
   LDFLAGS \
+  OBJCFLAGS \
+  OBJCXXFLAGS \
   ,$(var) $(var)_FOR_BUILD)
 
 define dpkg_buildflags_export_envvar
-- 
2.39.2

>From 9f8229fbb28fccc1d3edd1b6e5637c08be96bca6 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@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

>From 82a811cd9d2a46f37e820db7dd40be37c0acaaa2 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Wed, 10 Jan 2024 18:11:55 +0100
Subject: [PATCH 09/11] scripts/t: use loops instead of repetitions, check
 exports and overrides

Replace copied lines with Make loops.

Add tests:
  architecture variable override,
  buildflags set and export,
  GCJFLAGS,
  buildtool override and export.
---
 scripts/t/mk/architecture.mk | 60 +++++++++++++++------------------
 scripts/t/mk/buildflags.mk   | 53 +++++++++++++++++------------
 scripts/t/mk/buildtools.mk   | 65 +++++++++++++++++++-----------------
 3 files changed, 92 insertions(+), 86 deletions(-)

diff --git a/scripts/t/mk/architecture.mk b/scripts/t/mk/architecture.mk
index 2ac0222ca..d511cc759 100644
--- a/scripts/t/mk/architecture.mk
+++ b/scripts/t/mk/architecture.mk
@@ -1,36 +1,28 @@
+DEB_BUILD_ARCH := overridden
+
 include $(srcdir)/mk/architecture.mk
 
-test:
-	test "$(DEB_BUILD_ARCH)" = "$(TEST_DEB_BUILD_ARCH)"
-	test "$(DEB_BUILD_ARCH_ABI)" = "$(TEST_DEB_BUILD_ARCH_ABI)"
-	test "$(DEB_BUILD_ARCH_BITS)" = "$(TEST_DEB_BUILD_ARCH_BITS)"
-	test "$(DEB_BUILD_ARCH_CPU)" = "$(TEST_DEB_BUILD_ARCH_CPU)"
-	test "$(DEB_BUILD_ARCH_ENDIAN)" = "$(TEST_DEB_BUILD_ARCH_ENDIAN)"
-	test "$(DEB_BUILD_ARCH_LIBC)" = "$(TEST_DEB_BUILD_ARCH_LIBC)"
-	test "$(DEB_BUILD_ARCH_OS)" = "$(TEST_DEB_BUILD_ARCH_OS)"
-	test "$(DEB_BUILD_GNU_CPU)" = "$(TEST_DEB_BUILD_GNU_CPU)"
-	test "$(DEB_BUILD_GNU_SYSTEM)" = "$(TEST_DEB_BUILD_GNU_SYSTEM)"
-	test "$(DEB_BUILD_GNU_TYPE)" = "$(TEST_DEB_BUILD_GNU_TYPE)"
-	test "$(DEB_BUILD_MULTIARCH)" = "$(TEST_DEB_BUILD_MULTIARCH)"
-	test "$(DEB_HOST_ARCH)" = "$(TEST_DEB_HOST_ARCH)"
-	test "$(DEB_HOST_ARCH_ABI)" = "$(TEST_DEB_HOST_ARCH_ABI)"
-	test "$(DEB_HOST_ARCH_BITS)" = "$(TEST_DEB_HOST_ARCH_BITS)"
-	test "$(DEB_HOST_ARCH_CPU)" = "$(TEST_DEB_HOST_ARCH_CPU)"
-	test "$(DEB_HOST_ARCH_ENDIAN)" = "$(TEST_DEB_HOST_ARCH_ENDIAN)"
-	test "$(DEB_HOST_ARCH_LIBC)" = "$(TEST_DEB_HOST_ARCH_LIBC)"
-	test "$(DEB_HOST_ARCH_OS)" = "$(TEST_DEB_HOST_ARCH_OS)"
-	test "$(DEB_HOST_GNU_CPU)" = "$(TEST_DEB_HOST_GNU_CPU)"
-	test "$(DEB_HOST_GNU_SYSTEM)" = "$(TEST_DEB_HOST_GNU_SYSTEM)"
-	test "$(DEB_HOST_GNU_TYPE)" = "$(TEST_DEB_HOST_GNU_TYPE)"
-	test "$(DEB_HOST_MULTIARCH)" = "$(TEST_DEB_HOST_MULTIARCH)"
-	test "$(DEB_TARGET_ARCH)" = "$(TEST_DEB_TARGET_ARCH)"
-	test "$(DEB_TARGET_ARCH_ABI)" = "$(TEST_DEB_TARGET_ARCH_ABI)"
-	test "$(DEB_TARGET_ARCH_BITS)" = "$(TEST_DEB_TARGET_ARCH_BITS)"
-	test "$(DEB_TARGET_ARCH_CPU)" = "$(TEST_DEB_TARGET_ARCH_CPU)"
-	test "$(DEB_TARGET_ARCH_ENDIAN)" = "$(TEST_DEB_TARGET_ARCH_ENDIAN)"
-	test "$(DEB_TARGET_ARCH_LIBC)" = "$(TEST_DEB_TARGET_ARCH_LIBC)"
-	test "$(DEB_TARGET_ARCH_OS)" = "$(TEST_DEB_TARGET_ARCH_OS)"
-	test "$(DEB_TARGET_GNU_CPU)" = "$(TEST_DEB_TARGET_GNU_CPU)"
-	test "$(DEB_TARGET_GNU_SYSTEM)" = "$(TEST_DEB_TARGET_GNU_SYSTEM)"
-	test "$(DEB_TARGET_GNU_TYPE)" = "$(TEST_DEB_TARGET_GNU_TYPE)"
-	test "$(DEB_TARGET_MULTIARCH)" = "$(TEST_DEB_TARGET_MULTIARCH)"
+vars := $(foreach machine,BUILD HOST TARGET,$(foreach var,\
+  ARCH \
+  ARCH_ABI \
+  ARCH_BITS \
+  ARCH_CPU \
+  ARCH_ENDIAN \
+  ARCH_LIBC \
+  ARCH_OS \
+  GNU_CPU \
+  GNU_SYSTEM \
+  GNU_TYPE \
+  MULTIARCH \
+  ,DEB_$(machine)_$(var)))
+loop_targets := $(filter-out DEB_BUILD_ARCH,$(vars))
+
+test: $(loop_targets)
+	test '$(DEB_BUILD_ARCH)' = 'overridden'
+	test "$${DEB_BUILD_ARCH}" = 'overridden'
+
+$(loop_targets):
+  # Test the Make variable.
+	test '$($@)' = '$(TEST_$@)'
+  # Test the exported shell variable.
+	test "$${$@}" = '$(TEST_$@)'
diff --git a/scripts/t/mk/buildflags.mk b/scripts/t/mk/buildflags.mk
index 94d85a7e0..166c95548 100644
--- a/scripts/t/mk/buildflags.mk
+++ b/scripts/t/mk/buildflags.mk
@@ -1,26 +1,37 @@
 DEB_CPPFLAGS_MAINT_APPEND = -DTEST_MK=test-host
 DEB_CPPFLAGS_FOR_BUILD_MAINT_APPEND = -DTEST_MK=test-build
+DEB_CXXFLAGS_MAINT_SET := set
+DEB_CXXFLAGS_FOR_BUILD_MAINT_SET := set-build
+DPKG_EXPORT_BUILDFLAGS := 1
 
 include $(srcdir)/mk/buildflags.mk
 
-test:
-	test "$(ASFLAGS)" = "$(TEST_ASFLAGS)"
-	test "$(ASFLAGS_FOR_BUILD)" = "$(TEST_ASFLAGS_FOR_BUILD)"
-	test "$(CFLAGS)" = "$(TEST_CFLAGS)"
-	test "$(CFLAGS_FOR_BUILD)" = "$(TEST_CFLAGS_FOR_BUILD)"
-	test "$(CPPFLAGS)" = "$(TEST_CPPFLAGS) -DTEST_MK=test-host"
-	test "$(CPPFLAGS_FOR_BUILD)" = "$(TEST_CPPFLAGS_FOR_BUILD)-DTEST_MK=test-build"
-	test "$(CXXFLAGS)" = "$(TEST_CXXFLAGS)"
-	test "$(CXXFLAGS_FOR_BUILD)" = "$(TEST_CXXFLAGS_FOR_BUILD)"
-	test "$(DFLAGS)" = "$(TEST_DFLAGS)"
-	test "$(DFLAGS_FOR_BUILD)" = "$(TEST_DFLAGS_FOR_BUILD)"
-	test "$(FCFLAGS)" = "$(TEST_FCFLAGS)"
-	test "$(FCFLAGS_FOR_BUILD)" = "$(TEST_FCFLAGS_FOR_BUILD)"
-	test "$(FFLAGS)" = "$(TEST_FFLAGS)"
-	test "$(FFLAGS_FOR_BUILD)" = "$(TEST_FFLAGS_FOR_BUILD)"
-	test "$(LDFLAGS)" = "$(TEST_LDFLAGS)"
-	test "$(LDFLAGS_FOR_BUILD)" = "$(TEST_LDFLAGS_FOR_BUILD)"
-	test "$(OBJCFLAGS)" = "$(TEST_OBJCFLAGS)"
-	test "$(OBJCFLAGS_FOR_BUILD)" = "$(TEST_OBJCFLAGS_FOR_BUILD)"
-	test "$(OBJCXXFLAGS)" = "$(TEST_OBJCXXFLAGS)"
-	test "$(OBJCXXFLAGS_FOR_BUILD)" = "$(TEST_OBJCXXFLAGS_FOR_BUILD)"
+vars := \
+  ASFLAGS \
+  CFLAGS \
+  DFLAGS \
+  FCFLAGS \
+  FFLAGS \
+  GCJFLAGS \
+  LDFLAGS \
+  OBJCFLAGS \
+  OBJCXXFLAGS \
+  # EOL
+loop_targets := $(vars) $(vars:=_FOR_BUILD)
+
+test: $(loop_targets)
+	test '$(CXXFLAGS)' = 'set'
+	test "$${CXXFLAGS}" = 'set'
+	test '$(CXXFLAGS_FOR_BUILD)' = 'set-build'
+	test "$${CXXFLAGS_FOR_BUILD}" = 'set-build'
+
+	test '$(CPPFLAGS)' = '$(TEST_CPPFLAGS) -DTEST_MK=test-host'
+	test "$${CPPFLAGS}" = '$(TEST_CPPFLAGS) -DTEST_MK=test-host'
+	test '$(CPPFLAGS_FOR_BUILD)' = '$(TEST_CPPFLAGS_FOR_BUILD) -DTEST_MK=test-build'
+	test "$${CPPFLAGS_FOR_BUILD}" = '$(TEST_CPPFLAGS_FOR_BUILD) -DTEST_MK=test-build'
+
+$(loop_targets):
+  # Test the Make variable.
+	test '$($@)' = '$(TEST_$@)'
+  # Test the exported shell variable.
+	test "$${$@}" = '$(TEST_$@)'
diff --git a/scripts/t/mk/buildtools.mk b/scripts/t/mk/buildtools.mk
index 6c27c5cf2..1df8f8b61 100644
--- a/scripts/t/mk/buildtools.mk
+++ b/scripts/t/mk/buildtools.mk
@@ -1,33 +1,36 @@
+AR := overridden
+DPKG_EXPORT_BUILDTOOLS := 1
+
 include $(srcdir)/mk/buildtools.mk
 
-test:
-	test "$(AS)" = "$(TEST_AS)"
-	test "$(AS_FOR_BUILD)" = "$(TEST_AS_FOR_BUILD)"
-	test "$(CC)" = "$(TEST_CC)"
-	test "$(CC_FOR_BUILD)" = "$(TEST_CC_FOR_BUILD)"
-	test "$(CXX)" = "$(TEST_CXX)"
-	test "$(CXX_FOR_BUILD)" = "$(TEST_CXX_FOR_BUILD)"
-	test "$(OBJC)" = "$(TEST_OBJC)"
-	test "$(OBJC_FOR_BUILD)" = "$(TEST_OBJC_FOR_BUILD)"
-	test "$(OBJCXX)" = "$(TEST_OBJCXX)"
-	test "$(OBJCXX_FOR_BUILD)" = "$(TEST_OBJCXX_FOR_BUILD)"
-	test "$(F77)" = "$(TEST_F77)"
-	test "$(F77_FOR_BUILD)" = "$(TEST_F77_FOR_BUILD)"
-	test "$(FC)" = "$(TEST_FC)"
-	test "$(FC_FOR_BUILD)" = "$(TEST_FC_FOR_BUILD)"
-	test "$(LD)" = "$(TEST_LD)"
-	test "$(LD_FOR_BUILD)" = "$(TEST_LD_FOR_BUILD)"
-	test "$(STRIP)" = "$(TEST_STRIP)"
-	test "$(STRIP_FOR_BUILD)" = "$(TEST_STRIP_FOR_BUILD)"
-	test "$(OBJCOPY)" = "$(TEST_OBJCOPY)"
-	test "$(OBJCOPY_FOR_BUILD)" = "$(TEST_OBJCOPY_FOR_BUILD)"
-	test "$(OBJDUMP)" = "$(TEST_OBJDUMP)"
-	test "$(OBJDUMP_FOR_BUILD)" = "$(TEST_OBJDUMP_FOR_BUILD)"
-	test "$(NM)" = "$(TEST_NM)"
-	test "$(NM_FOR_BUILD)" = "$(TEST_NM_FOR_BUILD)"
-	test "$(AR)" = "$(TEST_AR)"
-	test "$(AR_FOR_BUILD)" = "$(TEST_AR_FOR_BUILD)"
-	test "$(RANLIB)" = "$(TEST_RANLIB)"
-	test "$(RANLIB_FOR_BUILD)" = "$(TEST_RANLIB_FOR_BUILD)"
-	test "$(PKG_CONFIG)" = "$(TEST_PKG_CONFIG)"
-	test "$(PKG_CONFIG_FOR_BUILD)" = "$(TEST_PKG_CONFIG_FOR_BUILD)"
+tools := \
+  AS \
+  CC \
+  CPP \
+  CXX \
+  F77 \
+  FC \
+  GCJ \
+  LD \
+  NM \
+  OBJC \
+  OBJCOPY \
+  OBJCXX \
+  OBJDUMP \
+  PKG_CONFIG \
+  RANLIB \
+  STRIP \
+  # EOL
+loop_targets := $(tools) $(tools:=_FOR_BUILD)
+
+test: $(loop_targets)
+	test '$(AR)' = 'overridden'
+	test "$${AR}" = 'overridden'
+	test '$(AR_FOR_BUILD)' = 'overridden'
+	test "$${AR_FOR_BUILD}" = 'overridden'
+
+$(loop_targets):
+  # Test the Make variable.
+	test '$($@)' = '$(TEST_$@)'
+  # Test the exported shell variable.
+	test "$${$@}" = '$(TEST_$@)'
-- 
2.39.2

>From 6fde4e1a9c486960860238a0afeebb5bc68b9493 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Sun, 10 Mar 2024 21:25:08 +0100
Subject: [PATCH 10/11] scripts/buildtools.mk: style suggestions

* $(filter nostrip,$(DEB_BUILD_OPTIONS)) is slightly better than
  $(findstring) because the latter matches 'arduinostrip'.

* An explicit test of $(origin) instead of ?= makes the intent of each
  stanza more readable (first ensure that TOOL is set, then
  TOOL_FOR_BUILT).

* Special case nostrip outside generic dpkg_buildtool_setvar
  procedure.
---
 scripts/mk/buildtools.mk | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/scripts/mk/buildtools.mk b/scripts/mk/buildtools.mk
index d70f546f5..aefef9b47 100644
--- a/scripts/mk/buildtools.mk
+++ b/scripts/mk/buildtools.mk
@@ -39,22 +39,17 @@ include $(dpkg_datadir)/architecture.mk
 # not defined or contain the make built-in defaults. On native builds if
 # TOOL is defined and TOOL_FOR_BUILD is not, we fallback to use TOOL.
 define dpkg_buildtool_setvar
-  ifeq (,$(findstring $(3),$(DEB_BUILD_OPTIONS)))
-    ifeq ($(origin $(1)),default)
-      $(1) = $(DEB_HOST_GNU_TYPE)-$(2)
-    else
-      $(1) ?= $(DEB_HOST_GNU_TYPE)-$(2)
-    endif
+  ifneq (,$(filter default undefined,$(origin $(1))))
+    $(1) = $(DEB_HOST_GNU_TYPE)-$(2)
+  endif
 
+  ifeq (undefined,$(origin $(1)_FOR_BUILD))
     # On native build fallback to use TOOL if that's defined.
     ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
-      $(1)_FOR_BUILD ?= $$($(1))
+      $(1)_FOR_BUILD = $$($(1))
     else
-      $(1)_FOR_BUILD ?= $(DEB_BUILD_GNU_TYPE)-$(2)
+      $(1)_FOR_BUILD = $(DEB_BUILD_GNU_TYPE)-$(2)
     endif
-  else
-    $(1) = :
-    $(1)_FOR_BUILD = :
   endif
 
   ifdef DPKG_EXPORT_BUILDTOOLS
@@ -72,7 +67,12 @@ $(eval $(call dpkg_buildtool_setvar,OBJCXX,g++))
 $(eval $(call dpkg_buildtool_setvar,F77,gfortran))
 $(eval $(call dpkg_buildtool_setvar,FC,gfortran))
 $(eval $(call dpkg_buildtool_setvar,LD,ld))
-$(eval $(call dpkg_buildtool_setvar,STRIP,strip,nostrip))
+ifneq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
+  STRIP = :
+  STRIP_FOR_BUILD = :
+  # But call dpkg_buildtool_setvar for the export.
+endif
+$(eval $(call dpkg_buildtool_setvar,STRIP,strip))
 $(eval $(call dpkg_buildtool_setvar,OBJCOPY,objcopy))
 $(eval $(call dpkg_buildtool_setvar,OBJDUMP,objdump))
 $(eval $(call dpkg_buildtool_setvar,NM,nm))
-- 
2.39.2

>From bd3f397242418a20d6a8f316e61e3bb88e5da343 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Sun, 10 Mar 2024 23:07:12 +0100
Subject: [PATCH 11/11] scripts/t/mk/buildflags.mk: fix test of _MAINT_APPEND
 when TEST_ is empty

---
 scripts/t/mk/buildflags.mk | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/scripts/t/mk/buildflags.mk b/scripts/t/mk/buildflags.mk
index 166c95548..914b1a36a 100644
--- a/scripts/t/mk/buildflags.mk
+++ b/scripts/t/mk/buildflags.mk
@@ -25,10 +25,11 @@ test: $(loop_targets)
 	test '$(CXXFLAGS_FOR_BUILD)' = 'set-build'
 	test "$${CXXFLAGS_FOR_BUILD}" = 'set-build'
 
-	test '$(CPPFLAGS)' = '$(TEST_CPPFLAGS) -DTEST_MK=test-host'
-	test "$${CPPFLAGS}" = '$(TEST_CPPFLAGS) -DTEST_MK=test-host'
-	test '$(CPPFLAGS_FOR_BUILD)' = '$(TEST_CPPFLAGS_FOR_BUILD) -DTEST_MK=test-build'
-	test "$${CPPFLAGS_FOR_BUILD}" = '$(TEST_CPPFLAGS_FOR_BUILD) -DTEST_MK=test-build'
+# strip eats the space when TEST_CPPFLAGS is empty.
+	test '$(CPPFLAGS)' = '$(strip $(TEST_CPPFLAGS) -DTEST_MK=test-host)'
+	test "$${CPPFLAGS}" = '$(strip $(TEST_CPPFLAGS) -DTEST_MK=test-host)'
+	test '$(CPPFLAGS_FOR_BUILD)' = '$(strip $(TEST_CPPFLAGS_FOR_BUILD) -DTEST_MK=test-build)'
+	test "$${CPPFLAGS_FOR_BUILD}" = '$(strip $(TEST_CPPFLAGS_FOR_BUILD) -DTEST_MK=test-build)'
 
 $(loop_targets):
   # Test the Make variable.
-- 
2.39.2

Reply via email to