Retry glade(1)/glade-2(1) on DISPLAY ":0" if the Xvnc connection failed.
[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
339                                                 ./errs*
340                                                 ./intl
341                                                 ./configure ./configure.scan
342                                                 ./config.guess ./config.status ./config.sub ./config.log ./config.cache
343                                                 ./config.h ./config.h.in
344                                                 ./confdefs.h ./conftest* ./autoh[0-9]* ./confcache
345                                                 ./config.rpath
346                                                 ./depcomp
347                                                 ./compile
348                                                 ./stamp-h ./stamp-h.in ./stamp-h1
349                                                 ./install-sh
350                                                 ./aclocal.m4
351                                                 ./autom4te.cache ./autom4te-*.cache
352                                                 ./m4
353                                                 ./missing
354                                                 ./mkinstalldirs
355                                                 ./libtool ./ltconfig ./ltmain.sh
356                                                 ./ABOUT-NLS
357                                                 ./<name>-[0-9]* ./<name>-*-[0-9]*
358                                                 ./<name>.spec ./<name>.m4 ./<name>.spec.m4
359                                                 ./debian/tmp ./debian/<name>
360                                                 ./<name>*_[0-9]*
361                                                 ./macros/macros.dep
362                                                 ./macros/glade-w.sh
363                                                 ./po/Makefile.in.in ./po/POTFILES* ./po/cat-id-tbl.c ./po/cat-id-tbl.tmp
364                                                 ./po/*.gmo ./po/*.mo ./po/stamp-cat-id ./po/<name>.pot ./po/ChangeLog
365                                                 ./po/Makevars ./po/Makevars.template ./po/Rules-quot ./po/*.sed ./po/*.sin ./po/*.header
366                                                 ),
367                                 map(("./$_"),($Options{"ChangeLog"} || "ChangeLog")),
368                                 map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
369                                                 *.stamp
370                                                 sgml*
371                                                 tmpl*
372                                                 html*
373                                                 xml
374                                                 *.txt
375                                                 *.txt.bak
376                                                 *.new
377                                                 *.sgml
378                                                 *.args
379                                                 *.hierarchy
380                                                 *.signals
381                                                 *.interfaces
382                                                 *.prerequisites
383                                                 )); }),$Options{"gtk-doc-dir"}),
384                                 map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
385                                                 *.html
386                                                 *.info*
387                                                 *.txt
388                                                 *.tex
389                                                 *.sgml
390                                                 )); }),$Options{"docbook-lite-dir"}),
391                                 map((!$_ ? () : @$_),$Options{"clean"}),
392                                 ));
393                 # sanity check
394                 for (@_cleanfiles_cache) {
395                                 confess "dir-specific 'clean'-pattern must start with './': $_" if m#^(?!\Q./\E).*/#;
396                                 };
397                 }
398         return @_cleanfiles_cache;
399 }
400
401 sub _cleanfilesfordir
402 {
403 my($dir,@cleanfiles)=@_;
404
405         return map({
406                            if (m#^\Q$dir\E/([^/]+)$#) { # this-dir: "./this-dir/file-name.c"
407                                         ($1);
408                                         }
409                         elsif (m#^[^/]+$#) {    # all-dirs: "file-name.c"; the same as "./*/file-name.c"
410                                         ($&);
411                                         }
412                         elsif (do {     # all-subdirs: "./parent-of-this-dir/*/file-name.c"
413                                                         m#/[*]/([^/]+)$#;
414                                                         ($_=$1) && $dir=~m#^\Q$`\E(?:/|$)#;
415                                                         }) {
416                                         ($_);
417                                         }
418                         else {
419                                         ();
420                                         }
421                         } @cleanfiles);
422 }
423
424 sub _cvsdirs
425 {
426 my(@startdirs)=@_;
427
428         my @r=();
429         my @todo=(@startdirs);
430         while (defined(my $dir=shift @todo)) {
431                 local *ENTRIES;
432                 my $entries_filename="$dir/CVS/Entries";
433                 open ENTRIES,$entries_filename or (cluck "open \"$entries_filename\": $!" and next);
434                 push @r,$dir;
435                 local $/="\n";
436                 my %local=();
437                 local $_;
438                 while (<ENTRIES>) {
439                         chomp;
440                         next if !m#^D/([^/]+)/#;
441                         $local{$1}=1;
442                         }
443                 close ENTRIES or cluck "close \"$entries_filename\": $!";
444                 if (-e (my $entries_log_filename=$dir."/CVS/Entries.Log")) {
445                         local *ENTRIES_LOG;
446                         if (open ENTRIES_LOG,$entries_log_filename or cluck "open \"$entries_log_filename\": $!") {
447                                 local $_;
448                                 while (<ENTRIES_LOG>) {
449                                         chomp;
450                                                  if (m#^A D/([^/]*)/#) {
451                                                 $local{$1}=1;
452                                                 }
453                                         elsif (m#^R D/([^/]*)/#) {
454                                                 delete $local{$1};
455                                                 }
456                                         else {
457                                                 cluck "$entries_log_filename: Unrecognized line $.: $_";
458                                                 }
459                                         }
460                                 close ENTRIES_LOG or cluck "close \"$entries_log_filename\": $!";
461                                 }
462                         }
463                 push @todo,map(("$dir/$_"),keys(%local));
464                 }
465         return @r;
466 }
467
468 sub _expandclass
469 {
470 my($patt)=@_;
471
472         return $patt if $patt!~/\Q[\E(.*?)\Q]\E/;
473         my($pre,$range,$post)=($`,$1,$');       # FIXME: local($`,$1,$') doesn't work - why?
474         1 while $range=~s#(.)-(.)# join("",map(chr,(ord($1)..ord($2))));
475                         #ge;
476         return map({ _expandclass("$pre$_$post"); } split("",$range));
477 }
478
479 sub run
480 {
481 my($class,%options)=@_;
482
483         local %Options=%options;
484         do { require $_ if -e; } for (home()."/.".$Options{"name"}.".autogen.pl");
485         do { $$_=1 if !defined($$_) && fgrep { /^\s*AUTOMAKE_OPTIONS\s*=[^#]*\bdist-tarZ\b/m; } "Makefile.am"; }
486                         for (\$Options{"dist-tarZ"});
487         Getopt::Long::Configure('noignorecase','prefix_pattern=(--|-|\+|)');
488         local @ARGV=@{$Options{"ARGV"}};
489         print _help() and confess if !GetOptions(
490                           "rpm"      ,sub { $class->_rpmbuild("sign"=>1); },
491                           "rpmtest"  ,sub { $class->_rpmbuild("sign"=>0); },
492                           "deb"      ,sub { $class->_debbuild("sign"=>1); },
493                           "debtest"  ,sub { $class->_debbuild("sign"=>0); },
494                           "cleanfilesfordir=s",sub { print "$_\n" for (_cleanfilesfordir($_[1],_cleanfiles())); exit 0; },
495                           "dist"     ,\$Options{"ARGV_dist"},
496                           "copy!"    ,\$Options{"ARGV_copy"},
497                           "clean"    ,\$Options{"ARGV_clean"},
498                           "fullclean",\$Options{"ARGV_fullclean"},
499                         "h|help"     ,sub { print _help(); exit 0; },
500                         $Options{"GetOptions_args"},
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),@copy_arg;
590         _system qw(autoconf);
591         _writefile "| patch configure",<<'CONFIGURE_SUBST_X_EOF';
592 --- configure-orig      Wed Aug 20 12:10:37 2003
593 +++ configure   Wed Aug 20 13:22:51 2003
594 @@ -21590,6 +21590,11 @@
595    rm -f $tmp/stdin
596    if test x"$ac_file" != x-; then
597      mv $tmp/out $ac_file
598 +    for f in $ac_file_inputs; do
599 +      if test -x $f; then
600 +        chmod +x $ac_file
601 +      fi
602 +    done
603    else
604      cat $tmp/out
605      rm -f $tmp/out
606 CONFIGURE_SUBST_X_EOF
607         _remove "nocheck","./configure.orig";   # It is usually produced by 'CONFIGURE_SUBST_X'.
608         # Why it is left there after RedHat autoconf-2.53-8 ?
609         _remove "nocheck",\1,"autom4te-*.cache";
610
611         return if $Options{"ARGV_dist"};
612
613         # 'configure' defaults to CFLAGS '-g -O2' but our --enable-maintainer-mode
614         # should force '-ggdb3'
615         $ENV{"CFLAGS"}||="";
616         # shared/static switching cannot be based on maintainer-mode in configure
617         _system(qw(./configure --enable-maintainer-mode),
618                         (!$Options{"want-libtoolize"} ? () : qw(--enable-shared --disable-static)),
619                         map((!$_ ? () : @$_),$Options{"configure_args"}),
620                         );
621 }
622
623 1;
624 __END__
625
626 =head1 NAME
627
628 AutoGen - autoautotools aware of CVS and rpm/deb
629
630
631 =head1 SYNOPSIS
632
633  use lib "./macros/";
634  use AutoGen;
635  AutoGen->run(
636                 "name"=>"fooproject",
637                 "COPYRIGHT_HOLDER"=>'Joe <joe@com>',
638                 "ARGV"=>\@ARGV,
639                 );
640
641
642 =head1 ABSTRACT
643
644 Projects usually have their B<autogen.sh> script files to run B<autoconf>(1),
645 B<automake>(1) and similiar tools. This project has some additional features:
646
647 =over
648
649 =item
650
651 Clean the checkout directory without B<Makefile>s for 'make clean'.
652
653 =item
654
655 Maintain B<.cvsignore> files containing the files being cleaned.
656
657 =item
658
659 Single-command package building of B<.rpm>/B<.deb> out of CVS checkout.
660
661 =item
662
663 Supports: B<gettext>, B<glib-gettext>, B<libtool>, B<autoconf>, B<automake>.
664
665 =back
666
667
668 =head1 OPTIONS
669
670 B<AutoGen> module itself parses its command-line arguments from B<"ARGV"> field
671 of the hash passed to B<&AutoGen->run>. Its initial double-dash ('B<-->')
672 prefixes can be omitted.
673
674 B<AutoGen> will build the sources for development purposes in the current
675 directory if no other packaging options were given.
676
677 =over
678
679 =item B<--rpm>
680
681 Build B<.rpm> packages. Either you need access to directory B</usr/src/redhat/>
682 (or B</usr/src/packages/> etc.) or you should set B<%_usrsrc> (default
683 B</usr/src/>) elsewhere in your B<$HOME/.rpmmacros>:
684         %_usrsrc /home/joe/src
685
686 As the resulting package will be digitally signed you should also set signing
687 properties in your B<$HOME/.rpmmacros>:
688         %_signature gpg
689         %_gpg_name  Joe <joe@com>
690
691 =item B<--rpmtest>
692
693 The same as B<--rpm> but the resulting package will not be I<GnuPG> digitally
694 signed. I<GnuPG> passphrase is requested in the middle of package building
695 which would be very discomfortable during project development.
696
697 =item B<--deb>
698
699 Build B<.deb> packages.
700
701 =item B<--debtest>
702
703 The same as B<--deb> but the resulting package will not be I<GnuPG> digitally
704 signed. I<GnuPG> passphrase is requested in the middle of package building
705 which would be very discomfortable during project development.
706
707 =item B<--dist>
708
709 Prepare all build files but do not run the final B<./configure>. The resulting
710 directory is similiar to the one produced by B<make dist> but DO NOT
711 INTERCHANGE THEM!
712
713 =item B<--copy>
714
715 Some distribution files are created just as symlinks to the system ones by
716 default. This option forces their copy such as B<make dist> does.
717
718 =item B<--clean>
719
720 Clean all files not belonging to the CVS repository. You SHOULD backup your
721 directory first during B<AutoGen> bootstrap. Only B<.cvsignore> files are
722 left in our directory tree.
723
724 =item B<--fullclean>
725
726 Similiar to B<--clean> option but even B<.cvsignore> files get deleted.
727
728 =item B<-h|--help>
729
730 Print short help message describing these I<OPTIONS>.
731
732 =back
733
734
735 =head1 CONFIGURATION
736
737 B<&AutoGen->run> method is passed configuration hash by your B<autogen.pl>.
738 Its possible fields:
739
740 =over
741
742 =item B<name>
743
744 Project basename - it should contain no special characters.
745
746
747 =item B<ARGV>
748
749 Pass here B<\@ARGV> from the user.
750
751
752 =item B<clean>
753
754 Array specifying files to be cleaned during B<./autogen.pl clean> and put into
755 B<.cvsignore> files. You still should add these entries also to B<CLEANFILES>,
756 B<DISTCLEANFILES> or B<MAINTAINERCLEANFILES> variables of B<Makefile.am>s.
757
758 There are several formats of B<clean> field possible entries:
759
760 =over
761
762 =item B<./path/to/file.c>
763
764 Full project-basedir relative path to the deleted file. Rule applies only to
765 the one specified directory. Although B<path/to/file.c> would be also possible
766 it is not recommended to have unified format with B<./only_basedir.c> entries
767 to delete file just in the root directory (as B<only_basedir.c> would delete
768 it in ALL subdirectories incl. the root one).
769
770 =item B<basename.c>
771
772 Delete B<basename_generated.c> found in any subdirectory of the project.
773 It has the same effect as B<./*/basename_generated.c> entry.
774
775 =item B<./subdir/*/name.c>
776
777 Delete B<name.c> in B<./subdir> and all of its subdirector descendants.
778 Entry B<subdir/*/name.c> would have the same effect.
779
780 =back
781
782
783 =item B<prep>
784
785 B<CODE> reference run to prepare the sources being built in the current
786 directory (if no packaging options given). Check for needed B<autotools>
787 versions required to build B<configure> script by
788 B<AutoGen->checkcommandversion>. Other version checks belong to B<configure.ac>
789 instead.
790
791 Referenced B<CODE> must return true value.
792
793
794 =item B<ChangeLog>
795
796 Alternative name of B<ChangeLog>, defaults to "B<ChangeLog>". You should define
797 alternative name if patching existing package with its own B<ChangeLog>.
798
799
800 =item B<gtk-doc-dir>
801
802 Optional directory name where B<gtkdocize>(1) generated files reside.
803
804
805 =back
806
807
808 =head1 FUNCTIONS
809
810 =over
811
812 =item B<AutoGen->checkcommandversion>(B<$command>,B<$minimal_version>)
813
814 Functions runs B<$command --version> and checks the resulting version number
815 against given B<$minimal_version>. Currently only the first 2 version numbers
816 are parsed.
817
818
819 =back
820
821
822 =head1 PATCHED PACKAGES
823
824 This section applies to projects where you patch foreign existing packages.
825
826 =over
827
828 =item
829
830 Use the following alternate B<autogen.pl> prolog if your branch does not belong
831 to the B<MAIN> CVS branch:
832  BEGIN {
833         # 'macros' could got checked out empty by our '-r captive' tag.
834         if (!-f "./macros/AutoGen.pm") {
835                 do { system $_ and die "$_: $!"; } for ("cvs co -A macros");
836                 }
837         }
838  use lib "./macros/";
839  use AutoGen;
840
841 =item
842
843 Put B<fooproject-1.2.3.tar.gz> archive to the project root directory
844 as B<orig-fooproject-1.2.3.tar.gz>.
845
846 =item
847
848 Set B<$VERSION> in B<configure.ac> file with you vendor prefix such as
849 B<1.2.3joe1>. Set B<$VERSION_BASE> to B<1.2.3> and B<$VERSION_EXT> to B<joe1>.
850 B<AC_SUBST>() them both.
851
852 =item B<.spec> file changes
853
854 =over
855
856 =item
857
858 Change B<Version: @VERSION@> to B<Version: @VERSION_BASE@>.
859
860 =item
861
862 Change (expected) line B<Release: 1> to B<Release: 1@VERSION_EXT@>.
863
864 =item
865
866 Change B<Source: ...@VERSION@.tar.gz> to B<Source: ...@VERSION_BASE@.tar.gz>.
867
868 =item
869
870 Add line B<Patch1: @PACKAGE@-@VERSION_BASE@-@VERSION_EXT@.patch>.
871 This patch file name is not mandatory.
872
873 =item
874
875 Remember to call B<autotools> commands manually in the B<%build>
876 section if their source files were modified in your vendor branch.
877
878 =back
879
880 =item B<debian>-specific changes
881
882 =over
883
884 =item
885
886 Include both B<debian/changelog.in> and B<debian/changelog> to B<Makefile.am>.
887
888 =item
889
890 Include B<debian/changelog> to B<clean> field in B<autogen.pl>.
891
892 =item
893
894 Use B<@VERSION@> in B<debian/changelog.in>. Package will be built as
895 Debian-native package (no I<.diff> file) as the patches are not just for Debian
896 packages, it is a new package (read: dpkg does not support branch patches).
897
898 =back
899
900 =back
901
902 =head1 AUTHOR
903
904 Jan Kratochvil <B<project-macros@jankratochvil.net>>,
905                 I<http://www.jankratochvil.net/>