Upped to 0.30. Doc fixes
[automake.git] / automake.in
blob04d5b48d67898d961bdf4e2d96680ae86fe16ad4
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
6     if $running_under_some_shell;
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994, 1995, 1996 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., 59 Temple Place - Suite 330, Boston, MA
24 # 02111-1307, USA.
26 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
27 # Perl reimplementation by Tom Tromey <tromey@drip.colorado.edu>.
30 # Parameters set by configure.  Not to be changed.
31 $VERSION = "@VERSION@";
32 $prefix = "@prefix@";
33 $am_dir = "@datadir@/@PACKAGE@";
35 # String constants.
36 $IGNORE_PATTERN = "^##([^#].*)?\$";
37 $WHITE_PATTERN = "^[ \t]*\$";
38 $COMMENT_PATTERN = "^#";
39 $RULE_PATTERN = "^([a-zA-Z_.][-.a-zA-Z0-9_.]*) *:";
40 $MACRO_PATTERN = "^([A-Za-z][A-Za-z0-9_]*)[ \t]*=[ \t]*(.*)\$";
43 # Constants to define the "strictness" level.
44 $NORMAL = 0;
45 $GNU = 1;
46 $GNITS = 2;
50 # Variables global to entire run.
52 # Strictness level as set on command line.
53 $default_strictness = $NORMAL;
55 # Name of strictness level, as set on command line.
56 $default_strictness_name = 'normal';
58 # This is TRUE if GNU make specific automatic dependency generation
59 # code should be included in generated Makefile.in.
60 $use_dependencies = 1;
62 # This holds our (eventual) exit status.  We don't actually exit until
63 # we have processed all input files.
64 $exit_status = 0;
66 # From the Perl manual.
67 $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
69 # TRUE if missing standard files should be installed.
70 $install_missing = 0;
72 # Files found by scanning configure.in for LIBOBJS.
73 %libsources = ();
75 # True if fp_C_PROTOTYPES appears in configure.in.
76 $fp_c_prototypes = 0;
78 # Names used in AC_CONFIG_HEADER call.  $config_name is the actual
79 # (first) argument.  $config_header is the '.in' file.  Ordinarily the
80 # second is derived from the first, but they can be different if the
81 # weird "NAME:FILE" syntax is used.
82 $config_name = '';
83 $config_header = '';
85 # Relative location of top build directory.
86 $top_builddir = '';
88 # List of Makefile.am's to process.
89 @input_files = ();
91 # List of files in AC_OUTPUT without Makefile.am.
92 @other_input_files = ();
94 # Whether AC_PROG_MAKE_SET has been seen in configure.in.
95 $seen_make_set = 0;
97 # Whether ud_GNU_GETTEXT has been seen in configure.in.
98 $seen_gettext = 0;
100 # 1 if AC_PROG_INSTALL seen, 2 if fp_PROG_INSTALL seen.
101 $seen_prog_install = 0;
103 # 1 if any scripts installed, 0 otherwise.
104 $scripts_installed = 0;
108 &initialize_global_constants;
110 # Parse command line.
111 &parse_arguments (@ARGV);
113 # Do configure.in scan only once.
114 &scan_configure;
116 die "automake: no \`Makefile.am' found or specified\n"
117     if ! @input_files;
119 # Now do all the work on each file.
120 foreach $am_file (@input_files)
122     if (! -f ($am_file . '.am'))
123     {
124         &am_error ('no such file');
125     }
126     else
127     {
128         &generate_makefile ($am_file);
129     }
132 &am_conf_error ($scripts_installed ? 'fp_PROG_INSTALL' : 'AC_PROG_INSTALL'
133                 . " must be used in configure.in")
134     unless $seen_prog_install > $scripts_installed;
136 exit $exit_status;
139 ################################################################
141 # Parse command line.
142 sub parse_arguments
144     local (@arglist) = @_;
146     # Start off as normal.
147     &set_strictness ('normal');
149     while (@arglist)
150     {
151         if ($arglist[0] eq "--version")
152         {
153             print "Automake version $VERSION\n";
154             exit 0;
155         }
156         elsif ($arglist[0] eq "--help")
157         {
158             &usage;
159         }
160         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
161         {
162             $am_dir = $1;
163         }
164         elsif ($arglist[0] eq '--amdir')
165         {
166             &require_argument (@arglist);
167             shift (@arglist);
168             $am_dir = $arglist[0];
169         }
170         elsif ($arglist[0] =~ /^--strictness=(.+)$/)
171         {
172             &set_strictness ($1);
173         }
174         elsif ($arglist[0] eq '--gnu')
175         {
176             &set_strictness ('gnu');
177         }
178         elsif ($arglist[0] eq '--gnits')
179         {
180             &set_strictness ('gnits');
181         }
182         elsif ($arglist[0] eq '--strictness')
183         {
184             &require_argument (@arglist);
185             shift (@arglist);
186             &set_strictness ($arglist[0]);
187         }
188         elsif ($arglist[0] eq '--include-deps')
189         {
190             $use_dependencies = 0;
191         }
192         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
193         {
194             # Set output directory.
195             $output_directory = $1;
196         }
197         elsif ($arglist[0] eq '--output-dir')
198         {
199             &require_argument (@arglist);
200             shift (@arglist);
201             $output_directory = $arglist[0];
202         }
203         elsif ($arglist[0] eq '--install-missing')
204         {
205             $install_missing = 1;
206         }
207         elsif ($arglist[0] eq '--')
208         {
209             # Stop option processing.
210             shift (@arglist);
211             push (@input_files, @arglist);
212             last;
213         }
214         elsif ($arglist[0] =~ /^-/)
215         {
216             die "automake: unrecognized option -- \`$arglist[0]'\n";
217         }
218         else
219         {
220             push (@input_files, $arglist[0]);
221         }
223         shift (@arglist);
224     }
226     # Take global strictness from whatever we currently have set.
227     $default_strictness = $strictness;
228     $default_strictness_name = $strictness_name;
231 # Ensure argument exists, or die.
232 sub require_argument
234     local ($arg, @arglist) = @_;
235     die "automake: no argument given for option \`$arg'\n"
236         if ! @arglist;
239 ################################################################
241 # Generate a Makefile.in given the name of the corresponding Makefile.
242 sub generate_makefile
244     local ($makefile) = @_;
246     print "creating ", $makefile, ".in\n";
248     &initialize_per_input;
249     $relative_dir = &dirname ($makefile);
250     # FIXME with new 'dist' target, don't need Makefile.in.  Probably
251     # should remove it here.
252     &push_dist_common ('Makefile.in', 'Makefile.am');
253     push (@sources, '$(SOURCES)') if defined $contents{'SOURCES'};
254     push (@objects, '$(OBJECTS)') if defined $contents{'OBJECTS'};
256     # This is always the default target.  This gives us freedom to do
257     # things in whatever order is convenient.
258     $output_rules .= "default: all\n\n";
259     push (@phony, 'default');
261     &read_am_file ($makefile . '.am');
262     &handle_options;
264     # Check first, because we might modify some state.
265     &check_gnu_standards;
266     &check_gnits_standards;
268     &handle_configure;
269     &handle_libraries;
270     &handle_programs;
271     &handle_scripts;
273     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
274     # on this (but currently does).
275     $contents{'SOURCES'} = join (' ', @sources);
276     $contents{'OBJECTS'} = join (' ', @objects);
278     &handle_texinfo;
279     &handle_man_pages;
280     &handle_data;
281     &handle_headers;
282     &handle_subdirs;
283     &handle_tags;
284     &handle_dist;
285     &handle_dependencies;
286     &handle_footer;
287     &handle_merge_targets;
288     &handle_installdirs;
289     &handle_clean;
290     &handle_phony;
292     if (! -d ($output_directory . '/' . $relative_dir))
293     {
294         &mkdir ($output_directory . '/' . $relative_dir);
295     }
296     if (! open (GM_FILE, "> " . $output_directory . '/' . $makefile . ".in"))
297     {
298         warn "automake: ${am_file}.in: cannot open: $!\n";
299         $exit_status = 1;
300         return;
301     }
303     print GM_FILE $output_vars;
304     print GM_FILE $output_rules;
305     print GM_FILE $output_trailer;
307     close (GM_FILE);
310 ################################################################
312 # Handle AUTOMAKE_OPTIONS variable.
313 sub handle_options
315     return if ! defined $contents{'AUTOMAKE_OPTIONS'};
317     foreach (split (/\s+/, $contents{'AUTOMAKE_OPTIONS'}))
318     {
319         $options{$_} = 1;
320         if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'normal')
321         {
322             &set_strictness ($_);
323         }
324         elsif ($_ eq 'no-installman' || $_ eq 'ansi2knr' || $_ eq 'dist-shar')
325         {
326             # Explicitly recognize these.
327         }
328         elsif ($_ eq 'no-dependencies')
329         {
330             # FIXME for now this is global.
331             $use_dependencies = 0;
332         }
333         else
334         {
335             &am_error ('option ', $_, 'not recognized');
336         }
337     }
340 # Return object extension.  Just once, put some code into the output.
341 sub get_object_extension
343     if (! $dir_holds_sources)
344     {
346         # Boilerplate.
347         $output_vars .= &file_contents ('compile-vars');
348         $output_rules .= &file_contents ('compile');
349         &push_phony_cleaners ('compile');
351         # Check for automatic de-ANSI-fication.
352         $dir_holds_sources = '.o';
353         push (@suffixes, '.c', '.o');
354         push (@clean, 'compile');
356         if (defined $options{'ansi2knr'} || defined $contents{'@kr@'})
357         {
358             &am_error ("option \`ansi2knr' in use but \`fp_C_PROTOTYPES' not in configure.in")
359                 if ! $fp_c_prototypes;
361             $dir_holds_sources = '$o';
362             push (@suffixes, '._c', '._o');
364             &require_file ($NORMAL, 'ansi2knr.c', 'ansi2knr.1');
366             $output_vars .= &file_contents ('kr-vars');
367             $output_rules .= &file_contents ('compile-kr');
368             $output_rules .= &file_contents ('clean-kr');
370             push (@clean, 'kr');
371             &push_phony_cleaners ('kr');
372         }
373     }
374     return $dir_holds_sources;
377 # Handle SOURCE->OBJECT transform for one program or library.
378 sub handle_source_transform
380     local ($one_file, $obj) = @_;
381     local ($objpat) = $obj;
382     $objpat =~ s/(\W)/\\\1/g;
384     # Look for file_SOURCES and file_OBJECTS.
385     if (defined $contents{$one_file . "_SOURCES"})
386     {
387         if (! defined $contents{$one_file . "_OBJECTS"})
388         {
389             # Turn sources into objects.
390             local (@files) = split (/\s+/, $contents{$one_file . "_SOURCES"});
391             local (@result) = ();
392             foreach (@files)
393             {
394                 # Skip header files.
395                 next if /\.h$/;
396                 # Skip things that look like macro references.
397                 next if /^\$\(.*\)$/;
398                 next if /^\$\{.*\}$/;
400                 if (/^(.*)\.[yl]$/)
401                 {
402                     # Automatically include generated .c file in
403                     # distribution.
404                     &push_dist_common ($1 . '.c');
405                 }
407                 # Transform source files into .o files.
408                 s/\.cc$/$obj/g;
409                 s/\.[cCmylfs]$/$obj/g;
410                 push (@result, $_);
412                 # Transform .o or $o file into .P file (for automatic
413                 # dependency code).
414                 s/$objpat$/.P/g;
415                 $dep_files{'$(srcdir)/.deps/' . $_} = 1;
416             }
418             &pretty_print ($one_file . "_OBJECTS =", '', @result);
419         }
420         else
421         {
422             &am_error ($one_file . '_OBJECTS', 'should not be defined');
423         }
425         push (@sources, '$(' . $one_file . "_SOURCES)");
426         push (@objects, '$(' . $one_file . "_OBJECTS)");
427     }
428     else
429     {
430         $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
431                          . $one_file . "_OBJECTS = ". $one_file
432                          . $obj . "\n");
433         push (@sources, $one_file . '.c');
434         push (@objects, $one_file . $obj);
435         $dep_files{'$(srcdir)/.deps/' . $one_file . '.P'} = 1;
436     }
438     if (defined $contents{'CONFIG_HEADER'})
439     {
440         $output_rules .= ('$(' . $one_file . "_OBJECTS): "
441                           . $contents{'CONFIG_HEADER'} . "\n");
442     }
444     return @result;
447 # Handle C programs.
448 sub handle_programs
450     local (@proglist) = &am_install_var ('-clean',
451                                          'programs', 'PROGRAMS',
452                                          'bin', 'sbin', 'libexec', 'noinst');
453     # FIXME error if PROGRAMS defined but no blah_PROGRAMS defined.
454     return if ! @proglist;
456     local ($obj) = &get_object_extension;
457     local ($one_file, $munge);
459     foreach $one_file (@proglist)
460     {
461         &handle_source_transform ($one_file, $obj);
463         if (! defined $contents{$one_file . "_LDADD"})
464         {
465             # User didn't define prog_LDADD override.  So do it.
466             $output_vars .= $one_file . '_LDADD = $(LDADD)' . "\n";
467         }
469         $output_rules .=
470             &file_contents_with_transform ('s/\@PROGRAM\@/' . $one_file
471                                            . '/go', 'program');
472     }
475 # Handle libraries.
476 sub handle_libraries
478     local (@liblist) = &am_install_var ('-no-all', '-clean',
479                                         'libraries', 'LIBRARIES',
480                                         'lib', 'pkglib', 'noinst');
481     # FIXME error if LIBRARIES defined but no blah_LIBRARIES defined.
482     return if ! @liblist;
484     # Generate _LIBFILES variables.  Too bad we can't do this in
485     # am_install_var.
486     local ($onedir, $onelib);
487     local (@outlist);
488     foreach $onedir ('lib', 'pkglib', 'noinst')
489     {
490         if (defined $contents{$onedir . '_LIBRARIES'})
491         {
492             @outlist = ();
493             foreach $onelib (split (/\s+/, $contents{$onedir . '_LIBRARIES'}))
494             {
495                 push (@outlist, 'lib' . $onelib . '.a');
496             }
497             &pretty_print ($onedir . '_LIBFILES =', '', @outlist);
498         }
499     }
500     push (@all, '$(LIBFILES)');
502     local ($obj) = &get_object_extension;
503     local ($munge);
504     foreach $onelib (@liblist)
505     {
506         if (defined $contents{$onelib . '_LIBADD'})
507         {
508             # We recognize certain things that are commonly put in
509             # LIBADD.
510             local ($lsearch);
512             foreach $lsearch (split (/\s+/, $contents{$onelib . '_LIBADD'}))
513             {
514                 # Automatically handle @LIBOBJS@ and @ALLOCA@.
515                 # Basically this means adding entries to dep_files.
516                 if ($lsearch eq '@LIBOBJS@')
517                 {
518                     local ($iter, $rewrite);
519                     foreach $iter (keys %libsources)
520                     {
521                         if ($iter ne 'alloca.c')
522                         {
523                             ($rewrite = $iter) =~ s/\.c$/.P/;
524                             $dep_files{'$(srcdir)/.deps/' . $rewrite} = 1;
525                             &require_file ($NORMAL, $iter);
526                         }
527                     }
528                 }
529                 elsif ($lsearch eq '@ALLOCA@')
530                 {
531                     &am_error ("\@ALLOCA\@ seen but \`AC_FUNC_ALLOCA' not in \`configure.in'")
532                         if ! defined $libsources{'alloca.c'};
533                     $dep_files{'$(srcdir)/.deps/alloca.P'} = 1;
534                     &require_file ($NORMAL, 'alloca.c');
535                 }
536             }
537         }
538         else
539         {
540             # Generate support for conditional object inclusion in
541             # libraries.
542             $output_vars .= $onelib . "_LIBADD =\n";
543         }
545         &handle_source_transform ($onelib, $obj);
547         $output_rules .=
548             &file_contents_with_transform ('s/\@LIBRARY\@/' . $onelib . '/go',
549                                            'library');
550     }
552     # Turn "foo" into "libfoo.a" and include macro definition.
553     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
555     if (! defined $contents{'LIBFILES'})
556     {
557         &pretty_print ('LIBFILES = ', '', @liblist);
558     }
559     $output_vars .= &file_contents ('libraries-vars');
562 # Handle scripts.
563 sub handle_scripts
565     # FIXME can't determine if these scripts are really being
566     # installed or not.
567     $scripts_installed = &am_install_var ('-clean',
568                                           'scripts', 'SCRIPTS',
569                                           'bin', 'sbin', 'libexec', 'noinst');
572 # Search a file for a "version.texi" Texinfo include.  Return the name
573 # of the include file if found, or the empty string if not.  A
574 # "version.texi" file is actually any file whose name matches
575 # "vers*.texi".
576 sub grep_for_vers_texi
578     local ($filename) = @_;
580     if (! open (TEXI, $filename))
581     {
582         &am_error ("couldn't open \`$filename': $!");
583         return '';
584     }
586     while (<TEXI>)
587     {
588         if (/^\@include\s+(vers[^.]*\.texi)\s*$/)
589         {
590             # Found it.
591             close (TEXI);
592             return $1;
593         }
594     }
596     close (TEXI);
597     return '';
600 # Handle all Texinfo source.
601 # Anyplace marked FIXME in this code is for multi-texi changes.
602 sub handle_texinfo
604     &am_error ("\`TEXINFOS' is an anachronism; use \`info_TEXINFOS'")
605         if defined $contents{'TEXINFOS'};
606     return if ! defined $contents{'info_TEXINFOS'};
608     local (@texis) = split (/\s+/, $contents{'info_TEXINFOS'});
609     if (@texis > 1)
610     {
611         &am_error ('sorry, only one file allowed in `info_TEXINFOS\'');
612         return;
613     }
615     local (@infos_list, @info_deps_list, @dvis_list, @texi_deps);
616     local ($infobase, $info_cursor);
617     local (%versions);
618     local ($done) = 0;
619     local ($vti);
620     local ($tc_cursor, @texi_cleans);
622     foreach $info_cursor (@texis)
623     {
624         ($infobase = $info_cursor) =~ s/\.texi$//;
626         # If 'version.texi' is referenced by input file, then include
627         # automatic versioning capability.
628         local ($vtexi)
629             = &grep_for_vers_texi ($relative_dir . "/" . $info_cursor);
630         if ($vtexi)
631         {
632             &am_error ("\`$vtexi', included in \`$info_cursor', also included in \`$versions{$vtexi}'")
633                 if (defined $versions{$vtexi});
634             $versions{$vtexi} = $info_cursor;
636             # Got a hit.
637             push (@texis, $vtexi);
638             # We number the stamp-vti files.  This is doable since the
639             # actual names don't matter much.  We only number starting
640             # with the second one, so that the common case looks nice.
641             $vti = 'vti' . ($done ? $done : '');
642             &push_dist_common ($vtexi, 'stamp-' . $vti);
643             push (@clean, $vti);
645             $output_rules .=
646                 &file_contents_with_transform
647                     ('s/\@TEXI\@/' . $info_cursor . '/g; '
648                      . 's/\@VTI\@/' . $vti . '/g; '
649                      . 's/\@VTEXI\@/' . $vtexi . '/g',
650                      'texi-version');
652             &push_phony_cleaners ($vti);
654             # Only require once.
655             &require_file ($NORMAL, 'mdate-sh') if ! $done;
656             ++$done;
657         }
659         # If user specified file_TEXINFOS, then use that as explicit
660         # dependency list.
661         @texi_deps = ();
662         push (@texi_deps, $info_cursor);
663         if (defined $contents{$infobase . "_TEXINFOS"})
664         {
665             push (@texi_deps, "\$" . $infobase . '_TEXINFOS');
666             &push_dist_common ("\$" . $infobase . '_TEXINFOS');
667         }
669         $output_rules .= ("\n" . $infobase . ".info: "
670                           . join (' ', @texi_deps) . "\n\n");
672         push (@infos_list, $infobase . '.info*');
673         push (@info_deps_list, $infobase . '.info');
674         push (@dvis_list, $infobase . '.dvi');
676         # Generate list of things to clean for this target.  We do
677         # this explicitly because otherwise too many things could be
678         # removed.  In particular the ".log" extension might
679         # reasonably be used in other contexts by the user.
680         foreach $tc_cursor ('aux', 'cp', 'cps', 'dvi', 'fn', 'fns',
681                             'ky', 'log', 'pg', 'toc', 'tp', 'vr', 'op')
682         {
683             push (@texi_cleans, $infobase . '.' . $tc_cursor);
684         }
685     }
687     # Some boilerplate.
688     $output_vars .= &file_contents ('texinfos-vars');
689     $output_rules .= &file_contents ('texinfos');
690     push (@phony, 'install-info', 'uninstall-info');
692     # How to clean.
693     $output_rules .= "\nmostlyclean-info:\n";
694     &pretty_print_rule ("\trm -f", "\t  ", @texi_cleans);
695     $output_rules .= ("\nclean-info:\n\ndistclean-info:\n\n"
696                       . "maintainer-clean-info:\n\t"
697                       . 'rm -f $(INFOS)' . "\n");
698     &push_phony_cleaners ('info');
700     push (@suffixes, '.texi', '.info', '.dvi');
701     push (@uninstall, 'uninstall-info');
702     push (@clean, 'info');
703     push (@info, '$(INFO_DEPS)');
704     push (@dvi, '$(DVIS)');
705     push (@installdirs, '$(infodir)');
706     # Make sure documentation is made and installed first.
707     unshift (@install_data, 'install-info');
708     unshift (@all, 'info');
710     $output_vars .= ("INFOS = " . join (' ', @infos_list) . "\n"
711                      . "INFO_DEPS = " . join (' ', @info_deps_list)  . "\n"
712                      . "DVIS = " . join (' ', @dvis_list) . "\n"
713                      # This next isn't strictly needed now -- the
714                      # places that look here could easily be changed
715                      # to look in info_TEXINFOS.  But this is probably
716                      # better, in case noinst_TEXINFOS is ever
717                      # supported.
718                      . "TEXINFOS = " . $contents{'info_TEXINFOS'} . "\n\n");
720     # Do some error checking.
721     &require_file ($NORMAL, 'texinfo.tex');
724 # Handle any man pages.
725 sub handle_man_pages
727     &am_error ("\`MANS' is an anachronism; use \`man_MANS'")
728         if defined $contents{'MANS'};
729     return if ! defined $contents{'man_MANS'};
731     # We generate the manpage install code by hand to avoid the use of
732     # basename in the generated Makefile.
733     local (@mans) = split (/\s+/, $contents{'man_MANS'});
734     local (%sections, %inames, %secmap, %fullsecmap);
735     foreach (@mans)
736     {
737         # FIXME: statement without effect:
738         /^(.*)\.([0-9])([a-z]*)$/;
739         $sections{$2} = 1;
740         $inames{$1} = $_;
741         $secmap{$1} = $2;
742         $fullsecmap{$1} = $2 . $3;
743     }
745     # We don't really need this, but we use it in case we ever want to
746     # support noinst_MANS.
747     $output_vars .= "MANS = " . $contents{'man_MANS'} . "\n";
749     # Generate list of install dirs.
750     $output_rules .= "install-man: \$(MANS)\n";
751     foreach (keys %sections)
752     {
753         push (@installdirs, '$(mandir)/man' . $_);
754         $output_rules .= ("\t" . '$(top_srcdir)/mkinstalldirs $(mandir)/man'
755                           . $_ . "\n");
756     }
757     push (@phony, 'install-man');
759     # Generate install target.
760     local ($key);
761     foreach $key (keys %inames)
762     {
763         $_ = $install_man_format;
764         s/\@SECTION\@/$secmap{$key}/g;
765         s/\@MAN\@/$inames{$key}/g;
766         s/\@FULLSECT\@/$fullsecmap{$key}/g;
767         s/\@MANBASE\@/$key/g;
768         $output_rules .= $_;
769     }
770     $output_rules .= "\n";
772     $output_rules .= "uninstall-man:\n";
773     foreach $key (keys %inames)
774     {
775         $_ = $uninstall_man_format;
776         s/\@SECTION\@/$secmap{$key}/g;
777         s/\@MAN\@/$inames{$key}/g;
778         s/\@FULLSECT\@/$fullsecmap{$key}/g;
779         s/\@MANBASE\@/$key/g;
780         $output_rules .= $_;
781     }
782     $output_rules .= "\n";
783     push (@phony, 'uninstall-man');
785     $output_vars .= &file_contents ('mans-vars');
787     if (! defined $options{'no-installman'})
788     {
789         push (@install_data, 'install-man');
790         push (@uninstall, 'uninstall-man');
791         push (@all, '$(MANS)');
792     }
795 # Handle DATA variables.
796 sub handle_data
798     &am_install_var ('data', 'DATA', 'data', 'sysconf',
799                      'sharedstate', 'localstate', 'pkgdata',
800                      'noinst');
803 # Handle TAGS.
804 sub handle_tags
806     local ($tagging) = 0;
808     push (@phony, 'tags');
809     if (defined $contents{'SUBDIRS'})
810     {
811         $output_rules .= &file_contents ('tags');
812         $tagging = 1;
813     }
814     elsif ($dir_holds_sources || defined $contents{'ETAGS_ARGS'})
815     {
816         $output_rules .= &file_contents ('tags-subd');
817         $tagging = 1;
818     }
820     if ($tagging)
821     {
822         $output_rules .= &file_contents ('tags-clean');
823         push (@clean, 'tags');
824         &push_phony_cleaners ('tags');
825     }
826     else
827     {
828         # Every Makefile must define some sort of TAGS rule.
829         # Otherwise, it would be possible for a top-level "make TAGS"
830         # to fail because some subdirectory failed.
831         $output_rules .= "tags: TAGS\nTAGS:\n\n";
832     }
835 # Generate actual 'dist' (or dist-shar) rule.
836 sub handle_dist_worker
838     local ($distshar) = @_;
839     local ($target) = $distshar ? 'dist-shar' : 'dist';
841     $output_rules .= $target . ': $(DEP_DISTFILES)' . "\n";
843     # Initialization; only at top level.
844     if ($relative_dir eq '.')
845     {
846         if ($strictness >= $GNITS)
847         {
848             # For Gnits users, this is pretty handy.  Look at 15 lines
849             # in case some explanatory text is desirable.
850             $output_rules .= '  @if sed 15q NEWS | grep -e "$(VERSION)" > /dev/null; then :; else \\
851           echo "NEWS not updated; not releasing" 1>&2; \\
852           exit 1;                               \\
853         fi
855         }
858         $output_rules .=
859             (
860              # Create dist directory.
861              '  rm -rf $(distdir)
862         mkdir $(distdir)
863         chmod 777 $(distdir)
865              # We need an absolute path for --output-dir.  Thus the
866              # weirdness.
867              . '        distdir=`cd $(distdir) && pwd` \\
868           && cd $(srcdir) \\
869           && automake --include-deps --output-dir=$$distdir --strictness='
870              # Set strictness of output.
871              . $strictness_name . "\n"
872              );
873     }
875     # In loop, test for file existence because sometimes a file gets
876     # included in DISTFILES twice.  For example this happens when a
877     # single source file is used in building more than one program.
878     # Also, there are situations in which "ln" can fail.  For instance
879     # a file to distribute could actually be a cross-filesystem
880     # symlink -- this can easily happen if "gettextize" was run on the
881     # distribution.  Note that DISTFILES can contain a wildcard (for
882     # info files, sigh), so we must use the echo trick.
884     $output_rules .= '  @for file in `cd $(srcdir) && echo $(DISTFILES)`; do \\
885           test -f $(distdir)/$$file \\
886           || ln $(srcdir)/$$file $(distdir)/$$file 2> /dev/null \\
887           || cp -p $(srcdir)/$$file $(distdir)/$$file; \\
888         done
891     # If we have SUBDIRS, create all dist subdirectories and do
892     # recursive build.
893     if (defined $contents{'SUBDIRS'})
894     {
895         # Test for directory existence here because previous automake
896         # invocation might have created some directories.
897         $output_rules .= '      for subdir in $(SUBDIRS); do            \\
898           test -d $(distdir)/$$subdir           \\
899           || mkdir $(distdir)/$$subdir          \\
900           || exit 1;                            \\
901           chmod 777 $(distdir)/$$subdir;        \\
902           (cd $$subdir && $(MAKE) dist) || exit 1; \\
903         done
905     }
907     # Make verbatim copies of some subdirectories if required.  This
908     # is a hack which might go away.
909     if (defined $contents{'DIST_SUBDIRS'})
910     {
911         $output_rules .= '      @for dir in $(DIST_SUBDIRS); do         \\
912           echo copying directory $$dir;         \\
913           tar chf - $$dir | (cd $(distdir) && tar xBpf -); \\
914         done
916     }
918     # Finalize.
919     if ($relative_dir eq '.')
920     {
921         $output_rules .= '      chmod -R a+r $(distdir)' . "\n\t";
922         if ($distshar)
923         {
924             $output_rules .= 'shar $(distdir) | gzip > $(distdir).shar.gz';
925         }
926         else
927         {
928             $output_rules .= 'tar chozf $(distdir).tar.gz $(distdir)';
929         }
930         $output_rules .= "\n\t" . 'rm -rf $(distdir)' . "\n";
931     }
933     push (@phony, $target);
936 # Handle 'dist' target.
937 sub handle_dist
939     # Look for common files that should be included in distribution.
940     local ($cfile);
941     foreach $cfile (@common_files)
942     {
943         if (-f ($relative_dir . "/" . $cfile))
944         {
945             &push_dist_common ($cfile);
946         }
947     }
949     # Keys of %dist_common are names of files to distributed.  We put
950     # README first because it then becomes easier to make a
951     # Usenet-compliant shar file (in these, README must be first).
952     # FIXME do more ordering of files here.
953     local (@coms);
954     if (defined $dist_common{'README'})
955     {
956         push (@coms, 'README');
957         undef $dist_common{'README'};
958     }
959     push (@coms, sort keys %dist_common);
961     &pretty_print ("DIST_COMMON =", '', @coms);
962     $output_vars .= "\n";
964     # Some boilerplate.
965     $output_vars .= &file_contents ('dist-vars');
967     # Put these things in rules section so it is easier for whoever
968     # reads Makefile.in.
969     if ($relative_dir eq '.')
970     {
971         $output_rules .= "\n" . 'distdir = $(PACKAGE)-$(VERSION)' . "\n";
972     }
973     else
974     {
975         $output_rules .= ("\nsubdir = " . $relative_dir . "\n"
976                           . 'distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)'
977                           . "\n");
978     }
980     # Generate 'dist' target, and maybe dist-shar.
981     &handle_dist_worker (0);
982     &handle_dist_worker (1) if defined $options{'dist-shar'};
985 # Handle auto-dependency code.
986 sub handle_dependencies
988     if ($use_dependencies)
989     {
990         # Include GNU-make-specific auto-dep code.
991         if ($dir_holds_sources)
992         {
993             &pretty_print ('DEP_FILES =', '', sort keys %dep_files);
994             $output_rules .= &file_contents ('depend');
995         }
996     }
997     else
998     {
999         # Include any auto-generated deps that are present.
1000         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
1001         {
1002             local ($depfile);
1003             local ($gpat) = $relative_dir . "/.deps/*.P";
1005             foreach $depfile (<${gpat}>)
1006             {
1007                 if (! open (DEP_FILE, $depfile))
1008                 {
1009                     &am_error ("couldn't open $depfile", $!);
1010                     next;
1011                 }
1013                 # Slurp entire file.
1014                 $output_rules .= join ('', <DEP_FILE>);
1016                 close (DEP_FILE);
1017             }
1019             $output_rules .= "\n";
1020         }
1021     }
1024 # Handle subdirectories.
1025 sub handle_subdirs
1027     if (! defined $contents{'SUBDIRS'})
1028     {
1029         &am_conf_error
1030             ("ud_GNU_GETTEXT in configure.in but SUBDIRS not defined")
1031                 if $seen_gettext && $relative_dir eq '.';
1032         return;
1033     }
1035     &am_conf_error
1036         ("ud_GNU_GETTEXT in configure.in but \`po' not in SUBDIRS")
1037             if $seen_gettext && $contents{'SUBDIRS'} !~ /\bpo\b/;
1038     &am_conf_error
1039         ("ud_GNU_GETTEXT in configure.in but \`intl' not in SUBDIRS")
1040             if $seen_gettext && $contents{'SUBDIRS'} !~ /\bintl\b/;
1042     &require_file ($NORMAL, 'ABOUT-NLS') if $seen_gettext;
1044     return if ! defined $contents{'SUBDIRS'};
1046     $output_rules .= &file_contents ('subdirs');
1048     # Push a bunch of phony targets.
1049     local ($phonies);
1050     foreach $phonies ('-data', '-exec', 'dirs')
1051     {
1052         push (@phony, 'install' . $phonies . '-recursive');
1053         push (@phony, 'uninstall' . $phonies . '-recursive');
1054     }
1055     foreach $phonies ('all', 'check', 'installcheck', 'info', 'dvi')
1056     {
1057         push (@phony, $phonies . '-recursive');
1058     }
1059     &push_phony_cleaners ('recursive');
1061     push (@all, "all-recursive");
1062     push (@check, "check-recursive");
1063     push (@installcheck, "installcheck-recursive");
1064     push (@info, "info-recursive");
1065     push (@dvi, "dvi-recursive");
1067     $recursive_install = 1;
1070 # Handle remaking and configure stuff.
1071 sub handle_configure
1073     # If SUBDIRS defined, require AC_PROG_MAKE_SET.
1074     &am_error ("AC_PROG_MAKE_SET must be used in configure.in")
1075         if defined $contents{'SUBDIRS'} && ! $seen_make_set;
1077     local ($top_reldir);
1078     if ($relative_dir ne '.')
1079     {
1080         # In subdirectory.
1081         $output_rules .= &file_contents ('remake-subd');
1082         $top_reldir = '../';
1083     }
1084     else
1085     {
1086         if (-f 'aclocal.m4')
1087         {
1088             $output_vars .= "ACLOCAL = aclocal.m4\n";
1089             &push_dist_common ('aclocal.m4');
1090         }
1091         $output_rules .= &file_contents ('remake');
1093         # Look for some files we need.
1094         &require_file ($NORMAL, 'install-sh', 'mkinstalldirs');
1096         &am_error
1097             ("\`install.sh' is an anachronism; use \`install-sh' instead")
1098                 if -f $relative_dir . '/install.sh';
1100         # If we have a configure header, require it.
1101         if ($config_header)
1102         {
1103             # FIXME this restriction should be lifted.
1104             # FIXME first see if it is even needed as-is.
1105             &am_error ("argument to AC_CONFIG_HEADER contains \`/'\n")
1106                 if ($config_header =~ /\//);
1108             &require_file ($NORMAL, $config_header);
1110             # Header defined and in this directory.
1111             if (-f 'acconfig.h')
1112             {
1113                 $output_vars .= "ACCONFIG = acconfig.h\n";
1114                 &push_dist_common ('acconfig.h');
1115             }
1116             if (-f $config_name . '.top')
1117             {
1118                 $output_vars .= "CONFIG_TOP = ${config_name}.top\n";
1119                 &push_dist_common ($config_name . '.top');
1120             }
1121             if (-f $config_name . '.bot')
1122             {
1123                 $output_vars .= "CONFIG_BOT = ${config_name}.bot\n";
1124                 &push_dist_common ($config_name . '.bot');
1125             }
1127             &push_dist_common ('stamp-h.in');
1129             $output_rules .= &file_contents ('remake-hdr');
1130             $output_vars .= "CONFIG_HEADER_IN = ${config_header}\n";
1131         }
1133         $top_reldir = '';
1134     }
1136     &am_error ("\`CONFIG_HEADER' is an anachronism; now determined from \`configure.in'")
1137         if defined $contents{'CONFIG_HEADER'};
1139     # Generate CONFIG_HEADER define, and define interally.
1140     $output_vars .= "CONFIG_HEADER = ${top_builddir}/${config_name}\n"
1141         if $config_name;
1142     $contents{'CONFIG_HEADER'} = "${top_builddir}/${config_name}"
1143         if $config_name;
1145     # Now look for other files in this directory which must be remade
1146     # by config.status, and generate rules for them.
1147     local ($file, $local);
1148     foreach $file (@other_input_files)
1149     {
1150         # Skip files not in this directory, any Makefile, and the
1151         # config header.  These last two must be handled specially.
1152         next unless &dirname ($file) eq $relative_dir;
1153         next if $file eq $top_builddir . '/' . $config_name;
1154         ($local = $file) =~ s/^.*\///;
1155         next if $local eq 'Makefile';
1157         # FIXME support ":" syntax of AC_OUTPUT here.
1158         $output_rules .= ($local . ': '
1159                           . '$(top_builddir)/config.status ' . $local . ".in\n"
1160                           . "\t"
1161                           . 'cd $(top_srcdir) && CONFIG_FILES='
1162                           . ($relative_dir eq '.' ? '' : '$(subdir)/')
1163                           . '$@ CONFIG_HEADERS= ./config.status'
1164                           . "\n");
1165         &require_file ($NORMAL, $local . '.in');
1166     }
1169 # Handle C headers.
1170 sub handle_headers
1172     &am_install_var ('header', 'HEADERS', 'include',
1173                      'oldinclude', 'pkginclude',
1174                      'noinst');
1177 # Handle footer elements.
1178 sub handle_footer
1180     if ($contents{'SOURCES'})
1181     {
1182         &pretty_print ('SOURCES =', '', split (/\s+/, $contents{'SOURCES'}));
1183     }
1184     if ($contents{'OBJECTS'})
1185     {
1186         &pretty_print ('OBJECTS =', '', split (/\s+/, $contents{'OBJECTS'}));
1187     }
1188     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
1189     {
1190         $output_vars .= "\n";
1191     }
1193     if (defined $contents{'SUFFIXES'})
1194     {
1195         push (@suffixes, '$(SUFFIXES)');
1196     }
1198     $output_trailer .= ".SUFFIXES:\n";
1199     if (@suffixes)
1200     {
1201         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
1202     }
1203     $output_trailer .= &file_contents ('footer');
1206 # Deal with installdirs target.
1207 sub handle_installdirs
1209     # GNU Makefile standards recommend this.
1210     $output_rules .= ("installdirs:"
1211                       . ($recursive_install
1212                          ? " installdirs-recursive\n"
1213                          : "\n"));
1214     push (@phony, 'installdirs');
1215     if (@installdirs)
1216     {
1217         &pretty_print_rule ("\t\$(top_srcdir)/mkinstalldirs ", "\t\t",
1218                             @installdirs);
1219     }
1220     $output_rules .= "\n";
1223 # There are several targets which need to be merged.  This is because
1224 # their complete definition is compiled from many parts.  Note that we
1225 # avoid double colon rules, otherwise we'd use them instead.
1226 sub handle_merge_targets
1228     &do_one_merge_target ('all', @all);
1229     &do_one_merge_target ('info', @info);
1230     &do_one_merge_target ('dvi', @dvi);
1232     if (! defined $contents{'SUBDIRS'} || $relative_dir ne '.')
1233     {
1234         # 'check' must depend on 'all', but not at top level.
1235         unshift (@check, 'all');
1236     }
1237     &do_one_merge_target ('check', @check);
1238     &do_one_merge_target ('installcheck', @installcheck);
1240     # Handle the various install targets specially.  We do this so
1241     # that (eg) "make install-exec" will run "install-exec-recursive"
1242     # if required, but "make install" won't run it twice.  Step one is
1243     # to see if the user specified local versions of any of the
1244     # targets we handle.
1245     if (defined $contents{'install-exec-local'})
1246     {
1247         push (@install_exec, 'install-exec-local');
1248     }
1249     if (defined $contents{'install-data-local'})
1250     {
1251         push (@install_data, 'install-data-local');
1252     }
1253     if (defined $contents{'uninstall-local'})
1254     {
1255         push (@uninstall, 'uninstall-local');
1256     }
1258     if (defined $contents{'install-local'})
1259     {
1260         &am_error ("use \`install-data' or \`install-exec', not \`install'");
1261     }
1263     # Step two: if we are doing recursive makes, write out the
1264     # appropriate rules.
1265     local (@install);
1266     if ($recursive_install)
1267     {
1268         push (@install, 'install-recursive');
1269         push (@uninstall, 'uninstall-recursive');
1271         if (@install_exec)
1272         {
1273             $output_rules .= ('install-exec-am: '
1274                               . join (' ', @install_exec)
1275                               . "\n\n");
1276             @install_exec = ('install-exec-recursive', 'install-exec-am');
1277             push (@install, 'install-exec-am');
1278             push (@phony, 'install-exec-am');
1279         }
1280         if (@install_data)
1281         {
1282             $output_rules .= ('install-data-am: '
1283                               . join (' ', @install_data)
1284                               . "\n\n");
1285             @install_data = ('install-data-recursive', 'install-data-am');
1286             push (@install, 'install-data-am');
1287             push (@phony, 'install-data-am');
1288         }
1289         if (@uninstall)
1290         {
1291             $output_rules .= ('uninstall-am: '
1292                               . join (' ', @uninstall)
1293                               . "\n\n");
1294             @uninstall = ('uninstall-recursive', 'uninstall-am');
1295             push (@phony, 'uninstall-am');
1296         }
1297     }
1299     # Step three: print definitions users can use.
1300     if (@install_exec)
1301     {
1302         $output_rules .= ("install-exec: "
1303                           . join (' ', @install_exec)
1304                           . "\n\n");
1305         push (@install, 'install-exec') if !$recursive_install;
1306         push (@phony, 'install-exec');
1307     }
1308     if (@install_data)
1309     {
1310         $output_rules .= ("install-data: "
1311                           . join (' ', @install_data)
1312                           . "\n\n");
1313         push (@install, 'install-data') if !$recursive_install;
1314         push (@phony, 'install-data');
1315     }
1317     # If no dependencies for 'install', add 'all'.  Why?  That way
1318     # "make install" at top level of distclean'd distribution won't
1319     # fail because stuff in 'lib' fails to build.
1320     push (@install, 'all') if ! @install;
1321     $output_rules .= ('install: '
1322                       . join (' ', @install)
1323                       # Use "@:" as empty command so nothing prints.
1324                       . "\n\t\@:"
1325                       . "\n\n"
1326                       . 'uninstall: '
1327                       . join (' ', @uninstall)
1328                       . "\n\n");
1329     push (@phony, 'install', 'uninstall');
1332 # Helper for handle_merge_targets.
1333 sub do_one_merge_target
1335     local ($name, @values) = @_;
1337     if (defined $contents{$name . '-local'})
1338     {
1339         # User defined local form of target.  So include it.
1340         push (@values, $name . '-local');
1341         push (@phony, $name . '-local');
1342     }
1344     $output_rules .= $name . ":";
1345     if (@values)
1346     {
1347         $output_rules .= ' ' . join (' ', @values);
1348     }
1349     $output_rules .= "\n\n";
1350     push (@phony, $name);
1353 # Handle all 'clean' targets.
1354 sub handle_clean
1356     push (@clean, 'generic');
1357     $output_rules .= &file_contents ('clean');
1358     &push_phony_cleaners ('generic');
1360     local ($target) = $recursive_install ? 'clean-am' : 'clean';
1361     &do_one_clean_target ($target, 'mostly', '', @clean);
1362     &do_one_clean_target ($target, '', 'mostly', @clean);
1363     &do_one_clean_target ($target, 'dist', '', @clean);
1364     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
1366     push (@phony, 'clean', 'mostlyclean', 'distclean', 'maintainer-clean');
1368     local (@deps);
1369     if ($recursive_install)
1370     {
1371         @deps = ('am', 'recursive');
1372         &do_one_clean_target ('', 'mostly', '', @deps);
1373         &do_one_clean_target ('', '', '', @deps);
1374         &do_one_clean_target ('', 'dist', '', @deps);
1375         &do_one_clean_target ('', 'maintainer-', '', @deps);
1376     }
1379 # Helper for handle_clean.
1380 sub do_one_clean_target
1382     local ($target, $name, $last_name, @deps) = @_;
1384     # Special case: if target not passed, then don't generate
1385     # dependency on next "lower" clean target (eg no
1386     # clean<-mostlyclean derivation).  In this case the target is
1387     # implicitly known to be 'clean'.
1388     local ($flag) = $target;
1389     $target = 'clean' if ! $flag;
1391     grep (($_ = $name . 'clean-' . $_) && 0, @deps);
1392     if ($flag)
1393     {
1394         if ($last_name || $name ne 'mostly')
1395         {
1396             push (@deps, $last_name . $target . " ");
1397         }
1398     }
1399     &pretty_print_rule ($name . $target . ": ", '', @deps);
1401     # FIXME shouldn't we really print these messages before running
1402     # the dependencies?
1403     if ($name . $target eq 'maintainer-clean')
1404     {
1405         # Print a special warning.
1406         $output_rules .=
1407             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
1408              . "\t\@echo \"it deletes files that may require special "
1409              . "tools to rebuild.\"\n");
1411         $output_rules .= "\trm -f config.status\n"
1412             if $relative_dir eq '.';
1413     }
1414     elsif ($name . $target eq 'distclean')
1415     {
1416         $output_rules .= "\trm -f config.status\n";
1417     }
1418     $output_rules .= "\n";
1421 # Handle .PHONY target.
1422 sub handle_phony
1424     &pretty_print_rule ('.PHONY:', '', @phony);
1425     $output_rules .= "\n";
1428 ################################################################
1430 # Scan configure.in for interesting things.
1431 # FIXME ensure VERSION, PACKAGE are set.
1432 sub scan_configure
1434     open (CONFIGURE, 'configure.in')
1435         || die "automake: couldn't open configure.in: $!\n";
1437     # Reinitialize libsources here.  This isn't really necessary,
1438     # since we currently assume there is only one configure.in.  But
1439     # that won't always be the case.
1440     %libsources = ();
1442     local ($in_ac_output, @make_list) = 0;
1443     local ($seen_arg_prog) = 0;
1444     local ($seen_canonical) = 0;
1445     while (<CONFIGURE>)
1446     {
1447         # Remove comments from current line.
1448         s/\bdnl\b.*$//;
1449         s/\#.*$//;
1451         # Populate libobjs array.
1452         if (/AC_FUNC_ALLOCA/)
1453         {
1454             $libsources{'alloca.c'} = 1;
1455         }
1456         elsif (/AC_FUNC_GETLOADAVG/)
1457         {
1458             $libsources{'getloadavg.c'} = 1;
1459         }
1460         elsif (/AC_FUNC_MEMCMP/)
1461         {
1462             $libsources{'memcmp.c'} = 1;
1463         }
1464         elsif (/AC_STRUCT_ST_BLOCKS/)
1465         {
1466             $libsources{'fileblocks.c'} = 1;
1467         }
1468         elsif (/AC_REPLACE_FUNCS\s*\((.*)\)/)
1469         {
1470             foreach (split (/\s+/, $1))
1471             {
1472                 $libsources{$_ . '.c'} = 1;
1473             }
1474         }
1475         elsif (/LIBOBJS="(.*)\.o\s+\$LIBOBJS"/)
1476         {
1477             $libsources{$1 . '.c'} = 1;
1478         }
1479         elsif (/LIBOBJS="\$LIBOBJS\s+(.*)\.o"/)
1480         {
1481             $libsources{$1 . '.c'} = 1;
1482         }
1484         # Process the AC_OUTPUT macro.
1485         if (! $in_ac_output && s/AC_OUTPUT\s*\(\[?//)
1486         {
1487             $in_ac_output = 1;
1488         }
1489         if ($in_ac_output)
1490         {
1491             s/\]//;
1492             $in_ac_output = 0 if s/[\),]//;
1494             # Look at potential Makefile.am's.
1495             foreach (split)
1496             {
1497                 if (-f $_ . '.am')
1498                 {
1499                     push (@make_list, $_);
1500                 }
1501                 else
1502                 {
1503                     push (@other_input_files, $_);
1504                 }
1505             }
1506         }
1508         # Check for ansi2knr.
1509         $fp_c_prototypes = 1 if /fp_C_PROTOTYPES/;
1511         # Check for NLS support.
1512         if (/ud_GNU_GETTEXT/)
1513         {
1514             $seen_gettext = 1;
1515         }
1517         # Handle configuration headers.
1518         if (/AC_CONFIG_HEADER\s*\((.*)\)/)
1519         {
1520             $config_name = $1;
1521             if ($config_name =~ /^([^:]+):(.+)$/)
1522             {
1523                 $config_name = $1;
1524                 $config_header = $2;
1525             }
1526             else
1527             {
1528                 $config_header = $config_name . '.in';
1529             }
1530         }
1532         $seen_canonical = 1 if /AC_CANONICAL_(HOST|SYSTEM)/;
1534         # Some things required by Automake.  FIXME we only really
1535         # require fp_PROG_INSTALL if some scripts are installed.  And
1536         # we only really require AC_ARG_PROGRAM if any program is
1537         # installed.
1538         $seen_make_set = 1 if /AC_PROG_MAKE_SET/;
1539         $seen_prog_install = 1 if ! $seen_prog_install && /AC_PROG_INSTALL/;
1540         $seen_prog_install = 2 if ! $seen_prog_install && /fp_PROG_INSTALL/;
1541         $seen_arg_prog = 1 if /AC_ARG_PROGRAM/;
1542     }
1544     # Set input files if not specified by user.
1545     @input_files = @make_list if (! @input_files);
1547     # AC_CANONICAL_HOST and AC_CANONICAL_SYSTEM need config.guess and
1548     # config.sub.
1549     &require_file ($NORMAL, 'config.guess', 'config.sub')
1550         if $seen_canonical;
1552     &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1553         unless $seen_arg_prog;
1555     close (CONFIGURE);
1558 ################################################################
1560 # Do any extra checking for GNU standards.
1561 sub check_gnu_standards
1563     &require_file ($GNU, 'ChangeLog');
1565     if ($relative_dir eq '.')
1566     {
1567         # In top level (or only) directory.
1568         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
1569                        'AUTHORS');
1570     }
1573 # Do any extra checking for GNITS standards.
1574 sub check_gnits_standards
1576     if ($strictness >= $GNITS && -f $relative_dir . '/COPYING.LIB')
1577     {
1578         &am_error
1579             ("\`${relative_dir}/COPYING.LIB' disallowed by Gnits standards");
1580     }
1582     if ($relative_dir eq '.')
1583     {
1584         # In top level (or only) directory.
1585         &require_file ($GNITS, 'THANKS');
1586     }
1589 ################################################################
1591 # Pretty-print something.  HEAD is what should be printed at the
1592 # beginning of the first line, FILL is what should be printed at the
1593 # beginning of every subsequent line.
1594 sub pretty_print_internal
1596     local ($head, $fill, @values) = @_;
1598     local ($column) = length ($head);
1599     local ($result) = $head;
1601     local ($bol) = 0;
1602     foreach (@values)
1603     {
1604         # "71" because we also print a space.
1605         if ($column + length ($_) > 71)
1606         {
1607             $result .= " \\\n" . $fill;
1608             $column = length ($fill);
1609             $bol = 1;
1610         }
1612         $result .= ' ' unless ($bol);
1613         $result .= $_;
1614         $column += length ($_) + 1;
1615         $bol = 0;
1616     }
1618     $result .= "\n";
1619     return $result;
1622 # Pretty-print something and append to output_vars.
1623 sub pretty_print
1625     $output_vars .= &pretty_print_internal (@_);
1628 # Pretty-print something and append to output_rules.
1629 sub pretty_print_rule
1631     $output_rules .= &pretty_print_internal (@_);
1635 ################################################################
1637 # Read Makefile.am and set up %contents.  Simultaneously copy lines
1638 # from Makefile.am into $output_trailer or $output_vars as
1639 # appropriate.  NOTE we put rules in the trailer section.  We want
1640 # user rules to come after our generated stuff.
1641 sub read_am_file
1643     local ($amfile) = @_;
1645     # Compute relative location of the top object directory.
1646     local (@topdir) = ();
1647     foreach (split (/\//, $relative_dir))
1648     {
1649         next if $_ eq '.' || $_ eq '';
1650         if ($_ eq '..')
1651         {
1652             pop @topdir;
1653         }
1654         else
1655         {
1656             push (@topdir, '..');
1657         }
1658     }
1659     @topdir = ('.') if ! @topdir;
1661     $top_builddir = join ('/', @topdir);
1662     local ($build_rx);
1663     ($build_rx = $top_builddir) =~ s/(\W)/\\$1/g;
1664     local ($header_vars) =
1665         &file_contents_with_transform
1666             ('s/\@top_builddir\@/' . $build_rx . '/g',
1667              'header-vars');
1669     open (AM_FILE, $amfile) || die "automake: couldn't open $amfile: $!\n";
1671     $output_vars .= ("# Makefile.in generated automatically by automake "
1672                      . $VERSION . " from Makefile.am\n");
1674     # Generate copyright for generated Makefile.in.
1675     $output_vars .= $gen_copyright;
1677     local ($saw_bk) = 0;
1678     local ($was_rule) = 0;
1679     local ($spacing) = '';
1680     local ($comment) = '';
1681     local ($last_var_name) = '';
1683     while (<AM_FILE>)
1684     {
1685         if (/$IGNORE_PATTERN/o)
1686         {
1687             # Merely delete comments beginning with two hashes.
1688         }
1689         elsif (/$WHITE_PATTERN/o)
1690         {
1691             # Stick a single white line before the incoming macro or rule.
1692             $spacing = "\n";
1693         }
1694         elsif (/$COMMENT_PATTERN/o)
1695         {
1696             # Stick comments before the incoming macro or rule.
1697             $comment .= $spacing . $_;
1698             $spacing = '';
1699         }
1700         else
1701         {
1702             last;
1703         }
1704     }
1706     $output_vars .= $comment . "\n" . $header_vars;
1707     $comment = '';
1708     $spacing = "\n";
1710     while ($_)
1711     {
1712         if (/$IGNORE_PATTERN/o)
1713         {
1714             # Merely delete comments beginning with two hashes.
1715         }
1716         elsif (/$WHITE_PATTERN/o)
1717         {
1718             # Stick a single white line before the incoming macro or rule.
1719             $spacing = "\n";
1720         }
1721         elsif (/$COMMENT_PATTERN/o)
1722         {
1723             # Stick comments before the incoming macro or rule.
1724             $comment .= $spacing . $_;
1725             $spacing = '';
1726         }
1727         elsif ($saw_bk)
1728         {
1729             if ($was_rule)
1730             {
1731                 $output_trailer .= $_;
1732                 $saw_bk = /\\$/;
1733             }
1734             else
1735             {
1736                 $output_vars .= $_;
1737                 $saw_bk = /\\$/;
1738                 # Chop newline and backslash if this line is
1739                 # continued.  FIXME maybe ensure trailing whitespace
1740                 # exists?
1741                 chop if $saw_bk;
1742                 chop if $saw_bk;
1743                 $contents{$last_var_name} .= $_;
1744             }
1745         }
1746         elsif (/$RULE_PATTERN/o)
1747         {
1748             # warn "** Saw rule .$1.\n";
1749             # Found a rule.
1750             $was_rule = 1;
1751             # Value here doesn't matter; for targets we only note
1752             # existence.
1753             $contents{$1} = 1;
1754             $output_trailer .= $comment . $spacing . $_;
1755             $comment = $spacing = '';
1756             $saw_bk = /\\$/;
1757         }
1758         elsif (/$MACRO_PATTERN/o)
1759         {
1760             # warn "** Saw macro .$1.\n";
1761             # Found a macro definition.
1762             $was_rule = 0;
1763             $last_var_name = $1;
1764             if (substr ($2, -1) eq "\\")
1765             {
1766                 $contents{$1} = substr ($2, 0, length ($2) - 1);
1767             }
1768             else
1769             {
1770                 $contents{$1} = $2;
1771             }
1772             $output_vars .= $comment . $spacing . $_;
1773             $comment = $spacing = '';
1774             $saw_bk = /\\$/;
1775         }
1776         elsif ($_ eq "\@kr\@\n")
1777         {
1778             # Special case: this means we want automatic
1779             # de-ANSI-fication.  This is deprecated.  Remove in the
1780             # future.
1781             $contents{'@kr@'} = 1;
1782             &am_error ('`@kr@\' is deprecated; put `ansi2knr\' in AUTOMAKE_OPTIONS instead');
1783         }
1784         else
1785         {
1786             # This isn't an error; it is probably a continued rule.
1787             # In fact, this is what we assume.
1788             $was_rule = 1;
1789             $output_trailer .= $comment . $spacing . $_;
1790             $comment = $spacing = '';
1791             $saw_bk = /\\$/;
1792         }
1794         $_ = <AM_FILE>;
1795     }
1797     $output_trailer .= $comment;
1800 ################################################################
1802 sub initialize_global_constants
1804     # Associative array of standard directory names.  Entry is TRUE if
1805     # corresponding directory should be installed during
1806     # 'install-exec' phase.
1807     %exec_dir_p =
1808         ('bin', 1,
1809          'sbin', 1,
1810          'libexec', 1,
1811          'data', 0,
1812          'sysconf', 1,
1813          'localstate', 1,
1814          'lib', 1,
1815          'info', 0,
1816          'man', 0,
1817          'include', 0,
1818          'oldinclude', 0,
1819          'pkgdata', 0,
1820          'pkglib', 1,
1821          'pkginclude', 0
1822          );
1824     # Helper text for dealing with man pages.
1825     $install_man_format =
1826     '   @sect=@SECTION@;                                \\
1827         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1828         echo installing @MAN@ as $(mandir)/man$$sect/$$inst; \\
1829         $(INSTALL_DATA) $(srcdir)/@MAN@ $(mandir)/man$$sect/$$inst
1832     $uninstall_man_format =
1833     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1834         rm -f $(mandir)/man@SECTION@/$$inst
1837     # Commonly found files we look for and automatically include in
1838     # DISTFILES.
1839     @common_files =
1840         (
1841          "README", "THANKS", "TODO", "NEWS", "COPYING", "COPYING.LIB",
1842          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
1843          "config.guess", "config.sub", "AUTHORS", "BACKLOG", "ABOUT-GNU"
1844          );
1846     # Commonly used files we auto-include, but only sometimes.
1847     @common_sometimes =
1848         (
1849          "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
1850          "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
1851          "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
1852          );
1854     $USAGE = "\
1855   --amdir=DIR           directory storing config files
1856   --gnits               same as --strictness=gnits
1857   --gnu                 same as --strictness=gnu
1858   --help                print this help, then exit
1859   --include-deps        include generated dependencies in Makefile.in
1860   --install-missing     install missing standard files
1861   --output-dir=DIR      put generated Makefile.in's into DIR
1862   --strictness=LEVEL    set strictness level.  LEVEL is normal, gnu, gnits
1863   --version             print version number, then exit\n";
1865     # Copyright on generated Makefile.ins.
1866     $gen_copyright = "\
1867 # Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
1868 # This Makefile.in is free software; the Free Software Foundation
1869 # gives unlimited permission to copy, distribute and modify it.
1873 # (Re)-Initialize per-Makefile.am variables.
1874 sub initialize_per_input
1876     # These two variables are used when generating each Makefile.in.
1877     # They hold the Makefile.in until it is ready to be printed.
1878     $output_rules = '';
1879     $output_vars = '';
1880     $output_trailer = '';
1882     # Suffixes found during a run.
1883     @suffixes = ();
1885     # This holds the contents of a Makefile.am, as parsed by
1886     # read_am_file.
1887     %contents = ();
1889     # This holds the "relative directory" of the current Makefile.in.
1890     # Eg for src/Makefile.in, this is "src".
1891     $relative_dir = '';
1893     # Directory where output files go.  Actually, output files are
1894     # relative to this directory.
1895     $output_directory = '.';
1897     # This holds a list of files that are included in the
1898     # distribution.
1899     %dist_common = ();
1901     # List of dependencies for the obvious targets.
1902     @install_data = ();
1903     @install_exec = ();
1904     @uninstall = ();
1905     @installdirs = ();
1907     @info = ();
1908     @dvi = ();
1909     @all = ();
1910     @check = ();
1911     @installcheck = ();
1912     @clean = ();
1914     @phony = ();
1916     # These are pretty obvious, too.  They are used to define the
1917     # SOURCES and OBJECTS variables.
1918     @sources = ();
1919     @objects = ();
1921     # TRUE if current directory holds any C source files.  (Actually
1922     # holds object extension, but this information is encapsulated in
1923     # the function get_object_extension).
1924     $dir_holds_sources = '';
1926     # TRUE if install targets should work recursively.
1927     $recursive_install = 0;
1929     # All .P files.
1930     %dep_files = ();
1932     # Strictness levels.
1933     $strictness = $default_strictness;
1934     $strictness_name = $default_strictness_name;
1936     # Options from AUTOMAKE_OPTIONS.
1937     %options = ();
1941 ################################################################
1943 # Return contents of a file from $am_dir, automatically skipping
1944 # macros or rules which are already known.  Runs command on each line
1945 # as it is read; this command can modify $_.
1946 sub file_contents_with_transform
1948     local ($command, $basename) = @_;
1949     local ($file) = $am_dir . '/' . $basename . '.am';
1951     open (FC_FILE, $file)
1952         || die "automake: installation error: cannot open \`$file'\n";
1954     local ($was_rule) = 0;
1955     local ($result_vars) = '';
1956     local ($result_rules) = '';
1957     local ($comment) = '';
1958     local ($spacing) = "\n";
1959     local ($skipping) = 0;
1961     while (<FC_FILE>)
1962     {
1963         eval $command;
1965         if (/$IGNORE_PATTERN/o)
1966         {
1967             # Merely delete comments beginning with two hashes.
1968         }
1969         elsif (/$WHITE_PATTERN/o)
1970         {
1971             # Stick a single white line before the incoming macro or rule.
1972             $spacing = "\n";
1973         }
1974         elsif (/$COMMENT_PATTERN/o)
1975         {
1976             # Stick comments before the incoming macro or rule.
1977             $comment .= $spacing . $_;
1978             $spacing = '';
1979         }
1980         elsif ($saw_bk)
1981         {
1982             if ($was_rule)
1983             {
1984                 $result_rules .= $_ if ! $skipping;
1985             }
1986             else
1987             {
1988                 $result_vars .= $_ if ! $skipping;
1989             }
1990             $saw_bk = /\\$/;
1991         }
1992         elsif (/$RULE_PATTERN/o)
1993         {
1994             # warn "** Found rule .$1.\n";
1995             # Found a rule.
1996             $was_rule = 1;
1997             $skipping = defined $contents{$1};
1998             # warn "** Skip $skipping\n" if $skipping;
1999             $result_rules .= $comment . $spacing . $_ if ! $skipping;
2000             $comment = $spacing = '';
2001             $saw_bk = /\\$/;
2002         }
2003         elsif (/$MACRO_PATTERN/o)
2004         {
2005             # warn "** Found macro .$1.\n";
2006             # Found a variable reference.
2007             $was_rule = 0;
2008             $skipping = defined $contents{$1};
2009             # warn "** Skip $skipping\n" if $skipping;
2010             $result_vars .= $comment . $spacing . $_ if ! $skipping;
2011             $comment = $spacing = '';
2012             $saw_bk = /\\$/;
2013         }
2014         else
2015         {
2016             # This isn't an error; it is probably a continued rule.
2017             # In fact, this is what we assume.
2018             $was_rule = 1;
2019             $result_rules .= $comment . $spacing . $_ if ! $skipping;
2020             $comment = $spacing = '';
2021             $saw_bk = /\\$/;
2022         }
2023     }
2025     close (FC_FILE);
2026     return $result_vars . $result_rules . $comment;
2029 # Like file_contents_with_transform, but no transform.
2030 sub file_contents
2032     return &file_contents_with_transform ('', @_);
2035 # Handle `where_HOW' variable magic.  Does all lookups, generates
2036 # install code,and possibly generates code to define the primary
2037 # variable.  The first argument is the name of the .am file to munge,
2038 # the second argument is the primary variable (eg HEADERS), and all
2039 # subsequent arguments are possible installation locations.  Returns
2040 # list of all values of all _HOW targets.
2042 # FIXME this should be rewritten to be cleaner.  It should be broken
2043 # up into multiple functions.
2045 # Usage is: am_install_var (OPTION..., file, HOW, where...)
2046 sub am_install_var
2048     local (@args) = @_;
2050     local ($do_all, $do_clean) = (1, 0);
2051     while (@args)
2052     {
2053         if ($args[0] eq '-clean')
2054         {
2055             $do_clean = 1;
2056         }
2057         elsif ($args[0] eq '-no-all')
2058         {
2059             $do_all = 0;
2060         }
2061         elsif ($args[0] !~ /^-/)
2062         {
2063             last;
2064         }
2065         shift (@args);
2066     }
2067     local ($file, $primary, @prefixes) = @args;
2069     local (@used) = ();
2070     local (@result) = ();
2072     local ($clean_file) = $file . '-clean';
2073     local ($one_name);
2074     local ($X);
2075     foreach $X (@prefixes)
2076     {
2077         $one_name = $X . '_' . $primary;
2078         if (defined $contents{$one_name})
2079         {
2080             # Append actual contents to result.
2081             push (@result, split (/\s+/, $contents{$one_name}));
2083             if ($do_clean)
2084             {
2085                 $output_rules .=
2086                     &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
2087                                                    $clean_file);
2089                 push (@clean, $X . $primary);
2090                 &push_phony_cleaners ($X . $primary);
2091             }
2093             push (@used, '$(' . $one_name . ')');
2094             if ($X eq 'noinst')
2095             {
2096                 # Objects in noinst_FOO never get installed.
2097                 next;
2098             }
2100             $output_rules .=
2101                 &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
2102                                                $file);
2104             push (@uninstall, 'uninstall-' . $X . $primary);
2105             push (@phony, 'uninstall-' . $X . $primary);
2106             push (@installdirs, '$(' . $X . 'dir)');
2107             if ($exec_dir_p{$X})
2108             {
2109                 push (@install_exec, 'install-' . $X . $primary);
2110             }
2111             else
2112             {
2113                 push (@install_data, 'install-' . $X . $primary);
2114             }
2115         }
2116     }
2118     if (! defined $contents{$primary} && @used)
2119     {
2120         # Define it.
2121         &pretty_print ($primary . ' =', '', @used);
2122         $output_vars .= "\n";
2123     }
2125     # Push here because PRIMARY might be configure time determined.
2126     push (@all, '$(' . $primary . ')') if ($do_all && @used);
2128     # Look for misspellings.  It is an error to have a variable ending
2129     # in a "reserved" suffix whose prefix is unknown, eg
2130     # "bni_PROGRAMS".
2131     local (%valid, $varname);
2132     grep ($valid{$_} = 0, @prefixes);
2133     foreach $varname (keys %contents)
2134     {
2135         if ($varname =~ /^(.*)_$primary$/ && ! defined $valid{$1})
2136         {
2137             &am_error ("invalid variable \"$varname\"");
2138         }
2139     }
2141     return (@result);
2145 ################################################################
2147 # Verify that the file must exist in the current directory.
2148 # Usage: require_file (strictness, file)
2149 # strictness is the strictness level at which this file becomes
2150 # required.
2151 sub require_file
2153     local ($mystrict, @files) = @_;
2154     local ($file, $fullfile);
2156     foreach $file (@files)
2157     {
2158         $fullfile = $relative_dir . "/" . $file;
2160         if (-f $fullfile)
2161         {
2162             &push_dist_common ($file);
2163         }
2164         elsif ($strictness >= $mystrict)
2165         {
2166             # Only install missing files according to our desired
2167             # strictness level.
2168             if ($install_missing && -f ($am_dir . '/' . $file))
2169             {
2170                 # Install the missing file.  Symlink if we can, copy
2171                 # if we must.
2172                 if ($symlink_exists)
2173                 {
2174                     symlink ($am_dir . '/' . $file, $fullfile);
2175                 }
2176                 else
2177                 {
2178                     system ('cp', $am_dir . '/' . $file, $fullfile);
2179                 }
2180                 &am_error
2181                     ("required file \"$fullfile\" not found; installing");
2182             }
2183             else
2184             {
2185                 # Only an error if strictness constraint violated.
2186                 &am_error ("required file \"$fullfile\" not found");
2187             }
2188         }
2189     }
2192 # Push a list of files onto dist_common.
2193 sub push_dist_common
2195     local (@files) = @_;
2196     local ($file);
2198     foreach $file (@files)
2199     {
2200         $dist_common{$file} = 1;
2201     }
2204 # Push a list of clean targets onto phony.
2205 sub push_phony_cleaners
2207     local ($base) = @_;
2208     local ($target);
2209     foreach $target ('mostly', 'dist', '', 'maintainer-')
2210     {
2211         push (@phony, $target . 'clean-' . $base);
2212     }
2215 # Set strictness.
2216 sub set_strictness
2218     $strictness_name = $_[0];
2219     if ($strictness_name eq 'gnu')
2220     {
2221         $strictness = $GNU;
2222     }
2223     elsif ($strictness_name eq 'gnits')
2224     {
2225         $strictness = $GNITS;
2226     }
2227     elsif ($strictness_name eq 'normal')
2228     {
2229         $strictness = $NORMAL;
2230     }
2231     else
2232     {
2233         die "automake: level \`$strictness_name' not recognized\n";
2234     }
2238 ################################################################
2240 # Return directory name of file.
2241 sub dirname
2243     local ($file) = @_;
2244     local ($sub);
2246     ($sub = $file) =~ s,/+[^/]+$,,g;
2247     $sub = '.' if $sub eq $file;
2248     return $sub;
2251 # Make a directory.
2252 sub mkdir
2254     local ($dirname) = @_;
2255     system ("mkdir", $dirname);
2258 ################################################################
2260 # Print an error message and set exit status.
2261 sub am_error
2263     warn "automake: ${am_file}.am: ", join (' ', @_), "\n";
2264     $exit_status = 1;
2267 # The same, but while scanning configure.in.
2268 sub am_conf_error
2270     # FIXME can run in subdirs.
2271     warn "automake: configure.in: ", join (' ', @_), "\n";
2272     $exit_status = 1;
2275 # Print usage information.
2276 sub usage
2278     print "Usage: automake [OPTION] ... [Makefile]...\n";
2279     print $USAGE;
2280     print "\nFiles which are automatically distributed, if found:\n";
2281     $~ = "USAGE_FORMAT";
2282     local (@lcomm) = sort ((@common_files, @common_sometimes));
2283     local ($one, $two, $three, $four);
2284     while (@lcomm > 0)
2285     {
2286         $one = shift @lcomm;
2287         $two = @lcomm ? shift @lcomm : '';
2288         $three = @lcomm ? shift @lcomm : '';
2289         $four = @lcomm ? shift @lcomm : '';
2290         write;
2291     }
2293     exit 0;
2296 format USAGE_FORMAT =
2297   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
2298   $one,               $two,               $three,             $four