Support deb packaging extension of 'orig' archive '.bz2'.
[macros.git] / AutoGen.pm
index 4d7428f..52074eb 100644 (file)
@@ -39,10 +39,13 @@ BEGIN {
                use File::Touch;        # &touch
                use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
 HERE
-               eval "$_\n; 1;" or push @missing,(/^\s*use\s+([^\s;]+)/)[0];
+               eval "$_\n; 1;" or push @missing,(/^\s*use\s+([^ ;]+)/)[0];
                }
        die "You are missing some modules - install them by:\n"
                                        ."\tperl -MCPAN -e 'install qw(".join(" ",@missing).")'\n"
+                                       ."If you see messages: Writing Makefile for <packagename> -- NOT OK\n"
+                                       ."you may did not have installed make(1) while configuring Perl CPAN.\n"
+                                       ."Install make(1) and type 'o conf init' in: perl -MCPAN -e shell\n"
                        if @missing;
        }
 
@@ -63,6 +66,23 @@ Supported parameters:
 '.($Options{"help"} || "");
 }
 
+sub checkcommandversion
+{
+my($class,$command,$version)=@_;
+
+       local *F;
+       do { open F,$_ or confess "Open $_: $!"; } for ("$command --version|");
+       local $/;
+       undef $/;
+       my $command_out=<F>;
+       close F;
+       my $command_version=($command_out=~m#([\d.]+)#)[0];
+       confess "$command(1) version not found in its output" if !$command_version;
+       confess "'$command' version $version or higher required"
+                       # Do not take 3rd+ numbers as it would not be a number
+                       if ($command_version=~/^(\d+[.]\d+)/)[0]<$version;
+}
+
 sub _readfile
 {
 my($filename)=@_;
@@ -71,7 +91,7 @@ my($filename)=@_;
        local *F;
        open F,$filename or confess "Open \"$filename\": $!";
        my $r=<F>;
-       close F or cluck "Close \"$filename\": $!";
+       close F or confess "Close \"$filename\": $!";   # Do not &cluck as it may be pipe result
        return $r;
 }
 
@@ -80,9 +100,9 @@ sub _writefile
 my($filename,@content)=@_;
 
        local *F;
-       open F,">".$filename or confess "rewrite \"$filename\": $!";
+       open F,($filename=~/^[|]/ ? "" : ">").$filename or confess "rewrite \"$filename\": $!";
        print F @content;
-       close F or cluck "close \"$filename\": $!";
+       close F or confess "close \"$filename\": $!";   # Do not &cluck as it may be pipe result
 }
 
 my %_rpmeval_cache;
@@ -102,15 +122,25 @@ my(@names)=@_;
        return $r[0];
 }
 
+sub _system_error
+{
+my($code,$cmd)=@_;
+
+       confess $cmd.": $code=".join(",",
+                       (!WIFEXITED($code)   ? () : ("EXITSTATUS(".WEXITSTATUS($code).")")),
+                       (!WIFSIGNALED($code) ? () : ("TERMSIG("   .WTERMSIG($code)   .")")),
+                       (!WIFSTOPPED($code)  ? () : ("STOPSIG("   .WSTOPSIG($code)   .")")),
+                       );
+}
+
 sub _system
 {
 my(@args)=@_;
 
-       system(@args) and confess join(" ",@args).": $?=".join(",",
-                       (!WIFEXITED($?)   ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
-                       (!WIFSIGNALED($?) ? () : ("TERMSIG("   .WTERMSIG($?)   .")")),
-                       (!WIFSTOPPED($?)  ? () : ("STOPSIG("   .WSTOPSIG($?)   .")")),
-                       );
+       my $rc_sub=pop @args if ref $args[$#args];
+       $rc_sub||=sub { $_[0]==0; };
+       my $rc=system(@args);
+       _system_error $?,join(" ",@args) if !WIFEXITED($?) || !&{$rc_sub}(WEXITSTATUS($?));
 }
 
 # Assumed wildcard pattern expansion to exactly one item if !$nocheck
@@ -119,8 +149,11 @@ sub _copy
 my(@files)=@_;
 
        my $nocheck=shift @files if $files[0] eq "nocheck";
+       my $flag=shift @files if ref $files[0];
        my $dest=pop @files;
-       @files==copy @files,$dest or $nocheck or confess "$!";
+       # expand pattern to properly match &copy resulting filenames count
+       @files=map({ glob $_; } @files);
+       @files==copy((!$flag ? () : $flag),@files,$dest) or $nocheck or confess "$!";
 }
 
 # Assumed wildcard pattern expansion to exactly one item if !$nocheck
@@ -130,6 +163,8 @@ my(@files)=@_;
 
        my $nocheck=shift @files if $files[0] eq "nocheck";
        my $flag=shift @files if ref $files[0];
+       # expand pattern to properly match &remove resulting filenames count
+       @files=map({ glob $_; } @files);
        @files==remove((!$flag ? () : $flag),@files) or $nocheck or confess "$!";
 }
 
