(handle_dependencies): Another dependency bug fix.
[automake.git] / automake.in
blob8c6a2a38817d3233aae6f0ef5bb5a7f0b6d3e809
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>) . "\n";
666                 close (DEP_FILE);
667             }
668         }       
669     }
672 # Handle subdirectories.
673 sub handle_subdirs
675     return if (! defined ($contents{'SUBDIRS'}));
677     $output_rules .= &file_contents ('subdirs');
679     push (@all, "all-recursive");
680     push (@check, "check-recursive");
681     push (@info, "info-recursive");
682     push (@dvi, "dvi-recursive");
684     $recursive_install = 1;
687 # Handle remaking and configure stuff.
688 sub handle_configure
690     if ($relative_dir ne '.')
691     {
692         # In subdirectory.
693         $output_rules .= &file_contents ('remake-subd');
694     }
695     else
696     {
697         if (-f 'aclocal.m4')
698         {
699             $output_vars .= "ACLOCAL = aclocal.m4\n";
700             push (@dist_common, 'aclocal.m4');
701         }
702         $output_rules .= &file_contents ('remake');
704         # Look for some files we need.
705         &require_file ('install-sh');
706         &require_file ('mkinstalldirs');
707     }
709     if (defined ($configure{'CONFIG_HEADER'})
710         && $contents{'CONFIG_HEADER'} !~ m,/,)
711     {
712         # Header defined and in this directory.
713         if (-f 'acconfig.h')
714         {
715             $output_vars .= "ACCONFIG = acconfig.h\n";
716             push (@dist_common, 'acconfig.h');
717         }
718         if (-f 'config.h.top')
719         {
720             $output_vars .= "CONFIG_TOP = config.h.top\n";
721             push (@dist_common, 'config.h.top');
722         }
723         if (-f 'config.h.bot')
724         {
725             $output_vars .= "CONFIG_BOT = config.h.bot\n";
726             push (@dist_common, 'config.h.bot');
727         }
729         push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
731         $output_rules .= &file_contents ('remake-hdr');
732     }
735 # Handle footer elements.
736 sub handle_footer
738     if ($contents{'SOURCES'})
739     {
740         $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
741     }
742     if ($contents{'OBJECTS'})
743     {
744         $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
745     }
746     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
747     {
748         $output_vars .= "\n";
749     }
751     $output_trailer .= ".SUFFIXES:\n";
752     if ($#suffixes >= 0)
753     {
754         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
755     }
756     $output_trailer .= "\n" . &file_contents ('footer');
759 # There are several targets which need to be merged.  This is because
760 # their complete definition is compiled from many parts.  Note that we
761 # avoid double colon rules, otherwise we'd use them instead.
762 sub handle_merge_targets
764     &do_one_merge_target ('all', @all);
765     &do_one_merge_target ('info', @info);
766     &do_one_merge_target ('dvi', @dvi);
767     &do_one_merge_target ('check', @check);
769     # Handle the various install targets specially.  We do this so
770     # that (eg) "make install-exec" will run "install-exec-recursive"
771     # if required, but "make install" won't run it twice.  Step one is
772     # to see if the user specified local versions of any of the
773     # targets we handle.
774     if (defined $contents{'install-exec-local'})
775     {
776         push (@install_exec, 'install-exec-local');
777     }
778     if (defined $contents{'install-data-local'})
779     {
780         push (@install_data, 'install-data-local');
781     }
782     if (defined $contents{'uninstall-local'})
783     {
784         push (@uninstall, 'uninstall-local');
785     }
787     if (defined $contents{'install-local'})
788     {
789         print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
790     }
792     # Step two: if we are doing recursive makes, write out the
793     # appropriate rules.
794     local (@install);
795     if ($recursive_install)
796     {
797         push (@install, 'install-recursive');
798         push (@uninstall, 'uninstall-recursive');
800         if ($#install_exec >= 0)
801         {
802             $output_rules .= ('install-exec-am: '
803                               . join (' ', @install_exec)
804                               . "\n\n");
805             @install_exec = ('install-exec-recursive', 'install-exec-am');
806             push (@install, 'install-exec-am');
807         }
808         if ($#install_data >= 0)
809         {
810             $output_rules .= ('install-data-am: '
811                               . join (' ', @install_data)
812                               . "\n\n");
813             @install_data = ('install-data-recursive', 'install-data-am');
814             push (@install, 'install-data-am');
815         }
816         if ($#uninstall >= 0)
817         {
818             $output_rules .= ('uninstall-am: '
819                               . join (' ', @uninstall)
820                               . "\n\n");
821             @uninstall = ('uninstall-recursive', 'uninstall-am');
822         }
823     }
825     # Step three: print definitions users can use.
826     if ($#install_exec >= 0)
827     {
828         $output_rules .= ("install-exec: "
829                           . join (' ', @install_exec)
830                           . "\n\n");
831         push (@install, 'install-exec') if (!$recursive_install);
832     }
833     if ($#install_data >= 0)
834     {
835         $output_rules .= ("install-data: "
836                           . join (' ', @install_data)
837                           . "\n\n");
838         push (@install, 'install-data') if (!$recursive_install);
839     }
841     $output_rules .= ('install: '
842                       . join (' ', @install)
843                       . "\n\n"
844                       . 'uninstall: '
845                       . join (' ', @uninstall)
846                       . "\n\n");
849 # Helper for handle_merge_targets.
850 sub do_one_merge_target
852     local ($name, @values) = @_;
854     if (defined $contents{$name . '-local'})
855     {
856         # User defined local form of target.  So include it.
857         push (@values, $name . '-local');
858     }
860     $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
863 # Handle all 'clean' targets.
864 sub handle_clean
866     local ($target) = $recursive_install ? 'clean-am' : 'clean';
867     &do_one_clean_target ($target, 'mostly', '', @clean);
868     &do_one_clean_target ($target, '', 'mostly', @clean);
869     &do_one_clean_target ($target, 'dist', '', @clean);
870     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
872     local (@deps);
873     if ($recursive_install)
874     {
875         @deps = ('am', 'recursive');
876         &do_one_clean_target ('', 'mostly', '', @deps);
877         &do_one_clean_target ('', '', '', @deps);
878         &do_one_clean_target ('', 'dist', '', @deps);
879         &do_one_clean_target ('', 'maintainer-', '', @deps);
880     }
883 # Helper for handle_clean.
884 sub do_one_clean_target
886     local ($target, $name, $last_name, @deps) = @_;
888     # Special case: if target not passed, then don't generate
889     # dependency on next "lower" clean target (eg no
890     # clean<-mostlyclean derivation).  In this case the target is
891     # implicitly known to be 'clean'.
892     local ($flag) = $target;
893     if (!$flag)
894     {
895         $target = 'clean';
896     }
898     $output_rules .= $name . $target . ": ";
899     if ($flag)
900     {
901         if ($last_name || $name ne 'mostly')
902         {
903             $output_rules .= $last_name . $target . " ";
904         }
905     }
907     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
908                       . "\n\n");
911 ################################################################
913 # Read Makefile.am and set up %contents.  Simultaneously copy lines
914 # from Makefile.am into $output_trailer or $output_vars as
915 # appropriate.  NOTE we put rules in the trailer section.  We want
916 # user rules to come after our generated stuff.
917 sub read_am_file
919     local ($amfile) = @_;
921     if (! open (AMFILE, $amfile))
922     {
923         print STDERR "automake: couldn't open $amfile: $!\n";
924         exit 1;
925     }
927     local ($saw_bk) = 0;
928     local ($was_rule) = 0;
929     local ($last_var_name) = '';
931     while (<AMFILE>)
932     {
933         chop;
935         if ($saw_bk)
936         {
937             if ($was_rule)
938             {
939                 $output_trailer .= $_ . "\n";
940             }
941             else
942             {
943                 $output_vars .= $_ . "\n";
944                 if (substr ($_, -1) eq "\\")
945                 {
946                     $contents{$last_var_name} .= substr ($_, 0,
947                                                          length ($_) - 1);
948                 }
949                 else
950                 {
951                     $contents{$last_var_name} .= $_;
952                 }
953             }
954         }
955         elsif ($_ eq '@kr@')
956         {
957             # Special case: this means we want automatic
958             # de-ANSI-fication.  FIXME think of a better way.
959             $contents{'@kr@'} = 1;
960         }
961         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
962         {
963             # Found a rule.
964             $was_rule = 1;
965             # Value here doesn't matter; for targets we only note
966             # existence.
967             $contents{$1} = 1;
968             $output_trailer .= $_ . "\n";
969         }
970         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
971         {
972             # Found a variable reference.
973             $was_rule = 0;
974             $last_var_name = $1;
975             if (substr ($2, -1) eq "\\")
976             {
977                 $contents{$1} = substr ($2, 0, length ($2) - 1);
978             }
979             else
980             {
981                 $contents{$1} = $2;
982             }
983             $output_vars .= $_ . "\n";
984         }
985         elsif (m/^$/)
986         {
987             # Special rule: if looking at a blank line, append it to
988             # whatever we saw last.
989             if ($was_rule)
990             {
991                 $output_trailer .= "\n";
992             }
993             else
994             {
995                 $output_vars .= "\n";
996             }
997         }
998         else
999         {
1000             # This isn't an error; it is probably a continued rule.
1001             # In fact, this is what we assume.
1002             $output_trailer .= $_ . "\n";
1003         }
1005         $saw_bk = (substr ($_, -1) eq "\\");
1006     }
1008     # Include some space after user code.
1009     $output_vars .= "\n";
1010     $output_trailer .= "\n";
1014 ################################################################
1016 # Return contents of a file from $am_dir.
1017 sub file_contents
1019     local ($basename) = @_;
1020     local ($file) = $am_dir . '/' . $basename . '.am';
1022     if (! open (FC_FILE, $file))
1023     {
1024         print STDERR "automake: installation error: cannot open \"$file\"\n";
1025         exit 1;
1026     }
1028     # Yes, we really want to slurp it.
1029     local ($results) = join ('', <FC_FILE>);
1031     close (FC_FILE);
1033     return $results;
1036 ################################################################
1038 # Verify that the file must exist in the current directory.
1039 sub require_file
1041     local ($file) = @_;
1042     local ($fullfile) = $relative_dir . "/" . $file;
1044     if (! -f $fullfile)
1045     {
1046         print STDERR "automake: required file \"$fullfile\" not found\n";
1047         $exit_status = 1;
1048     }
1049     else
1050     {
1051         push (@dist_common, $file);
1052     }
1056 ################################################################
1058 # Return directory name of file.
1059 sub dirname
1061     local ($file) = @_;
1062     local ($sub);
1064     ($sub = $file) =~ s,/+[^/]+,,g;
1065     if ($sub eq $file)
1066     {
1067         $sub = '.';
1068     }
1070     return $sub;
1073 ################################################################
1075 # Print usage information.
1076 sub usage
1078     print "Usage: automake [OPTION] ... [Makefile]...\n";
1079     print $USAGE;
1080     print "\nFiles which are automatically distributed, if found:\n";
1081     $~ = "USAGE_FORMAT";
1082     local (@lcomm) = sort ((@common_files, @common_sometimes));
1083     local ($one, $two);
1084     while ($#lcomm >= 0)
1085     {
1086         $one = shift (@lcomm);
1087         $two = shift (@lcomm);
1088         write;
1089     }
1091     exit 0;
1094 format USAGE_FORMAT =
1095   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1096   $one,                $two