(generate_header): Don't use header.am.
[automake.git] / automake.in
blob6b7e126dc3382b13b6e036614299ef719f611845
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval "exec /usr/local/bin/perl -S $0 $*"
6     if $running_under_some_shell;
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994 Free Software Foundation, Inc.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
26 # Perl reimplementation by Tom Tromey <tromey@drip.colorado.edu>.
29 # Parameters set by configure.  Not to be changed.
30 $PACKAGE = "@PACKAGE@";
31 $VERSION = "@VERSION@";
32 $prefix = "@prefix@";
33 $am_dir = "@datadir@/@PACKAGE@";
35 # Commonly found files we look for and automatically include in
36 # DIST_FILES.
37 @common_files =
38     (
39      "THANKS", "TODO", "README", "NEWS", "COPYING", "COPYING.LIB",
40      "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
41      "config.guess", "config.sub"
42      );
44 # Commonly used files we auto-include, but only sometimes.
45 @common_sometimes =
46     (
47      "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
48      "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
49      "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
50      );
52 $USAGE = "  --amdir=DIR           directory storing config files
53   --help                print this help, then exit
54   --version             print version number, then exit
55   --include-deps        include generated dependencies in Makefile\n";
59 # This is TRUE if GNU make specific automatic dependency generation
60 # code should be included in generated Makefile.in.
61 $use_dependencies = 1;
63 # This holds our (eventual) exit status.  We don't actually exit until
64 # we have processed all input files.
65 $exit_status = 0;
67 # These two variables are used when generating each Makefile.in.  They
68 # hold the Makefile.in until it is ready to be printed.
69 $output_rules = '';
70 $output_vars = '';
71 $output_trailer = '';
73 # Suffixes found during a run.
74 @suffixes = ();
76 # This holds the contents of a Makefile.am, as parsed by read_am_file.
77 %contents = ();
79 # This holds the "relative directory" of the current Makefile.in.  Eg
80 # for src/Makefile.in, this is "src".
81 $relative_dir = '';
83 # This holds a list of files that are included in the distribution.
84 @dist_common = ();
86 # List of dependencies for the obvious targets.
87 @install_data = ();
88 @install_exec = ();
89 @uninstall = ();
91 @info = ();
92 @dvi = ();
93 @all = ('${ALL}');
94 @check = ();
95 @clean = ();
97 # TRUE if current directory holds any C source files.
98 $dir_holds_sources = 0;
100 # TRUE if install targets should work recursively.
101 $recursive_install = 0;
105 # Parse command line.
106 @input_files = &parse_arguments (@ARGV);
108 # Now do all the work on each file.
109 foreach $am_file (@input_files)
111     if (! -f ($am_file . '.am'))
112     {
113         print STDERR "automake: $am_file" . ".am: no such file\n";
114         $exit_status = 1;
115     }
116     else
117     {
118         &generate_makefile ($am_file);
119     }
122 exit $exit_status;
125 ################################################################
127 # Parse command line.
128 sub parse_arguments
130     local (@arglist) = @_;
131     local (@make_list);
133     while ($#arglist >= 0)
134     {
135         if ($arglist[0] eq "--version")
136         {
137             print "Automake version $VERSION\n";
138             exit 0;
139         }
140         elsif ($arglist[0] eq "--help")
141         {
142             &usage;
143         }
144         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
145         {
146             $am_dir = $1;
147         }
148         elsif ($arglist[0] eq '--amdir')
149         {
150             if ($#arglist == 1)
151             {
152                 print STDERR
153                     "automake: no argument given for option \`$arglist[0]'\n";
154                 exit 1;
155             }
156             shift (@arglist);
157             $am_dir = $arglist[0];
158         }
159         elsif ($arglist[0] eq '--include-deps')
160         {
161             $use_dependencies = 0;
162         }
163         elsif ($arglist[0] eq '--')
164         {
165             # Stop option processing.
166             shift (@arglist);
167             push (@make_list, @arglist);
168             last;
169         }
170         elsif ($arglist[0] =~ /^-/)
171         {
172             print STDERR "automake: unrecognized option -- \`$arglist[0]'\n";
173             exit 1;
174         }
175         else
176         {
177             push (@make_list, $arglist[0]);
178         }
180         shift (@arglist);
181     }
183     if ($#make_list < 0)
184     {
185         # Look around for some files.
186         push (@make_list, 'Makefile') if -f 'Makefile.am';
188         foreach (<*/Makefile.am>)
189         {
190             s/\.am$//;
191             push (@make_list, $_);
192         }
194         if ($#make_list >= 0)
195         {
196             print "automake: using ", join (' ', @make_list), "\n";
197         }
198         else
199         {
200             print STDERR "automake: no \"Makefile.am\" found or specified\n";
201             exit 1;
202         }
203     }
205     return (@make_list);
208 ################################################################
210 # Generate a Makefile.in given the name of the corresponding Makefile.
211 sub generate_makefile
213     local ($makefile) = @_;
214     
215     print "creating ", $makefile, ".in\n";
217     $relative_dir = &dirname ($makefile);
218     $output_rules = '';
219     $output_vars = '';
220     $output_trailer = '';
221     @suffixes = ();
222     %contents = ();
223     @dist_common = ('Makefile.in', 'Makefile.am');
224     @install_data = ();
225     @install_exec = ();
226     @uninstall = ();
227     $dir_holds_sources = 0;
228     $recursive_install = 0;
229     @info = ();
230     @dvi = ();
231     @all = ('${ALL}');
232     @check = ();
233     @clean = ();
235     # Generate header before reading .am file.  The header must come
236     # before anything else, and read_am_file copies code into the
237     # output.
238     &generate_header;
240     # This is always the default target.  This gives us freedom to do
241     # things in whatever order is convenient.
242     $output_rules .= "default: all\n\n";
244     &read_am_file ($makefile . '.am');
246     # Program stuff.
247     local ($programs) = (defined ($contents{'AM_PROGRAMS'})
248                          ? $contents{'AM_PROGRAMS'}
249                          : $contents{'PROGRAMS'});
250     local ($libprograms) = (defined ($contents{'AM_LIBPROGRAMS'})
251                             ? $contents{'AM_LIBPROGRAMS'}
252                             : $contents{'LIBPROGRAMS'});
253     local ($libraries) = (defined ($contents{'AM_LIBRARIES'})
254                           ? $contents{'AM_LIBRARIES'}
255                           : $contents{'LIBRARIES'});
256     local ($scripts) = (defined ($contents{'AM_SCRIPTS'})
257                         ? $contents{'AM_SCRIPTS'}
258                         : $contents{'SCRIPTS'});
259     local ($libscripts) = (defined ($contents{'AM_LIBSCRIPTS'})
260                            ? $contents{'AM_LIBSCRIPTS'}
261                            : $contents{'LIBSCRIPTS'});
263     &handle_programs ($programs, $libprograms, $libraries);
264     &handle_scripts ($scripts, $libscripts);
265     &handle_libraries ($libraries);
267     &handle_texinfo;
268     &handle_man_pages;
269     &handle_data;
270     &handle_subdirs;
271     &handle_configure;
272     &handle_tags;
273     &handle_dist;
274     &handle_dependencies;
275     &handle_footer;
276     &handle_merge_targets;
277     &handle_clean;
279     if (! open (GM_FILE, "> " . $makefile . ".in"))
280     {
281         print STDERR "automake: cannot open ", $makefile, ".in: ", $!, "\n";
282         $exit_status = 1;
283         return;
284     }
286     print GM_FILE $output_vars;
287     print GM_FILE $output_rules;
288     print GM_FILE $output_trailer;
290     close (GM_FILE);
293 ################################################################
295 # Generate header of Makefile.in.
296 sub generate_header
298     $output_vars =
299         ($output_vars
300          . "# Makefile.in generated automatically by automake "
301          . $VERSION
302          . " from Makefile.am\n");
304     $output_vars = $output_vars . &file_contents ('header-vars');
307 # Handle C programs and libraries.
308 sub handle_programs
310     local ($programs, $libprograms, $libraries) = @_;
312     if (!$programs && !$libprograms && !$libraries)
313     {
314         # None exist.
315         return;
316     }
317     $dir_holds_sources = 1;
319     # Boilerplate.
320     $output_vars .= &file_contents ('compile-vars');
321     $output_rules .= &file_contents ('compile');
323     # Check for automatic de-ANSI-fication.
324     local ($obj) = '.o';
325     push (@suffixes, '.c', '.o');
326     push (@clean, 'compile');
328     if (defined $contents{'@kr@'})
329     {
330         $obj = '.${kr}o';
331         push (@suffixes, '._c', '._o');
333         &require_file ('ansi2knr.c');
334         &require_file ('ansi2knr.1');
336         $output_vars .= &file_contents ('kr-vars');
337         $output_rules .= &file_contents ('compile-kr');
338         $output_rules .= &file_contents ('clean-kr');
340         push (@clean, 'kr');
341     }
343     local (@sources, @objects);
344     push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
345     push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
347     local ($one_file);
348     foreach $one_file (split (' ', ($programs . ' '
349                                     . $libprograms . ' '
350                                     . $libraries)))
351     {
352         # Look for file_SOURCES and file_OBJECTS.  FIXME file_OBJECTS
353         # should probably not be used(?)
354         if (defined $contents{$one_file . "_SOURCES"})
355         {
356             if (! defined $contents{$one_file . "_OBJECTS"})
357             {
358                 # Turn sources into objects.
359                 $_ = $contents{$one_file . "_SOURCES"};
361                 # Ugh: Perl syntax vs Emacs.
362                 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
364                 s/\.cc/$obj/g;
365                 s/$krc1/$obj/g;
366                 s/$krc2/$obj/g;
367                 s/\.[cCmylfs]/$obj/g;
369                 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
370             }
372             push (@sources, '${' . $one_file . "_SOURCES}");
373             push (@objects, '${' . $one_file . "_OBJECTS}");
374         }
375         else
376         {
377             $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
378                              . $one_file . "_OBJECTS = ". $one_file
379                              . $obj . "\n");
380             push (@sources, $one_file . '.c');
381             push (@objects, $one_file . $obj);
382         }
384         if (defined $contents{'CONFIG_HEADER'})
385         {
386             $output_rules .= ('$(' . $one_file . "_OBJECTS): "
387                               . $contents{'CONFIG_HEADER'} . "\n");
388         }
389     }
391     $output_vars .= "\n";
393     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
394     # on this.
395     $contents{'SOURCES'} = join (' ', @sources);
396     $contents{'OBJECTS'} = join (' ', @objects);
398     # Some boilerplate, and install rules.
399     if ($programs)
400     {
401         $output_rules .= &file_contents ('programs');
402         push (@install_exec, "install-programs");
403         push (@uninstall, 'uninstall-programs');
404         push (@clean, 'programs');
405     }
406     if ($libprograms)
407     {
408         $output_rules .= &file_contents ('libprograms');
409         push (@install_exec, 'install-libprograms');
410         push (@uninstall, 'uninstall-libprograms');
411         push (@clean, 'libprograms');
412     }
414     # Handle linking.
415     if ($programs || $libprograms)
416     {
417         local ($fcont) = &file_contents ('program');
418         local ($munge);
419         foreach $one_file (split (' ', $programs . ' ' . $libprograms))
420         {
421             if (! defined $contents{$one_file . "_LDADD"})
422             {
423                 # User didn't define prog_LDADD override.  So do it.
424                 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
425             }
427             ($munge = $fcont) =~ s/@PROGRAM@/$one_file/g;
428             $output_rules .= $munge;
429         }
430     }
433 # Handle libraries.
434 sub handle_libraries
436     local ($libraries) = @_;
438     return if (!$libraries);
440     local (@liblist) = split (' ', $libraries);
442     $output_rules .= &file_contents ('libraries');
443     local ($onefile) = &file_contents ('library');
444     local ($onelib, $munge);
445     foreach $onelib (@liblist)
446     {
447         ($munge = $onefile) =~ s/@LIBRARY@/$onelib/g;
448         $output_rules .= $munge;
449     }
451     # Turn "foo" into "libfoo.a" and include macro definition.
452     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
453     $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
454                      . &file_contents ('libraries-vars'));
456     push (@install_exec, 'install-libraries');
457     push (@uninstall, 'uninstall-libraries');
458     push (@clean, 'libraries');
461 # Handle scripts.
462 sub handle_scripts
464     local ($scripts, $libscripts) = @_;
466     if ($scripts)
467     {
468         $output_rules .= &file_contents ('scripts');
469         push (@install_exec, 'install-scripts');
470         push (@uninstall, 'uninstall-scripts');
471         push (@clean, 'scripts');
472     }
474     if ($libscripts)
475     {
476         $output_rules .= &file_contents ('libscripts');
477         push (@install_exec, 'install-libscripts');
478         push (@uninstall, 'uninstall-libscripts');
479         push (@clean, 'libscripts');
480     }
483 # Handle all Texinfo source.
484 sub handle_texinfo
486     return if (! defined $contents{TEXINFOS});
488     local (@texis) = split (' ', $contents{TEXINFOS});
489     if ($#texis > 0)
490     {
491         print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
492         $exit_status = 1;
493         return;
494     }
496     local ($infobase);
497     ($infobase = $texis[0]) =~ s/\.texi$//;
499     # If 'version.texi' is referenced by input file, then include
500     # automatic versioning capability.
501     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
502             . " > /dev/null 2>&1");
503     if (!$?)
504     {
505         # Got a hit.
506         push (@texis, 'version.texi');
507         push (@dist_common, 'version.texi', 'stamp-vti');
508         push (@clean, 'vti');
510         local ($tfile);
511         ($tfile = &file_contents ('texi-version')) =~ s/@TEXI@/$texis[0]/g;
512         $output_rules = $output_rules . $tfile;
514         &require_file ('mdate-sh');
515     }
517     # If user specified file_TEXINFOS, then use that as explicit
518     # dependency list.
519     if (defined $contents{$infobase . "_TEXINFOS"})
520     {
521         push (@texis, "\$" . $infobase . '_TEXINFOS');
522         push (@dist_common, "\$" . $infobase . '_TEXINFOS');
523     }
525     if ($#texis >= 0)
526     {
527         $output_rules = ($output_rules . $infobase . ".info: "
528                          . join (' ', @texis) . "\n\n");
529     }
531     # Some boilerplate.
532     $output_vars = $output_vars . &file_contents ('texinfos-vars');
533     $output_rules = $output_rules . &file_contents ('texinfos');
535     push (@suffixes, '.texi', '.info', '.dvi');
536     push (@uninstall, 'uninstall-info');
537     push (@clean, 'info');
538     push (@info, '$(INFO_DEPS)');
539     push (@dvi, '$(DVIS)');
540     # Make sure documentation is made and installed first.
541     unshift (@install_data, 'install-info');
542     unshift (@all, 'info');
544     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
545                      . "INFO_DEPS = " . $infobase . ".info\n"
546                      . "DVIS = " . $infobase . ".dvi\n\n");
548     # Do some error checking.
549     &require_file ('texinfo.tex');
552 # Handle any man pages.
553 sub handle_man_pages
555     return if (! defined $contents{'MANS'});
557     $output_vars .= &file_contents ('mans-vars');
558     $output_rules .= &file_contents ('mans');
560     push (@install_data, 'install-man');
561     push (@uninstall, 'uninstall-man');
564 # Handle DATA and PACKAGEDATA.
565 sub handle_data
567     if (defined $contents{'DATA'})
568     {
569         $output_rules .= &file_contents ('data');
570         push (@install_data, 'install-ddata');
571         push (@uninstall, 'uninstall-ddata');
572     }
574     if (defined $contents{'PACKAGEDATA'})
575     {
576         $output_rules .= &file_contents ('packagedata');
577         push (@install_data, 'install-pdata');
578         push (@uninstall, 'uninstall-pdata');
579     }
582 # Handle TAGS.
583 sub handle_tags
585     local ($tagging) = 0;
587     if (defined ($contents{'SUBDIRS'}))
588     {
589         $output_rules .= &file_contents ('tags');
590         $tagging = 1;
591     }
592     elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
593     {
594         $output_rules .= &file_contents ('tags-subd');
595         $tagging = 1;
596     }
598     if ($tagging)
599     {
600         $output_rules .= &file_contents ('tags-clean');
601         push (@clean, 'tags');
602     }
605 # Handle 'dist' target.
606 sub handle_dist
608     # Look for common files that should be included in distribution.
609     local ($cfile);
610     foreach $cfile (@common_files)
611     {
612         if (-f ($relative_dir . "/" . $cfile))
613         {
614             push (@dist_common, $cfile);
615         }
616     }
618     $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
620     # Some boilerplate.
621     $output_vars .= &file_contents ('dist-vars');
622     if ($relative_dir ne '.')
623     {
624         # In a subdirectory.
625         $output_vars .= "subdir = " . $relative_dir . "\n\n";
626         $output_rules .= &file_contents ('dist-subd');
627     }
628     else
629     {
630         $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
631                                          ? 'dist-subd-top'
632                                          : 'dist');
633     }
636 # Handle auto-dependency code.
637 sub handle_dependencies
639     if ($use_dependencies)
640     {
641         # Include GNU-make-specific auto-dep code.
642         if ($dir_holds_sources)
643         {
644             $output_rules .= &file_contents ('depend');
645         }
646     }
647     else
648     {
649         # Include any auto-generated deps that are present.
650         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
651         {
652             local ($depfile);
653             local ($gpat) = $relative_dir . "/.deps/*.P";
655             foreach $depfile (<${gpat}>)
656             {
657                 if (! open (DEP_FILE, $depfile))
658                 {
659                     print STDERR "automake: couldn't open $depfile: $!\n";
660                     next;
661                 }
663                 # Slurp entire file.
664                 $output_rules .= join ('', <DEP_FILE>);
666                 close (DEP_FILE);
667             }
669             $output_rules .= "\n";
670         }       
671     }
674 # Handle subdirectories.
675 sub handle_subdirs
677     return if (! defined ($contents{'SUBDIRS'}));
679     $output_rules .= &file_contents ('subdirs');
681     push (@all, "all-recursive");
682     push (@check, "check-recursive");
683     push (@info, "info-recursive");
684     push (@dvi, "dvi-recursive");
686     $recursive_install = 1;
689 # Handle remaking and configure stuff.
690 sub handle_configure
692     if ($relative_dir ne '.')
693     {
694         # In subdirectory.
695         $output_rules .= &file_contents ('remake-subd');
696     }
697     else
698     {
699         if (-f 'aclocal.m4')
700         {
701             $output_vars .= "ACLOCAL = aclocal.m4\n";
702             push (@dist_common, 'aclocal.m4');
703         }
704         $output_rules .= &file_contents ('remake');
706         # Look for some files we need.
707         &require_file ('install-sh');
708         &require_file ('mkinstalldirs');
709     }
711     if (defined ($configure{'CONFIG_HEADER'})
712         && $contents{'CONFIG_HEADER'} !~ m,/,)
713     {
714         # Header defined and in this directory.
715         if (-f 'acconfig.h')
716         {
717             $output_vars .= "ACCONFIG = acconfig.h\n";
718             push (@dist_common, 'acconfig.h');
719         }
720         if (-f 'config.h.top')
721         {
722             $output_vars .= "CONFIG_TOP = config.h.top\n";
723             push (@dist_common, 'config.h.top');
724         }
725         if (-f 'config.h.bot')
726         {
727             $output_vars .= "CONFIG_BOT = config.h.bot\n";
728             push (@dist_common, 'config.h.bot');
729         }
731         push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
733         $output_rules .= &file_contents ('remake-hdr');
734     }
737 # Handle footer elements.
738 sub handle_footer
740     if ($contents{'SOURCES'})
741     {
742         $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
743     }
744     if ($contents{'OBJECTS'})
745     {
746         $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
747     }
748     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
749     {
750         $output_vars .= "\n";
751     }
753     $output_trailer .= ".SUFFIXES:\n";
754     if ($#suffixes >= 0)
755     {
756         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
757     }
758     $output_trailer .= "\n" . &file_contents ('footer');
761 # There are several targets which need to be merged.  This is because
762 # their complete definition is compiled from many parts.  Note that we
763 # avoid double colon rules, otherwise we'd use them instead.
764 sub handle_merge_targets
766     &do_one_merge_target ('all', @all);
767     &do_one_merge_target ('info', @info);
768     &do_one_merge_target ('dvi', @dvi);
769     &do_one_merge_target ('check', @check);
771     # Handle the various install targets specially.  We do this so
772     # that (eg) "make install-exec" will run "install-exec-recursive"
773     # if required, but "make install" won't run it twice.  Step one is
774     # to see if the user specified local versions of any of the
775     # targets we handle.
776     if (defined $contents{'install-exec-local'})
777     {
778         push (@install_exec, 'install-exec-local');
779     }
780     if (defined $contents{'install-data-local'})
781     {
782         push (@install_data, 'install-data-local');
783     }
784     if (defined $contents{'uninstall-local'})
785     {
786         push (@uninstall, 'uninstall-local');
787     }
789     if (defined $contents{'install-local'})
790     {
791         print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
792     }
794     # Step two: if we are doing recursive makes, write out the
795     # appropriate rules.
796     local (@install);
797     if ($recursive_install)
798     {
799         push (@install, 'install-recursive');
800         push (@uninstall, 'uninstall-recursive');
802         if ($#install_exec >= 0)
803         {
804             $output_rules .= ('install-exec-am: '
805                               . join (' ', @install_exec)
806                               . "\n\n");
807             @install_exec = ('install-exec-recursive', 'install-exec-am');
808             push (@install, 'install-exec-am');
809         }
810         if ($#install_data >= 0)
811         {
812             $output_rules .= ('install-data-am: '
813                               . join (' ', @install_data)
814                               . "\n\n");
815             @install_data = ('install-data-recursive', 'install-data-am');
816             push (@install, 'install-data-am');
817         }
818         if ($#uninstall >= 0)
819         {
820             $output_rules .= ('uninstall-am: '
821                               . join (' ', @uninstall)
822                               . "\n\n");
823             @uninstall = ('uninstall-recursive', 'uninstall-am');
824         }
825     }
827     # Step three: print definitions users can use.
828     if ($#install_exec >= 0)
829     {
830         $output_rules .= ("install-exec: "
831                           . join (' ', @install_exec)
832                           . "\n\n");
833         push (@install, 'install-exec') if (!$recursive_install);
834     }
835     if ($#install_data >= 0)
836     {
837         $output_rules .= ("install-data: "
838                           . join (' ', @install_data)
839                           . "\n\n");
840         push (@install, 'install-data') if (!$recursive_install);
841     }
843     $output_rules .= ('install: '
844                       . join (' ', @install)
845                       . "\n\n"
846                       . 'uninstall: '
847                       . join (' ', @uninstall)
848                       . "\n\n");
851 # Helper for handle_merge_targets.
852 sub do_one_merge_target
854     local ($name, @values) = @_;
856     if (defined $contents{$name . '-local'})
857     {
858         # User defined local form of target.  So include it.
859         push (@values, $name . '-local');
860     }
862     $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
865 # Handle all 'clean' targets.
866 sub handle_clean
868     push (@clean, 'generic');
869     $output_rules .= &file_contents ('clean');
871     local ($target) = $recursive_install ? 'clean-am' : 'clean';
872     &do_one_clean_target ($target, 'mostly', '', @clean);
873     &do_one_clean_target ($target, '', 'mostly', @clean);
874     &do_one_clean_target ($target, 'dist', '', @clean);
875     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
877     local (@deps);
878     if ($recursive_install)
879     {
880         @deps = ('am', 'recursive');
881         &do_one_clean_target ('', 'mostly', '', @deps);
882         &do_one_clean_target ('', '', '', @deps);
883         &do_one_clean_target ('', 'dist', '', @deps);
884         &do_one_clean_target ('', 'maintainer-', '', @deps);
885     }
888 # Helper for handle_clean.
889 sub do_one_clean_target
891     local ($target, $name, $last_name, @deps) = @_;
893     # Special case: if target not passed, then don't generate
894     # dependency on next "lower" clean target (eg no
895     # clean<-mostlyclean derivation).  In this case the target is
896     # implicitly known to be 'clean'.
897     local ($flag) = $target;
898     if (!$flag)
899     {
900         $target = 'clean';
901     }
903     $output_rules .= $name . $target . ": ";
904     if ($flag)
905     {
906         if ($last_name || $name ne 'mostly')
907         {
908             $output_rules .= $last_name . $target . " ";
909         }
910     }
912     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
913                       . "\n");
915     # FIXME shouldn't we really print these messages before running
916     # the dependencies?
917     if ($name . $target eq 'maintainer-clean')
918     {
919         # Print a special warning.
920         $output_rules .=
921             ("\t@echo \"This command is intended for maintainers to use;\"\n"
922              . "\t@echo \"it deletes files that may require special "
923              . "tools to rebuild.\"\n");
924     }
925     $output_rules .= "\n";
928 ################################################################
930 # Read Makefile.am and set up %contents.  Simultaneously copy lines
931 # from Makefile.am into $output_trailer or $output_vars as
932 # appropriate.  NOTE we put rules in the trailer section.  We want
933 # user rules to come after our generated stuff.
934 sub read_am_file
936     local ($amfile) = @_;
938     if (! open (AMFILE, $amfile))
939     {
940         print STDERR "automake: couldn't open $amfile: $!\n";
941         exit 1;
942     }
944     local ($saw_bk) = 0;
945     local ($was_rule) = 0;
946     local ($last_var_name) = '';
948     while (<AMFILE>)
949     {
950         chop;
952         if ($saw_bk)
953         {
954             if ($was_rule)
955             {
956                 $output_trailer .= $_ . "\n";
957             }
958             else
959             {
960                 $output_vars .= $_ . "\n";
961                 if (substr ($_, -1) eq "\\")
962                 {
963                     $contents{$last_var_name} .= substr ($_, 0,
964                                                          length ($_) - 1);
965                 }
966                 else
967                 {
968                     $contents{$last_var_name} .= $_;
969                 }
970             }
971         }
972         elsif ($_ eq '@kr@')
973         {
974             # Special case: this means we want automatic
975             # de-ANSI-fication.  FIXME think of a better way.
976             $contents{'@kr@'} = 1;
977         }
978         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
979         {
980             # Found a rule.
981             $was_rule = 1;
982             # Value here doesn't matter; for targets we only note
983             # existence.
984             $contents{$1} = 1;
985             $output_trailer .= $_ . "\n";
986         }
987         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
988         {
989             # Found a variable reference.
990             $was_rule = 0;
991             $last_var_name = $1;
992             if (substr ($2, -1) eq "\\")
993             {
994                 $contents{$1} = substr ($2, 0, length ($2) - 1);
995             }
996             else
997             {
998                 $contents{$1} = $2;
999             }
1000             $output_vars .= $_ . "\n";
1001         }
1002         elsif (m/^$/)
1003         {
1004             # Special rule: if looking at a blank line, append it to
1005             # whatever we saw last.
1006             if ($was_rule)
1007             {
1008                 $output_trailer .= "\n";
1009             }
1010             else
1011             {
1012                 $output_vars .= "\n";
1013             }
1014         }
1015         else
1016         {
1017             # This isn't an error; it is probably a continued rule.
1018             # In fact, this is what we assume.
1019             $output_trailer .= $_ . "\n";
1020         }
1022         $saw_bk = (substr ($_, -1) eq "\\");
1023     }
1025     # Include some space after user code.
1026     $output_vars .= "\n";
1027     $output_trailer .= "\n";
1031 ################################################################
1033 # Return contents of a file from $am_dir.
1034 sub file_contents
1036     local ($basename) = @_;
1037     local ($file) = $am_dir . '/' . $basename . '.am';
1039     if (! open (FC_FILE, $file))
1040     {
1041         print STDERR "automake: installation error: cannot open \"$file\"\n";
1042         exit 1;
1043     }
1045     # Yes, we really want to slurp it.
1046     local ($results) = join ('', <FC_FILE>);
1048     close (FC_FILE);
1050     return $results;
1053 ################################################################
1055 # Verify that the file must exist in the current directory.
1056 sub require_file
1058     local ($file) = @_;
1059     local ($fullfile) = $relative_dir . "/" . $file;
1061     if (! -f $fullfile)
1062     {
1063         print STDERR "automake: required file \"$fullfile\" not found\n";
1064         $exit_status = 1;
1065     }
1066     else
1067     {
1068         push (@dist_common, $file);
1069     }
1073 ################################################################
1075 # Return directory name of file.
1076 sub dirname
1078     local ($file) = @_;
1079     local ($sub);
1081     ($sub = $file) =~ s,/+[^/]+,,g;
1082     if ($sub eq $file)
1083     {
1084         $sub = '.';
1085     }
1087     return $sub;
1090 ################################################################
1092 # Print usage information.
1093 sub usage
1095     print "Usage: automake [OPTION] ... [Makefile]...\n";
1096     print $USAGE;
1097     print "\nFiles which are automatically distributed, if found:\n";
1098     $~ = "USAGE_FORMAT";
1099     local (@lcomm) = sort ((@common_files, @common_sometimes));
1100     local ($one, $two);
1101     while ($#lcomm >= 0)
1102     {
1103         $one = shift (@lcomm);
1104         $two = shift (@lcomm);
1105         write;
1106     }
1108     exit 0;
1111 format USAGE_FORMAT =
1112   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1113   $one,                $two