]> git.donarmstrong.com Git - debhelper.git/blobdiff - Debian/Debhelper/Dh_Lib.pm
r518: * dh_movefiles has long been a sore point in debhelper. Inherited
[debhelper.git] / Debian / Debhelper / Dh_Lib.pm
index 588ea063cce39d687ef3e5239fd38c3038f712c4..0aa883b1f3214e6b6acda5df7de295a6075cf165 100644 (file)
@@ -12,13 +12,17 @@ use vars qw(@ISA @EXPORT %dh);
 @ISA=qw(Exporter);
 @EXPORT=qw(&init &doit &complex_doit &verbose_print &error &warning &tmpdir
            &pkgfile &pkgext &isnative &autoscript &filearray &GetPackages
-           &basename &dirname &xargs %dh &compat);
+           &basename &dirname &xargs %dh &compat &addsubstvar &delsubstvar
+            &excludefile);
 
-my $max_compat=3;
+my $max_compat=4;
 
 sub init {
        # If DH_OPTIONS is set, prepend it @ARGV.
        if (defined($ENV{DH_OPTIONS})) {
+               # Ignore leading/trailing whitespace.
+               $ENV{DH_OPTIONS}=~s/^\s+//;
+               $ENV{DH_OPTIONS}=~s/\s+$//;
                unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
        }
 
@@ -79,7 +83,7 @@ sub init {
 
 # Pass it an array containing the arguments of a shell command like would
 # be run by exec(). It turns that into a line like you might enter at the
-# shell, escaping metacharacters and quoting qrguments that contain spaces.
+# shell, escaping metacharacters and quoting arguments that contain spaces.
 sub escape_shell {
        my @args=@_;
        my $line="";
@@ -196,7 +200,8 @@ sub warning {
 # Returns the basename of the argument passed to it.
 sub basename {
        my $fn=shift;
-       
+
+       $fn=~s/\/$//g; # ignore trailing slashes
        $fn=~s:^.*/(.*?)$:$1:;
        return $fn;
 }
@@ -205,6 +210,7 @@ sub basename {
 sub dirname {
        my $fn=shift;
        
+       $fn=~s/\/$//g; # ignore trailing slashes
        $fn=~s:^(.*)/.*?$:$1:;
        return $fn;
 }
@@ -218,6 +224,12 @@ sub compat {
        if (defined $ENV{DH_COMPAT}) {
                $c=$ENV{DH_COMPAT};
        }
+       elsif (-e 'debian/compat') {
+               # Try the file..
+               open (COMPAT_IN, "debian/compat") || die "debian/compat: $!";
+               $c=<COMPAT_IN>;
+               chomp $c;
+       }
 
        if ($c > $max_compat) {
                error("Sorry, but $max_compat is the highest compatibility level of debhelper currently supported.");
@@ -350,17 +362,79 @@ sub autoscript {
        complex_doit("echo '# End automatically added section' >> $outfile");
 }
 
-# Reads in the specified file, one word at a time, and returns an array of
-# the result. If a value is passed in as the second parameter, then glob
+# Removes a whole substvar line.
+sub delsubstvar {
+       my $package=shift;
+       my $substvar=shift;
+
+       my $ext=pkgext($package);
+       my $substvarfile="debian/${ext}substvars";
+
+       if (-e $substvarfile) {
+               complex_doit("grep -v '^${substvar}=' $substvarfile > $substvarfile.new || true");
+               doit("mv", "$substvarfile.new","$substvarfile");
+       }
+}
+                               
+# Adds a dependency on some package to the specified
+# substvar in a package's substvar's file.
+sub addsubstvar {
+       my $package=shift;
+       my $substvar=shift;
+       my $deppackage=shift;
+       my $verinfo=shift;
+       my $remove=shift;
+
+       my $ext=pkgext($package);
+       my $substvarfile="debian/${ext}substvars";
+       my $str=$deppackage;
+       $str.=" ($verinfo)" if defined $verinfo && length $verinfo;
+
+       # Figure out what the line will look like, based on what's there
+       # now, and what we're to add or remove.
+       my $line="";
+       if (-e $substvarfile) {
+               my %items;
+               open(SUBSTVARS_IN, "$substvarfile") || die "read $substvarfile: $!";
+               while (<SUBSTVARS_IN>) {
+                       chomp;
+                       if (/^\Q$substvar\E=(.*)/) {
+                               %items = map { $_ => 1} split(", ", $1);
+                               
+                               last;
+                       }
+               }
+               close SUBSTVARS_IN;
+               if (! $remove) {
+                       $items{$str}=1;
+               }
+               else {
+                       delete $items{$str};
+               }
+               $line=join(", ", keys %items);
+       }
+       elsif (! $remove) {
+               $line=$str;
+       }
+
+       if (length $line) {
+                complex_doit("echo '${substvar}=$line' >> $substvarfile");
+       }
+}
+
+# Reads in the specified file, one line at a time. splits on words, 
+# and returns an array of arrays of the contents.
+# If a value is passed in as the second parameter, then glob
 # expansion is done in the directory specified by the parameter ("." is
 # frequently a good choice).
-sub filearray {
+sub filedoublearray {
        my $file=shift;
        my $globdir=shift;
 
        my @ret;
        open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
        while (<DH_FARRAY_IN>) {
+               my @line;
                # Only do glob expansion in v3 mode.
                #
                # The tricky bit is that the glob expansion is done
@@ -369,18 +443,34 @@ sub filearray {
                if (defined $globdir && ! compat(2)) {
                        for (map { glob "$globdir/$_" } split) {
                                s#^$globdir/##;
-                               push @ret, $_;
+                               push @line, $_;
                        }
                }
                else {
-                       push @ret, split;
+                       @line = split;
                }
+               push @ret, [@line];
        }
        close DH_FARRAY_IN;
        
        return @ret;
 }
 
+# Reads in the specified file, one word at a time, and returns an array of
+# the result. Can do globbing as does filedoublearray.
+sub filearray {
+       return map { @$_ } filedoublearray(@_);
+}
+
+# Passed a filename, returns true if -X says that file should be excluded.
+sub excludefile {
+        my $filename = shift;
+        foreach my $f (@{$dh{EXCLUDE}}) {
+                return 1 if $filename =~ /\Q$f\E/;
+        }
+        return 0;
+}
+
 # Returns the build architecture. (Memoized)
 {
        my $arch;