diff mbox series

kbuild: deb-pkg: support DEB_BUILD_OPTIONS=parallel=N in debian/rules

Message ID 20230820091505.3812160-1-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series kbuild: deb-pkg: support DEB_BUILD_OPTIONS=parallel=N in debian/rules | expand

Commit Message

Masahiro Yamada Aug. 20, 2023, 9:15 a.m. UTC
'make srcdeb-pkg' generates a source package, which you can build
later by using dpkg-buildpackage.

In older dpkg versions, 'dpkg-buildpackage -j N' sets not only
DEB_BUILD_OPTIONS but also MAKEFLAGS. Hence, passing -j (--jobs)
to dpkg-buildpackage was enough to run parallel building.

The behavior was changed by commit 1d0ea9b2ba3f ("dpkg-buildpackage:
Change -j, --jobs semantics to non-force mode") of dpkg project. [1]

Since then, 'dpkg-buildpackage -j N' sets only DEB_BUILD_OPTIONS,
which is not parsed by the current debian/rules. You cannot build it
in parallel unless you pass --jobs-force instead or set the MAKEFLAGS
environment variable.

Debian policy [2] suggests the following code snippet for debian/rules.

  ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
      NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
      MAKEFLAGS += -j$(NUMJOBS)
  endif

I added slightly different code to debian/rules so 'make -j N deb-pkg'
works as before. In this case, the '-j N' should not be specified in
debian/rules again. 'make deb-pkg' without the -j option must explicitly
pass -j1 to dpkb-buildpackage because otherwise DEB_BUILD_OPTIONS
contains parallel=<nproc> by default.

This should work with almost all Make versions Kbuild supports.

Only the corner case I found not working is 'make-3.82 -j deb-pkg',
which results in single thread building. It is not a big deal because
-j without an argument, which does not limit the number of jobs, is
rarely used.

As far as I tested, the MAKEFLAGS format varies by Make versions.

                 command line option         $(MAKEFLAGS) in recipe
 Make 3.82            -j                            j
                      -j1                         <none>
                      -j2                          -j
 Make 4.0/4.1         -j                           -j
                      -j1                         <none>
                      -j2                          -j
 Make 4.2+            -j                           -j
                      -j1                          -j1
                      -j2                          -j2

[1] https://salsa.debian.org/dpkg-team/dpkg/-/commit/1d0ea9b2ba3f6a2de5b1a6ff55f3df7b71f73db6
[2] https://www.debian.org/doc/debian-policy/ch-source.html#s-debianrules-options

Reported-by: Bastian Germann <bage@linutronix.de>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/Makefile.package     | 4 +++-
 scripts/package/debian/rules | 8 +++++---
 2 files changed, 8 insertions(+), 4 deletions(-)

Comments

Masahiro Yamada Aug. 20, 2023, 9:05 p.m. UTC | #1
On Mon, Aug 21, 2023 at 1:20 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> 'make srcdeb-pkg' generates a source package, which you can build
> later by using dpkg-buildpackage.
>
> In older dpkg versions, 'dpkg-buildpackage -j N' sets not only
> DEB_BUILD_OPTIONS but also MAKEFLAGS. Hence, passing -j (--jobs)
> to dpkg-buildpackage was enough to run parallel building.
>
> The behavior was changed by commit 1d0ea9b2ba3f ("dpkg-buildpackage:
> Change -j, --jobs semantics to non-force mode") of dpkg project. [1]
>
> Since then, 'dpkg-buildpackage -j N' sets only DEB_BUILD_OPTIONS,
> which is not parsed by the current debian/rules. You cannot build it
> in parallel unless you pass --jobs-force instead or set the MAKEFLAGS
> environment variable.
>
> Debian policy [2] suggests the following code snippet for debian/rules.
>
>   ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
>       NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
>       MAKEFLAGS += -j$(NUMJOBS)
>   endif
>
> I added slightly different code to debian/rules so 'make -j N deb-pkg'
> works as before. In this case, the '-j N' should not be specified in
> debian/rules again. 'make deb-pkg' without the -j option must explicitly
> pass -j1 to dpkb-buildpackage because otherwise DEB_BUILD_OPTIONS
> contains parallel=<nproc> by default.
>
> This should work with almost all Make versions Kbuild supports.
>
> Only the corner case I found not working is 'make-3.82 -j deb-pkg',
> which results in single thread building. It is not a big deal because
> -j without an argument, which does not limit the number of jobs, is
> rarely used.
>
> As far as I tested, the MAKEFLAGS format varies by Make versions.
>
>                  command line option         $(MAKEFLAGS) in recipe
>  Make 3.82            -j                            j
>                       -j1                         <none>
>                       -j2                          -j
>  Make 4.0/4.1         -j                           -j
>                       -j1                         <none>
>                       -j2                          -j
>  Make 4.2+            -j                           -j
>                       -j1                          -j1
>                       -j2                          -j2



On second thought, this is not a good idea.

I will send v2 with a different solution.
diff mbox series

Patch

diff --git a/scripts/Makefile.package b/scripts/Makefile.package
index f8a948ec2c6b..f8b9ba4705c8 100644
--- a/scripts/Makefile.package
+++ b/scripts/Makefile.package
@@ -148,7 +148,9 @@  deb-pkg srcdeb-pkg bindeb-pkg:
 	$(if $(findstring source, $(build-type)), \
 		--unsigned-source --compression=$(KDEB_SOURCE_COMPRESS)) \
 	$(if $(findstring binary, $(build-type)), \
-		--rules-file='$(MAKE) -f debian/rules' -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch), \
+		--rules-file='$(MAKE) -f debian/rules' \
+		$(if $(filter-out -j1,$(filter -j%,$(MAKEFLAGS))),,--jobs=1) \
+		-r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch), \
 		--no-check-builddeps) \
 	$(DPKG_FLAGS))
 
diff --git a/scripts/package/debian/rules b/scripts/package/debian/rules
index 226e127efd63..4753173daaae 100755
--- a/scripts/package/debian/rules
+++ b/scripts/package/debian/rules
@@ -5,11 +5,13 @@  include debian/rules.vars
 
 srctree ?= .
 
+JOBS = $(if $(filter -j%,$(MAKEFLAGS)),,$(patsubst parallel=%,-j%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))))
+
 .PHONY: binary binary-indep binary-arch
 binary: binary-arch binary-indep
 binary-indep: build-indep
 binary-arch: build-arch
-	$(MAKE) -f $(srctree)/Makefile ARCH=$(ARCH) \
+	$(MAKE) $(JOBS) -f $(srctree)/Makefile ARCH=$(ARCH) \
 	KERNELRELEASE=$(KERNELRELEASE) \
 	run-command KBUILD_RUN_COMMAND=+$(srctree)/scripts/package/builddeb
 
@@ -17,7 +19,7 @@  binary-arch: build-arch
 build: build-arch build-indep
 build-indep:
 build-arch:
-	$(MAKE) -f $(srctree)/Makefile ARCH=$(ARCH) \
+	$(MAKE) $(JOBS) -f $(srctree)/Makefile ARCH=$(ARCH) \
 	KERNELRELEASE=$(KERNELRELEASE) \
 	$(shell $(srctree)/scripts/package/deb-build-option) \
 	olddefconfig all
@@ -25,4 +27,4 @@  build-arch:
 .PHONY: clean
 clean:
 	rm -rf debian/files debian/linux-*
-	$(MAKE) -f $(srctree)/Makefile ARCH=$(ARCH) clean
+	$(MAKE) $(JOBS) -f $(srctree)/Makefile ARCH=$(ARCH) clean