*** empty log message ***
[automake.git] / automake.in
blobefff99a2ae8e3c86a21cd879db0a1c709cbdcc99
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     push (@clean, 'compile');
329     if (defined $contents{'@kr@'})
330     {
331         $obj = '.${kr}o';
332         push (@suffixes, '._c', '._o');
334         &require_file ('ansi2knr.c');
335         &require_file ('ansi2knr.1');
337         $output_vars .= &file_contents ('kr-vars');
338         $output_rules .= &file_contents ('compile-kr');
339         $output_rules .= &file_contents ('clean-kr');
341         push (@clean, 'kr');
342     }
344     local (@sources, @objects);
345     push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
346     push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
348     local ($one_file);
349     foreach $one_file (split (' ', ($programs . ' '
350                                     . $libprograms . ' '
351                                     . $libraries)))
352     {
353         # Look for file_SOURCES and file_OBJECTS.  FIXME file_OBJECTS
354         # should probably not be used(?)
355         if (defined $contents{$one_file . "_SOURCES"})
356         {
357             if (! defined $contents{$one_file . "_OBJECTS"})
358             {
359                 # Turn sources into objects.
360                 $_ = $contents{$one_file . "_SOURCES"};
362                 # Ugh: Perl syntax vs Emacs.
363                 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
365                 s/\.cc/$obj/g;
366                 s/$krc1/$obj/g;
367                 s/$krc2/$obj/g;
368                 s/\.[cCmylfs]/$obj/g;
370                 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
371             }
373             push (@sources, '${' . $one_file . "_SOURCES}");
374             push (@objects, '${' . $one_file . "_OBJECTS}");
375         }
376         else
377         {
378             $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
379                              . $one_file . "_OBJECTS = ". $one_file
380                              . $obj . "\n");
381             push (@sources, $one_file . '.c');
382             push (@objects, $one_file . $obj);
383         }
385         if (defined $contents{'CONFIG_HEADER'})
386         {
387             $output_rules .= ('$(' . $one_file . "_OBJECTS): "
388                               . $contents{'CONFIG_HEADER'} . "\n");
389         }
390     }
392     $output_vars .= "\n";
394     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
395     # on this.
396     $contents{'SOURCES'} = join (' ', @sources);
397     $contents{'OBJECTS'} = join (' ', @objects);
399     # Some boilerplate, and install rules.
400     if ($programs)
401     {
402         $output_rules .= &file_contents ('programs');
403         push (@install_exec, "install-programs");
404         push (@uninstall, 'uninstall-programs');
405         push (@clean, 'programs');
406     }
407     if ($libprograms)
408     {
409         $output_rules .= &file_contents ('libprograms');
410         push (@install_exec, 'install-libprograms');
411         push (@uninstall, 'uninstall-libprograms');
412         push (@clean, 'libprograms');
413     }
415     # Handle linking.
416     if ($programs || $libprograms)
417     {
418         local ($fcont) = &file_contents ('program');
419         local ($munge);
420         foreach $one_file (split (' ', $programs . ' ' . $libprograms))
421         {
422             if (! defined $contents{$one_file . "_LDADD"})
423             {
424                 # User didn't define prog_LDADD override.  So do it.
425                 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
426             }
428             ($munge = $fcont) =~ s/@PROGRAM@/$one_file/g;
429             $output_rules .= $munge;
430         }
431     }
434 # Handle libraries.
435 sub handle_libraries
437     local ($libraries) = @_;
439     return if (!$libraries);
441     local (@liblist) = split (' ', $libraries);
443     $output_rules .= &file_contents ('libraries');
444     local ($onefile) = &file_contents ('library');
445     local ($onelib, $munge);
446     foreach $onelib (@liblist)
447     {
448         ($munge = $onefile) =~ s/@LIBRARY@/$onelib/g;
449         $output_rules .= $munge;
450     }
452     # Turn "foo" into "libfoo.a" and include macro definition.
453     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
454     $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
455                      . &file_contents ('libraries-vars'));
457     push (@install_exec, 'install-libraries');
458     push (@uninstall, 'uninstall-libraries');
459     push (@clean, 'libraries');
462 # Handle scripts.
463 sub handle_scripts
465     local ($scripts, $libscripts) = @_;
467     if ($scripts)
468     {
469         $output_rules .= &file_contents ('scripts');
470         push (@install_exec, 'install-scripts');
471         push (@uninstall, 'uninstall-scripts');
472         push (@clean, 'scripts');
473     }
475     if ($libscripts)
476     {
477         $output_rules .= &file_contents ('libscripts');
478         push (@install_exec, 'install-libscripts');
479         push (@uninstall, 'uninstall-libscripts');
480         push (@clean, 'libscripts');
481     }
484 # Handle all Texinfo source.
485 sub handle_texinfo
487     return if (! defined $contents{TEXINFOS});
489     local (@texis) = split (' ', $contents{TEXINFOS});
490     if ($#texis > 0)
491     {
492         print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
493         $exit_status = 1;
494         return;
495     }
497     local ($infobase);
498     ($infobase = $texis[0]) =~ s/\.texi$//;
500     # If 'version.texi' is referenced by input file, then include
501     # automatic versioning capability.
502     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
503             . " > /dev/null 2>&1");
504     if (!$?)
505     {
506         # Got a hit.
507         push (@texis, 'version.texi');
508         push (@dist_common, 'version.texi', 'stamp-vti');
509         push (@clean, 'vti');
511         local ($tfile);
512         ($tfile = &file_contents ('texi-version')) =~ s/@TEXI@/$texis[0]/g;
513         $output_rules = $output_rules . $tfile;
515         &require_file ('mdate-sh');
516     }
518     # If user specified file_TEXINFOS, then use that as explicit
519     # dependency list.
520     if (defined $contents{$infobase . "_TEXINFOS"})
521     {
522         push (@texis, "\$" . $infobase . '_TEXINFOS');
523         push (@dist_common, "\$" . $infobase . '_TEXINFOS');
524     }
526     if ($#texis >= 0)
527     {
528         $output_rules = ($output_rules . $infobase . ".info: "
529                          . join (' ', @texis) . "\n\n");
530     }
532     # Some boilerplate.
533     $output_vars = $output_vars . &file_contents ('texinfos-vars');
534     $output_rules = $output_rules . &file_contents ('texinfos');
536     push (@suffixes, '.texi', '.info', '.dvi');
537     push (@uninstall, 'uninstall-info');
538     push (@clean, 'info');
539     push (@info, '$(INFO_DEPS)');
540     push (@dvi, '$(DVIS)');
541     # Make sure documentation is made and installed first.
542     unshift (@install_data, 'install-info');
543     unshift (@all, 'info');
545     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
546                      . "INFO_DEPS = " . $infobase . ".info\n"
547                      . "DVIS = " . $infobase . ".dvi\n\n");
549     # Do some error checking.
550     &require_file ('texinfo.tex');
553 # Handle any man pages.
554 sub handle_man_pages
556     return if (! defined $contents{'MANS'});
558     $output_vars .= &file_contents ('mans-vars');
559     $output_rules .= &file_contents ('mans');
561     push (@install_data, 'install-man');
562     push (@uninstall, 'uninstall-man');
565 # Handle DATA and PACKAGEDATA.
566 sub handle_data
568     if (defined $contents{'DATA'})
569     {
570         $output_rules .= &file_contents ('data');
571         push (@install_data, 'install-ddata');
572         push (@uninstall, 'uninstall-ddata');
573     }
575     if (defined $contents{'PACKAGEDATA'})
576     {
577         $output_rules .= &file_contents ('packagedata');
578         push (@install_data, 'install-pdata');
579         push (@uninstall, 'uninstall-pdata');
580     }
583 # Handle TAGS.
584 sub handle_tags
586     local ($tagging) = 0;
588     if (defined ($contents{'SUBDIRS'}))
589     {
590         $output_rules .= &file_contents ('tags');
591         $tagging = 1;
592     }
593     elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
594     {
595         $output_rules .= &file_contents ('tags-subd');
596         $tagging = 1;
597     }
599     if ($tagging)
600     {
601         $output_rules .= &file_contents ('tags-clean');
602         push (@clean, 'tags');
603     }
606 # Handle 'dist' target.
607 sub handle_dist
609     # Look for common files that should be included in distribution.
610     local ($cfile);
611     foreach $cfile (@common_files)
612     {
613         if (-f ($relative_dir . "/" . $cfile))
614         {
615             push (@dist_common, $cfile);
616         }
617     }
619     $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
621     # Some boilerplate.
622     if ($relative_dir ne '.')
623     {
624         # In a subdirectory.
625         $output_vars .= (&file_contents ('dist-subd-vars')
626                          . "subdir = " . $relative_dir . "\n\n");
627         $output_rules .= &file_contents ('dist-subd');
628     }
629     else
630     {
631         $output_vars .= &file_contents ('dist-vars');
632         $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
633                                          ? 'dist-subd-top'
634                                          : 'dist');
635     }
638 # Handle auto-dependency code.
639 sub handle_dependencies
641     if ($use_dependencies)
642     {
643         # Include GNU-make-specific auto-dep code.
644         if ($dir_holds_sources)
645         {
646             $output_rules .= &file_contents ('depend');
647         }
648     }
649     else
650     {
651         # Include any auto-generated deps that are present.
652         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
653         {
654             local ($depfile);
655             local ($gpat) = $relative_dir . "/.deps/*.P";
657             foreach $depfile (<${gpat}>)
658             {
659                 if (! open (DEP_FILE, $depfile))
660                 {
661                     print STDERR "automake: couldn't open $depfile: $!\n";
662                     next;
663                 }
665                 # Slurp entire file.
666                 $output_rules .= join ('', <DEP_FILE>);
668                 close (DEP_FILE);
669             }
671             $output_rules .= "\n";
672         }       
673     }
676 # Handle subdirectories.
677 sub handle_subdirs
679     return if (! defined ($contents{'SUBDIRS'}));
681     $output_rules .= &file_contents ('subdirs');
683     push (@all, "all-recursive");
684     push (@check, "check-recursive");
685     push (@info, "info-recursive");
686     push (@dvi, "dvi-recursive");
688     $recursive_install = 1;
691 # Handle remaking and configure stuff.
692 sub handle_configure
694     if ($relative_dir ne '.')
695     {
696         # In subdirectory.
697         $output_rules .= &file_contents ('remake-subd');
698     }
699     else
700     {
701         if (-f 'aclocal.m4')
702         {
703             $output_vars .= "ACLOCAL = aclocal.m4\n";
704             push (@dist_common, 'aclocal.m4');
705         }
706         $output_rules .= &file_contents ('remake');
708         # Look for some files we need.
709         &require_file ('install-sh');
710         &require_file ('mkinstalldirs');
711     }
713     if (defined ($configure{'CONFIG_HEADER'})
714         && $contents{'CONFIG_HEADER'} !~ m,/,)
715     {
716         # Header defined and in this directory.
717         if (-f 'acconfig.h')
718         {
719             $output_vars .= "ACCONFIG = acconfig.h\n";
720             push (@dist_common, 'acconfig.h');
721         }
722         if (-f 'config.h.top')
723         {
724             $output_vars .= "CONFIG_TOP = config.h.top\n";
725             push (@dist_common, 'config.h.top');
726         }
727         if (-f 'config.h.bot')
728         {
729             $output_vars .= "CONFIG_BOT = config.h.bot\n";
730             push (@dist_common, 'config.h.bot');
731         }
733         push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
735         $output_rules .= &file_contents ('remake-hdr');
736     }
739 # Handle footer elements.
740 sub handle_footer
742     if ($contents{'SOURCES'})
743     {
744         $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
745     }
746     if ($contents{'OBJECTS'})
747     {
748         $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
749     }
750     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
751     {
752         $output_vars .= "\n";
753     }
755     $output_trailer .= ".SUFFIXES:\n";
756     if ($#suffixes >= 0)
757     {
758         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
759     }
760     $output_trailer .= "\n" . &file_contents ('footer');
763 # There are several targets which need to be merged.  This is because
764 # their complete definition is compiled from many parts.  Note that we
765 # avoid double colon rules, otherwise we'd use them instead.
766 sub handle_merge_targets
768     &do_one_merge_target ('all', @all);
769     &do_one_merge_target ('info', @info);
770     &do_one_merge_target ('dvi', @dvi);
771     &do_one_merge_target ('check', @check);
773     # Handle the various install targets specially.  We do this so
774     # that (eg) "make install-exec" will run "install-exec-recursive"
775     # if required, but "make install" won't run it twice.  Step one is
776     # to see if the user specified local versions of any of the
777     # targets we handle.
778     if (defined $contents{'install-exec-local'})
779     {
780         push (@install_exec, 'install-exec-local');
781     }
782     if (defined $contents{'install-data-local'})
783     {
784         push (@install_data, 'install-data-local');
785     }
786     if (defined $contents{'uninstall-local'})
787     {
788         push (@uninstall, 'uninstall-local');
789     }
791     if (defined $contents{'install-local'})
792     {
793         print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
794     }
796     # Step two: if we are doing recursive makes, write out the
797     # appropriate rules.
798     local (@install);
799     if ($recursive_install)
800     {
801         push (@install, 'install-recursive');
802         push (@uninstall, 'uninstall-recursive');
804         if ($#install_exec >= 0)
805         {
806             $output_rules .= ('install-exec-am: '
807                               . join (' ', @install_exec)
808                               . "\n\n");
809             @install_exec = ('install-exec-recursive', 'install-exec-am');
810             push (@install, 'install-exec-am');
811         }
812         if ($#install_data >= 0)
813         {
814             $output_rules .= ('install-data-am: '
815                               . join (' ', @install_data)
816                               . "\n\n");
817             @install_data = ('install-data-recursive', 'install-data-am');
818             push (@install, 'install-data-am');
819         }
820         if ($#uninstall >= 0)
821         {
822             $output_rules .= ('uninstall-am: '
823                               . join (' ', @uninstall)
824                               . "\n\n");
825             @uninstall = ('uninstall-recursive', 'uninstall-am');
826         }
827     }
829     # Step three: print definitions users can use.
830     if ($#install_exec >= 0)
831     {
832         $output_rules .= ("install-exec: "
833                           . join (' ', @install_exec)
834                           . "\n\n");
835         push (@install, 'install-exec') if (!$recursive_install);
836     }
837     if ($#install_data >= 0)
838     {
839         $output_rules .= ("install-data: "
840                           . join (' ', @install_data)
841                           . "\n\n");
842         push (@install, 'install-data') if (!$recursive_install);
843     }
845     $output_rules .= ('install: '
846                       . join (' ', @install)
847                       . "\n\n"
848                       . 'uninstall: '
849                       . join (' ', @uninstall)
850                       . "\n\n");
853 # Helper for handle_merge_targets.
854 sub do_one_merge_target
856     local ($name, @values) = @_;
858     if (defined $contents{$name . '-local'})
859     {
860         # User defined local form of target.  So include it.
861         push (@values, $name . '-local');
862     }
864     $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
867 # Handle all 'clean' targets.
868 sub handle_clean
870     push (@clean, 'generic');
871     $output_rules .= &file_contents ('clean');
873     local ($target) = $recursive_install ? 'clean-am' : 'clean';
874     &do_one_clean_target ($target, 'mostly', '', @clean);
875     &do_one_clean_target ($target, '', 'mostly', @clean);
876     &do_one_clean_target ($target, 'dist', '', @clean);
877     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
879     local (@deps);
880     if ($recursive_install)
881     {
882         @deps = ('am', 'recursive');
883         &do_one_clean_target ('', 'mostly', '', @deps);
884         &do_one_clean_target ('', '', '', @deps);
885         &do_one_clean_target ('', 'dist', '', @deps);
886         &do_one_clean_target ('', 'maintainer-', '', @deps);
887     }
890 # Helper for handle_clean.
891 sub do_one_clean_target
893     local ($target, $name, $last_name, @deps) = @_;
895     # Special case: if target not passed, then don't generate
896     # dependency on next "lower" clean target (eg no
897     # clean<-mostlyclean derivation).  In this case the target is
898     # implicitly known to be 'clean'.
899     local ($flag) = $target;
900     if (!$flag)
901     {
902         $target = 'clean';
903     }
905     $output_rules .= $name . $target . ": ";
906     if ($flag)
907     {
908         if ($last_name || $name ne 'mostly')
909         {
910             $output_rules .= $last_name . $target . " ";
911         }
912     }
914     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
915                       . "\n");
917     # FIXME shouldn't we really print these messages before running
918     # the dependencies?
919     if ($name . $target eq 'maintainer-clean')
920     {
921         # Print a special warning.
922         $output_rules .=
923             ("\t@echo \"This command is intended for maintainers to use;\"\n"
924              . "\t@echo \"it deletes files that may require special "
925              . "tools to rebuild.\"\n");
926     }
927     $output_rules .= "\n";
930 ################################################################
932 # Read Makefile.am and set up %contents.  Simultaneously copy lines
933 # from Makefile.am into $output_trailer or $output_vars as
934 # appropriate.  NOTE we put rules in the trailer section.  We want
935 # user rules to come after our generated stuff.
936 sub read_am_file
938     local ($amfile) = @_;
940     if (! open (AMFILE, $amfile))
941     {
942         print STDERR "automake: couldn't open $amfile: $!\n";
943         exit 1;
944     }
946     local ($saw_bk) = 0;
947     local ($was_rule) = 0;
948     local ($last_var_name) = '';
950     while (<AMFILE>)
951     {
952         chop;
954         if ($saw_bk)
955         {
956             if ($was_rule)
957             {
958                 $output_trailer .= $_ . "\n";
959             }
960             else
961             {
962                 $output_vars .= $_ . "\n";
963                 if (substr ($_, -1) eq "\\")
964                 {
965                     $contents{$last_var_name} .= substr ($_, 0,
966                                                          length ($_) - 1);
967                 }
968                 else
969                 {
970                     $contents{$last_var_name} .= $_;
971                 }
972             }
973         }
974         elsif ($_ eq '@kr@')
975         {
976             # Special case: this means we want automatic
977             # de-ANSI-fication.  FIXME think of a better way.
978             $contents{'@kr@'} = 1;
979         }
980         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
981         {
982             # Found a rule.
983             $was_rule = 1;
984             # Value here doesn't matter; for targets we only note
985             # existence.
986             $contents{$1} = 1;
987             $output_trailer .= $_ . "\n";
988         }
989         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
990         {
991             # Found a variable reference.
992             $was_rule = 0;
993             $last_var_name = $1;
994             if (substr ($2, -1) eq "\\")
995             {
996                 $contents{$1} = substr ($2, 0, length ($2) - 1);
997             }
998             else
999             {
1000                 $contents{$1} = $2;
1001             }
1002             $output_vars .= $_ . "\n";
1003         }
1004         elsif (m/^$/)
1005         {
1006             # Special rule: if looking at a blank line, append it to
1007             # whatever we saw last.
1008             if ($was_rule)
1009             {
1010                 $output_trailer .= "\n";
1011             }
1012             else
1013             {
1014                 $output_vars .= "\n";
1015             }
1016         }
1017         else
1018         {
1019             # This isn't an error; it is probably a continued rule.
1020             # In fact, this is what we assume.
1021             $output_trailer .= $_ . "\n";
1022         }
1024         $saw_bk = (substr ($_, -1) eq "\\");
1025     }
1027     # Include some space after user code.
1028     $output_vars .= "\n";
1029     $output_trailer .= "\n";
1033 ################################################################
1035 # Return contents of a file from $am_dir.
1036 sub file_contents
1038     local ($basename) = @_;
1039     local ($file) = $am_dir . '/' . $basename . '.am';
1041     if (! open (FC_FILE, $file))
1042     {
1043         print STDERR "automake: installation error: cannot open \"$file\"\n";
1044         exit 1;
1045     }
1047     # Yes, we really want to slurp it.
1048     local ($results) = join ('', <FC_FILE>);
1050     close (FC_FILE);
1052     return $results;
1055 ################################################################
1057 # Verify that the file must exist in the current directory.
1058 sub require_file
1060     local ($file) = @_;
1061     local ($fullfile) = $relative_dir . "/" . $file;
1063     if (! -f $fullfile)
1064     {
1065         print STDERR "automake: required file \"$fullfile\" not found\n";
1066         $exit_status = 1;
1067     }
1068     else
1069     {
1070         push (@dist_common, $file);
1071     }
1075 ################################################################
1077 # Return directory name of file.
1078 sub dirname
1080     local ($file) = @_;
1081     local ($sub);
1083     ($sub = $file) =~ s,/+[^/]+,,g;
1084     if ($sub eq $file)
1085     {
1086         $sub = '.';
1087     }
1089     return $sub;
1092 ################################################################
1094 # Print usage information.
1095 sub usage
1097     print "Usage: automake [OPTION] ... [Makefile]...\n";
1098     print $USAGE;
1099     print "\nFiles which are automatically distributed, if found:\n";
1100     $~ = "USAGE_FORMAT";
1101     local (@lcomm) = sort ((@common_files, @common_sometimes));
1102     local ($one, $two);
1103     while ($#lcomm >= 0)
1104     {
1105         $one = shift (@lcomm);
1106         $two = shift (@lcomm);
1107         write;
1108     }
1110     exit 0;
1113 format USAGE_FORMAT =
1114   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1115   $one,                $two