]> git.donarmstrong.com Git - debhelper.git/blobdiff - Debian/Debhelper/Dh_Lib.pm
move obscure function to EOF
[debhelper.git] / Debian / Debhelper / Dh_Lib.pm
index 963a8590b2fc8be14fd0bb331d20b4706808269d..602b77fbc012f97dae29b069c279f2ea64d12390 100644 (file)
@@ -15,7 +15,8 @@ use vars qw(@ISA @EXPORT %dh);
            &filedoublearray &getpackages &basename &dirname &xargs %dh
            &compat &addsubstvar &delsubstvar &excludefile &package_arch
            &is_udeb &udeb_filename &debhelper_script_subst &escape_shell
-           &inhibit_log &load_log);
+           &inhibit_log &load_log &write_log &dpkg_architecture_value
+           &sourcepackage &get_make_jobserver_status);
 
 my $max_compat=7;
 
@@ -174,7 +175,7 @@ sub doit {
        verbose_print(escape_shell(@_));
 
        if (! $dh{NO_ACT}) {
-               system(@_) == 0 || _error_exitcode($_[0]);
+               system(@_) == 0 || _error_exitcode(join(" ", @_));
        }
 }
 
@@ -300,6 +301,7 @@ sub dirname {
                                # Try the file..
                                open (COMPAT_IN, "debian/compat") || error "debian/compat: $!";
                                my $l=<COMPAT_IN>;
+                               close COMPAT_IN;
                                if (! defined $l || ! length $l) {
                                        warning("debian/compat is empty, assuming level $c");
                                }
@@ -345,12 +347,15 @@ sub tmpdir {
 #
 # It tries several filenames:
 #   * debian/package.filename.buildarch
+#   * debian/package.filename.buildos
 #   * debian/package.filename
-#   * debian/file (if the package is the main package)
-# If --name was specified then tonly the first two are tried, and they must
-# have the name after the pacage name:
+#   * debian/filename (if the package is the main package)
+# If --name was specified then the files
+# must have the name after the package name:
 #   * debian/package.name.filename.buildarch
+#   * debian/package.name.filename.buildos
 #   * debian/package.name.filename
+#   * debian/name.filename (if the package is the main package)
 sub pkgfile {
        my $package=shift;
        my $filename=shift;
@@ -360,6 +365,7 @@ sub pkgfile {
        }
        
        my @try=("debian/$package.$filename.".buildarch(),
+                "debian/$package.$filename.".buildos(),
                 "debian/$package.$filename");
        if ($package eq $dh{MAINPACKAGE}) {
                push @try, "debian/$filename";
@@ -600,19 +606,37 @@ sub excludefile {
         return 0;
 }
 
+sub dpkg_architecture_value {
+       my $var = shift;
+       my $value=`dpkg-architecture -q$var` || error("dpkg-architecture failed");
+       chomp $value;
+       return $value;
+}
+
 # Returns the build architecture. (Memoized)
 {
        my $arch;
        
        sub buildarch {
-               return $arch if defined $arch;
-
-               $arch=`dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null` || error("dpkg-architecture failed");
-               chomp $arch;
+               if (!defined $arch) {
+                   $arch=dpkg_architecture_value('DEB_HOST_ARCH');
+               }
                return $arch;
        }
 }
 
+# Returns the build OS. (Memoized)
+{
+       my $os;
+
+       sub buildos {
+               if (!defined $os) {
+                       $os=dpkg_architecture_value("DEB_HOST_ARCH_OS");
+               }
+               return $os;
+       }
+}
+
 # Passed an arch and a list of arches to match against, returns true if matched
 sub samearch {
        my $arch=shift;
@@ -625,10 +649,26 @@ sub samearch {
        return 0;
 }
 
+# Returns source package name
+sub sourcepackage {
+       open (CONTROL, 'debian/control') ||
+           error("cannot read debian/control: $!\n");
+       while (<CONTROL>) {
+               chomp;
+               s/\s+$//;
+               if (/^Source:\s*(.*)/) {
+                       close CONTROL;
+                       return $1;
+               }
+       }
+
+       close CONTROL;
+       error("could not find Source: line in control file.");
+}
+
 # Returns a list of packages in the control file.
-# Must pass "arch" or "indep" or "same" to specify arch-dependant or
-# -independant or same arch packages. If nothing is specified, returns all
-# packages.
+# Pass "arch" or "indep" to specify arch-dependant or
+# independant. If nothing is specified, returns all packages.
 # As a side effect, populates %package_arches and %package_types with the
 # types of all packages (not only those returned).
 my (%package_types, %package_arches);
@@ -639,12 +679,6 @@ sub getpackages {
        %package_arches=();
        
        $type="" if ! defined $type;
-       
-       # Look up the build arch if we need to.
-       my $buildarch='';
-       if ($type eq 'same') {
-               $buildarch=buildarch();
-       }
 
        my $package="";
        my $arch="";
@@ -679,10 +713,12 @@ sub getpackages {
                                $package_types{$package}=$package_type;
                                $package_arches{$package}=$arch;
                        }
+
                        if ($package &&
                            (($type eq 'indep' && $arch eq 'all') ||
-                            ($type eq 'arch' && $arch ne 'all') ||
-                            ($type eq 'same' && ($arch eq 'any' || samearch($buildarch, $arch))) ||
+                            ($type eq 'arch' && ($arch eq 'any' ||
+                                            ($arch ne 'all' &&
+                                             samearch(buildarch(), $arch)))) ||
                             ! $type)) {
                                push @list, $package;
                                $package="";
@@ -758,4 +794,44 @@ sub debhelper_script_subst {
        }
 }
 
+# A helper subroutine for detecting (based on MAKEFLAGS) if make jobserver 
+# is enabled, if it is available or MAKEFLAGS contains "jobs" option.
+# It returns current status (jobserver, jobserver-unavailable or jobs-N where
+# N is number of jobs, 0 if infinite) and MAKEFLAGS cleaned up from 
+# job control options.
+sub get_make_jobserver_status {
+       my $jobsre = qr/(?:^|\s)(?:(?:-j\s*|--jobs(?:=|\s+))(\d+)?|--jobs)\b/;
+       my $status = "";
+       my $makeflags;
+
+       if (exists $ENV{MAKEFLAGS}) {
+               $makeflags = $ENV{MAKEFLAGS};
+               if ($makeflags =~ /(?:^|\s)--jobserver-fds=(\d+)/) {
+                       $status = "jobserver";
+                       if (!open(my $in, "<&", "$1")) {
+                               # Job server is unavailable
+                               $status .= "-unavailable";
+                       }
+                       else {
+                               close $in;
+                       }
+                       # Clean makeflags up
+                       $makeflags =~ s/(?:^|\s)--jobserver-fds=\S+//g;
+                       $makeflags =~ s/(?:^|\s)-j\b//g;
+               }
+               elsif (my @m = ($makeflags =~ /$jobsre/g)) {
+                       # Job count is specified in MAKEFLAGS. Whenever make reads it, a new
+                       # jobserver will be started. Job count returned is 0 if infinite.
+                       $status = "jobs-" . (defined $m[$#m] ? $m[$#m] : "0");
+                       # Clean makeflags up from "jobs" option(s)
+                       $makeflags =~ s/$jobsre//g;
+               }
+       }
+       if ($status) {
+               # MAKEFLAGS could be unset if it is empty
+               $makeflags = undef if $makeflags =~ /^\s*$/;
+       }
+       return wantarray ? ($status, $makeflags) : $status;
+}
+
 1