+Debian .deb packaging.
[macros.git] / AutoGen.pm
index 4f2fd1f..554310a 100644 (file)
@@ -19,6 +19,7 @@
 
 
 package AutoGen;
+require 5.6.0; # at least 'use warnings;' but we need some 5.6.0+ modules anyway
 use vars qw($VERSION);
 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
 use strict;
@@ -38,7 +39,7 @@ 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"
@@ -62,6 +63,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)=@_;
@@ -119,6 +137,8 @@ my(@files)=@_;
 
        my $nocheck=shift @files if $files[0] eq "nocheck";
        my $dest=pop @files;
+       # expand pattern to properly match &copy resulting filenames count
+       @files=map({ glob $_; } @files);
        @files==copy @files,$dest or $nocheck or confess "$!";
 }
 
@@ -129,6 +149,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 "$!";
 }
 
@@ -142,6 +164,23 @@ my(@files)=@_;
        _remove @files;
 }
 
+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 "ChangeLog";    # force its rebuild by Makefile/rcs2log
+       _system "make dist $name.spec";
+}
+
 # $args{
 #      "sign"=>bool,
 #      },
@@ -153,26 +192,40 @@ 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]],
-                       );
-       _system "make dist $name.spec";
+       $class->_prepdist($name);
        _copy "$name-*.tar.gz",_rpmeval("_sourcedir");
-       _system(join(" ","rpm",
+       _system(join(" ","rpmbuild",
                        "-ba",
+                       "--rmsource",   # _remove _rpmeval("_sourcedir")."/$name-*.tar.gz";
+                       "--clean",      # _remove _rpmeval("_builddir")."/$name-VERSION";
                        (!$args{"sign"} ? () : "--sign"),
                        "$name.spec",
                        ));
        _system "make dist-tarZ" if $Options{"dist-tarZ"};
-       _remove _rpmeval{"_sourcedir"}."/$name-*.tar.gz";
-       _move _rpmeval{"_srcrpmdir"}."/$name-*.src.rpm",".";
-       _move "nocheck",_rpmeval{"_rpmdir"}."/"._rpmeval{"_arch"}."/$name-*."._rpmeval{"_arch"}.".rpm",".";
-  _system "ls -l $name-*";
-       # NOTREACHED
+       _move _rpmeval("_srcrpmdir")."/$name-*.src.rpm",".";
+       _move _rpmeval("_rpmdir")."/"._rpmeval("_target_cpu")."/$name-*."._rpmeval("_target_cpu").".rpm",".";
+       _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 "tar xzf $name-*.tar.gz";
+       _remove "$name-*.tar.gz";       # permit "cd $name-*" below
+       _system(join(" ","cd $name-*;dpkg-buildpackage",
+                       "-rfakeroot",
+                       ($args{"sign"} ? () : ("-us","-uc")),
+                       ));
+       _remove \1,"$name-*";
+       _system "ls -l ${name}_*";
+       exit 0; # should never return
 }
 
 # WARNING: doesn't respect %Options change!
@@ -212,10 +265,11 @@ sub _cleanfiles
                                                ./confdefs.h ./conftest* ./autoh[0-9]* ./confcache
                                                ./config.rpath
                                                ./depcomp
+                                               ./compile
                                                ./stamp-h ./stamp-h.in ./stamp-h1
                                                ./install-sh
                                                ./aclocal.m4
-                                               ./autom4te-*.cache
+                                               ./autom4te.cache ./autom4te-*.cache
                                                ./m4
                                                ./missing
                                                ./mkinstalldirs
@@ -224,6 +278,8 @@ sub _cleanfiles
                                                ./ABOUT-NLS
                                                ./<name>-[0-9]* ./<name>-devel-[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
@@ -234,11 +290,16 @@ sub _cleanfiles
                                                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
@@ -329,8 +390,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
@@ -344,8 +407,10 @@ my($class,%options)=@_;
        Getopt::Long::Configure('noignorecase','prefix_pattern=(--|-|\+|)');
        local @ARGV=@{$Options{"ARGV"}};
        print _help() and confess if !GetOptions(
-                         "rpm"      ,sub { $class->_rpmbuild("sign"=>1); return; },
-                         "rpmtest"  ,sub { $class->_rpmbuild("sign"=>0); return; },
+                         "rpm"      ,sub { $class->_rpmbuild("sign"=>1); },
+                         "rpmtest"  ,sub { $class->_rpmbuild("sign"=>0); },
+                         "deb"      ,sub { $class->_debbuild("sign"=>1); },
+                         "debtest"  ,sub { $class->_debbuild("sign"=>0); },
                          "dist"     ,\$Options{"ARGV_dist"},
                          "copy!"    ,\$Options{"ARGV_copy"},
                          "fullclean",\$Options{"ARGV_fullclean"},
@@ -372,6 +437,8 @@ my($class,%options)=@_;
        my $configure_in=_readfile("configure.in");
        do { $$_=1 if !defined($$_) && $configure_in=~/^AM_GNU_GETTEXT\b/m; }
                        for (\$Options{"want-gettextize"});
+       do { $$_=1 if !defined($$_) && $configure_in=~/^AM_GLIB_GNU_GETTEXT\b/m; }
+                       for (\$Options{"want-glib-gettextize"});
        do { $$_=1 if !defined($$_) && $configure_in=~/^AM_PROG_LIBTOOL\b/m; }
                        for (\$Options{"want-libtoolize"}); 
        do { $$_=1 if !defined($$_) && $configure_in=~/^A[CM]_CONFIG_HEADER\b/m; }
@@ -411,6 +478,7 @@ my($class,%options)=@_;
                                                                                                        .q< -e '1,/^@@.*@@$$/d'>
                                                                                                        .q< -e '/^[+-]"POT-Creation-Date:/d'>
                                                                                                        .q< -e '/^[^+-]/d'>
+                                                                                                       .q< -e '/^[+-]#/d'>
                                                                                        .q< | wc -l` -eq 0;then>
                                                                        .q< touch --reference=$${lang}.po $${lang}.po~;>
                                                                                        .q< mv -f $${lang}.po~ $${lang}.po;>
@@ -422,6 +490,11 @@ my($class,%options)=@_;
                        _writefile $Makefile_in_in,$file;
                        }
                }
+       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"};
@@ -435,9 +508,12 @@ my($class,%options)=@_;
 
        return if $Options{"ARGV_dist"};
 
+       # 'configure' defaults to CFLAGS '-g -O2' but our --enable-maintainer-mode
+       # should force '-ggdb3'
+       $ENV{"CFLAGS"}||="";
        # shared/static switching cannot be based on maintainer-mode in configure
        _system(qw(./configure --enable-maintainer-mode),
-                       ($Options{"want-libtoolize"} && qw(--enable-shared --disable-static)),
+                       (!$Options{"want-libtoolize"} ? () : qw(--enable-shared --disable-static)),
                        map((!$_ ? () : @$_),$Options{"configure_args"}),
                        );
 }