Dropped the support of: $Options{"GetOptions_args"}
[macros.git] / AutoGen.pm
index 64dbad3..eb2bbd2 100644 (file)
@@ -1,8 +1,8 @@
 #! /usr/bin/perl
 # 
 # $Id$
-# Module to generate all the initial makefiles etc.
-# Copyright (C) 2002 Jan Kratochvil <project-macros@jankratochvil.net>
+# autoautotools aware of CVS and rpm/deb.
+# Copyright (C) 2002-2003 Jan Kratochvil <project-macros@jankratochvil.net>
 # 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -43,6 +43,9 @@ HERE
                }
        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;
        }
 
@@ -56,9 +59,12 @@ Beware: '.basename($0).' is a tool only for maintainers!
 Supported parameters:
  --rpm       Build RPM packages locally (needs /usr/src/(redhat|packages)/ access)
  --rpmtest   Build RPM like "rpm" but w/o gpg/pgp signing
+ --deb       Build DEB packages locally
+ --debtest   Build DEB like "deb" but w/o gpg/pgp signing
+ --dist      Prepare all build files but do not run the final "./configure"
+ --copy      Behave exactly like in default mode but copy all instead of symlinks
  --clean     Standard cleanup method
  --fullclean Like clean but even the .cvsignore files are removed
- --copy      Behave exactly like in default mode but copy all instead of symlinks
  -h|--help   Print this help message.
 '.($Options{"help"} || "");
 }
@@ -88,7 +94,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;
 }
 
@@ -97,9 +103,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;
@@ -214,32 +220,39 @@ my($class,%args)=@_;
        $class->_prepdist($name);
        _system "make $name.spec";
        my $spec=_readfile "$name.spec";
-       my $patch=($spec=~/^Patch\d*:\s*(.*)$/m)[0];
+       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}") {
-                       @origs=glob "orig-$name-*.tar.{gz,Z,bz2}";
+               for my $glob ("orig-$name-*.tar.{gz,Z,bz2}") {
+                       @origs=glob $glob;
                        confess "Invalid glob $glob: ".join(",",@origs) if 1!=@origs;
                        }
-               my $base=($origs[0]=~/^orig-(.*)[.]tar[.](?:gz|Z)$/)[0];
+               my($origbase,$ext)=($origs[0]=~/^orig-(.*)[.]tar[.](gz|Z|bz2)$/);
                _copy $origs[0],_rpmeval("_sourcedir")."/".($origs[0]=~/^orig-(.*)$/)[0];
-               _system "tar xzf ".$origs[0];
-               _mkdir $base."-orig";
+               _system "tar x".($ext eq "bz2" ? "j" : "z")."f ".$origs[0];
+               _mkdir $origbase."-orig";
                # FIXME: Copy also dot-prefixed files!
-               _move \1,$base."/*",$base."-orig/";
-               _system "tar xzf $name-*.tar.gz";
+               _move \1,$origbase."/*",$origbase."-orig/";
+               rmdir $origbase or confess "rmdir $origbase: $!";
+               my @ours;
+               for my $glob ("$name-*.tar.gz") {
+                       @ours=glob $glob;
+                       confess "Invalid glob $glob: ".join(",",@ours) if 1!=@ours;
+                       }
+               my($ourbase)=($ours[0]=~/^(.*)[.]tar[.]gz$/);
+               _system "tar xzf ".$ours[0];
                # 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/"
+                               ." $origbase-orig/ $ourbase/"
                                ." >"._rpmeval("_sourcedir")."/".$patch,
                                sub { $_[0]==0 || $_[0]==1; };  # diff(1) returns non-zero return code on any diffs.
