Compatibility with recent: automake-1.10-5
[macros.git] / AutoGen.pm
1 #! /usr/bin/perl
2
3 # $Id$
4 # autoautotools aware of CVS and rpm/deb.
5 # Copyright (C) 2002-2003 Jan Kratochvil <project-macros@jankratochvil.net>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; exactly version 2 of June 1991 is required
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 package AutoGen;
22 require 5.6.0;  # at least 'use warnings;' but we need some 5.6.0+ modules anyway
23 use vars qw($VERSION);
24 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
25 use strict;
26 use warnings;
27
28 BEGIN {
29         my @missing;
30         for (split "\n",<<'HERE') {
31                 use Carp qw(cluck confess);
32                 use Getopt::Long;       # &GetOptions, $Getopt::Long::*
33                 use File::Basename;     # &basename
34                 use File::Grep qw(fgrep);
35                 use File::HomeDir;      # &home
36                 use File::Remove qw(remove);
37                 use File::NCopy qw(copy);
38                 use File::chdir;        # $CWD
39                 use File::Touch;        # &touch
40                 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
41 HERE
42                 eval "$_\n; 1;" or push @missing,(/^\s*use\s+([^ ;]+)/)[0];
43                 }
44         die "You are missing some modules - install them by:\n"
45                                         ."\tperl -MCPAN -e 'install qw(".join(" ",@missing).")'\n"
46                                         ."If you see messages: Writing Makefile for <packagename> -- NOT OK\n"
47                                         ."you may did not have installed make(1) while configuring Perl CPAN.\n"
48                                         ."Install make(1) and type 'o conf init' in: perl -MCPAN -e shell\n"
49                         if @missing;
50         }
51
52 our %Options;
53
54 sub _help
55 {
56         return '
57 Beware: '.basename($0).' is a tool only for maintainers!
58
59 Supported parameters:
60  --rpm       Build RPM packages locally (needs /usr/src/(redhat|packages)/ access)
61  --rpmtest   Build RPM like "rpm" but w/o gpg/pgp signing
62  --deb       Build DEB packages locally
63  --debtest   Build DEB like "deb" but w/o gpg/pgp signing
64  --dist      Prepare all build files but do not run the final "./configure"
65  --copy      Behave exactly like in default mode but copy all instead of symlinks
66  --clean     Standard cleanup method
67  --fullclean Like clean but even the .cvsignore files are removed
68  -h|--help   Print this help message.
69 '.($Options{"help"} || "");
70 }
71
72 sub checkcommandversion
73 {
74 my($class,$command,$version)=@_;
75
76         local *F;
77         do { open F,$_ or confess "Open $_: $!"; } for ("$command --version|");
78         local $/;
79         undef $/;
80         my $command_out=<F>;
81         close F;
82         my $command_version=($command_out=~m#([\d.]+)#)[0];
83         confess "$command(1) version not found in its output" if !$command_version;
84         confess "'$command' version $version or higher required"
85                         # Do not take 3rd+ numbers as it would not be a number
86                         if ($command_version=~/^(\d+[.]\d+)/)[0]<$version;
87 }
88
89 sub _readfile
90 {
91 my($filename)=@_;
92
93         local $/=undef();
94         local *F;
95         open F,$filename or confess "Open \"$filename\": $!";
96         my $r=<F>;
97         close F or confess "Close \"$filename\": $!";   # Do not &cluck as it may be pipe result
98         return $r;
99 }
100
101 sub _writefile
102 {
103 my($filename,@content)=@_;
104
105         local *F;
106         open F,($filename=~/^[|]/ ? "" : ">").$filename or confess "rewrite \"$filename\": $!";
107         print F @content;
108         close F or confess "close \"$filename\": $!";   # Do not &cluck as it may be pipe result
109 }
110
111 my %_rpmeval_cache;
112 sub _rpmeval
113 {
114 my(@names)=@_;
115
116         my @r=map({
117                 my $name=$_;
118                 my $nameref=\$_rpmeval_cache{$name};
119                 $$nameref=_readfile('rpm --eval %'.$name.'|') if !defined $$nameref;
120                 chomp $$nameref;
121                 $$nameref;
122                 } @names);
123         return @r if wantarray();
124         confess "Scalar return for ".scalar(@r)." values" if 1!=@r;
125         return $r[0];
126 }
127
128 sub _system_error
129 {
130 my($code,$cmd)=@_;
131
132         confess $cmd.": $code=".join(",",
133                         (!WIFEXITED($code)   ? () : ("EXITSTATUS(".WEXITSTATUS($code).")")),
134                         (!WIFSIGNALED($code) ? () : ("TERMSIG("   .WTERMSIG($code)   .")")),
135                         (!WIFSTOPPED($code)  ? () : ("STOPSIG("   .WSTOPSIG($code)   .")")),
136                         );
137 }
138
139 sub _system
140 {
141 my(@args)=@_;
142
143         my $rc_sub=pop @args if ref $args[$#args];
144         $rc_sub||=sub { $_[0]==0; };
145         my $rc=system(@args);
146         _system_error $?,join(" ",@args) if !WIFEXITED($?) || !&{$rc_sub}(WEXITSTATUS($?));
147 }
148
149 # Assumed wildcard pattern expansion to exactly one item if !$nocheck
150 sub _copy
151 {
152 my(@files)=@_;
153
154         my $nocheck=shift @files if $files[0] eq "nocheck";
155         my $flag=shift @files if ref $files[0];
156         my $dest=pop @files;
157         # expand pattern to properly match &copy resulting filenames count
158         @files=map({ glob $_; } @files);
159         @files==copy((!$flag ? () : $flag),@files,$dest) or $nocheck or confess "$!";
160 }
161
162 # Assumed wildcard pattern expansion to exactly one item if !$nocheck
163 sub _remove
164 {
165 my(@files)=@_;
166
167         my $nocheck=shift @files if $files[0] eq "nocheck";
168         my $flag=shift @files if ref $files[0];
169         # expand pattern to properly match &remove resulting filenames count
170         @files=map({ glob $_; } @files);
171         @files==remove((!$flag ? () : $flag),@files) or $nocheck or confess "$!";
172 }
173
174 # FIXME: File::NCopy exists but File::NMove doesn't
175 sub _move
176 {
177 my(@files)=@_;
178
179         my $dest=pop @files;
180         _copy @files,$dest;
181         _remove @files;
182 }
183
184 sub _mkdir
185 {
186 my(@dirs)=@_;
187
188         for (@dirs) {
189                 mkdir $_ or confess "$!";
190                 }
191 }
192
193 sub _prepdist
194 {
195 my($class,$name)=@_;
196
197         my($specsrc)=map((-e $_ ? $_ : "$name.spec.in"),"$name.spec.m4.in");
198         my $spec=_readfile $specsrc;
199         $spec=~s/\\\n/ /gs;
200         my $configure_args=($spec=~/^[%]configure\b[ \t]*(.*)$/m)[0];
201         $configure_args=~s/--disable-gtk-doc\b/--enable-gtk-doc/g;      # optional; gtk-doc reqd for 'make dist'
202         $class->run(%Options,
203                         "ARGV"=>[qw(--copy)],
204                         "configure_args"=>[split /\s+/,$configure_args],
205                         );
206         _remove "nocheck",($Options{"ChangeLog"} || "ChangeLog");       # force its rebuild by Makefile/rcs2log
207 }
208
209 # $args{
210 #       "sign"=>bool,
211 #       },
212 sub _rpmbuild
213 {
214 my($class,%args)=@_;
215
216         my $name=$Options{"name"};
217         _remove "nocheck",\1,
218                         _rpmeval("_tmppath" )."/$name-*-root",
219                         _rpmeval("_builddir")."/$name-*";
220         $class->_prepdist($name);
221         _system "make $name.spec";
222         my $spec=_readfile "$name.spec";
223         my $patch=($spec=~/^Patch\d*\s*:\s*(.*)$/m)[0];
224         _system "make dist";
225         if (!$patch) {
226                 _copy "$name-*.tar.gz",_rpmeval("_sourcedir");
227                 }
228         else {
229                 my @origs;
230                 for my $glob ("orig-$name-*.tar.{gz,Z,bz2}") {
231                         @origs=glob $glob;
232                         confess "Invalid glob $glob: ".join(",",@origs) if 1!=@origs;
233                         }
234                 my($origbase,$ext)=($origs[0]=~/^orig-(.*)[.]tar[.](gz|Z|bz2)$/);
235                 _copy $origs[0],_rpmeval("_sourcedir")."/".($origs[0]=~/^orig-(.*)$/)[0];
236                 _system "tar x".($ext eq "bz2" ? "j" : "z")."f ".$origs[0];
237                 _mkdir $origbase."-orig";
238                 # FIXME: Copy also dot-prefixed files!
239                 _move \1,$origbase."/*",$origbase."-orig/";
240                 rmdir $origbase or confess "rmdir $origbase: $!";
241                 my @ours;
242                 for my $glob ("$name-*.tar.gz") {
243                         @ours=glob $glob;
244                         confess "Invalid glob $glob: ".join(",",@ours) if 1!=@ours;
245                         }
246                 my($ourbase)=($ours[0]=~/^(.*)[.]tar[.]gz$/);
247                 _system "tar xzf ".$ours[0];
248                 # Use single-argument system() as we need shell redirection.
249                 # FIXME: Use directory-independent _cleanfiles(), not root-directory '.cvsignore'.
250                 #        "-X -" does not work, it needs to be stat(2)able file.
251                 _system "diff -ruP -X .cvsignore -I '".'[$]\(Id\|RCSfile\)\>.*[$]'."'"
252                                 ." $origbase-orig/ $ourbase/"
253                                 ." >"._rpmeval("_sourcedir")."/".$patch,
254                                 sub { $_[0]==0 || $_[0]==1; };  # diff(1) returns non-zero return code on any diffs.
255                 _remove \1,$ourbase,$origbase."-orig";
256                 }
257         _system(join(" ","rpmbuild",
258                         "-ba",
259                         "--rmsource",   # _remove _rpmeval("_sourcedir")."/$name-*.tar.gz";
260                         "--clean",      # _remove _rpmeval("_builddir")."/$name-VERSION";
261                         (!$args{"sign"} ? () : "--sign"),
262                         "$name.spec",
263                         ));
264         _system "make dist-tarZ" if $Options{"dist-tarZ"};
265         _move _rpmeval("_srcrpmdir")."/$name-*.src.rpm",".";
266         _move _rpmeval("_rpmdir")."/"._rpmeval("_target_cpu")."/$name-*."._rpmeval("_target_cpu").".rpm",".";
267         _system "ls -l $name-*";
268         exit 0; # should never return
269 }
270
271 # $args{
272 #       "sign"=>bool,
273 #       },
274 sub _debbuild
275 {
276 my($class,%args)=@_;
277
278         my $name=$Options{"name"};
279         $class->_prepdist($name);
280         _system "make distdir";
281         # Copy 'orig' archive after &_prepdist which would delete it.
282         my @origs;
283         my $base;
284         for my $glob ("orig-$name-*.tar.{gz,Z,bz2}") {
285                 @origs=glob $glob;
286                 if (@origs) {
287                         confess "Invalid glob $glob: ".join(",",@origs) if 1!=@origs;
288                         $origs[0]=~/^orig-([^-]+)-(.*)([.]tar[.][^.]+)$/;
289                         my $deborig="$1_$2.orig$3";
290                         $base="$1-$2";
291                         _copy $origs[0],$deborig;
292                         }
293                 }
294         my @subdirs;
295         for my $glob ("$name-*") {
296                 @subdirs=glob $glob;
297                 confess "Invalid glob $glob: ".join(",",@subdirs) if 1!=@subdirs;
298                 }
299         $base||=$subdirs[0];
300         rename $subdirs[0],$base or confess "$!";
301         _system(join(" ","cd ".$base.";dpkg-buildpackage",
302                         "-rfakeroot",
303                         ($args{"sign"} ? () : ("-us","-uc")),
304                         ));
305         _remove \1,$base;
306         _system "ls -l ${name}*_[0-9]*";
307         exit 0; # should never return
308 }
309
310 # WARNING: doesn't respect %Options change!
311 my @_cleanfiles_cache;
312 sub _cleanfiles
313 {
314         # maintainer-clean hack is not safe, please list all files for 'rm'.
315         # When the filename doesn't contain '/', it is applied to ALL directories.
316         # Please note that files exactly in root dir MUST have ./ in the front
317         #   (to not to be considered as ALL-directories files).
318
319         if (!@_cleanfiles_cache) {
320                 @_cleanfiles_cache=map({
321                                                 local $_=$_;    # Prevent: Modification of a read-only value attempted
322                                                 s/\Q<name>\E/$Options{"name"}/ego;
323                                                 s#/+#/#g;
324                                                 # "*xyzzy" basename -> "*xyzzy",".*xyzzy" for proper cleaning
325                                                 (!m#^((?:.*/)?)([*][^/]*)$# ? ($_) : ("$1$2","$1.$2"));
326                                                 }
327                                 (
328                                 ".#*",  # Possible attempt to put comments in qw() list
329                                 qw(
330                                                 *~
331                                                 *.orig *.rej
332                                                 core
333                                                 Makefile Makefile.in
334                                                 TAGS tags ID
335                                                 .deps .libs
336                                                 *.[oa] *.l[oa] *.l[oa]T
337                                                 .cvsignore
338                                                 pod2htm[di].tmp
339
340                                                 ./errs*
341                                                 ./intl
342                                                 ./configure ./configure.scan
343                                                 ./config.guess ./config.status ./config.sub ./config.log ./config.cache
344                                                 ./config.h ./config.h.in
345                                                 ./confdefs.h ./conftest* ./autoh[0-9]* ./confcache
346                                                 ./config.rpath
347                                                 ./depcomp
348                                                 ./compile
349                                                 ./stamp-h ./stamp-h.in ./stamp-h1
350                                                 ./install-sh
351                                                 ./aclocal.m4
352                                                 ./autom4te.cache ./autom4te-*.cache
353                                                 ./m4
354                                                 ./missing
355                                                 ./mkinstalldirs
356                                                 ./libtool ./ltconfig ./ltmain.sh
357                                                 ./ABOUT-NLS
358                                                 ./<name>-[0-9]* ./<name>-*-[0-9]*
359                                                 ./<name>.spec ./<name>.m4 ./<name>.spec.m4
360                                                 ./debian/tmp ./debian/<name>
361                                                 ./<name>*_[0-9]*
362                                                 ./macros/macros.dep
363                                                 ./macros/glade-w.sh
364                                                 ./po/Makefile.in.in ./po/POTFILES* ./po/cat-id-tbl.c ./po/cat-id-tbl.tmp
365                                                 ./po/*.gmo ./po/*.mo ./po/stamp-cat-id ./po/<name>.pot ./po/ChangeLog
366                                                 ./po/Makevars ./po/Makevars.template ./po/Rules-quot ./po/*.sed ./po/*.sin ./po/*.header
367                                                 ),
368                                 map(("./$_"),($Options{"ChangeLog"} || "ChangeLog")),
369                                 map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
370                                                 *.stamp
371                                                 sgml*
372                                                 tmpl*
373                                                 html*
374                                                 xml
375                                                 *.txt
376                                                 *.txt.bak
377                                                 *.new
378                                                 *.sgml
379                                                 *.args
380                                                 *.hierarchy
381                                                 *.signals
382                                                 *.interfaces
383                                                 *.prerequisites
384                                                 )); }),$Options{"gtk-doc-dir"}),
385                                 map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
386                                                 *.html
387                                                 *.info*
388                                                 *.txt
389                                                 *.tex
390                                                 *.sgml
391                                                 )); }),$Options{"docbook-lite-dir"}),
392                                 map((!$_ ? () : @$_),$Options{"clean"}),
393                                 ));
394                 # sanity check
395                 for (@_cleanfiles_cache) {
396                                 confess "dir-specific 'clean'-pattern must start with './': $_" if m#^(?!\Q./\E).*/#;
397                                 };
398                 }
399         return @_cleanfiles_cache;
400 }
401
402 sub _cleanfilesfordir
403 {
404 my($dir,@cleanfiles)=@_;
405
406         return map({
407                            if (m#^\Q$dir\E/([^/]+)$#) { # this-dir: "./this-dir/file-name.c"
408                                         ($1);
409                                         }
410                         elsif (m#^[^/]+$#) {    # all-dirs: "file-name.c"; the same as "./*/file-name.c"
411                                         ($&);
412                                         }
413                         elsif (do {     # all-subdirs: "./parent-of-this-dir/*/file-name.c"
414                                                         m#/[*]/([^/]+)$#;
415                                                         ($_=$1) && $dir=~m#^\Q$`\E(?:/|$)#;
416                                                         }) {
417                                         ($_);
418                                         }
419                         else {
420                                         ();
421                                         }
422                         } @cleanfiles);
423 }
424
425 sub _cvsdirs
426 {
427 my(@startdirs)=@_;
428
429         my @r=();
430         my @todo=(@startdirs);
431         while (defined(my $dir=shift @todo)) {
432                 local *ENTRIES;
433                 my $entries_filename="$dir/CVS/Entries";
434                 open ENTRIES,$entries_filename or (cluck "open \"$entries_filename\": $!" and next);
435                 push @r,$dir;
436                 local $/="\n";
437                 my %local=();
438                 local $_;
439                 while (<ENTRIES>) {
440                         chomp;
441                         next if !m#^D/([^/]+)/#;
442                         $local{$1}=1;
443                         }
444                 close ENTRIES or cluck "close \"$entries_filename\": $!";
445                 if (-e (my $entries_log_filename=$dir."/CVS/Entries.Log")) {
446                         local *ENTRIES_LOG;
447                         if (open ENTRIES_LOG,$entries_log_filename or cluck "open \"$entries_log_filename\": $!") {
448                                 local $_;
449                                 while (<ENTRIES_LOG>) {
450                                         chomp;
451                                                  if (m#^A D/([^/]*)/#) {
452                                                 $local{$1}=1;
453                                                 }
454                                         elsif (m#^R D/([^/]*)/#) {
455                                                 delete $local{$1};
456                                                 }
457                                         else {
458                                                 cluck "$entries_log_filename: Unrecognized line $.: $_";
459                                                 }
460                                         }
461                                 close ENTRIES_LOG or cluck "close \"$entries_log_filename\": $!";
462                                 }
463                         }
464                 push @todo,map(("$dir/$_"),keys(%local));
465                 }
466         return @r;
467 }
468
469 sub _expandclass
470 {
471 my($patt)=@_;
472
473         return $patt if $patt!~/\Q[\E(.*?)\Q]\E/;
474         my($pre,$range,$post)=($`,$1,$');       # FIXME: local($`,$1,$') doesn't work - why?
475         1 while $range=~s#(.)-(.)# join("",map(chr,(ord($1)..ord($2))));
476                         #ge;
477         return map({ _expandclass("$pre$_$post"); } split("",$range));
478 }
479
480 sub run
481 {
482 my($class,%options)=@_;
483
484         local %Options=%options;
485         do { require $_ if -e; } for (home()."/.".$Options{"name"}.".autogen.pl");
486         do { $$_=1 if !defined($$_) && fgrep { /^\s*AUTOMAKE_OPTIONS\s*=[^#]*\bdist-tarZ\b/m; } "Makefile.am"; }
487                         for (\$Options{"dist-tarZ"});
488         Getopt::Long::Configure('noignorecase','prefix_pattern=(--|-|\+|)');
489         local @ARGV=@{$Options{"ARGV"}};
490         print _help() and confess if !GetOptions(
491                           "rpm"      ,sub { $class->_rpmbuild("sign"=>1); },
492                           "rpmtest"  ,sub { $class->_rpmbuild("sign"=>0); },
493                           "deb"      ,sub { $class->_debbuild("sign"=>1); },
494                           "debtest"  ,sub { $class->_debbuild("sign"=>0); },
495                           "cleanfilesfordir=s",sub { print "$_\n" for (_cleanfilesfordir($_[1],_cleanfiles())); exit 0; },
496                           "dist"     ,\$Options{"ARGV_dist"},
497                           "copy!"    ,\$Options{"ARGV_copy"},
498                           "clean"    ,\$Options{"ARGV_clean"},
499                           "fullclean",\$Options{"ARGV_fullclean"},
500                         "h|help"     ,sub { print _help(); exit 0; },
501                         ) || @ARGV;
502
503         for my $subdir (map((!$_ ? () : @$_),$Options{"subdirs"})) {
504                 local $CWD=$subdir;
505                 _system "./autogen.pl",@{$Options{"ARGV"}},"--dist";    # use "--dist" just as fallback!
506                 }
507
508         for my $dir (_cvsdirs(".")) {
509                 my @cleanfilesfordir=_cleanfilesfordir $dir,_cleanfiles();
510                 _writefile $dir."/.cvsignore",map("$_\n",@cleanfilesfordir) if !$Options{"ARGV_fullclean"};
511                 _remove "nocheck",\1,map({ _expandclass("$dir/$_"); } grep({
512                                 $Options{"ARGV_fullclean"} or $_ ne ".cvsignore";
513                                 } @cleanfilesfordir));
514                 }
515         return if $Options{"ARGV_clean"} || $Options{"ARGV_fullclean"};
516
517         $Options{"aclocal_args"}=[qw(-I macros),map((!$_ ? () : @$_),$Options{"aclocal_args"})];
518         my $configure_name;
519         do { $configure_name||=$_ if -f $_ } for ("configure.in");
520         do { $configure_name||=$_ if -f $_ } for ("configure.ac");
521         $configure_name or confess "Cannot find configure.{in,ac}";
522         my $configure_in=_readfile($configure_name);
523         do { $$_=1 if !defined($$_) && $configure_in=~/^\s*AM_GNU_GETTEXT\b/m; }
524                         for (\$Options{"want-gettextize"});
525         do { $$_=1 if !defined($$_) && $configure_in=~/^\s*AM_GLIB_GNU_GETTEXT\b/m; }
526                         for (\$Options{"want-glib-gettextize"});
527         do { $$_=1 if !defined($$_) && $configure_in=~/^\s*AM_PROG_LIBTOOL\b/m; }
528                         for (\$Options{"want-libtoolize"}); 
529         do { $$_=1 if !defined($$_) && $configure_in=~/^\s*A[CM]_CONFIG_HEADER\b/m; }
530                         for (\$Options{"want-autoheader"});
531         my @copy_arg=(!$Options{"ARGV_copy"} ? () : "--copy");
532
533         do { &$_ if $_; } for ($Options{"prep"});
534         touch "po/POTFILES.in" if -d "po";
535         if ($Options{"want-gettextize"}) {
536                 # don't use multi-arg system() here as it would reject "</dev/null" redirection etc.
537                 for ("expect -c '"
538                                 .'spawn gettextize --intl --no-changelog '.join(" ",@copy_arg).';'
539                                 .'expect -timeout -1 "Press Return to acknowledge" {send "\r";exp_continue;} eof;'
540                                 ."'") {
541                         _system $_ and confess $_;
542                         }
543                 for ($configure_name,"Makefile.am") {
544                         STDERR->printflush("gettextize recovery rename \"$_~\"->\"$_\"... ");
545                         rename "$_~","$_" or confess "$!";
546                         STDERR->printflush("ok\n");
547                         }
548                 if (!-e "po/Makevars") {
549                         my $Makevars_template="po/Makevars.template";
550                         my $makevars=_readfile $Makevars_template;
551                         $makevars=~s/^(COPYRIGHT_HOLDER)\b.*$/"$1=".$Options{"COPYRIGHT_HOLDER"}/meg
552                                         or confess "COPYRIGHT_HOLDER not found in $Makevars_template";
553                         _writefile "po/Makevars",$makevars;
554                         }
555                 # Prevent updating of contents during touch of any source file;
556                 # change the .po contents only when some data get updated
557                 for my $Makefile_in_in ("po/Makefile.in.in") {
558                         my $file=_readfile $Makefile_in_in;
559                         $file=~s%(\$\Q(MSGMERGE_UPDATE)\E) (\$\$\Q{lang}.po \E\$\Q(DOMAIN).pot\E)$%
560                                         $1.q< --backup=simple --suffix="~" >.$2.q<;>
561                                                         .q< if test `diff -u $${lang}.po~ $${lang}.po>
562                                                                                         .q< | sed>
563                                                                                                         .q< -e '1,/^@@.*@@$$/d'>
564                                                                                                         .q< -e '/^[+-]"POT-Creation-Date:/d'>
565                                                                                                         .q< -e '/^[^+-]/d'>
566                                                                                                         .q< -e '/^[+-]#/d'>
567                                                                                         .q< | wc -l` -eq 0;then>
568                                                                         .q< touch --reference=$${lang}.po $${lang}.po~;>
569                                                                                         .q< mv -f $${lang}.po~ $${lang}.po;>
570                                                         .q< else>
571                                                                         .q< rm -f $${lang}.po~;>
572                                                         .q< fi>
573                                         %me or confess;
574                         unlink $Makefile_in_in or confess "$!";
575                         _writefile $Makefile_in_in,$file;
576                         }
577                 }
578         if ($Options{"want-glib-gettextize"}) {
579                 _system "glib-gettextize",@copy_arg;
580                 # "po/ChangeLog" is somehow missing at this point
581                 File::Touch->new("atime_only"=>1)->touch("po/ChangeLog");
582                 }
583         _system "aclocal",map((!$_ ? () : @$_),$Options{"aclocal_args"});
584         _system qw(libtoolize),@copy_arg if $Options{"want-libtoolize"};
585         _system qw(autoheader) if $Options{"want-autoheader"};
586         # "ChangeLog" is reqd by automake(1)
587         # Don't remove it afterwards as it may still be needed during automatic automake Makefile rebuilds
588         File::Touch->new("atime_only"=>1)->touch("ChangeLog") if !$Options{"ChangeLog"};
589         _system qw(automake --add-missing -Wno-portability),@copy_arg;
590         _system qw(autoconf);
591         for my $patch (
592                         <<'CONFIGURE_SUBST_X_EOF',
593 --- configure-orig      2007-03-27 01:13:44.000000000 +0200
594 +++ configure   2007-03-27 01:17:07.000000000 +0200
595 @@ -3749,7 +3749,13 @@
596    rm -f "$tmp/stdin"
597    case $ac_file in
598    -) cat "$tmp/out"; rm -f "$tmp/out";;
599 -  *) rm -f "$ac_file"; mv "$tmp/out" $ac_file;;
600 +  *) rm -f "$ac_file"; mv "$tmp/out" $ac_file
601 +     for f in $ac_file_inputs; do
602 +       if test -x $f; then
603 +         chmod +x $ac_file
604 +       fi
605 +     done
606 +     ;;
607    esac
608   ;;
609  
610 CONFIGURE_SUBST_X_EOF
611                         <<'CONFIGURE_SUBST_X_EOF',
612 --- configure-orig      Wed Aug 20 12:10:37 2003
613 +++ configure   Wed Aug 20 13:22:51 2003
614 @@ -21590,6 +21590,11 @@
615    rm -f $tmp/stdin
616    if test x"$ac_file" != x-; then
617      mv $tmp/out $ac_file
618 +    for f in $ac_file_inputs; do
619 +      if test -x $f; then
620 +        chmod +x $ac_file
621 +      fi
622 +    done
623    else
624      cat $tmp/out
625      rm -f $tmp/out
626 CONFIGURE_SUBST_X_EOF
627                         undef(),
628                         ) {
629                 confess if !$patch;
630                 last if eval {
631                         _writefile "| patch configure",$patch;
632                         1;
633                         };
634                 _remove "nocheck","./configure.rej";
635                 }
636         _remove "nocheck","./configure.orig";   # It is usually produced by 'CONFIGURE_SUBST_X'.
637         # Why it is left there after RedHat autoconf-2.53-8 ?
638         _remove "nocheck",\1,"autom4te-*.cache";
639
640         return if $Options{"ARGV_dist"};
641
642         # 'configure' defaults to CFLAGS '-g -O2' but our --enable-maintainer-mode
643         # should force '-ggdb3'
644         $ENV{"CFLAGS"}||="";
645         # shared/static switching cannot be based on maintainer-mode in configure
646         _system(qw(./configure --enable-maintainer-mode),
647                         (!$Options{"want-libtoolize"} ? () : qw(--enable-shared --disable-static)),
648                         map((!$_ ? () : @$_),$Options{"configure_args"}),
649                         );
650 }
651
652 1;
653 __END__
654
655 =head1 NAME
656
657 AutoGen - autoautotools aware of CVS and rpm/deb
658
659
660 =head1 SYNOPSIS
661
662  use lib "./macros/";
663  use AutoGen;
664  AutoGen->run(
665                 "name"=>"fooproject",
666                 "COPYRIGHT_HOLDER"=>'Joe <joe@com>',
667                 "ARGV"=>\@ARGV,
668                 );
669
670
671 =head1 ABSTRACT
672
673 Projects usually have their B<autogen.sh> script files to run B<autoconf>(1),
674 B<automake>(1) and similiar tools. This project has some additional features:
675
676 =over
677
678 Clean the checkout directory without B<Makefile>s for 'make clean'.
679
680 Maintain B<.cvsignore> files containing the files being cleaned.
681
682 Single-command package building of B<.rpm>/B<.deb> out of CVS checkout.
683
684 Supports: B<gettext>, B<glib-gettext>, B<libtool>, B<autoconf>, B<automake>.
685
686 =back
687
688
689 =head1 OPTIONS
690
691 B<AutoGen> module itself parses its command-line arguments from B<"ARGV"> field
692 of the hash passed to B<&AutoGen->run>. Its initial double-dash ('B<-->')
693 prefixes can be omitted.
694
695 B<AutoGen> will build the sources for development purposes in the current
696 directory if no other packaging options were given.
697
698 =over
699
700 =item B<--rpm>
701
702 Build B<.rpm> packages. Either you need access to directory B</usr/src/redhat/>
703 (or B</usr/src/packages/> etc.) or you should set B<%_usrsrc> (default
704 B</usr/src/>) elsewhere in your B<$HOME/.rpmmacros>:
705         %_usrsrc /home/joe/src
706
707 As the resulting package will be digitally signed you should also set signing
708 properties in your B<$HOME/.rpmmacros>:
709         %_signature gpg
710         %_gpg_name  Joe <joe@com>
711
712 =item B<--rpmtest>
713
714 The same as B<--rpm> but the resulting package will not be I<GnuPG> digitally
715 signed. I<GnuPG> passphrase is requested in the middle of package building
716 which would be very discomfortable during project development.
717
718 =item B<--deb>
719
720 Build B<.deb> packages.
721
722 =item B<--debtest>
723
724 The same as B<--deb> but the resulting package will not be I<GnuPG> digitally
725 signed. I<GnuPG> passphrase is requested in the middle of package building
726 which would be very discomfortable during project development.
727
728 =item B<--dist>
729
730 Prepare all build files but do not run the final B<./configure>. The resulting
731 directory is similiar to the one produced by B<make dist> but DO NOT
732 INTERCHANGE THEM!
733
734 =item B<--copy>
735
736 Some distribution files are created just as symlinks to the system ones by
737 default. This option forces their copy such as B<make dist> does.
738
739 =item B<--clean>
740
741 Clean all files not belonging to the CVS repository. You SHOULD backup your
742 directory first during B<AutoGen> bootstrap. Only B<.cvsignore> files are
743 left in our directory tree.
744
745 =item B<--fullclean>
746
747 Similiar to B<--clean> option but even B<.cvsignore> files get deleted.
748
749 =item B<-h|--help>
750
751 Print short help message describing these I<OPTIONS>.
752
753 =back
754
755
756 =head1 CONFIGURATION
757
758 B<&AutoGen->run> method is passed configuration hash by your B<autogen.pl>.
759 Its possible fields:
760
761 =over
762
763 =item B<name>
764
765 Project basename - it should contain no special characters.
766
767
768 =item B<ARGV>
769
770 Pass here B<\@ARGV> from the user.
771
772
773 =item B<clean>
774
775 Array specifying files to be cleaned during B<./autogen.pl clean> and put into
776 B<.cvsignore> files. You still should add these entries also to B<CLEANFILES>,
777 B<DISTCLEANFILES> or B<MAINTAINERCLEANFILES> variables of B<Makefile.am>s.
778
779 There are several formats of B<clean> field possible entries:
780
781 =item B<clean> entry: B<./path/to/file.c>
782
783 Full project-basedir relative path to the deleted file. Rule applies only to
784 the one specified directory. Although B<path/to/file.c> would be also possible
785 it is not recommended to have unified format with B<./only_basedir.c> entries
786 to delete file just in the root directory (as B<only_basedir.c> would delete
787 it in ALL subdirectories incl. the root one).
788
789 =item B<clean> entry: B<basename.c>
790
791 Delete B<basename_generated.c> found in any subdirectory of the project.
792 It has the same effect as B<./*/basename_generated.c> entry.
793
794 =item B<clean> entry: B<./subdir/*/name.c>
795
796 Delete B<name.c> in B<./subdir> and all of its subdirector descendants.
797 Entry B<subdir/*/name.c> would have the same effect.
798
799
800 =item B<prep>
801
802 B<CODE> reference run to prepare the sources being built in the current
803 directory (if no packaging options given). Check for needed B<autotools>
804 versions required to build B<configure> script by
805 B<AutoGen->checkcommandversion>. Other version checks belong to B<configure.ac>
806 instead.
807
808 Referenced B<CODE> must return true value.
809
810
811 =item B<ChangeLog>
812
813 Alternative name of B<ChangeLog>, defaults to "B<ChangeLog>". You should define
814 alternative name if patching existing package with its own B<ChangeLog>.
815
816
817 =item B<gtk-doc-dir>
818
819 Optional directory name where B<gtkdocize>(1) generated files reside.
820
821
822 =back
823
824
825 =head1 FUNCTIONS
826
827 =over
828
829 =item B<AutoGen->checkcommandversion>(B<$command>,B<$minimal_version>)
830
831 Functions runs B<$command --version> and checks the resulting version number
832 against given B<$minimal_version>. Currently only the first 2 version numbers
833 are parsed.
834
835
836 =back
837
838
839 =head1 PATCHED PACKAGES
840
841 This section applies to projects where you patch foreign existing packages.
842
843 =over
844
845 =item Prolog
846
847 Use the following alternate B<autogen.pl> prolog if your branch does not belong
848 to the B<MAIN> CVS branch:
849  BEGIN {
850         # 'macros' could got checked out empty by our '-r captive' tag.
851         if (!-f "./macros/AutoGen.pm") {
852                 do { system $_ and die "$_: $!"; } for ("cvs co -A macros");
853                 }
854         }
855  use lib "./macros/";
856  use AutoGen;
857
858 =item Original archive
859
860 Put B<fooproject-1.2.3.tar.gz> archive to the project root directory
861 as B<orig-fooproject-1.2.3.tar.gz>.
862
863 =item Versioning
864
865 Set B<$VERSION> in B<configure.ac> file with you vendor prefix such as
866 B<1.2.3joe1>. Set B<$VERSION_BASE> to B<1.2.3> and B<$VERSION_EXT> to B<joe1>.
867 B<AC_SUBST>() them both.
868
869 =item B<.spec> file changes
870
871 Change B<Version: @VERSION@> to B<Version: @VERSION_BASE@>.
872
873 Change (expected) line B<Release: 1> to B<Release: 1@VERSION_EXT@>.
874
875 Change B<Source: ...@VERSION@.tar.gz> to B<Source: ...@VERSION_BASE@.tar.gz>.
876
877 Add line B<Patch1: @PACKAGE@-@VERSION_BASE@-@VERSION_EXT@.patch>.
878 This patch file name is not mandatory.
879
880 Remember to call B<autotools> commands manually in the B<%build>
881 section if their source files were modified in your vendor branch.
882
883 =item B<debian>-specific changes
884
885 Include both B<debian/changelog.in> and B<debian/changelog> to B<Makefile.am>.
886
887 Include B<debian/changelog> to B<clean> field in B<autogen.pl>.
888
889 Use B<@VERSION@> in B<debian/changelog.in>. Package will be built as
890 Debian-native package (no I<.diff> file) as the patches are not just for Debian
891 packages, it is a new package (read: dpkg does not support branch patches).
892
893 =back
894
895 =head1 AUTHOR
896
897 Jan Kratochvil <B<project-macros@jankratochvil.net>>,
898 I<http://www.jankratochvil.net/>