@@ -143,6 +178,31 @@ my(@files)=@_;
        _remove @files;
 }
 
+sub _mkdir
+{
+my(@dirs)=@_;
+
+       for (@dirs) {
+               mkdir $_ or confess "$!";
+               }
+}
+
+sub _prepdist
+{
+my($class,$name)=@_;
+
+       my($specsrc)=map((-e $_ ? $_ : "$name.spec.in"),"$name.spec.m4.in");
+       my $spec=_readfile $specsrc;
+       $spec=~s/\\\n/ /gs;
+       my $configure_args=($spec=~/^[%]configure\b[ \t]*(.*)$/m)[0];
+       $configure_args=~s/--disable-gtk-doc\b/--enable-gtk-doc/g;      # optional; gtk-doc reqd for 'make dist'
+       $class->run(%Options,
+                       "ARGV"=>[qw(--copy)],
+                       "configure_args"=>[split /\s+/,$configure_args],
+                       );
+       _remove "nocheck",($Options{"ChangeLog"} || "ChangeLog");       # force its rebuild by Makefile/rcs2log
+}
+
 # $args{
 #      "sign"=>bool,
 #      },
@@ -154,16 +214,36 @@ my($class,%args)=@_;
        _remove "nocheck",\1,
                        _rpmeval("_tmppath" )."/$name-*-root",
                        _rpmeval("_builddir")."/$name-*";