-               _remove \1,$base,$base."-orig";
+               _remove \1,$ourbase,$origbase."-orig";
                }
        _system(join(" ","rpmbuild",
                        "-ba",
@@ -265,12 +278,32 @@ my($class,%args)=@_;
        my $name=$Options{"name"};
        $class->_prepdist($name);
        _system "make distdir";
-       _system(join(" ","cd $name-*;dpkg-buildpackage",
+       # Copy 'orig' archive after &_prepdist which would delete it.
+       my @origs;
+       my $base;
+       for my $glob ("orig-$name-*.tar.{gz,Z,bz2}") {
+               @origs=glob $glob;
+               if (@origs) {
+                       confess "Invalid glob $glob: ".join(",",@origs) if 1!=@origs;
+                       $origs[0]=~/^orig-([^-]+)-(.*)([.]tar[.][^.]+)$/;
+                       my $deborig="$1_$2.orig$3";
+                       $base="$1-$2";
+                       _copy $origs[0],$deborig;
+                       }
+               }
+       my @subdirs;
+       for my $glob ("$name-*") {
+               @subdirs=glob $glob;
+               confess "Invalid glob $glob: ".join(",",@subdirs) if 1!=@subdirs;
+               }
+       $base||=$subdirs[0];
+       rename $subdirs[0],$base or confess "$!";
+       _system(join(" ","cd ".$base.";dpkg-buildpackage",
                        "-rfakeroot",
                        ($args{"sign"} ? () : ("-us","-uc")),
                        ));
-       _remove \1,"$name-*";
-       _system "ls -l ${name}_*";
+       _remove \1,$base;
+       _system "ls -l ${name}*_[0-9]*";
        exit 0; # should never return
 }
 
@@ -302,6 +335,7 @@ sub _cleanfiles
                                                .deps .libs
                                                *.[oa] *.l[oa] *.l[oa]T
                                                .cvsignore
+                                               pod2htm[di].tmp
 
                                                ./errs*
                                                ./intl
@@ -321,11 +355,12 @@ sub _cleanfiles
                                                ./mkinstalldirs
                                                ./libtool ./ltconfig ./ltmain.sh
                                                ./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]*
+                                               ./<name>*_[0-9]*
                                                ./macros/macros.dep
+                                               ./macros/glade-w.sh
                                                ./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
@@ -366,7 +401,7 @@ sub _cleanfiles
 
 sub _cleanfilesfordir
 {
-my($dir)=@_;
+my($dir,@cleanfiles)=@_;
 
        return map({
                           if (m#^\Q$dir\E/([^/]+)$#) { # this-dir: "./this-dir/file-name.c"
@@ -384,7 +419,7 @@ my($dir)=@_;
                        else {
                                        ();
                                        }
-                       } _cleanfiles());
+                       } @cleanfiles);
 }
 
 sub _cvsdirs
@@ -457,12 +492,12 @@ my($class,%options)=@_;
                          "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],_cleanfiles())); exit 0; },
                          "dist"     ,\$Options{"ARGV_dist"},
                          "copy!"    ,\$Options{"ARGV_copy"},
-                         "fullclean",\$Options{"ARGV_fullclean"},
                          "clean"    ,\$Options{"ARGV_clean"},
+                         "fullclean",\$Options{"ARGV_fullclean"},
                        "h|help"     ,sub { print _help(); exit 0; },
-                       $Options{"GetOptions_args"},
                        ) || @ARGV;
 
        for my $subdir (map((!$_ ? () : @$_),$Options{"subdirs"})) {
@@ -471,7 +506,7 @@ my($class,%options)=@_;
                }
 
        for my $dir (_cvsdirs(".")) {
-               my @cleanfilesfordir=_cleanfilesfordir $dir;
+               my @cleanfilesfordir=_cleanfilesfordir $dir,_cleanfiles();
                _writefile $dir."/.cvsignore",map("$_\n",@cleanfilesfordir) if !$Options{"ARGV_fullclean"};
                _remove "nocheck",\1,map({ _expandclass("$dir/$_"); } grep({
                                $Options{"ARGV_fullclean"} or $_ ne ".cvsignore";
@@ -480,14 +515,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");
 
@@ -501,7 +540,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");
@@ -549,6 +588,23 @@ my($class,%options)=@_;
        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
+       _remove "nocheck","./configure.orig";   # It is usually produced by 'CONFIGURE_SUBST_X'.
        # Why it is left there after RedHat autoconf-2.53-8 ?
        _remove "nocheck",\1,"autom4te-*.cache";
 
@@ -567,9 +623,247 @@ my($class,%options)=@_;
 1;
 __END__
 
-Tested with:
-       RedHat autoconf-2.53-8
-       RedHat automake-1.6.3-1
-       RedHat gettext-0.11.4-3
-       RedHat libtool-1.4.2-12
-       RedHat perl-5.8.0-48
+=head1 NAME
+
+AutoGen - autoautotools aware of CVS and rpm/deb
+
+
+=head1 SYNOPSIS
+
+ use lib "./macros/";
+ use AutoGen;
+ AutoGen->run(
+               "name"=>"fooproject",
+               "COPYRIGHT_HOLDER"=>'Joe <joe@com>',
+               "ARGV"=>\@ARGV,
+               );
+
+
+=head1 ABSTRACT
+
+Projects usually have their B<autogen.sh> script files to run B<autoconf>(1),
+B<automake>(1) and similiar tools. This project has some additional features:
+
+=over
+
+Clean the checkout directory without B<Makefile>s for 'make clean'.
+
+Maintain B<.cvsignore> files containing the files being cleaned.
+
+Single-command package building of B<.rpm>/B<.deb> out of CVS checkout.
+
+Supports: B<gettext>, B<glib-gettext>, B<libtool>, B<autoconf>, B<automake>.
+
+=back
+
+
+=head1 OPTIONS
+
+B<AutoGen> module itself parses its command-line arguments from B<"ARGV"> field
+of the hash passed to B<&AutoGen->run>. Its initial double-dash ('B<-->')
+prefixes can be omitted.
+
+B<AutoGen> will build the sources for development purposes in the current
+directory if no other packaging options were given.
+
+=over
+
+=item B<--rpm>
+
+Build B<.rpm> packages. Either you need access to directory B</usr/src/redhat/>
+(or B</usr/src/packages/> etc.) or you should set B<%_usrsrc> (default
+B</usr/src/>) elsewhere in your B<$HOME/.rpmmacros>:
+       %_usrsrc /home/joe/src
+
+As the resulting package will be digitally signed you should also set signing
+properties in your B<$HOME/.rpmmacros>:
+       %_signature gpg
+       %_gpg_name  Joe <joe@com>
+
+=item B<--rpmtest>
+
+The same as B<--rpm> but the resulting package will not be I<GnuPG> digitally
+signed. I<GnuPG> passphrase is requested in the middle of package building
+which would be very discomfortable during project development.
+
+=item B<--deb>
+
+Build B<.deb> packages.
+
+=item B<--debtest>
+
+The same as B<--deb> but the resulting package will not be I<GnuPG> digitally
+signed. I<GnuPG> passphrase is requested in the middle of package building
+which would be very discomfortable during project development.
+
+=item B<--dist>
+
+Prepare all build files but do not run the final B<./configure>. The resulting
+directory is similiar to the one produced by B<make dist> but DO NOT
+INTERCHANGE THEM!
+
+=item B<--copy>
+
+Some distribution files are created just as symlinks to the system ones by
+default. This option forces their copy such as B<make dist> does.
+
+=item B<--clean>
+
+Clean all files not belonging to the CVS repository. You SHOULD backup your
+directory first during B<AutoGen> bootstrap. Only B<.cvsignore> files are
+left in our directory tree.
+
+=item B<--fullclean>
+
+Similiar to B<--clean> option but even B<.cvsignore> files get deleted.
+
+=item B<-h|--help>
+
+Print short help message describing these I<OPTIONS>.
+
+=back
+
+
+=head1 CONFIGURATION
+
+B<&AutoGen->run> method is passed configuration hash by your B<autogen.pl>.
+Its possible fields:
+
+=over
+
+=item B<name>
+
+Project basename - it should contain no special characters.
+
+
+=item B<ARGV>
+
+Pass here B<\@ARGV> from the user.
+
+
+=item B<clean>
+
+Array specifying files to be cleaned during B<./autogen.pl clean> and put into
+B<.cvsignore> files. You still should add these entries also to B<CLEANFILES>,
+B<DISTCLEANFILES> or B<MAINTAINERCLEANFILES> variables of B<Makefile.am>s.
+
+There are several formats of B<clean> field possible entries:
+
+=item B<clean> entry: B<./path/to/file.c>
+
+Full project-basedir relative path to the deleted file. Rule applies only to
+the one specified directory. Although B<path/to/file.c> would be also possible
+it is not recommended to have unified format with B<./only_basedir.c> entries
+to delete file just in the root directory (as B<only_basedir.c> would delete
+it in ALL subdirectories incl. the root one).
+
+=item B<clean> entry: B<basename.c>
+
+Delete B<basename_generated.c> found in any subdirectory of the project.
+It has the same effect as B<./*/basename_generated.c> entry.
+
+=item B<clean> entry: B<./subdir/*/name.c>
+
+Delete B<name.c> in B<./subdir> and all of its subdirector descendants.
+Entry B<subdir/*/name.c> would have the same effect.
+
+
+=item B<prep>
+
+B<CODE> reference run to prepare the sources being built in the current
+directory (if no packaging options given). Check for needed B<autotools>
+versions required to build B<configure> script by
+B<AutoGen->checkcommandversion>. Other version checks belong to B<configure.ac>
+instead.
+
+Referenced B<CODE> must return true value.
+
+
+=item B<ChangeLog>
+
+Alternative name of B<ChangeLog>, defaults to "B<ChangeLog>". You should define
+alternative name if patching existing package with its own B<ChangeLog>.
+
+
+=item B<gtk-doc-dir>
+
+Optional directory name where B<gtkdocize>(1) generated files reside.
+
+
+=back
+
+
+=head1 FUNCTIONS
+
+=over
+
+=item B<AutoGen->checkcommandversion>(B<$command>,B<$minimal_version>)
+
+Functions runs B<$command --version> and checks the resulting version number
+against given B<$minimal_version>. Currently only the first 2 version numbers
+are parsed.
+
+
+=back
+
+
+=head1 PATCHED PACKAGES
+
+This section applies to projects where you patch foreign existing packages.
+
+=over
+
+=item Prolog
+
+Use the following alternate B<autogen.pl> prolog if your branch does not belong
+to the B<MAIN> CVS branch:
+ BEGIN {
+       # 'macros' could got checked out empty by our '-r captive' tag.
+       if (!-f "./macros/AutoGen.pm") {
+               do { system $_ and die "$_: $!"; } for ("cvs co -A macros");
+               }
+       }
+ use lib "./macros/";
+ use AutoGen;
+
+=item Original archive
+
+Put B<fooproject-1.2.3.tar.gz> archive to the project root directory
+as B<orig-fooproject-1.2.3.tar.gz>.
+
+=item Versioning
+
+Set B<$VERSION> in B<configure.ac> file with you vendor prefix such as
+B<1.2.3joe1>. Set B<$VERSION_BASE> to B<1.2.3> and B<$VERSION_EXT> to B<joe1>.
+B<AC_SUBST>() them both.
+
+=item B<.spec> file changes
+
+Change B<Version: @VERSION@> to B<Version: @VERSION_BASE@>.
+
+Change (expected) line B<Release: 1> to B<Release: 1@VERSION_EXT@>.
+
+Change B<Source: ...@VERSION@.tar.gz> to B<Source: ...@VERSION_BASE@.tar.gz>.
+
+Add line B<Patch1: @PACKAGE@-@VERSION_BASE@-@VERSION_EXT@.patch>.
+This patch file name is not mandatory.
+
+Remember to call B<autotools> commands manually in the B<%build>
+section if their source files were modified in your vendor branch.
+
+=item B<debian>-specific changes
+
+Include both B<debian/changelog.in> and B<debian/changelog> to B<Makefile.am>.
+
+Include B<debian/changelog> to B<clean> field in B<autogen.pl>.
+
+Use B<@VERSION@> in B<debian/changelog.in>. Package will be built as
+Debian-native package (no I<.diff> file) as the patches are not just for Debian
+packages, it is a new package (read: dpkg does not support branch patches).
+
+=back
+
+=head1 AUTHOR
+
+Jan Kratochvil <B<project-macros@jankratochvil.net>>,
+I<http://www.jankratochvil.net/>