4bbb8fd142dd9e7360a00a174af2e1685d77ad7e
[macros.git] / AutoGen.pm
1 #! /usr/bin/perl
2
3 # $Id$
4 # Module to generate all the initial makefiles etc.
5 # Copyright (C) 2002 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                 use File::Glob qw(:glob);       # &bsd_glob, &GLOB_*
42 HERE
43                 eval "$_\n; 1;" or push @missing,(/^\s*use\s+([^ ;]+)/)[0];
44                 }
45         die "You are missing some modules - install them by:\n"
46                                         ."\tperl -MCPAN -e 'install qw(".join(" ",@missing).")'\n"
47                         if @missing;
48         }
49
50 our %Options;
51
52 sub _help
53 {
54         return '
55 Beware: '.basename($0).' is a tool only for maintainers!
56
57 Supported parameters:
58  --rpm       Build RPM packages locally (needs /usr/src/(redhat|packages)/ access)
59  --rpmtest   Build RPM like "rpm" but w/o gpg/pgp signing
60  --clean     Standard cleanup method
61  --fullclean Like clean but even the .cvsignore files are removed
62  --copy      Behave exactly like in default mode but copy all instead of symlinks
63  -h|--help   Print this help message.
64 '.($Options{"help"} || "");
65 }
66
67 sub _readfile
68 {
69 my($filename)=@_;
70
71         local $/=undef();
72         local *F;
73         open F,$filename or confess "Open \"$filename\": $!";
74         my $r=<F>;
75         close F or cluck "Close \"$filename\": $!";
76         return $r;
77 }
78
79 sub _writefile
80 {
81 my($filename,@content)=@_;
82
83         local *F;
84         open F,">".$filename or confess "rewrite \"$filename\": $!";
85         print F @content;
86         close F or cluck "close \"$filename\": $!";
87 }
88
89 my %_rpmeval_cache;
90 sub _rpmeval
91 {
92 my(@names)=@_;
93
94         my @r=map({
95                 my $name=$_;
96                 my $nameref=\$_rpmeval_cache{$name};
97                 $$nameref=_readfile('rpm --eval %'.$name.'|') if !defined $$nameref;
98                 chomp $$nameref;
99                 $$nameref;
100                 } @names);
101         return @r if wantarray();
102         confess "Scalar return for ".scalar(@r)." values" if 1!=@r;
103         return $r[0];
104 }
105
106 sub _system
107 {
108 my(@args)=@_;
109
110         system(@args) and confess join(" ",@args).": $?=".join(",",
111                         (!WIFEXITED($?)   ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
112                         (!WIFSIGNALED($?) ? () : ("TERMSIG("   .WTERMSIG($?)   .")")),
113                         (!WIFSTOPPED($?)  ? () : ("STOPSIG("   .WSTOPSIG($?)   .")")),
114                         );
115 }
116
117 # Assumed wildcard pattern expansion to exactly one item if !$nocheck
118 sub _copy
119 {
120 my(@files)=@_;
121
122         my $nocheck=shift @files if $files[0] eq "nocheck";
123         my $dest=pop @files;
124         # expand pattern to properly match &copy resulting filenames count
125         @files=map({ bsd_glob $_,&GLOB_ERR|&GLOB_NOSORT; } @files);
126         confess "$!" if $File::Glob::GLOB_ERROR;
127         @files==copy @files,$dest or $nocheck or confess "$!";
128 }
129
130 # Assumed wildcard pattern expansion to exactly one item if !$nocheck
131 sub _remove
132 {
133 my(@files)=@_;
134
135         my $nocheck=shift @files if $files[0] eq "nocheck";
136         my $flag=shift @files if ref $files[0];
137         # expand pattern to properly match &copy resulting filenames count
138         @files=map({ bsd_glob $_,&GLOB_ERR|&GLOB_NOSORT; } @files);
139         confess "$!" if $File::Glob::GLOB_ERROR;
140         @files==remove((!$flag ? () : $flag),@files) or $nocheck or confess "$!";
141 }
142
143 # FIXME: File::NCopy exists but File::NMove doesn't
144 sub _move
145 {
146 my(@files)=@_;
147
148         my $dest=pop @files;
149         _copy @files,$dest;
150         _remove @files;
151 }
152
153 # $args{
154 #       "sign"=>bool,
155 #       },
156 sub _rpmbuild
157 {
158 my($class,%args)=@_;
159
160         my $name=$Options{"name"};
161         _remove "nocheck",\1,
162                         _rpmeval("_tmppath" )."/$name-*-root",
163                         _rpmeval("_builddir")."/$name-*";
164         my($specsrc)=map((-e $_ ? $_ : "$name.spec.in"),"$name.spec.m4.in");
165         my $spec=_readfile $specsrc;
166         $spec=~s/\\\n/ /gs;
167         $class->run(%Options,
168                         "ARGV"=>["--copy"],
169                         "configure_args"=>[split /\s+/,($spec=~/^[%]configure\b[ \t]*(.*)$/m)[0]],
170                         );
171         _remove "ChangeLog";    # force its rebuild by Makefile/rcs2log
172         _system "make dist $name.spec";
173         _copy "$name-*.tar.gz",_rpmeval("_sourcedir");
174         _system(join(" ","rpmbuild",
175                         "-ba",
176                         "--rmsource",   # _remove _rpmeval("_sourcedir")."/$name-*.tar.gz";
177                         "--clean",      # _remove _rpmeval("_builddir")."/$name-VERSION";
178                         (!$args{"sign"} ? () : "--sign"),
179                         "$name.spec",
180                         ));
181         _system "make dist-tarZ" if $Options{"dist-tarZ"};
182         _move _rpmeval("_srcrpmdir")."/$name-*.src.rpm",".";
183         _move _rpmeval("_rpmdir")."/"._rpmeval("_target_cpu")."/$name-*."._rpmeval("_target_cpu").".rpm",".";
184   _system "ls -l $name-*";
185         exit 0; # should never return
186 }
187
188 # WARNING: doesn't respect %Options change!
189 my @_cleanfiles_cache;
190 sub _cleanfiles
191 {
192         # maintainer-clean hack is not safe, please list all files for 'rm'.
193         # When the filename doesn't contain '/', it is applied to ALL directories.
194         # Please note that files exactly in root dir MUST have ./ in the front
195         #   (to not to be considered as ALL-directories files).
196
197         if (!@_cleanfiles_cache) {
198                 @_cleanfiles_cache=map({
199                                                 local $_=$_;    # Prevent: Modification of a read-only value attempted
200                                                 s/\Q<name>\E/$Options{"name"}/ego;
201                                                 s#/+#/#g;
202                                                 # "*xyzzy" basename -> "*xyzzy",".*xyzzy" for proper cleaning
203                                                 (!m#^((?:.*/)?)([*][^/]*)$# ? ($_) : ("$1$2","$1.$2"));
204                                                 }
205                                 (
206                                 ".#*",  # Possible attempt to put comments in qw() list
207                                 qw(
208                                                 *~
209                                                 *.orig *.rej
210                                                 core
211                                                 Makefile Makefile.in
212                                                 TAGS tags ID
213                                                 .deps .libs
214                                                 *.[oa] *.l[oa] *.l[oa]T
215                                                 .cvsignore
216
217                                                 ./errs*
218                                                 ./intl
219                                                 ./configure ./configure.scan
220                                                 ./config.guess ./config.status ./config.sub ./config.log ./config.cache
221                                                 ./config.h ./config.h.in
222                                                 ./confdefs.h ./conftest* ./autoh[0-9]* ./confcache
223                                                 ./config.rpath
224                                                 ./depcomp
225                                                 ./compile
226                                                 ./stamp-h ./stamp-h.in ./stamp-h1
227                                                 ./install-sh
228                                                 ./aclocal.m4
229                                                 ./autom4te.cache ./autom4te-*.cache
230                                                 ./m4
231                                                 ./missing
232                                                 ./mkinstalldirs
233                                                 ./libtool ./ltconfig ./ltmain.sh
234                                                 ./ChangeLog
235                                                 ./ABOUT-NLS
236                                                 ./<name>-[0-9]* ./<name>-devel-[0-9]*
237                                                 ./<name>.spec ./<name>.m4 ./<name>.spec.m4
238                                                 ./macros/macros.dep
239                                                 ./po/Makefile.in.in ./po/POTFILES* ./po/cat-id-tbl.c ./po/cat-id-tbl.tmp
240                                                 ./po/*.gmo ./po/*.mo ./po/stamp-cat-id ./po/<name>.pot ./po/ChangeLog
241                                                 ./po/Makevars ./po/Makevars.template ./po/Rules-quot ./po/*.sed ./po/*.sin ./po/*.header
242                                                 ),
243                                 map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
244                                                 *.stamp
245                                                 sgml*
246                                                 tmpl*
247                                                 html*
248                                                 *.txt
249                                                 *.txt.bak
250                                                 *.args
251                                                 *.hierarchy
252                                                 *.signals
253                                                 )); }),$Options{"gtk-doc-dir"}),
254                                 map((!$_ ? () : do { my $dir=$_; map("$dir/$_",qw(
255                                                 *.html
256                                                 *.info*
257                                                 *.txt
258                                                 *.tex
259                                                 *.sgml
260                                                 )); }),$Options{"docbook-lite-dir"}),
261                                 map((!$_ ? () : @$_),$Options{"clean"}),
262                                 ));
263                 # sanity check
264                 for (@_cleanfiles_cache) {
265                                 confess "dir-specific 'clean'-pattern must start with './': $_" if m#^(?!\Q./\E).*/#;
266                                 };
267                 }
268         return @_cleanfiles_cache;
269 }
270
271 sub _cleanfilesfordir
272 {
273 my($dir)=@_;
274
275         return map({
276                            if (m#^\Q$dir\E/([^/]+)$#) { # this-dir: "./this-dir/file-name.c"
277                                         ($1);
278                                         }
279                         elsif (m#^[^/]+$#) {    # all-dirs: "file-name.c"; the same as "./*/file-name.c"
280                                         ($&);
281                                         }
282                         elsif (do {     # all-subdirs: "./parent-of-this-dir/*/file-name.c"
283                                                         m#/[*]/([^/]+)$#;
284                                                         ($_=$1) && $dir=~m#^\Q$`\E(?:/|$)#;
285                                                         }) {
286                                         ($_);
287                                         }
288                         else {
289                                         ();
290                                         }
291                         } _cleanfiles());
292 }
293
294 sub _cvsdirs
295 {
296 my(@startdirs)=@_;
297
298         my @r=();
299         my @todo=(@startdirs);
300         while (defined(my $dir=shift @todo)) {
301                 local *ENTRIES;
302                 my $entries_filename="$dir/CVS/Entries";
303                 open ENTRIES,$entries_filename or (cluck "open \"$entries_filename\": $!" and next);
304                 push @r,$dir;
305                 local $/="\n";
306                 my %local=();
307                 local $_;
308                 while (<ENTRIES>) {
309                         chomp;
310                         next if !m#^D/([^/]+)/#;
311                         $local{$1}=1;
312                         }
313                 close ENTRIES or cluck "close \"$entries_filename\": $!";
314                 if (-e (my $entries_log_filename=$dir."/CVS/Entries.Log")) {
315                         local *ENTRIES_LOG;
316                         if (open ENTRIES_LOG,$entries_log_filename or cluck "open \"$entries_log_filename\": $!") {
317                                 local $_;
318                                 while (<ENTRIES_LOG>) {
319                                         chomp;
320                                                  if (m#^A D/([^/]*)/#) {
321                                                 $local{$1}=1;
322                                                 }
323                                         elsif (m#^R D/([^/]*)/#) {
324                                                 delete $local{$1};
325                                                 }
326                                         else {
327                                                 cluck "$entries_log_filename: Unrecognized line $.: $_";
328                                                 }
329                                         }
330                                 close ENTRIES_LOG or cluck "close \"$entries_log_filename\": $!";
331                                 }
332                         }
333                 push @todo,map(("$dir/$_"),keys(%local));
334                 }
335         return @r;
336 }
337
338 sub _expandclass
339 {
340 my($patt)=@_;
341
342         return $patt if $patt!~/\Q[\E(.*?)\Q]\E/;
343         my($pre,$range,$post)=($`,$1,$');       # FIXME: local($`,$1,$') doesn't work - why?
344         1 while $range=~s#(.)-(.)# join("",map(chr,(ord($1)..ord($2))));
345                         #ge;
346         return map({ _expandclass("$pre$_$post"); } split("",$range));
347 }
348
349 sub run
350 {
351 my($class,%options)=@_;
352
353         local %Options=%options;
354         do { require $_ if -e; } for (home()."/.".$Options{"name"}.".autogen.pl");
355         do { $$_=1 if !defined($$_) && fgrep { /^\s*AUTOMAKE_OPTIONS\s*=[^#]*\bdist-tarZ\b/m; } "Makefile.am"; }
356                         for (\$Options{"dist-tarZ"});
357         Getopt::Long::Configure('noignorecase','prefix_pattern=(--|-|\+|)');
358         local @ARGV=@{$Options{"ARGV"}};
359         print _help() and confess if !GetOptions(
360                           "rpm"      ,sub { $class->_rpmbuild("sign"=>1); },
361                           "rpmtest"  ,sub { $class->_rpmbuild("sign"=>0); },
362                           "dist"     ,\$Options{"ARGV_dist"},
363                           "copy!"    ,\$Options{"ARGV_copy"},
364                           "fullclean",\$Options{"ARGV_fullclean"},
365                           "clean"    ,\$Options{"ARGV_clean"},
366                         "h|help"     ,sub { print _help(); exit 0; },
367                         $Options{"GetOptions_args"},
368                         ) || @ARGV;
369
370         for my $subdir (map((!$_ ? () : @$_),$Options{"subdirs"})) {
371                 local $CWD=$subdir;
372                 _system "./autogen.pl",@{$Options{"ARGV"}},"--dist";    # use "--dist" just as fallback!
373                 }
374
375         for my $dir (_cvsdirs(".")) {
376                 my @cleanfilesfordir=_cleanfilesfordir $dir;
377                 _writefile $dir."/.cvsignore",map("$_\n",@cleanfilesfordir) if !$Options{"ARGV_fullclean"};
378                 _remove "nocheck",\1,map({ _expandclass("$dir/$_"); } grep({
379                                 $Options{"ARGV_fullclean"} or $_ ne ".cvsignore";
380                                 } @cleanfilesfordir));
381                 }
382         return if $Options{"ARGV_clean"} || $Options{"ARGV_fullclean"};
383
384         $Options{"aclocal_args"}=[qw(-I macros),map((!$_ ? () : @$_),$Options{"aclocal_args"})];
385         my $configure_in=_readfile("configure.in");
386         do { $$_=1 if !defined($$_) && $configure_in=~/^AM_GNU_GETTEXT\b/m; }
387                         for (\$Options{"want-gettextize"});
388         do { $$_=1 if !defined($$_) && $configure_in=~/^AM_GLIB_GNU_GETTEXT\b/m; }
389                         for (\$Options{"want-glib-gettextize"});
390         do { $$_=1 if !defined($$_) && $configure_in=~/^AM_PROG_LIBTOOL\b/m; }
391                         for (\$Options{"want-libtoolize"}); 
392         do { $$_=1 if !defined($$_) && $configure_in=~/^A[CM]_CONFIG_HEADER\b/m; }
393                         for (\$Options{"want-autoheader"});
394         my @copy_arg=(!$Options{"ARGV_copy"} ? () : "--copy");
395
396         do { &$_ if $_; } for ($Options{"prep"});
397         touch "po/POTFILES.in" if -d "po";
398         if ($Options{"want-gettextize"}) {
399                 # don't use multi-arg system() here as it would reject "</dev/null" redirection etc.
400                 for ("expect -c '"
401                                 .'spawn gettextize --intl --no-changelog '.join(" ",@copy_arg).';'
402                                 .'expect -timeout -1 "Press Return to acknowledge" {send "\r";exp_continue;} eof;'
403                                 ."'") {
404                         _system $_ and confess $_;
405                         }
406                 for ("configure.in","Makefile.am") {
407                         STDERR->printflush("gettextize recovery rename \"$_~\"->\"$_\"... ");
408                         rename "$_~","$_" or confess "$!";
409                         STDERR->printflush("ok\n");
410                         }
411                 if (!-e "po/Makevars") {
412                         my $Makevars_template="po/Makevars.template";
413                         my $makevars=_readfile $Makevars_template;
414                         $makevars=~s/^(COPYRIGHT_HOLDER)\b.*$/"$1=".$Options{"COPYRIGHT_HOLDER"}/meg
415                                         or confess "COPYRIGHT_HOLDER not found in $Makevars_template";
416                         _writefile "po/Makevars",$makevars;
417                         }
418                 # Prevent updating of contents during touch of any source file;
419                 # change the .po contents only when some data get updated
420                 for my $Makefile_in_in ("po/Makefile.in.in") {
421                         my $file=_readfile $Makefile_in_in;
422                         $file=~s%(\$\Q(MSGMERGE_UPDATE)\E) (\$\$\Q{lang}.po \E\$\Q(DOMAIN).pot\E)$%
423                                         $1.q< --backup=simple --suffix="~" >.$2.q<;>
424                                                         .q< if test `diff -u $${lang}.po~ $${lang}.po>
425                                                                                         .q< | sed>
426                                                                                                         .q< -e '1,/^@@.*@@$$/d'>
427                                                                                                         .q< -e '/^[+-]"POT-Creation-Date:/d'>
428                                                                                                         .q< -e '/^[^+-]/d'>
429                                                                                                         .q< -e '/^[+-]#/d'>
430                                                                                         .q< | wc -l` -eq 0;then>
431                                                                         .q< touch --reference=$${lang}.po $${lang}.po~;>
432                                                                                         .q< mv -f $${lang}.po~ $${lang}.po;>
433                                                         .q< else>
434                                                                         .q< rm -f $${lang}.po~;>
435                                                         .q< fi>
436                                         %me or confess;
437                         unlink $Makefile_in_in or confess "$!";
438                         _writefile $Makefile_in_in,$file;
439                         }
440                 }
441         if ($Options{"want-glib-gettextize"}) {
442                 _system "glib-gettextize",@copy_arg;
443                 # "po/ChangeLog" is somehow missing at this point
444                 File::Touch->new("atime_only"=>1)->touch("po/ChangeLog");
445                 }
446         _system "aclocal",map((!$_ ? () : @$_),$Options{"aclocal_args"});
447         _system qw(libtoolize),@copy_arg if $Options{"want-libtoolize"};
448         _system qw(autoheader) if $Options{"want-autoheader"};
449         # "ChangeLog" is reqd by automake(1)
450         # Don't remove it afterwards as it may still be needed during automatic automake Makefile rebuilds
451         File::Touch->new("atime_only"=>1)->touch("ChangeLog");
452         _system qw(automake --add-missing),@copy_arg;
453         _system qw(autoconf);
454         # Why it is left there after RedHat autoconf-2.53-8 ?
455         _remove "nocheck",\1,"autom4te-*.cache";
456
457         return if $Options{"ARGV_dist"};
458
459         # 'configure' defaults to CFLAGS '-g -O2' but our --enable-maintainer-mode
460         # should force '-ggdb3'
461         $ENV{"CFLAGS"}||="";
462         # shared/static switching cannot be based on maintainer-mode in configure
463         _system(qw(./configure --enable-maintainer-mode),
464                         (!$Options{"want-libtoolize"} ? () : qw(--enable-shared --disable-static)),
465                         map((!$_ ? () : @$_),$Options{"configure_args"}),
466                         );
467 }
468
469 1;
470 __END__
471
472 Tested with:
473         RedHat autoconf-2.53-8
474         RedHat automake-1.6.3-1
475         RedHat gettext-0.11.4-3
476         RedHat libtool-1.4.2-12
477         RedHat perl-5.8.0-48