(handle_dependencies): Put trailing newline after all deps.
[automake.git] / automake.in
blob865cbf5f5c819a0c853eb86c19bb36cb436dc27d
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');
305     $output_rules = $output_rules . &file_contents ('header');
308 # Handle C programs and libraries.
309 sub handle_programs
311     local ($programs, $libprograms, $libraries) = @_;
313     if (!$programs && !$libprograms && !$libraries)
314     {
315         # None exist.
316         return;
317     }
318     $dir_holds_sources = 1;
320     # Boilerplate.
321     $output_vars .= &file_contents ('compile-vars');
322     $output_rules .= &file_contents ('compile');
324     # Check for automatic de-ANSI-fication.
325     local ($obj) = '.o';
326     push (@suffixes, '.c', '.o');
327     if (defined $contents{'@kr@'})
328     {
329         $obj = '.${kr}o';
330         push (@suffixes, '._c', '._o');
332         &require_file ('ansi2knr.c');
333         &require_file ('ansi2knr.1');
335         $output_vars .= &file_contents ('kr-vars');
336         $output_rules .= &file_contents ('compile-kr');
337         $output_rules .= &file_contents ('clean-kr');
339         push (@clean, 'kr');
340     }
342     local (@sources, @objects);
343     push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
344     push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
346     local ($one_file);
347     foreach $one_file (split (' ', ($programs . ' '
348                                     . $libprograms . ' '
349                                     . $libraries)))
350     {
351         # Look for file_SOURCES and file_OBJECTS.  FIXME file_OBJECTS
352         # should probably not be used(?)
353         if (defined $contents{$one_file . "_SOURCES"})
354         {
355             if (! defined $contents{$one_file . "_OBJECTS"})
356             {
357                 # Turn sources into objects.
358                 $_ = $contents{$one_file . "_SOURCES"};
360                 # Ugh: Perl syntax vs Emacs.
361                 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
363                 s/\.cc/$obj/g;
364                 s/$krc1/$obj/g;
365                 s/$krc2/$obj/g;
366                 s/\.[cCmylfs]/$obj/g;
368                 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
369             }
371             push (@sources, '${' . $one_file . "_SOURCES}");
372             push (@objects, '${' . $one_file . "_OBJECTS}");
373         }
374         else
375         {
376             $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
377                              . $one_file . "_OBJECTS = ". $one_file
378                              . $obj . "\n");
379             push (@sources, $one_file . '.c');
380             push (@objects, $one_file . $obj);
381         }
383         if (defined $contents{'CONFIG_HEADER'})
384         {
385             $output_rules .= ('$(' . $one_file . "_OBJECTS): "
386                               . $contents{'CONFIG_HEADER'} . "\n");
387         }
388     }
390     $output_vars .= "\n";
392     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
393     # on this.
394     $contents{'SOURCES'} = join (' ', @sources);
395     $contents{'OBJECTS'} = join (' ', @objects);
397     # Some boilerplate, and install rules.
398     if ($programs)
399     {
400         $output_rules .= &file_contents ('programs');
401         push (@install_exec, "install-programs");
402         push (@uninstall, 'uninstall-programs');
403         push (@clean, 'programs');
404     }
405     if ($libprograms)
406     {
407         $output_rules .= &file_contents ('libprograms');
408         push (@install_exec, 'install-libprograms');
409         push (@uninstall, 'uninstall-libprograms');
410         push (@clean, 'libprograms');
411     }
413     # Handle linking.
414     if ($programs || $libprograms)
415     {
416         local ($fcont) = &file_contents ('program');
417         local ($munge);
418         foreach $one_file (split (' ', $programs . ' ' . $libprograms))
419         {
420             if (! defined $contents{$one_file . "_LDADD"})
421             {
422                 # User didn't define prog_LDADD override.  So do it.
423                 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
424             }
426             ($munge = $fcont) =~ s/@PROGRAM@/$one_file/g;
427             $output_rules .= $munge;
428         }
429     }
432 # Handle libraries.
433 sub handle_libraries
435     local ($libraries) = @_;
437     return if (!$libraries);
439     local (@liblist) = split (' ', $libraries);
441     $output_rules .= &file_contents ('libraries');
442     local ($onefile) = &file_contents ('library');
443     local ($onelib, $munge);
444     foreach $onelib (@liblist)
445     {
446         ($munge = $onefile) =~ s/@LIBRARY@/$onelib/g;
447         $output_rules .= $munge;
448     }
450     # Turn "foo" into "libfoo.a" and include macro definition.
451     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
452     $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
453                      . &file_contents ('libraries-vars'));
455     push (@install_exec, 'install-libraries');
456     push (@uninstall, 'uninstall-libraries');
457     push (@clean, 'libraries');
460 # Handle scripts.
461 sub handle_scripts
463     local ($scripts, $libscripts) = @_;
465     if ($scripts)
466     {
467         $output_rules .= &file_contents ('scripts');
468         push (@install_exec, 'install-scripts');
469         push (@uninstall, 'uninstall-scripts');
470         push (@clean, 'scripts');
471     }
473     if ($libscripts)
474     {
475         $output_rules .= &file_contents ('libscripts');
476         push (@install_exec, 'install-libscripts');
477         push (@uninstall, 'uninstall-libscripts');
478         push (@clean, 'libscripts');
479     }
482 # Handle all Texinfo source.
483 sub handle_texinfo
485     return if (! defined $contents{TEXINFOS});
487     local (@texis) = split (' ', $contents{TEXINFOS});
488     if ($#texis > 0)
489     {
490         print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
491         $exit_status = 1;
492         return;
493     }
495     local ($infobase);
496     ($infobase = $texis[0]) =~ s/\.texi$//;
498     # If 'version.texi' is referenced by input file, then include
499     # automatic versioning capability.
500     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
501             . " > /dev/null 2>&1");
502     if (!$?)
503     {
504         # Got a hit.
505         push (@texis, 'version.texi');
506         push (@dist_common, 'version.texi', 'stamp-vti');
507         push (@clean, 'vti');
509         local ($tfile);
510         ($tfile = &file_contents ('texi-version')) =~ s/@TEXI@/$texis[0]/g;
511         $output_rules = $output_rules . $tfile;
513         &require_file ('mdate-sh');
514     }
516     # If user specified file_TEXINFOS, then use that as explicit
517     # dependency list.
518     if (defined $contents{$infobase . "_TEXINFOS"})
519     {
520         push (@texis, "\$" . $infobase . '_TEXINFOS');
521         push (@dist_common, "\$" . $infobase . '_TEXINFOS');
522     }
524     if ($#texis >= 0)
525     {
526         $output_rules = ($output_rules . $infobase . ".info: "
527                          . join (' ', @texis) . "\n\n");
528     }
530     # Some boilerplate.
531     $output_vars = $output_vars . &file_contents ('texinfos-vars');
532     $output_rules = $output_rules . &file_contents ('texinfos');
534     push (@suffixes, '.texi', '.info', '.dvi');
535     push (@uninstall, 'uninstall-info');
536     push (@clean, 'info');
537     push (@info, '$(INFO_DEPS)');
538     push (@dvi, '$(DVIS)');
539     # Make sure documentation is made and installed first.
540     unshift (@install_data, 'install-info');
541     unshift (@all, 'info');
543     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
544                      . "INFO_DEPS = " . $infobase . ".info\n"
545                      . "DVIS = " . $infobase . ".dvi\n\n");
547     # Do some error checking.
548     &require_file ('texinfo.tex');
551 # Handle any man pages.
552 sub handle_man_pages
554     return if (! defined $contents{'MANS'});
556     $output_vars .= &file_contents ('mans-vars');
557     $output_rules .= &file_contents ('mans');
559     push (@install_data, 'install-man');
560     push (@uninstall, 'uninstall-man');
563 # Handle DATA and PACKAGEDATA.
564 sub handle_data
566     if (defined $contents{'DATA'})
567     {
568         $output_rules .= &file_contents ('data');
569         push (@install_data, 'install-ddata');
570         push (@uninstall, 'uninstall-ddata');
571     }
573     if (defined $contents{'PACKAGEDATA'})
574     {
575         $output_rules .= &file_contents ('packagedata');
576         push (@install_data, 'install-pdata');
577         push (@uninstall, 'uninstall-pdata');
578     }
581 # Handle TAGS.
582 sub handle_tags
584     local ($tagging) = 0;
586     if (defined ($contents{'SUBDIRS'}))
587     {
588         $output_rules .= &file_contents ('tags');
589         $tagging = 1;
590     }
591     elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
592     {
593         $output_rules .= &file_contents ('tags-subd');
594         $tagging = 1;
595     }
597     if ($tagging)
598     {
599         $output_rules .= &file_contents ('tags-clean');
600         push (@clean, 'tags');
601     }
604 # Handle 'dist' target.
605 sub handle_dist
607     # Look for common files that should be included in distribution.
608     local ($cfile);
609     foreach $cfile (@common_files)
610     {
611         if (-f ($relative_dir . "/" . $cfile))
612         {
613             push (@dist_common, $cfile);
614         }
615     }
617     $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
619     # Some boilerplate.
620     if ($relative_dir ne '.')
621     {
622         # In a subdirectory.
623         $output_vars .= (&file_contents ('dist-subd-vars')
624                          . "subdir = " . $relative_dir . "\n\n");
625         $output_rules .= &file_contents ('dist-subd');
626     }
627     else
628     {
629         $output_vars .= &file_contents ('dist-vars');
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     local ($target) = $recursive_install ? 'clean-am' : 'clean';
869     &do_one_clean_target ($target, 'mostly', '', @clean);
870     &do_one_clean_target ($target, '', 'mostly', @clean);
871     &do_one_clean_target ($target, 'dist', '', @clean);
872     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
874     local (@deps);
875     if ($recursive_install)
876     {
877         @deps = ('am', 'recursive');
878         &do_one_clean_target ('', 'mostly', '', @deps);
879         &do_one_clean_target ('', '', '', @deps);
880         &do_one_clean_target ('', 'dist', '', @deps);
881         &do_one_clean_target ('', 'maintainer-', '', @deps);
882     }
885 # Helper for handle_clean.
886 sub do_one_clean_target
888     local ($target, $name, $last_name, @deps) = @_;
890     # Special case: if target not passed, then don't generate
891     # dependency on next "lower" clean target (eg no
892     # clean<-mostlyclean derivation).  In this case the target is
893     # implicitly known to be 'clean'.
894     local ($flag) = $target;
895     if (!$flag)
896     {
897         $target = 'clean';
898     }
900     $output_rules .= $name . $target . ": ";
901     if ($flag)
902     {
903         if ($last_name || $name ne 'mostly')
904         {
905             $output_rules .= $last_name . $target . " ";
906         }
907     }
909     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
910                       . "\n\n");
913 ################################################################
915 # Read Makefile.am and set up %contents.  Simultaneously copy lines
916 # from Makefile.am into $output_trailer or $output_vars as
917 # appropriate.  NOTE we put rules in the trailer section.  We want
918 # user rules to come after our generated stuff.
919 sub read_am_file
921     local ($amfile) = @_;
923     if (! open (AMFILE, $amfile))
924     {
925         print STDERR "automake: couldn't open $amfile: $!\n";
926         exit 1;
927     }
929     local ($saw_bk) = 0;
930     local ($was_rule) = 0;
931     local ($last_var_name) = '';
933     while (<AMFILE>)
934     {
935         chop;
937         if ($saw_bk)
938         {
939             if ($was_rule)
940             {
941                 $output_trailer .= $_ . "\n";
942             }
943             else
944             {
945                 $output_vars .= $_ . "\n";
946                 if (substr ($_, -1) eq "\\")
947                 {
948                     $contents{$last_var_name} .= substr ($_, 0,
949                                                          length ($_) - 1);
950                 }
951                 else
952                 {
953                     $contents{$last_var_name} .= $_;
954                 }
955             }
956         }
957         elsif ($_ eq '@kr@')
958         {
959             # Special case: this means we want automatic
960             # de-ANSI-fication.  FIXME think of a better way.
961             $contents{'@kr@'} = 1;
962         }
963         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
964         {
965             # Found a rule.
966             $was_rule = 1;
967             # Value here doesn't matter; for targets we only note
968             # existence.
969             $contents{$1} = 1;
970             $output_trailer .= $_ . "\n";
971         }
972         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
973         {
974             # Found a variable reference.
975             $was_rule = 0;
976             $last_var_name = $1;
977             if (substr ($2, -1) eq "\\")
978             {
979                 $contents{$1} = substr ($2, 0, length ($2) - 1);
980             }
981             else
982             {
983                 $contents{$1} = $2;
984             }
985             $output_vars .= $_ . "\n";
986         }
987         elsif (m/^$/)
988         {
989             # Special rule: if looking at a blank line, append it to
990             # whatever we saw last.
991             if ($was_rule)
992             {
993                 $output_trailer .= "\n";
994             }
995             else
996             {
997                 $output_vars .= "\n";
998             }
999         }
1000         else
1001         {
1002             # This isn't an error; it is probably a continued rule.
1003             # In fact, this is what we assume.
1004             $output_trailer .= $_ . "\n";
1005         }
1007         $saw_bk = (substr ($_, -1) eq "\\");
1008     }
1010     # Include some space after user code.
1011     $output_vars .= "\n";
1012     $output_trailer .= "\n";
1016 ################################################################
1018 # Return contents of a file from $am_dir.
1019 sub file_contents
1021     local ($basename) = @_;
1022     local ($file) = $am_dir . '/' . $basename . '.am';
1024     if (! open (FC_FILE, $file))
1025     {
1026         print STDERR "automake: installation error: cannot open \"$file\"\n";
1027         exit 1;
1028     }
1030     # Yes, we really want to slurp it.
1031     local ($results) = join ('', <FC_FILE>);
1033     close (FC_FILE);
1035     return $results;
1038 ################################################################
1040 # Verify that the file must exist in the current directory.
1041 sub require_file
1043     local ($file) = @_;
1044     local ($fullfile) = $relative_dir . "/" . $file;
1046     if (! -f $fullfile)
1047     {
1048         print STDERR "automake: required file \"$fullfile\" not found\n";
1049         $exit_status = 1;
1050     }
1051     else
1052     {
1053         push (@dist_common, $file);
1054     }
1058 ################################################################
1060 # Return directory name of file.
1061 sub dirname
1063     local ($file) = @_;
1064     local ($sub);
1066     ($sub = $file) =~ s,/+[^/]+,,g;
1067     if ($sub eq $file)
1068     {
1069         $sub = '.';
1070     }
1072     return $sub;
1075 ################################################################
1077 # Print usage information.
1078 sub usage
1080     print "Usage: automake [OPTION] ... [Makefile]...\n";
1081     print $USAGE;
1082     print "\nFiles which are automatically distributed, if found:\n";
1083     $~ = "USAGE_FORMAT";
1084     local (@lcomm) = sort ((@common_files, @common_sometimes));
1085     local ($one, $two);
1086     while ($#lcomm >= 0)
1087     {
1088         $one = shift (@lcomm);
1089         $two = shift (@lcomm);
1090         write;
1091     }
1093     exit 0;
1096 format USAGE_FORMAT =
1097   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1098   $one,                $two