+# Returns the arch a package will build for.
+sub package_arch {
+ my $package=shift;
+
+ if (! exists $package_arches{$package}) {
+ warning "package $package is not in control info";
+ return buildarch();
+ }
+ return $package_arches{$package} eq 'all' ? "all" : buildarch();
+}
+
+# Return true if a given package is really a udeb.
+sub is_udeb {
+ my $package=shift;
+
+ if (! exists $package_types{$package}) {
+ warning "package $package is not in control info";
+ return 0;
+ }
+ return $package_types{$package} eq 'udeb';
+}
+
+# Generates the filename that is used for a udeb package.
+sub udeb_filename {
+ my $package=shift;
+
+ my $filearch=package_arch($package);
+ isnative($package); # side effect
+ my $version=$dh{VERSION};
+ $version=~s/^[0-9]+://; # strip any epoch
+ return "${package}_${version}_$filearch.udeb";
+}
+
+# Handles #DEBHELPER# substitution in a script; also can generate a new
+# script from scratch if none exists but there is a .debhelper file for it.
+sub debhelper_script_subst {
+ my $package=shift;
+ my $script=shift;
+
+ my $tmp=tmpdir($package);
+ my $ext=pkgext($package);
+ my $file=pkgfile($package,$script);
+
+ if ($file ne '') {
+ if (-f "debian/$ext$script.debhelper") {
+ # Add this into the script, where it has #DEBHELPER#
+ complex_doit("perl -pe 's~#DEBHELPER#~qx{cat debian/$ext$script.debhelper}~eg' < $file > $tmp/DEBIAN/$script");
+ }
+ else {
+ # Just get rid of any #DEBHELPER# in the script.
+ complex_doit("sed s/#DEBHELPER#// < $file > $tmp/DEBIAN/$script");
+ }
+ doit("chown","0:0","$tmp/DEBIAN/$script");
+ doit("chmod",755,"$tmp/DEBIAN/$script");
+ }
+ elsif ( -f "debian/$ext$script.debhelper" ) {
+ complex_doit("printf '#!/bin/sh\nset -e\n' > $tmp/DEBIAN/$script");
+ complex_doit("cat debian/$ext$script.debhelper >> $tmp/DEBIAN/$script");
+ doit("chown","0:0","$tmp/DEBIAN/$script");
+ doit("chmod",755,"$tmp/DEBIAN/$script");
+ }
+}
+
+# Checks if make's jobserver is enabled via MAKEFLAGS, but
+# the FD used to communicate with it is actually not available.
+sub is_make_jobserver_unavailable {
+ if (exists $ENV{MAKEFLAGS} &&
+ $ENV{MAKEFLAGS} =~ /(?:^|\s)--jobserver-fds=(\d+)/) {
+ if (!open(my $in, "<&$1")) {
+ return 1; # unavailable
+ }
+ else {
+ close $in;
+ return 0; # available
+ }
+ }
+
+ return; # no jobserver specified
+}
+
+# Cleans out jobserver options from MAKEFLAGS.
+sub clean_jobserver_makeflags {
+ if (exists $ENV{MAKEFLAGS}) {
+ if ($ENV{MAKEFLAGS} =~ /(?:^|\s)--jobserver-fds=(\d+)/) {
+ $ENV{MAKEFLAGS} =~ s/(?:^|\s)--jobserver-fds=\S+//g;
+ $ENV{MAKEFLAGS} =~ s/(?:^|\s)-j\b//g;
+ }
+ delete $ENV{MAKEFLAGS} if $ENV{MAKEFLAGS} =~ /^\s*$/;
+ }
+}
+
+# If cross-compiling, returns appropriate cross version of command.
+sub cross_command {
+ my $command=shift;
+ if (dpkg_architecture_value("DEB_BUILD_GNU_TYPE")
+ ne dpkg_architecture_value("DEB_HOST_GNU_TYPE")) {
+ return dpkg_architecture_value("DEB_HOST_GNU_TYPE")."-$command";
+ }
+ else {
+ return $command;
+ }
+}
+
+# Sets environment variables from dpkg-buildflags. Avoids changing
+# any existing environment variables.
+sub set_buildflags {
+ return if $ENV{DH_INTERNAL_BUILDFLAGS} || compat(8);
+ $ENV{DH_INTERNAL_BUILDFLAGS}=1;
+
+ eval "use Dpkg::BuildFlags";
+ if ($@) {
+ warning "unable to load build flags: $@";
+ return;
+ }
+
+ my $buildflags = Dpkg::BuildFlags->new();
+ $buildflags->load_config();
+ foreach my $flag ($buildflags->list()) {
+ next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
+ if (! exists $ENV{$flag}) {
+ $ENV{$flag} = $buildflags->get($flag);
+ }
+ }
+}
+
+# Gets a DEB_BUILD_OPTIONS option, if set.
+sub get_buildoption {
+ my $wanted=shift;
+
+ return undef unless exists $ENV{DEB_BUILD_OPTIONS};
+
+ foreach my $opt (split(/\s+/, $ENV{DEB_BUILD_OPTIONS})) {
+ # currently parallel= is the only one with a parameter
+ if ($opt =~ /^parallel=(-?\d+)$/ && $wanted eq 'parallel') {
+ return $1;
+ }
+ elsif ($opt eq $wanted) {
+ return 1;
+ }
+ }
+}
+