-       my($specsrc)=map((-e $_ ? $_ : "$name.spec.in"),"$name.spec.m4.in");
-       my $spec=_readfile $specsrc;
-       $spec=~s/\\\n/ /gs;
-       $class->run(%Options,
-                       "ARGV"=>["--copy"],
-                       "configure_args"=>[split /\s+/,($spec=~/^%configure\s+(.*)$/m)[0]],
-                       );
-       _remove "ChangeLog";    # force its rebuild by Makefile/rcs2log
-       _system "make dist $name.spec";
-       _copy "$name-*.tar.gz",_rpmeval("_sourcedir");
+       $class->_prepdist($name);
+       _system "make $name.spec";
+       my $spec=_readfile "$name.spec";
+       my $patch=($spec=~/^Patch\d*\s*:\s*(.*)$/m)[0];
+       _system "make dist";
+       if (!$patch) {
+               _copy "$name-*.tar.gz",_rpmeval("_sourcedir");
+               }
+       else {
+               my @origs;
+               for my $glob ("orig-$name-*.tar.{gz,Z,bz2}") {
+                       @origs=glob $glob;
+                       confess "Invalid glob $glob: ".join(",",@origs) if 1!=@origs;
+                       }
+               my($base,$ext)=($origs[0]=~/^orig-(.*)[.]tar[.](gz|Z|bz2)$/);
+               _copy $origs[0],_rpmeval("_sourcedir")."/".($origs[0]=~/^orig-(.*)$/)[0];
+               _system "tar x".($ext eq "bz2" ? "j" : "z")."f ".$origs[0];
+               _mkdir $base."-orig";
+               # FIXME: Copy also dot-prefixed files!
+               _move \1,$base."/*",$base."-orig/";
+               _system "tar xzf $name-*.tar.gz";
+               # Use single-argument system() as we need shell redirection.
+               # FIXME: Use directory-independent _cleanfiles(), not root-directory '.cvsignore'.
+               #        "-X -" does not work, it needs to be stat(2)able file.
+               _system "diff -ruP -X .cvsignore -I '".'[$]\(Id\|RCSfile\)\>.*[$]'."'"
+                               ." $base-orig/ $base/"
+                               ." >"._rpmeval("_sourcedir")."/".$patch,
+                               sub { $_[0]==0 || $_[0]==1; };  # diff(1) returns non-zero return code on any diffs.
+               _remove \1,$base,$base."-orig";
+               }
        _system(join(" ","rpmbuild",
                        "-ba",
                        "--rmsource",   # _remove _rpmeval("_sourcedir")."/$name-*.tar.gz";
@@ -174,7 +254,41 @@ my($class,%args)=@_;
        _system "make dist-tarZ" if $Options{"dist-tarZ"};
        _move _rpmeval("_srcrpmdir")."/$name-*.src.rpm",".";
        _move _rpmeval("_rpmdir")."/"._rpmeval("_target_cpu")."/$name-*."._rpmeval("_target_cpu").".rpm",".";
-  _system "ls -l $name-*";
+       _system "ls -l $name-*";
+       exit 0; # should never return
+}
+
+# $args{
+#      "sign"=>bool,
+#      },
+sub _debbuild
+{
+my($class,%args)=@_;
+
+       my $name=$Options{"name"};
+       $class->_prepdist($name);
+       _system "make distdir";
+       # Copy 'orig' archive after &_prepdist which would delete it.
+       my @origs;
+       for my $glob ("orig-$name-*.tar.{gz,Z,bz2}") {
+               @origs=glob $glob;
+               if (@origs) {
+                       confess "Invalid glob $glob: ".join(",",@origs) if 1!=@origs;
+                       (my $deborig=$origs[0])=~s/^orig-([^-]+)-(.*)([.]tar[.][^.]+)$/$1_$2.orig$3/;
+                       _copy $origs[0],$deborig;
+                       }
+               }
+       my @subdirs;
+       for my $glob ("$name-*") {
+               @subdirs=glob $glob;
+               confess "Invalid glob $glob: ".join(",",@subdirs) if 1!=@subdirs;
+               }
+       _system(join(" ","cd ".$subdirs[0].";dpkg-buildpackage",
+                       "-rfakeroot",
+                       ($args{"sign"} ? () : ("-us","-uc")),
+                       ));
+       _remove \1,$subdirs[0];
+       _system "ls -l ${name}*_[0-9]*";
        exit 0; # should never return
 }
 
@@ -219,30 +333,37 @@ sub _cleanfiles
                                                ./stamp-h ./stamp-h.in ./stamp-h1
                                                ./install-sh
                                                ./aclocal.m4
-                                               ./autom4te-*.cache
+                                               ./autom4te.cache ./autom4te-*.cache
                                                ./m4
                                                ./missing
                                                ./mkinstalldirs
                                                ./libtool ./ltconfig ./ltmain.sh
-                                               ./ChangeLog
                                                ./ABOUT-NLS
-                                               ./<name>-[0-9]* ./<name>-devel-[0-9]*
+                                               ./<name>-[0-9]* ./<name>-*-[0-9]*
                                                ./<name>.spec ./<name>.m4 ./<name>.spec.m4
+                                               ./debian/tmp ./debian/<name>
+                                               ./<name>*_[0-9]*
                                                ./macros/macros.dep
                                                ./po/Makefile.in.in ./po/POTFILES* ./po/cat-id-tbl.c ./po/cat-id-tbl.tmp
                                                ./po/*.gmo ./po/*.mo ./po/stamp-cat-id ./po/<name>.pot ./po/ChangeLog
                                                ./po/Makevars ./po/Makevars.template ./po/Rules-quot ./po/*.sed ./po/*.sin ./po/*.header
                                                ),
+                               map(("./$_"),($Options{"ChangeLog"} || "ChangeLog")),
                                map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
                                                *.stamp
                                                sgml*
                                                tmpl*
                                                html*
+                                               xml
                                                *.txt
                                                *.txt.bak
+                                               *.new
+                                               *.sgml
                                                *.args
                                                *.hierarchy
                                                *.signals
+                                               *.interfaces
+                                               *.prerequisites
                                                )); }),$Options{"gtk-doc-dir"}),
                                map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
                                                *.html
@@ -333,8 +454,10 @@ sub _expandclass
 my($patt)=@_;
 
        return $patt if $patt!~/\Q[\E(.*?)\Q]\E/;
-       my($pre,$post)=($`,$'); # FIXME: local($`,$') doesn't work - why?
-       return map({ _expandclass("$pre$_$post"); } split("",$1));
+       my($pre,$range,$post)=($`,$1,$');       # FIXME: local($`,$1,$') doesn't work - why?
+       1 while $range=~s#(.)-(.)# join("",map(chr,(ord($1)..ord($2))));
+                       #ge;
+       return map({ _expandclass("$pre$_$post"); } split("",$range));
 }
 
 sub run
@@ -350,6 +473,9 @@ my($class,%options)=@_;
        print _help() and confess if !GetOptions(
                          "rpm"      ,sub { $class->_rpmbuild("sign"=>1); },
                          "rpmtest"  ,sub { $class->_rpmbuild("sign"=>0); },
+                         "deb"      ,sub { $class->_debbuild("sign"=>1); },
+                         "debtest"  ,sub { $class->_debbuild("sign"=>0); },
+                         "cleanfilesfordir=s",sub { print "$_\n" for (_cleanfilesfordir $_[1]); exit 0; },
                          "dist"     ,\$Options{"ARGV_dist"},
                          "copy!"    ,\$Options{"ARGV_copy"},
                          "fullclean",\$Options{"ARGV_fullclean"},
@@ -373,14 +499,18 @@ my($class,%options)=@_;
        return if $Options{"ARGV_clean"} || $Options{"ARGV_fullclean"};
 
        $Options{"aclocal_args"}=[qw(-I macros),map((!$_ ? () : @$_),$Options{"aclocal_args"})];
-       my $configure_in=_readfile("configure.in");
-       do { $$_=1 if !defined($$_) && $configure_in=~/^AM_GNU_GETTEXT\b/m; }
+       my $configure_name;
+       do { $configure_name||=$_ if -f $_ } for ("configure.in");
+       do { $configure_name||=$_ if -f $_ } for ("configure.ac");
+       $configure_name or confess "Cannot find configure.{in,ac}";
+       my $configure_in=_readfile($configure_name);
+       do { $$_=1 if !defined($$_) && $configure_in=~/^\s*AM_GNU_GETTEXT\b/m; }
                        for (\$Options{"want-gettextize"});
-       do { $$_=1 if !defined($$_) && $configure_in=~/^AM_GLIB_GNU_GETTEXT\b/m; }
+       do { $$_=1 if !defined($$_) && $configure_in=~/^\s*AM_GLIB_GNU_GETTEXT\b/m; }
                        for (\$Options{"want-glib-gettextize"});
-       do { $$_=1 if !defined($$_) && $configure_in=~/^AM_PROG_LIBTOOL\b/m; }
+       do { $$_=1 if !defined($$_) && $configure_in=~/^\s*AM_PROG_LIBTOOL\b/m; }
                        for (\$Options{"want-libtoolize"}); 
-       do { $$_=1 if !defined($$_) && $configure_in=~/^A[CM]_CONFIG_HEADER\b/m; }
+       do { $$_=1 if !defined($$_) && $configure_in=~/^\s*A[CM]_CONFIG_HEADER\b/m; }
                        for (\$Options{"want-autoheader"});
        my @copy_arg=(!$Options{"ARGV_copy"} ? () : "--copy");
 
@@ -394,7 +524,7 @@ my($class,%options)=@_;
                                ."'") {
                        _system $_ and confess $_;
                        }
-               for ("configure.in","Makefile.am") {
+               for ($configure_name,"Makefile.am") {
                        STDERR->printflush("gettextize recovery rename \"$_~\"->\"$_\"... ");
                        rename "$_~","$_" or confess "$!";
                        STDERR->printflush("ok\n");
@@ -429,15 +559,35 @@ my($class,%options)=@_;
                        _writefile $Makefile_in_in,$file;
                        }
                }
-       _system "glib-gettextize",@copy_arg if $Options{"want-glib-gettextize"};
+       if ($Options{"want-glib-gettextize"}) {
+               _system "glib-gettextize",@copy_arg;
+               # "po/ChangeLog" is somehow missing at this point
+               File::Touch->new("atime_only"=>1)->touch("po/ChangeLog");
+               }
        _system "aclocal",map((!$_ ? () : @$_),$Options{"aclocal_args"});
        _system qw(libtoolize),@copy_arg if $Options{"want-libtoolize"};
        _system qw(autoheader) if $Options{"want-autoheader"};
        # "ChangeLog" is reqd by automake(1)
        # Don't remove it afterwards as it may still be needed during automatic automake Makefile rebuilds
-       File::Touch->new("atime_only"=>1)->touch("ChangeLog");
+       File::Touch->new("atime_only"=>1)->touch("ChangeLog") if !$Options{"ChangeLog"};
        _system qw(automake --add-missing),@copy_arg;
        _system qw(autoconf);
+       _writefile "| patch configure",<<'CONFIGURE_SUBST_X_EOF';
+--- configure-orig     Wed Aug 20 12:10:37 2003
++++ configure  Wed Aug 20 13:22:51 2003
+@@ -21590,6 +21590,11 @@
+   rm -f $tmp/stdin
+   if test x"$ac_file" != x-; then
+     mv $tmp/out $ac_file
++    for f in $ac_file_inputs; do
++      if test -x $f; then
++        chmod +x $ac_file
++      fi
++    done
+   else
+     cat $tmp/out
+     rm -f $tmp/out
+CONFIGURE_SUBST_X_EOF
        # Why it is left there after RedHat autoconf-2.53-8 ?
        _remove "nocheck",\1,"autom4te-*.cache";