4ffdb14083bf7cfadb7cdc207fc17ddb5c3b238d
[platform/upstream/automake.git] / automake.in
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
4
5 eval "exec /usr/local/bin/perl -S $0 $*"
6     if $running_under_some_shell;
7
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994 Free Software Foundation, Inc.
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 # 02111-1307, USA.
25
26 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
27 # Perl reimplementation by Tom Tromey <tromey@drip.colorado.edu>.
28
29
30 # Parameters set by configure.  Not to be changed.
31 $VERSION = "@VERSION@";
32 $prefix = "@prefix@";
33 $am_dir = "@datadir@/@PACKAGE@";
34
35 \f
36
37 # This is TRUE if GNU make specific automatic dependency generation
38 # code should be included in generated Makefile.in.
39 $use_dependencies = 1;
40
41 # This holds our (eventual) exit status.  We don't actually exit until
42 # we have processed all input files.
43 $exit_status = 0;
44
45 # These two variables are used when generating each Makefile.in.  They
46 # hold the Makefile.in until it is ready to be printed.
47 $output_rules = '';
48 $output_vars = '';
49 $output_trailer = '';
50
51 # Suffixes found during a run.
52 @suffixes = ();
53
54 # This holds the contents of a Makefile.am, as parsed by read_am_file.
55 %contents = ();
56
57 # This holds the "relative directory" of the current Makefile.in.  Eg
58 # for src/Makefile.in, this is "src".
59 $relative_dir = '';
60
61 # Directory where output files go.  Actually, output files are
62 # relative to this directory.
63 $output_directory = '.';
64
65 # This holds a list of files that are included in the distribution.
66 @dist_common = ();
67
68 # List of dependencies for the obvious targets.
69 @install_data = ();
70 @install_exec = ();
71 @uninstall = ();
72 @installdirs = ();
73
74 @info = ();
75 @dvi = ();
76 @all = ();
77 @check = ();
78 @installcheck = ();
79 @clean = ();
80
81 # TRUE if current directory holds any C source files.
82 $dir_holds_sources = 0;
83
84 # TRUE if install targets should work recursively.
85 $recursive_install = 0;
86
87 \f
88
89 &init_globals;
90
91 # Parse command line.
92 @input_files = &parse_arguments (@ARGV);
93
94 # Now do all the work on each file.
95 foreach $am_file (@input_files)
96 {
97     if (! -f ($am_file . '.am'))
98     {
99         print STDERR "automake: $am_file" . ".am: no such file\n";
100         $exit_status = 1;
101     }
102     else
103     {
104         &generate_makefile ($am_file);
105     }
106 }
107
108 exit $exit_status;
109
110
111 ################################################################
112
113 # Parse command line.
114 sub parse_arguments
115 {
116     local (@arglist) = @_;
117     local (@make_list);
118
119     while ($#arglist >= 0)
120     {
121         if ($arglist[0] eq "--version")
122         {
123             print "Automake version $VERSION\n";
124             exit 0;
125         }
126         elsif ($arglist[0] eq "--help")
127         {
128             &usage;
129         }
130         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
131         {
132             $am_dir = $1;
133         }
134         elsif ($arglist[0] eq '--amdir')
135         {
136             if ($#arglist == 0)
137             {
138                 print STDERR
139                     "automake: no argument given for option \`$arglist[0]'\n";
140                 exit 1;
141             }
142             shift (@arglist);
143             $am_dir = $arglist[0];
144         }
145         elsif ($arglist[0] eq '--include-deps')
146         {
147             $use_dependencies = 0;
148         }
149         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
150         {
151             # Set output directory.
152             $output_directory = $1;
153         }
154         elsif ($arglist[0] eq '--output-dir')
155         {
156             if ($#arglist == 0)
157             {
158                 print STDERR
159                     "automake: no argument given for option \`$arglist[0]'\n";
160                 exit 1;
161             }
162             shift (@arglist);
163             $output_directory = $arglist[0];
164         }
165         elsif ($arglist[0] eq '--')
166         {
167             # Stop option processing.
168             shift (@arglist);
169             push (@make_list, @arglist);
170             last;
171         }
172         elsif ($arglist[0] =~ /^-/)
173         {
174             print STDERR "automake: unrecognized option -- \`$arglist[0]'\n";
175             exit 1;
176         }
177         else
178         {
179             push (@make_list, $arglist[0]);
180         }
181
182         shift (@arglist);
183     }
184
185     if ($#make_list < 0)
186     {
187         # Look around for some files.
188         push (@make_list, 'Makefile') if -f 'Makefile.am';
189
190         foreach (<*/Makefile.am>)
191         {
192             s/\.am$//;
193             push (@make_list, $_);
194         }
195
196         if ($#make_list >= 0)
197         {
198             print "automake: using ", join (' ', @make_list), "\n";
199         }
200         else
201         {
202             print STDERR "automake: no \"Makefile.am\" found or specified\n";
203             exit 1;
204         }
205     }
206
207     return (@make_list);
208 }
209
210 ################################################################
211
212 # Generate a Makefile.in given the name of the corresponding Makefile.
213 sub generate_makefile
214 {
215     local ($makefile) = @_;
216     
217     print "creating ", $makefile, ".in\n";
218
219     $relative_dir = &dirname ($makefile);
220     $output_rules = '';
221     $output_vars = '';
222     $output_trailer = '';
223     @suffixes = ();
224     %contents = ();
225     # FIXME with new 'dist' target, don't need Makefile.in.  Probably
226     # should remove it here.
227     @dist_common = ('Makefile.in', 'Makefile.am');
228     @install_data = ();
229     @install_exec = ();
230     @uninstall = ();
231     @installdirs = ();
232     $dir_holds_sources = 0;
233     $recursive_install = 0;
234     @info = ();
235     @dvi = ();
236     @all = ();
237     @check = ();
238     @installcheck = ();
239     @clean = ();
240
241     # Generate header before reading .am file.  The header must come
242     # before anything else, and read_am_file copies code into the
243     # output.
244     &generate_header;
245
246     # This is always the default target.  This gives us freedom to do
247     # things in whatever order is convenient.
248     $output_rules .= "default: all\n\n";
249
250     &read_am_file ($makefile . '.am');
251
252     # Program stuff.
253     local ($programs) = &am_variable ('PROGRAMS');
254     local ($libprograms) = &am_variable ('LIBPROGRAMS');
255     local ($libraries) = &am_variable ('LIBRARIES');
256     local ($scripts) = &am_variable ('SCRIPTS');
257     local ($libscripts) = &am_variable ('LIBSCRIPTS');
258
259     &handle_programs ($programs, $libprograms, $libraries);
260     &handle_scripts ($scripts, $libscripts);
261     &handle_libraries ($libraries);
262
263     &handle_texinfo;
264     &handle_man_pages;
265     &handle_data;
266     &handle_subdirs;
267     &handle_configure;
268     &handle_tags;
269     &handle_dist;
270     &handle_dependencies;
271     &handle_footer;
272     &handle_merge_targets;
273     &handle_installdirs;
274     &handle_clean;
275
276     if (! -d ($output_directory . '/' . $relative_dir))
277     {
278         &mkdir ($output_directory . '/' . $relative_dir);
279     }
280     if (! open (GM_FILE, "> " . $output_directory . '/' . $makefile . ".in"))
281     {
282         print STDERR "automake: cannot open ", $makefile, ".in: ", $!, "\n";
283         $exit_status = 1;
284         return;
285     }
286
287     print GM_FILE $output_vars;
288     print GM_FILE $output_rules;
289     print GM_FILE $output_trailer;
290
291     close (GM_FILE);
292 }
293
294 ################################################################
295
296 # Generate header of Makefile.in.
297 sub generate_header
298 {
299     $output_vars =
300         ($output_vars
301          . "# Makefile.in generated automatically by automake "
302          . $VERSION
303          . " from Makefile.am\n");
304
305     $output_vars = $output_vars . &file_contents ('header-vars');
306 }
307
308 # Handle C programs and libraries.
309 sub handle_programs
310 {
311     local ($programs, $libprograms, $libraries) = @_;
312
313     if (!$programs && !$libprograms && !$libraries)
314     {
315         # None exist.
316         return;
317     }
318     $dir_holds_sources = 1;
319
320     # Boilerplate.
321     $output_vars .= &file_contents ('compile-vars');
322     $output_rules .= &file_contents ('compile');
323
324     # Check for automatic de-ANSI-fication.
325     local ($obj) = '.o';
326     push (@suffixes, '.c', '.o');
327     push (@clean, 'compile');
328
329     if (defined $contents{'@kr@'})
330     {
331         $obj = '.${kr}o';
332         push (@suffixes, '._c', '._o');
333
334         &require_file ('ansi2knr.c');
335         &require_file ('ansi2knr.1');
336
337         $output_vars .= &file_contents ('kr-vars');
338         $output_rules .= &file_contents ('compile-kr');
339         $output_rules .= &file_contents ('clean-kr');
340
341         push (@clean, 'kr');
342     }
343
344     local (@sources, @objects);
345     push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
346     push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
347
348     local ($one_file);
349     foreach $one_file (split (' ', ($programs . ' '
350                                     . $libprograms . ' '
351                                     . $libraries)))
352     {
353         # Look for file_SOURCES and file_OBJECTS.  FIXME file_OBJECTS
354         # should probably not be used(?)
355         if (defined $contents{$one_file . "_SOURCES"})
356         {
357             if (! defined $contents{$one_file . "_OBJECTS"})
358             {
359                 # Turn sources into objects.
360                 $_ = $contents{$one_file . "_SOURCES"};
361
362                 # Ugh: Perl syntax vs Emacs.
363                 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
364
365                 s/\.cc/$obj/g;
366                 s/$krc1/$obj/g;
367                 s/$krc2/$obj/g;
368                 s/\.[cCmylfs]/$obj/g;
369
370                 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
371             }
372
373             push (@sources, '${' . $one_file . "_SOURCES}");
374             push (@objects, '${' . $one_file . "_OBJECTS}");
375         }
376         else
377         {
378             $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
379                              . $one_file . "_OBJECTS = ". $one_file
380                              . $obj . "\n");
381             push (@sources, $one_file . '.c');
382             push (@objects, $one_file . $obj);
383         }
384
385         if (defined $contents{'CONFIG_HEADER'})
386         {
387             $output_rules .= ('$(' . $one_file . "_OBJECTS): "
388                               . $contents{'CONFIG_HEADER'} . "\n");
389         }
390     }
391
392     $output_vars .= "\n";
393
394     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
395     # on this.
396     $contents{'SOURCES'} = join (' ', @sources);
397     $contents{'OBJECTS'} = join (' ', @objects);
398
399     # Some boilerplate, and install rules.
400     if ($programs)
401     {
402         $output_rules .= &file_contents ('programs');
403         push (@install_exec, "install-programs");
404         push (@uninstall, 'uninstall-programs');
405         push (@clean, 'programs');
406         push (@installdirs, '$(bindir)');
407         push (@all, '$(PROGRAMS)');
408     }
409     if ($libprograms)
410     {
411         $output_rules .= &file_contents ('libprograms');
412         push (@install_exec, 'install-libprograms');
413         push (@uninstall, 'uninstall-libprograms');
414         push (@clean, 'libprograms');
415         push (@installdirs, '$(libexecdir)');
416         push (@all, '$(LIBPROGRAMS)');
417     }
418
419     # Handle linking.
420     if ($programs || $libprograms)
421     {
422         local ($fcont) = &file_contents ('program');
423         local ($munge);
424         foreach $one_file (split (' ', $programs . ' ' . $libprograms))
425         {
426             if (! defined $contents{$one_file . "_LDADD"})
427             {
428                 # User didn't define prog_LDADD override.  So do it.
429                 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
430             }
431
432             ($munge = $fcont) =~ s/\@PROGRAM\@/$one_file/g;
433             $output_rules .= $munge;
434         }
435     }
436 }
437
438 # Handle libraries.
439 sub handle_libraries
440 {
441     local ($libraries) = @_;
442
443     return if (!$libraries);
444
445     local (@liblist) = split (' ', $libraries);
446
447     $output_rules .= &file_contents ('libraries');
448     local ($onefile) = &file_contents ('library');
449     local ($onelib, $munge);
450     foreach $onelib (@liblist)
451     {
452         if (! defined $contents{$onelib . '_LIBADD'})
453         {
454             # Generate support for conditional objection inclusion in
455             # libraries.
456             $output_vars .= $onelib . "_LIBADD =\n";
457         }
458
459         ($munge = $onefile) =~ s/\@LIBRARY\@/$onelib/g;
460         $output_rules .= $munge;
461     }
462
463     # Turn "foo" into "libfoo.a" and include macro definition.
464     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
465     $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
466                      . &file_contents ('libraries-vars'));
467
468     push (@install_exec, 'install-libraries');
469     push (@uninstall, 'uninstall-libraries');
470     push (@clean, 'libraries');
471     push (@all, '$(LIBFILES)');
472 }
473
474 # Handle scripts.
475 sub handle_scripts
476 {
477     local ($scripts, $libscripts) = @_;
478
479     if ($scripts)
480     {
481         $output_rules .= &file_contents ('scripts');
482         push (@install_exec, 'install-scripts');
483         push (@uninstall, 'uninstall-scripts');
484         push (@clean, 'scripts');
485         push (@installdirs, '$(bindir)');
486         push (@all, '$(SCRIPTS)');
487     }
488
489     if ($libscripts)
490     {
491         $output_rules .= &file_contents ('libscripts');
492         push (@install_exec, 'install-libscripts');
493         push (@uninstall, 'uninstall-libscripts');
494         push (@clean, 'libscripts');
495         push (@installdirs, '$(libexecdir');
496         push (@all, '$(LIBSCRIPTS)');
497     }
498 }
499
500 # Handle all Texinfo source.
501 sub handle_texinfo
502 {
503     local ($texis) = &am_variable ('TEXINFOS');
504     return if (!$texis);
505
506     local (@texis) = split (' ', $texis);
507     if ($#texis > 0)
508     {
509         print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
510         $exit_status = 1;
511         return;
512     }
513
514     local ($infobase);
515     ($infobase = $texis[0]) =~ s/\.texi$//;
516
517     # If 'version.texi' is referenced by input file, then include
518     # automatic versioning capability.
519     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
520             . " > /dev/null 2>&1");
521     if (!$?)
522     {
523         # Got a hit.
524         push (@texis, 'version.texi');
525         push (@dist_common, 'version.texi', 'stamp-vti');
526         push (@clean, 'vti');
527
528         local ($tfile);
529         ($tfile = &file_contents ('texi-version')) =~ s/\@TEXI\@/$texis[0]/g;
530         $output_rules = $output_rules . $tfile;
531
532         &require_file ('mdate-sh');
533     }
534
535     # If user specified file_TEXINFOS, then use that as explicit
536     # dependency list.
537     if (defined $contents{$infobase . "_TEXINFOS"})
538     {
539         push (@texis, "\$" . $infobase . '_TEXINFOS');
540         push (@dist_common, "\$" . $infobase . '_TEXINFOS');
541     }
542
543     if ($#texis >= 0)
544     {
545         $output_rules = ($output_rules . $infobase . ".info: "
546                          . join (' ', @texis) . "\n\n");
547     }
548
549     # Some boilerplate.
550     $output_vars = $output_vars . &file_contents ('texinfos-vars');
551     $output_rules = $output_rules . &file_contents ('texinfos');
552
553     push (@suffixes, '.texi', '.info', '.dvi');
554     push (@uninstall, 'uninstall-info');
555     push (@clean, 'info');
556     push (@info, '$(INFO_DEPS)');
557     push (@dvi, '$(DVIS)');
558     push (@installdirs, '$(infodir)');
559     # Make sure documentation is made and installed first.
560     unshift (@install_data, 'install-info');
561     unshift (@all, 'info');
562
563     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
564                      . "INFO_DEPS = " . $infobase . ".info\n"
565                      . "DVIS = " . $infobase . ".dvi\n\n");
566
567     # Do some error checking.
568     &require_file ('texinfo.tex');
569 }
570
571 # Handle any man pages.
572 sub handle_man_pages
573 {
574     return if (! defined $contents{'MANS'});
575
576     # We generate the manpage install code by hand to avoid the use of
577     # basename in the generated Makefile.
578     local (@mans) = split (' ', $contents{'MANS'});
579     local (%sections, %inames, %secmap, %fullsecmap);
580     foreach (@mans)
581     {
582         m/^(.*)\.([0-9])([a-z]*)$/;
583         $sections{$2} = 1;
584         $inames{$1} = $_;
585         $secmap{$1} = $2;
586         $fullsecmap{$1} = $2 . $3;
587     }
588
589     # Generate list of install dirs.
590     $output_rules .= "install-man:\n";
591     foreach (keys %sections)
592     {
593         push (@installdirs, '$(mandir)/man' . $_);
594         $output_rules .= ("\t" . '$(top_srcdir)/mkinstalldirs $(mandir)/man'
595                           . $_ . "\n");
596     }
597
598     # Generate install target.
599     local ($key);
600     foreach $key (keys %inames)
601     {
602         $_ = $install_man_format;
603         s/\@SECTION\@/$secmap{$key}/g;
604         s/\@MAN\@/$inames{$key}/g;
605         s/\@FULLSECT\@/$fullsecmap{$key}/g;
606         s/\@MANBASE\@/$key/g;
607         $output_rules .= $_;
608     }
609     $output_rules .= "\n";
610
611     $output_rules .= "uninstall-man:\n";
612     foreach $key (keys %inames)
613     {
614         $_ = $uninstall_man_format;
615         s/\@SECTION\@/$secmap{$key}/g;
616         s/\@MAN\@/$inames{$key}/g;
617         s/\@FULLSECT\@/$fullsecmap{$key}/g;
618         s/\@MANBASE\@/$key/g;
619         $output_rules .= $_;
620     }
621     $output_rules .= "\n";
622
623     $output_vars .= &file_contents ('mans-vars');
624
625     push (@install_data, 'install-man');
626     push (@uninstall, 'uninstall-man');
627     push (@all, '$(MANS)');
628 }
629
630 # Handle DATA and PACKAGEDATA.
631 sub handle_data
632 {
633     if (defined $contents{'DATA'})
634     {
635         $output_rules .= &file_contents ('data');
636         push (@install_data, 'install-ddata');
637         push (@uninstall, 'uninstall-ddata');
638         push (@installdirs, '$(datadir)');
639     }
640
641     if (defined $contents{'PACKAGEDATA'})
642     {
643         $output_rules .= &file_contents ('packagedata');
644         push (@install_data, 'install-pdata');
645         push (@uninstall, 'uninstall-pdata');
646         push (@installdirs, '$(datadir)/$(PACKAGE)');
647     }
648 }
649
650 # Handle TAGS.
651 sub handle_tags
652 {
653     local ($tagging) = 0;
654
655     if (defined ($contents{'SUBDIRS'}))
656     {
657         $output_rules .= &file_contents ('tags');
658         $tagging = 1;
659     }
660     elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
661     {
662         $output_rules .= &file_contents ('tags-subd');
663         $tagging = 1;
664     }
665
666     if ($tagging)
667     {
668         $output_rules .= &file_contents ('tags-clean');
669         push (@clean, 'tags');
670     }
671     else
672     {
673         # Every Makefile must define some sort of TAGS rule.
674         # Otherwise, it would be possible for a top-level "make TAGS"
675         # to fail because some subdirectory failed.
676         $output_rules .= "tags: TAGS\nTAGS:\n\n";
677     }
678 }
679
680 # Handle 'dist' target.
681 sub handle_dist
682 {
683     # Look for common files that should be included in distribution.
684     local ($cfile);
685     foreach $cfile (@common_files)
686     {
687         if (-f ($relative_dir . "/" . $cfile))
688         {
689             push (@dist_common, $cfile);
690         }
691     }
692
693     $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
694
695     # Some boilerplate.
696     $output_vars .= &file_contents ('dist-vars');
697     if ($relative_dir ne '.')
698     {
699         # In a subdirectory.
700         $output_vars .= "subdir = " . $relative_dir . "\n\n";
701         $output_rules .= &file_contents ('dist-subd');
702     }
703     else
704     {
705         $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
706                                          ? 'dist-subd-top'
707                                          : 'dist');
708     }
709 }
710
711 # Handle auto-dependency code.
712 sub handle_dependencies
713 {
714     if ($use_dependencies)
715     {
716         # Include GNU-make-specific auto-dep code.
717         if ($dir_holds_sources)
718         {
719             $output_rules .= &file_contents ('depend');
720         }
721     }
722     else
723     {
724         # Include any auto-generated deps that are present.
725         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
726         {
727             local ($depfile);
728             local ($gpat) = $relative_dir . "/.deps/*.P";
729
730             foreach $depfile (<${gpat}>)
731             {
732                 if (! open (DEP_FILE, $depfile))
733                 {
734                     print STDERR "automake: couldn't open $depfile: $!\n";
735                     next;
736                 }
737
738                 # Slurp entire file.
739                 $output_rules .= join ('', <DEP_FILE>);
740
741                 close (DEP_FILE);
742             }
743
744             $output_rules .= "\n";
745         }       
746     }
747 }
748
749 # Handle subdirectories.
750 sub handle_subdirs
751 {
752     return if (! defined ($contents{'SUBDIRS'}));
753
754     $output_rules .= &file_contents ('subdirs');
755
756     push (@all, "all-recursive");
757     push (@check, "check-recursive");
758     push (@installcheck, "installcheck-recursive");
759     push (@info, "info-recursive");
760     push (@dvi, "dvi-recursive");
761
762     $recursive_install = 1;
763 }
764
765 # Handle remaking and configure stuff.
766 sub handle_configure
767 {
768     if ($relative_dir ne '.')
769     {
770         # In subdirectory.
771         $output_rules .= &file_contents ('remake-subd');
772     }
773     else
774     {
775         if (-f 'aclocal.m4')
776         {
777             $output_vars .= "ACLOCAL = aclocal.m4\n";
778             push (@dist_common, 'aclocal.m4');
779         }
780         $output_rules .= &file_contents ('remake');
781
782         # Look for some files we need.
783         &require_file ('install-sh');
784         &require_file ('mkinstalldirs');
785     }
786
787     if (defined ($contents{'CONFIG_HEADER'})
788         && $contents{'CONFIG_HEADER'} !~ m,/,)
789     {
790         # Header defined and in this directory.
791         if (-f 'acconfig.h')
792         {
793             $output_vars .= "ACCONFIG = acconfig.h\n";
794             push (@dist_common, 'acconfig.h');
795         }
796         if (-f 'config.h.top')
797         {
798             $output_vars .= "CONFIG_TOP = config.h.top\n";
799             push (@dist_common, 'config.h.top');
800         }
801         if (-f 'config.h.bot')
802         {
803             $output_vars .= "CONFIG_BOT = config.h.bot\n";
804             push (@dist_common, 'config.h.bot');
805         }
806
807         push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
808
809         $output_rules .= &file_contents ('remake-hdr');
810     }
811 }
812
813 # Handle footer elements.
814 sub handle_footer
815 {
816     if ($contents{'SOURCES'})
817     {
818         $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
819     }
820     if ($contents{'OBJECTS'})
821     {
822         $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
823     }
824     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
825     {
826         $output_vars .= "\n";
827     }
828
829     if (defined $contents{'SUFFIXES'})
830     {
831         push (@suffixes, '$(SUFFIXES)');
832     }
833
834     $output_trailer .= ".SUFFIXES:\n";
835     if ($#suffixes >= 0)
836     {
837         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
838     }
839     $output_trailer .= "\n" . &file_contents ('footer');
840 }
841
842 # Deal with installdirs target.
843 sub handle_installdirs
844 {
845     # GNU Makefile standards recommend this.  FIXME prettyprint rule
846     # here.
847     $output_rules .= ("installdirs:"
848                       . ($recursive_install
849                          ? " installdirs-recursive\n"
850                          : "\n"));
851     if ($#installdirs >= 0)
852     {
853         $output_rules .= ("\t\$(top_srcdir)/mkinstalldirs "
854                           . join (' ', @installdirs)
855                           . "\n");
856     }
857     $output_rules .= "\n";
858 }
859
860 # There are several targets which need to be merged.  This is because
861 # their complete definition is compiled from many parts.  Note that we
862 # avoid double colon rules, otherwise we'd use them instead.
863 sub handle_merge_targets
864 {
865     &do_one_merge_target ('all', @all);
866     &do_one_merge_target ('info', @info);
867     &do_one_merge_target ('dvi', @dvi);
868     &do_one_merge_target ('check', @check);
869     &do_one_merge_target ('installcheck', @installcheck);
870
871     # Handle the various install targets specially.  We do this so
872     # that (eg) "make install-exec" will run "install-exec-recursive"
873     # if required, but "make install" won't run it twice.  Step one is
874     # to see if the user specified local versions of any of the
875     # targets we handle.
876     if (defined $contents{'install-exec-local'})
877     {
878         push (@install_exec, 'install-exec-local');
879     }
880     if (defined $contents{'install-data-local'})
881     {
882         push (@install_data, 'install-data-local');
883     }
884     if (defined $contents{'uninstall-local'})
885     {
886         push (@uninstall, 'uninstall-local');
887     }
888
889     if (defined $contents{'install-local'})
890     {
891         print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
892     }
893
894     # Step two: if we are doing recursive makes, write out the
895     # appropriate rules.
896     local (@install);
897     if ($recursive_install)
898     {
899         push (@install, 'install-recursive');
900         push (@uninstall, 'uninstall-recursive');
901
902         if ($#install_exec >= 0)
903         {
904             $output_rules .= ('install-exec-am: '
905                               . join (' ', @install_exec)
906                               . "\n\n");
907             @install_exec = ('install-exec-recursive', 'install-exec-am');
908             push (@install, 'install-exec-am');
909         }
910         if ($#install_data >= 0)
911         {
912             $output_rules .= ('install-data-am: '
913                               . join (' ', @install_data)
914                               . "\n\n");
915             @install_data = ('install-data-recursive', 'install-data-am');
916             push (@install, 'install-data-am');
917         }
918         if ($#uninstall >= 0)
919         {
920             $output_rules .= ('uninstall-am: '
921                               . join (' ', @uninstall)
922                               . "\n\n");
923             @uninstall = ('uninstall-recursive', 'uninstall-am');
924         }
925     }
926
927     # Step three: print definitions users can use.
928     if ($#install_exec >= 0)
929     {
930         $output_rules .= ("install-exec: "
931                           . join (' ', @install_exec)
932                           . "\n\n");
933         push (@install, 'install-exec') if (!$recursive_install);
934     }
935     if ($#install_data >= 0)
936     {
937         $output_rules .= ("install-data: "
938                           . join (' ', @install_data)
939                           . "\n\n");
940         push (@install, 'install-data') if (!$recursive_install);
941     }
942
943     $output_rules .= ('install: '
944                       . join (' ', @install)
945                       . "\n\n"
946                       . 'uninstall: '
947                       . join (' ', @uninstall)
948                       . "\n\n");
949 }
950
951 # Helper for handle_merge_targets.
952 sub do_one_merge_target
953 {
954     local ($name, @values) = @_;
955
956     if (defined $contents{$name . '-local'})
957     {
958         # User defined local form of target.  So include it.
959         push (@values, $name . '-local');
960     }
961
962     $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
963 }
964
965 # Handle all 'clean' targets.
966 sub handle_clean
967 {
968     push (@clean, 'generic');
969     $output_rules .= &file_contents ('clean');
970
971     local ($target) = $recursive_install ? 'clean-am' : 'clean';
972     &do_one_clean_target ($target, 'mostly', '', @clean);
973     &do_one_clean_target ($target, '', 'mostly', @clean);
974     &do_one_clean_target ($target, 'dist', '', @clean);
975     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
976
977     local (@deps);
978     if ($recursive_install)
979     {
980         @deps = ('am', 'recursive');
981         &do_one_clean_target ('', 'mostly', '', @deps);
982         &do_one_clean_target ('', '', '', @deps);
983         &do_one_clean_target ('', 'dist', '', @deps);
984         &do_one_clean_target ('', 'maintainer-', '', @deps);
985     }
986 }
987
988 # Helper for handle_clean.
989 sub do_one_clean_target
990 {
991     local ($target, $name, $last_name, @deps) = @_;
992
993     # Special case: if target not passed, then don't generate
994     # dependency on next "lower" clean target (eg no
995     # clean<-mostlyclean derivation).  In this case the target is
996     # implicitly known to be 'clean'.
997     local ($flag) = $target;
998     if (!$flag)
999     {
1000         $target = 'clean';
1001     }
1002
1003     $output_rules .= $name . $target . ": ";
1004     if ($flag)
1005     {
1006         if ($last_name || $name ne 'mostly')
1007         {
1008             $output_rules .= $last_name . $target . " ";
1009         }
1010     }
1011
1012     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
1013                       . "\n");
1014
1015     # FIXME shouldn't we really print these messages before running
1016     # the dependencies?
1017     if ($name . $target eq 'maintainer-clean')
1018     {
1019         # Print a special warning.
1020         $output_rules .=
1021             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
1022              . "\t\@echo \"it deletes files that may require special "
1023              . "tools to rebuild.\"\n");
1024     }
1025     $output_rules .= "\n";
1026 }
1027
1028 ################################################################
1029
1030 # Read Makefile.am and set up %contents.  Simultaneously copy lines
1031 # from Makefile.am into $output_trailer or $output_vars as
1032 # appropriate.  NOTE we put rules in the trailer section.  We want
1033 # user rules to come after our generated stuff.
1034 sub read_am_file
1035 {
1036     local ($amfile) = @_;
1037
1038     if (! open (AMFILE, $amfile))
1039     {
1040         print STDERR "automake: couldn't open $amfile: $!\n";
1041         exit 1;
1042     }
1043
1044     local ($saw_bk) = 0;
1045     local ($was_rule) = 0;
1046     local ($last_var_name) = '';
1047
1048     while (<AMFILE>)
1049     {
1050         chop;
1051
1052         if ($saw_bk)
1053         {
1054             if ($was_rule)
1055             {
1056                 $output_trailer .= $_ . "\n";
1057             }
1058             else
1059             {
1060                 $output_vars .= $_ . "\n";
1061                 if (substr ($_, -1) eq "\\")
1062                 {
1063                     $contents{$last_var_name} .= substr ($_, 0,
1064                                                          length ($_) - 1);
1065                 }
1066                 else
1067                 {
1068                     $contents{$last_var_name} .= $_;
1069                 }
1070             }
1071         }
1072         elsif ($_ eq '@kr@')
1073         {
1074             # Special case: this means we want automatic
1075             # de-ANSI-fication.  FIXME think of a better way.
1076             $contents{'@kr@'} = 1;
1077         }
1078         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
1079         {
1080             # Found a rule.
1081             $was_rule = 1;
1082             # Value here doesn't matter; for targets we only note
1083             # existence.
1084             $contents{$1} = 1;
1085             $output_trailer .= $_ . "\n";
1086         }
1087         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
1088         {
1089             # Found a variable reference.
1090             $was_rule = 0;
1091             $last_var_name = $1;
1092             if (substr ($2, -1) eq "\\")
1093             {
1094                 $contents{$1} = substr ($2, 0, length ($2) - 1);
1095             }
1096             else
1097             {
1098                 $contents{$1} = $2;
1099             }
1100             $output_vars .= $_ . "\n";
1101         }
1102         elsif (m/^$/)
1103         {
1104             # Special rule: if looking at a blank line, append it to
1105             # whatever we saw last.
1106             if ($was_rule)
1107             {
1108                 $output_trailer .= "\n";
1109             }
1110             else
1111             {
1112                 $output_vars .= "\n";
1113             }
1114         }
1115         else
1116         {
1117             # This isn't an error; it is probably a continued rule.
1118             # In fact, this is what we assume.
1119             $output_trailer .= $_ . "\n";
1120         }
1121
1122         $saw_bk = (substr ($_, -1) eq "\\");
1123     }
1124
1125     # Include some space after user code.
1126     $output_vars .= "\n";
1127     $output_trailer .= "\n";
1128 }
1129
1130 ################################################################
1131
1132 # Initialize global variables.
1133 sub init_globals
1134 {
1135     # Helper text for dealing with man pages.
1136     $install_man_format =
1137     '   sect=@SECTION@;                         \\
1138         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1139         echo installing @MAN@ as $(mandir)/man$$sect/$$inst; \\
1140         $(INSTALL_DATA) $(srcdir)/@MAN@ $(mandir)/man$$sect/$$inst; \\
1141         if test -d $(mandir)/cat$$sect; then    \\
1142           echo formatting @MAN@ as $(mandir)/cat$$sect/$$inst;  \\
1143           $(NROFF) -man $(srcdir)/@MAN@ > $(mandir)/cat$$sect/$$inst; \\
1144         else                                    \\
1145           true;                                 \\
1146         fi
1147 ';
1148
1149     $uninstall_man_format =
1150     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1151         rm -f $(mandir)/man@SECTION@/$$inst $(mandir)/cat@SECTION@/$$inst
1152 ';
1153
1154     # Commonly found files we look for and automatically include in
1155     # DIST_FILES.
1156     @common_files =
1157         (
1158          "THANKS", "TODO", "README", "NEWS", "COPYING", "COPYING.LIB",
1159          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
1160          "config.guess", "config.sub"
1161          );
1162
1163     # Commonly used files we auto-include, but only sometimes.
1164     @common_sometimes =
1165         (
1166          "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
1167          "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
1168          "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
1169          );
1170
1171     $USAGE = "\
1172   --amdir=DIR           directory storing config files
1173   --help                print this help, then exit
1174   --include-deps        include generated dependencies in Makefile.in
1175   --output-dir=DIR      put generated Makefile.in's into DIR
1176   --version             print version number, then exit\n";
1177
1178 }
1179
1180 ################################################################
1181
1182 # Return contents of a file from $am_dir.
1183 sub file_contents
1184 {
1185     local ($basename) = @_;
1186     local ($file) = $am_dir . '/' . $basename . '.am';
1187
1188     if (! open (FC_FILE, $file))
1189     {
1190         print STDERR "automake: installation error: cannot open \"$file\"\n";
1191         exit 1;
1192     }
1193
1194     # Lines starting with "##" are comments for developer use only.
1195     # Maybe this is a bad idea?
1196     local ($result) = '';
1197     while (<FC_FILE>)
1198     {
1199         $result .= $_ unless ( m/^##/);
1200     }
1201     close (FC_FILE);
1202     return $result;
1203 }
1204
1205 # Return contents of some Makefile.am variable.  Allow for AM_ style
1206 # overrides.
1207 sub am_variable
1208 {
1209     local ($varname) = @_;
1210
1211     return (defined ($contents{'AM_' . $varname})
1212             ? $contents{'AM_' . $varname}
1213             : $contents{$varname});
1214 }
1215
1216 ################################################################
1217
1218 # Verify that the file must exist in the current directory.
1219 sub require_file
1220 {
1221     local ($file) = @_;
1222     local ($fullfile) = $relative_dir . "/" . $file;
1223
1224     if (! -f $fullfile)
1225     {
1226         print STDERR "automake: required file \"$fullfile\" not found\n";
1227         $exit_status = 1;
1228     }
1229     else
1230     {
1231         push (@dist_common, $file);
1232     }
1233 }
1234
1235
1236 ################################################################
1237
1238 # Return directory name of file.
1239 sub dirname
1240 {
1241     local ($file) = @_;
1242     local ($sub);
1243
1244     ($sub = $file) =~ s,/+[^/]+,,g;
1245     if ($sub eq $file)
1246     {
1247         $sub = '.';
1248     }
1249
1250     return $sub;
1251 }
1252
1253 # Make a directory.
1254 sub mkdir
1255 {
1256     local ($dirname) = @_;
1257     system ("mkdir", $dirname);
1258 }
1259
1260 ################################################################
1261
1262 # Print usage information.
1263 sub usage
1264 {
1265     print "Usage: automake [OPTION] ... [Makefile]...\n";
1266     print $USAGE;
1267     print "\nFiles which are automatically distributed, if found:\n";
1268     $~ = "USAGE_FORMAT";
1269     local (@lcomm) = sort ((@common_files, @common_sometimes));
1270     local ($one, $two);
1271     while ($#lcomm >= 0)
1272     {
1273         $one = shift (@lcomm);
1274         $two = shift (@lcomm);
1275         write;
1276     }
1277
1278     exit 0;
1279 }
1280
1281 format USAGE_FORMAT =
1282   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1283   $one,                $two
1284 .