Joey Hess <jo...@debian.org> writes:

> I think this should only be done if DEB_BUILD_OPTIONS has parallel=N,
> and should limit dpkg-deb processes to the N. If someone sends a patch
> doing that, I'll apply it.

Such a patch (against git) is attached below. I did little testing only:
it doesn't break building without DEB_BUILD_OPTIONS=parallel=N, and it
seems to work with parallel=4 too.

-- 
|8]

>From efc0aa75c47328018e41a4629fa02fb9cff4dbd0 Mon Sep 17 00:00:00 2001
From: Gergely Nagy <alger...@madhouse-project.org>
Date: Sun, 17 Jul 2011 12:20:41 +0200
Subject: [PATCH] dh_builddeb: support for parallel builds of debs

Implement support for parallel deb builds, when DEB_BUILD_OPTIONS has
parallels=N - limiting the number of forked processes to N.

Requested-By: Kari Pahula <k...@debian.org>
Signed-Off-By: Gergely Nagy <alger...@madhouse-project.org>
---
 dh_builddeb |   56 +++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/dh_builddeb b/dh_builddeb
index b15c943..5378be5 100755
--- a/dh_builddeb
+++ b/dh_builddeb
@@ -63,30 +63,52 @@ else {
 	$dh{FILENAME}="/$dh{FILENAME}";
 }
 
+my $processes=0;
+my $max_procs=0;
+if (defined $ENV{DEB_BUILD_OPTIONS} && $ENV{DEB_BUILD_OPTIONS}=~/parallel=(\d+)/) {
+	$max_procs=$1;
+}
+
 foreach my $package (@{$dh{DOPACKAGES}}) {
-	my $tmp=tmpdir($package);
-	if (exists $ENV{DH_ALWAYS_EXCLUDE} && length $ENV{DH_ALWAYS_EXCLUDE}) {
-		if (! compat(5)) {
-			complex_doit("find $tmp $dh{EXCLUDE_FIND} | xargs rm -rf");
+	my ($pid, $forked) = (0, 0);
+
+	if ($processes < $max_procs) {
+		$pid=fork();
+		$forked=1;
+	}
+
+	if ($pid == 0) {
+		my $tmp=tmpdir($package);
+		if (exists $ENV{DH_ALWAYS_EXCLUDE} && length $ENV{DH_ALWAYS_EXCLUDE}) {
+			if (! compat(5)) {
+				complex_doit("find $tmp $dh{EXCLUDE_FIND} | xargs rm -rf");
+			}
+			else {
+				# Old broken code here for compatibility. Does not
+				# remove everything.
+				complex_doit("find $tmp -name $_ | xargs rm -rf")
+					foreach split(":", $ENV{DH_ALWAYS_EXCLUDE});
+			}
+		}
+		if (! is_udeb($package)) {
+			doit("dpkg-deb", @{$dh{U_PARAMS}}, "--build", $tmp, $dh{DESTDIR}.$dh{FILENAME});
 		}
 		else {
-			# Old broken code here for compatibility. Does not
-			# remove everything.
-			complex_doit("find $tmp -name $_ | xargs rm -rf")
-				foreach split(":", $ENV{DH_ALWAYS_EXCLUDE});
+			my $filename=$dh{FILENAME};
+			if (! $filename) {
+				$filename="/".udeb_filename($package);
+			}
+			doit("dpkg-deb", @{$dh{U_PARAMS}}, "--build", $tmp, $dh{DESTDIR}.$filename);
 		}
-	}
-	if (! is_udeb($package)) {
-		doit("dpkg-deb", @{$dh{U_PARAMS}}, "--build", $tmp, $dh{DESTDIR}.$dh{FILENAME});
-	}
-	else {
-		my $filename=$dh{FILENAME};
-		if (! $filename) {
-			$filename="/".udeb_filename($package);
+		exit (0) if ($forked);
+	} else {
+		if (++$processes > 1) {
+			wait;
+			--$processes;
 		}
-		doit("dpkg-deb", @{$dh{U_PARAMS}}, "--build", $tmp, $dh{DESTDIR}.$filename);
 	}
 }
+while (($max_procs > 0) && (wait != -1)) {}
 
 =head1 SEE ALSO
 
-- 
1.7.2.5

Reply via email to