Bug fixes, test feature
[automake.git] / automake.in
blob4b04145b2cd74cadfeecca5738e9ef27fbae6dce
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval 'exec @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.  NOTE: assign
31 # VERSION as string so that eg version 0.30 will print correctly.
32 $VERSION = "@VERSION@";
33 $prefix = "@prefix@";
34 $am_dir = "@datadir@/@PACKAGE@";
36 # String constants.
37 $IGNORE_PATTERN = "^##([^#].*)?\$";
38 $WHITE_PATTERN = "^[ \t]*\$";
39 $COMMENT_PATTERN = "^#";
40 $RULE_PATTERN = "^([a-zA-Z_.][-.a-zA-Z0-9_.]*) *:";
41 $MACRO_PATTERN = "^([A-Za-z][A-Za-z0-9_]*)[ \t]*=[ \t]*(.*)\$";
44 # Constants to define the "strictness" level.
45 $NORMAL = 0;
46 $GNU = 1;
47 $GNITS = 2;
51 # Variables global to entire run.
53 # Strictness level as set on command line.
54 $default_strictness = $GNU;
56 # Name of strictness level, as set on command line.
57 $default_strictness_name = 'gnu';
59 # This is TRUE if GNU make specific automatic dependency generation
60 # code should be included in generated Makefile.in.
61 $cmdline_use_dependencies = 1;
63 # TRUE if in verbose mode.
64 $verbose = 0;
66 # This holds our (eventual) exit status.  We don't actually exit until
67 # we have processed all input files.
68 $exit_status = 0;
70 # From the Perl manual.
71 $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
73 # TRUE if missing standard files should be installed.
74 $install_missing = 0;
76 # Files found by scanning configure.in for LIBOBJS.
77 %libsources = ();
79 # True if fp_C_PROTOTYPES appears in configure.in.
80 $fp_c_prototypes = 0;
82 # Names used in AC_CONFIG_HEADER call.  $config_name is the actual
83 # (first) argument.  $config_header is the '.in' file.  Ordinarily the
84 # second is derived from the first, but they can be different if the
85 # weird "NAME:FILE" syntax is used.
86 $config_name = '';
87 $config_header = '';
88 # Line number at which AC_CONFIG_HEADER appears in configure.in.
89 $config_header_line = 0;
91 # Relative location of top build directory.
92 $top_builddir = '';
94 # List of Makefile.am's to process.
95 @input_files = ();
97 # List of files in AC_OUTPUT without Makefile.am.
98 @other_input_files = ();
100 # Whether AC_PROG_MAKE_SET has been seen in configure.in.
101 $seen_make_set = 0;
103 # Whether ud_GNU_GETTEXT has been seen in configure.in.
104 $seen_gettext = 0;
106 # 1 if AC_PROG_INSTALL seen, 2 if fp_PROG_INSTALL seen.
107 $seen_prog_install = 0;
109 # 1 if any scripts installed, 0 otherwise.
110 $scripts_installed = 0;
112 # Whether AC_PATH_XTRA has been seen in configure.in.
113 $seen_path_xtra = 0;
115 # Whether YACC variable has been seen in configure.in.
116 $seen_prog_yacc = 0;
118 # TRUE if we've seen AC_CANONICAL_(HOST|SYSTEM).  The presence of
119 # AC_CHECK_TOOL also sets this.
120 $seen_canonical = 0;
122 # TRUE if we've seen AC_PROG_RANLIB.
123 $seen_ranlib = 0;
125 # Charsets used by maintainer and in distribution.  MAINT_CHARSET is
126 # handled in a funny way: if seen in the top-level Makefile.am, it is
127 # used for every directory which does not specify a different value.
128 # The rationale here is that some directories (eg gettext) might be
129 # distributions of other packages, and thus require their own charset
130 # info.  However, the DIST_CHARSET must be the same for the entire
131 # package; it can only be set at top-level.
132 # FIXME this yields bugs when rebuilding.  What to do?  Always
133 # read (and sometimes discard) top-level Makefile.am?
134 $maint_charset = '';
135 $dist_charset = 'utf8';         # recode doesn't support this yet.
139 &initialize_global_constants;
141 # Parse command line.
142 &parse_arguments (@ARGV);
144 # Do configure.in scan only once.
145 &scan_configure;
147 die "automake: no \`Makefile.am' found or specified\n"
148     if ! @input_files;
150 # Now do all the work on each file.
151 foreach $am_file (@input_files)
153     # FIXME should support the AC_OUTPUT ":" syntax here.
154     if (! -f ($am_file . '.am'))
155     {
156         &am_error ('no such file');
157     }
158     else
159     {
160         &generate_makefile ($am_file);
161     }
164 if ($seen_prog_install <= $scripts_installed)
166     &am_conf_error (($scripts_installed ? 'fp_PROG_INSTALL' : 'AC_PROG_INSTALL')
167                     . " must be used in configure.in");
168     &keyed_aclocal_warning ('fp_PROG_INSTALL')
169         if $scripts_installed;
172 exit $exit_status;
175 ################################################################
177 # Parse command line.
178 sub parse_arguments
180     local (@arglist) = @_;
182     # Start off as normal.
183     &set_strictness ('normal');
185     while (@arglist)
186     {
187         if ($arglist[0] eq "--version")
188         {
189             print "Automake version $VERSION\n";
190             exit 0;
191         }
192         elsif ($arglist[0] eq "--help")
193         {
194             &usage;
195         }
196         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
197         {
198             $am_dir = $1;
199         }
200         elsif ($arglist[0] eq '--amdir')
201         {
202             &require_argument (@arglist);
203             shift (@arglist);
204             $am_dir = $arglist[0];
205         }
206         elsif ($arglist[0] =~ /^--strictness=(.+)$/)
207         {
208             &set_strictness ($1);
209         }
210         elsif ($arglist[0] eq '--gnu')
211         {
212             &set_strictness ('gnu');
213         }
214         elsif ($arglist[0] eq '--gnits')
215         {
216             &set_strictness ('gnits');
217         }
218         elsif ($arglist[0] eq '--strictness')
219         {
220             &require_argument (@arglist);
221             shift (@arglist);
222             &set_strictness ($arglist[0]);
223         }
224         elsif ($arglist[0] eq '--include-deps')
225         {
226             $cmdline_use_dependencies = 0;
227         }
228         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
229         {
230             # Set output directory.
231             $output_directory = $1;
232         }
233         elsif ($arglist[0] eq '--output-dir')
234         {
235             &require_argument (@arglist);
236             shift (@arglist);
237             $output_directory = $arglist[0];
238         }
239         elsif ($arglist[0] eq '--install-missing')
240         {
241             $install_missing = 1;
242         }
243         elsif ($arglist[0] eq '--verbose')
244         {
245             $verbose = 1;
246         }
247         elsif ($arglist[0] eq '--')
248         {
249             # Stop option processing.
250             shift (@arglist);
251             push (@input_files, @arglist);
252             last;
253         }
254         elsif ($arglist[0] =~ /^-/)
255         {
256             die "automake: unrecognized option -- \`$arglist[0]'\n";
257         }
258         else
259         {
260             push (@input_files, $arglist[0]);
261         }
263         shift (@arglist);
264     }
266     # Take global strictness from whatever we currently have set.
267     $default_strictness = $strictness;
268     $default_strictness_name = $strictness_name;
271 # Ensure argument exists, or die.
272 sub require_argument
274     local ($arg, @arglist) = @_;
275     die "automake: no argument given for option \`$arg'\n"
276         if ! @arglist;
279 ################################################################
281 # Generate a Makefile.in given the name of the corresponding Makefile.
282 sub generate_makefile
284     local ($makefile) = @_;
286     print "automake: creating ", $makefile, ".in\n" if $verbose;
288     &initialize_per_input;
289     $relative_dir = &dirname ($makefile);
291     # AC_CANONICAL_HOST and AC_CANONICAL_SYSTEM need config.guess and
292     # config.sub.
293     &require_file ($NORMAL, 'config.guess', 'config.sub')
294         if $relative_dir eq '.' && $seen_canonical;
296     # FIXME with new 'dist' target, don't need Makefile.in.  Probably
297     # should remove it here.
298     &push_dist_common ('Makefile.in', 'Makefile.am');
299     push (@sources, '$(SOURCES)') if defined $contents{'SOURCES'};
300     push (@objects, '$(OBJECTS)') if defined $contents{'OBJECTS'};
302     # This is always the default target.  This gives us freedom to do
303     # things in whatever order is convenient.
304     $output_rules .= "default: all\n\n";
305     push (@phony, 'default');
307     &read_am_file ($makefile . '.am');
308     &handle_options;
310     # Check first, because we might modify some state.
311     &check_gnu_standards;
312     &check_gnits_standards;
314     &handle_configure;
315     &handle_libraries;
316     &handle_programs;
317     &handle_scripts;
319     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
320     # on this (but currently does).
321     $contents{'SOURCES'} = join (' ', @sources);
322     $contents{'OBJECTS'} = join (' ', @objects);
324     &handle_texinfo;
325     &handle_man_pages;
326     &handle_data;
327     &handle_headers;
328     &handle_subdirs;
329     &handle_tags;
330     &handle_dist;
331     &handle_dependencies;
332     &handle_tests;
333     &handle_footer;
334     &handle_merge_targets;
335     &handle_installdirs;
336     &handle_clean;
337     &handle_phony;
339     if (! -d ($output_directory . '/' . $relative_dir))
340     {
341         &mkdir ($output_directory . '/' . $relative_dir);
342     }
343     if (! open (GM_FILE, "> " . $output_directory . '/' . $makefile . ".in"))
344     {
345         warn "automake: ${am_file}.in: cannot open: $!\n";
346         $exit_status = 1;
347         return;
348     }
350     print GM_FILE $output_vars;
351     print GM_FILE $output_rules;
352     print GM_FILE $output_trailer;
354     close (GM_FILE);
357 ################################################################
359 # Handle AUTOMAKE_OPTIONS variable.
360 sub handle_options
362     return if ! defined $contents{'AUTOMAKE_OPTIONS'};
364     foreach (split (/\s+/, $contents{'AUTOMAKE_OPTIONS'}))
365     {
366         $options{$_} = 1;
367         if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'normal')
368         {
369             &set_strictness ($_);
370         }
371         elsif ($_ eq 'no-installman' || $_ eq 'ansi2knr' || $_ eq 'dist-shar')
372         {
373             # Explicitly recognize these.
374         }
375         elsif ($_ eq 'no-dependencies')
376         {
377             $use_dependencies = 0;
378         }
379         elsif (/[0-9]+\.?[0-9]+/)
380         {
381             # Got a version number.  Is the syntax too strict?
382             if ($VERSION < $_)
383             {
384                 &am_line_error ('AUTOMAKE_OPTIONS',
385                                 "require version $_, only have $VERSION");
386                 # FIXME -- what else to do?
387                 exit 1;
388             }
389         }
390         else
391         {
392             &am_line_error ('AUTOMAKE_OPTIONS',
393                             'option ', $_, 'not recognized');
394         }
395     }
398 # Return object extension.  Just once, put some code into the output.
399 sub get_object_extension
401     if (! $dir_holds_sources)
402     {
403         # Boilerplate.
404         local ($xform) = '';
405         if (defined $contents{'CONFIG_HEADER'})
406         {
407             ($xform = &dirname ($contents{'CONFIG_HEADER'}))
408                 =~ s/(\W)/\\$1/g;
409             $xform = 's/\@CONFIG_INCLUDE_SPEC\@/-I' . $xform . '/go';
410         }
411         $output_vars .= &file_contents_with_transform ($xform,
412                                                        'compile-vars');
413         $output_rules .= &file_contents ('compile');
414         &push_phony_cleaners ('compile');
416         # If using X, include some extra variable definitions.  NOTE
417         # we don't want to force these into CFLAGS or anything,
418         # because not all programs will necessarily use X.
419         if ($seen_path_xtra)
420         {
421             $output_vars .= ("X_CFLAGS = \@X_CFLAGS\@\n"
422                              . "X_LIBS = \@X_LIBS\@\n"
423                              . "X_EXTRA_LIBS = \@X_EXTRA_LIBS\@\n"
424                              . "X_PRE_LIBS = \@X_PRE_LIBS\@\n");
425         }
427         # Check for automatic de-ANSI-fication.
428         $dir_holds_sources = '.o';
429         push (@suffixes, '.c', '.o');
430         push (@clean, 'compile');
432         if (defined $options{'ansi2knr'})
433         {
434             if (! $fp_c_prototypes)
435             {
436                 &am_line_error ('AUTOMAKE_OPTIONS',
437                                 "option \`ansi2knr' in use but \`fp_C_PROTOTYPES' not in configure.in");
438                 &keyed_aclocal_warning ('fp_C_PROTOTYPES');
439                 # Only give this error once.
440                 $fp_c_prototypes = 1;
441             }
443             $dir_holds_sources = '$o';
444             push (@suffixes, '._c', '._o');
446             &require_file ($NORMAL, 'ansi2knr.c', 'ansi2knr.1');
448             $output_vars .= &file_contents ('kr-vars');
449             $output_rules .= &file_contents ('compile-kr');
450             $output_rules .= &file_contents ('clean-kr');
452             push (@clean, 'kr');
453             &push_phony_cleaners ('kr');
454         }
455     }
456     return $dir_holds_sources;
459 # Handle SOURCE->OBJECT transform for one program or library.
460 sub handle_source_transform
462     local ($one_file, $obj) = @_;
463     local ($objpat) = $obj;
464     $objpat =~ s/(\W)/\\$1/g;
466     # Look for file_SOURCES and file_OBJECTS.
467     if (defined $contents{$one_file . "_SOURCES"})
468     {
469         if (! defined $contents{$one_file . "_OBJECTS"})
470         {
471             # Turn sources into objects.
472             local (@files) = split (/\s+/, $contents{$one_file . "_SOURCES"});
473             local (@result) = ();
474             foreach (@files)
475             {
476                 # Skip header files, including C++-ish ones.
477                 next if /\.[hH]$/;
478                 next if /\.hxx$/;
479                 # Skip things that look like macro references.
480                 next if /^\$\(.*\)$/;
481                 next if /^\$\{.*\}$/;
483                 if (/^(.*)\.[yl]$/)
484                 {
485                     # Automatically include generated .c file in
486                     # distribution.
487                     &push_dist_common ($1 . '.c');
488                 }
490                 # Transform source files into .o files.
491                 s/\.cc$/$obj/g;
492                 s/\.cxx$/$obj/g;
493                 s/\.[cCmylfs]$/$obj/g;
494                 push (@result, $_);
496                 # Transform .o or $o file into .P file (for automatic
497                 # dependency code).
498                 s/$objpat$/.P/g;
499                 $dep_files{'$(srcdir)/.deps/' . $_} = 1;
500             }
502             &pretty_print ($one_file . "_OBJECTS =", "", @result);
503         }
504         else
505         {
506             &am_line_error ($one_file . '_OBJECTS',
507                             $one_file . '_OBJECTS', 'should not be defined');
508         }
510         push (@sources, '$(' . $one_file . "_SOURCES)");
511         push (@objects, '$(' . $one_file . "_OBJECTS)");
512     }
513     else
514     {
515         $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
516                          . $one_file . "_OBJECTS = ". $one_file
517                          . $obj . "\n");
518         push (@sources, $one_file . '.c');
519         push (@objects, $one_file . $obj);
520         $dep_files{'$(srcdir)/.deps/' . $one_file . '.P'} = 1;
521     }
523     if (defined $contents{'CONFIG_HEADER'})
524     {
525         $output_rules .= ('$(' . $one_file . "_OBJECTS): "
526                           . $contents{'CONFIG_HEADER'} . "\n");
527     }
529     return @result;
532 # Handle C programs.
533 sub handle_programs
535     local (@proglist) = &am_install_var ('-clean',
536                                          'programs', 'PROGRAMS',
537                                          'bin', 'sbin', 'libexec', 'pkglib',
538                                          'noinst');
539     return if ! @proglist;
541     local ($obj) = &get_object_extension;
542     local ($one_file, $munge);
544     foreach $one_file (@proglist)
545     {
546         &handle_source_transform ($one_file, $obj);
548         if (! defined $contents{$one_file . "_LDADD"})
549         {
550             # User didn't define prog_LDADD override.  So do it.
551             $output_vars .= $one_file . '_LDADD = $(LDADD)' . "\n";
552         }
554         $output_rules .=
555             &file_contents_with_transform ('s/\@PROGRAM\@/' . $one_file
556                                            . '/go', 'program');
557     }
560 # Handle libraries.
561 sub handle_libraries
563     local (@liblist) = &am_install_var ('-no-all', '-clean',
564                                         'libraries', 'LIBRARIES',
565                                         'lib', 'pkglib', 'noinst');
566     return if ! @liblist;
568     if (! $seen_ranlib)
569     {
570         # FIXME need am_line_error here.  But we don't know which
571         # variable exists.  Must add a loop...
572         &am_error ("building a library but \`AC_PROG_RANLIB' not in configure.in");
573         # Only get this error once.
574         $seen_ranlib = 1;
575     }
577     # Generate _LIBFILES variables.  Too bad we can't do this in
578     # am_install_var.
579     local ($onedir, $onelib);
580     local (@outlist);
581     foreach $onedir ('lib', 'pkglib', 'noinst')
582     {
583         if (defined $contents{$onedir . '_LIBRARIES'})
584         {
585             @outlist = ();
586             foreach $onelib (split (/\s+/, $contents{$onedir . '_LIBRARIES'}))
587             {
588                 push (@outlist, 'lib' . $onelib . '.a');
589             }
590             &pretty_print ($onedir . '_LIBFILES =', "", @outlist);
591         }
592     }
593     push (@all, '$(LIBFILES)');
595     local ($obj) = &get_object_extension;
596     local ($munge);
597     foreach $onelib (@liblist)
598     {
599         if (defined $contents{$onelib . '_LIBADD'})
600         {
601             # We recognize certain things that are commonly put in
602             # LIBADD.
603             local ($lsearch);
605             foreach $lsearch (split (/\s+/, $contents{$onelib . '_LIBADD'}))
606             {
607                 # Automatically handle @LIBOBJS@ and @ALLOCA@.
608                 # Basically this means adding entries to dep_files.
609                 if ($lsearch eq '@LIBOBJS@')
610                 {
611                     local ($iter, $rewrite);
612                     foreach $iter (keys %libsources)
613                     {
614                         if ($iter ne 'alloca.c')
615                         {
616                             ($rewrite = $iter) =~ s/\.c$/.P/;
617                             $dep_files{'$(srcdir)/.deps/' . $rewrite} = 1;
618                             &require_file ($NORMAL, $iter);
619                         }
620                     }
621                 }
622                 elsif ($lsearch eq '@ALLOCA@')
623                 {
624                     &am_line_error ($onelib . '_LIBADD',
625                                     "\@ALLOCA\@ seen but \`AC_FUNC_ALLOCA' not in \`configure.in'")
626                         if ! defined $libsources{'alloca.c'};
627                     $dep_files{'$(srcdir)/.deps/alloca.P'} = 1;
628                     &require_file ($NORMAL, 'alloca.c');
629                 }
630             }
631         }
632         else
633         {
634             # Generate support for conditional object inclusion in
635             # libraries.
636             $output_vars .= $onelib . "_LIBADD =\n";
637         }
639         &handle_source_transform ($onelib, $obj);
641         $output_rules .=
642             &file_contents_with_transform ('s/\@LIBRARY\@/' . $onelib . '/go',
643                                            'library');
644     }
646     # Turn "foo" into "libfoo.a" and include macro definition.
647     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
649     if (! defined $contents{'LIBFILES'})
650     {
651         &pretty_print ('LIBFILES = ', "", @liblist);
652     }
653     $output_vars .= &file_contents ('libraries-vars');
656 # Handle scripts.
657 sub handle_scripts
659     # NOTE we no longer automatically clean SCRIPTS, because it is
660     # useful to sometimes distribute scripts verbatim.  This happens
661     # eg in Automake itself.
662     $scripts_installed = &am_install_var ('scripts', 'SCRIPTS',
663                                           'bin', 'sbin', 'libexec', 'pkgdata',
664                                           'noinst');
666     # We really only want a boolean value.
667     $scripts_installed = 1 if $scripts_installed;
670 # Search a file for a "version.texi" Texinfo include.  Return the name
671 # of the include file if found, or the empty string if not.  A
672 # "version.texi" file is actually any file whose name matches
673 # "vers*.texi".
674 sub grep_for_vers_texi
676     local ($filename) = @_;
678     if (! open (TEXI, $filename))
679     {
680         &am_error ("couldn't open \`$filename': $!");
681         return '';
682     }
683     print "automake: reading $filename\n" if $verbose;
685     while (<TEXI>)
686     {
687         if (/^\@include\s+(vers[^.]*\.texi)\s*$/)
688         {
689             # Found it.
690             close (TEXI);
691             return $1;
692         }
693     }
695     close (TEXI);
696     return '';
699 # Handle all Texinfo source.
700 sub handle_texinfo
702     # FIXME can this really be right?  Might want to have configure
703     # determine list of texinfos.  Fix this when someone complains.
704     &am_line_error ('TEXINFOS',
705                     "\`TEXINFOS' is an anachronism; use \`info_TEXINFOS'")
706         if defined $contents{'TEXINFOS'};
707     return if (! defined $contents{'info_TEXINFOS'}
708                && ! defined $contents{'html_TEXINFOS'});
710     local (@texis) = split (/\s+/, $contents{'info_TEXINFOS'});
712     local (@infos_list, @info_deps_list, @dvis_list, @texi_deps);
713     local ($infobase, $info_cursor);
714     local (%versions);
715     local ($done) = 0;
716     local ($vti);
717     local ($tc_cursor, @texi_cleans);
719     foreach $info_cursor (@texis)
720     {
721         ($infobase = $info_cursor) =~ s/\.texi$//;
723         # If 'version.texi' is referenced by input file, then include
724         # automatic versioning capability.
725         local ($vtexi)
726             = &grep_for_vers_texi ($relative_dir . "/" . $info_cursor);
727         if ($vtexi)
728         {
729             &am_error ("\`$vtexi', included in \`$info_cursor', also included in \`$versions{$vtexi}'")
730                 if (defined $versions{$vtexi});
731             $versions{$vtexi} = $info_cursor;
733             # Got a hit.
734             push (@texis, $vtexi);
735             # We number the stamp-vti files.  This is doable since the
736             # actual names don't matter much.  We only number starting
737             # with the second one, so that the common case looks nice.
738             $vti = 'vti' . ($done ? $done : '');
739             &push_dist_common ($vtexi, 'stamp-' . $vti);
740             push (@clean, $vti);
742             $output_rules .=
743                 &file_contents_with_transform
744                     ('s/\@TEXI\@/' . $info_cursor . '/g; '
745                      . 's/\@VTI\@/' . $vti . '/g; '
746                      . 's/\@VTEXI\@/' . $vtexi . '/g',
747                      'texi-version');
749             &push_phony_cleaners ($vti);
751             # Only require once.
752             &require_file ($NORMAL, 'mdate-sh') if ! $done;
753             ++$done;
754         }
756         # If user specified file_TEXINFOS, then use that as explicit
757         # dependency list.
758         @texi_deps = ();
759         push (@texi_deps, $info_cursor, $vtexi);
760         if (defined $contents{$infobase . "_TEXINFOS"})
761         {
762             push (@texi_deps, "\$" . $infobase . '_TEXINFOS');
763             &push_dist_common ("\$" . $infobase . '_TEXINFOS');
764         }
766         $output_rules .= ("\n" . $infobase . ".info: "
767                           . join (' ', @texi_deps) . "\n\n");
769         push (@infos_list, $infobase . '.info*');
770         push (@info_deps_list, $infobase . '.info');
771         push (@dvis_list, $infobase . '.dvi');
773         # Generate list of things to clean for this target.  We do
774         # this explicitly because otherwise too many things could be
775         # removed.  In particular the ".log" extension might
776         # reasonably be used in other contexts by the user.
777         foreach $tc_cursor ('aux', 'cp', 'cps', 'dvi', 'fn', 'fns',
778                             'ky', 'log', 'pg', 'toc', 'tp', 'vr', 'op')
779         {
780             push (@texi_cleans, $infobase . '.' . $tc_cursor);
781         }
782     }
784     # Some boilerplate.
785     $output_vars .= &file_contents ('texinfos-vars');
786     $output_rules .= &file_contents ('texinfos');
787     push (@phony, 'install-info', 'uninstall-info');
789     # How to clean.
790     $output_rules .= "\nmostlyclean-info:\n";
791     &pretty_print_rule ("\trm -f", "\t  ", @texi_cleans);
792     $output_rules .= ("\nclean-info:\n\ndistclean-info:\n\n"
793                       . "maintainer-clean-info:\n\t"
794                       . 'rm -f $(INFOS)' . "\n");
795     &push_phony_cleaners ('info');
797     push (@suffixes, '.texi', '.info', '.dvi');
798     push (@uninstall, 'uninstall-info');
799     push (@clean, 'info');
800     push (@info, '$(INFO_DEPS)');
801     push (@dvi, '$(DVIS)');
802     push (@installdirs, '$(infodir)');
803     unshift (@install_data, 'install-info');
805     # Make sure documentation is made and installed first.  Use
806     # $(INFO_DEPS), not 'info', because otherwise recursive makes get
807     # run twice during "make all".  FIXME add test case.
808     unshift (@all, '$(INFO_DEPS)');
810     $output_vars .= ("INFOS = " . join (' ', @infos_list) . "\n"
811                      . "INFO_DEPS = " . join (' ', @info_deps_list)  . "\n"
812                      . "DVIS = " . join (' ', @dvis_list) . "\n"
813                      # This next isn't strictly needed now -- the
814                      # places that look here could easily be changed
815                      # to look in info_TEXINFOS.  But this is probably
816                      # better, in case noinst_TEXINFOS is ever
817                      # supported.
818                      . "TEXINFOS = " . $contents{'info_TEXINFOS'} . "\n\n");
820     # Do some error checking.
821     &require_file ($NORMAL, 'texinfo.tex');
824 # Handle any man pages.
825 sub handle_man_pages
827     &am_line_error ('MANS', "\`MANS' is an anachronism; use \`man_MANS'")
828         if defined $contents{'MANS'};
829     return if ! defined $contents{'man_MANS'};
831     # We generate the manpage install code by hand to avoid the use of
832     # basename in the generated Makefile.
833     local (@mans) = split (/\s+/, $contents{'man_MANS'});
834     local (%sections, %inames, %secmap, %fullsecmap);
835     foreach (@mans)
836     {
837         # FIXME: statement without effect:
838         /^(.*)\.([0-9])([a-z]*)$/;
839         $sections{$2} = 1;
840         $inames{$1} = $_;
841         $secmap{$1} = $2;
842         $fullsecmap{$1} = $2 . $3;
843     }
845     # We don't really need this, but we use it in case we ever want to
846     # support noinst_MANS.
847     $output_vars .= "MANS = " . $contents{'man_MANS'} . "\n";
849     # Generate list of install dirs.
850     $output_rules .= "install-man: \$(MANS)\n";
851     foreach (keys %sections)
852     {
853         push (@installdirs, '$(mandir)/man' . $_);
854         $output_rules .= ("\t" . '$(top_srcdir)/mkinstalldirs $(mandir)/man'
855                           . $_ . "\n");
856     }
857     push (@phony, 'install-man');
859     # Generate install target.
860     local ($key);
861     foreach $key (keys %inames)
862     {
863         $_ = $install_man_format;
864         s/\@SECTION\@/$secmap{$key}/g;
865         s/\@MAN\@/$inames{$key}/g;
866         s/\@FULLSECT\@/$fullsecmap{$key}/g;
867         s/\@MANBASE\@/$key/g;
868         $output_rules .= $_;
869     }
870     $output_rules .= "\n";
872     $output_rules .= "uninstall-man:\n";
873     foreach $key (keys %inames)
874     {
875         $_ = $uninstall_man_format;
876         s/\@SECTION\@/$secmap{$key}/g;
877         s/\@MAN\@/$inames{$key}/g;
878         s/\@FULLSECT\@/$fullsecmap{$key}/g;
879         s/\@MANBASE\@/$key/g;
880         $output_rules .= $_;
881     }
882     $output_rules .= "\n";
883     push (@phony, 'uninstall-man');
885     $output_vars .= &file_contents ('mans-vars');
887     if (! defined $options{'no-installman'})
888     {
889         push (@install_data, 'install-man');
890         push (@uninstall, 'uninstall-man');
891         push (@all, '$(MANS)');
892     }
895 # Handle DATA variables.
896 sub handle_data
898     &am_install_var ('data', 'DATA', 'data', 'sysconf',
899                      'sharedstate', 'localstate', 'pkgdata',
900                      'noinst');
903 # Handle TAGS.
904 sub handle_tags
906     local ($tagging) = 0;
908     push (@phony, 'tags');
909     if (defined $contents{'SUBDIRS'})
910     {
911         $output_rules .= &file_contents ('tags');
912         $tagging = 1;
913     }
914     elsif ($dir_holds_sources || defined $contents{'ETAGS_ARGS'})
915     {
916         $output_rules .= &file_contents ('tags-subd');
917         $tagging = 1;
918     }
920     if ($tagging)
921     {
922         $output_rules .= &file_contents ('tags-clean');
923         push (@clean, 'tags');
924         &push_phony_cleaners ('tags');
925     }
926     else
927     {
928         # Every Makefile must define some sort of TAGS rule.
929         # Otherwise, it would be possible for a top-level "make TAGS"
930         # to fail because some subdirectory failed.
931         $output_rules .= "tags: TAGS\nTAGS:\n\n";
932     }
935 # Generate actual 'dist' (or dist-shar) rule.
936 sub handle_dist_worker
938     local ($distshar) = @_;
939     local ($target) = $distshar ? 'dist-shar' : 'dist';
941     $output_rules .= $target . ': $(DEP_DISTFILES)' . "\n";
943     # Initialization; only at top level.
944     if ($relative_dir eq '.')
945     {
946         if ($strictness >= $GNITS)
947         {
948             # For Gnits users, this is pretty handy.  Look at 15 lines
949             # in case some explanatory text is desirable.
950             $output_rules .= '  @if sed 15q NEWS | grep -e "$(VERSION)" > /dev/null; then :; else \\
951           echo "NEWS not updated; not releasing" 1>&2; \\
952           exit 1;                               \\
953         fi
955         }
958         $output_rules .=
959             (
960              # Create dist directory.
961              '  rm -rf $(distdir)
962         mkdir $(distdir)
963         chmod 777 $(distdir)
965              # We need an absolute path for --output-dir.  Thus the
966              # weirdness.
967              . '        distdir=`cd $(distdir) && pwd` \\
968           && cd $(srcdir) \\
969           && automake --include-deps --output-dir=$$distdir --strictness='
970              # Set strictness of output.
971              . $strictness_name . "\n"
972              );
973     }
975     # In loop, test for file existence because sometimes a file gets
976     # included in DISTFILES twice.  For example this happens when a
977     # single source file is used in building more than one program.
978     # Also, there are situations in which "ln" can fail.  For instance
979     # a file to distribute could actually be a cross-filesystem
980     # symlink -- this can easily happen if "gettextize" was run on the
981     # distribution.  Note that DISTFILES can contain a wildcard (for
982     # info files, sigh), so we must use the echo trick.
984     $output_rules .= '  @for file in `cd $(srcdir) && echo $(DISTFILES)`; do \\
985           test -f $(distdir)/$$file \\
986           || ln $(srcdir)/$$file $(distdir)/$$file 2> /dev/null \\
987           || cp -p $(srcdir)/$$file $(distdir)/$$file; \\
988         done
991     # If we have SUBDIRS, create all dist subdirectories and do
992     # recursive build.
993     if (defined $contents{'SUBDIRS'})
994     {
995         # Test for directory existence here because previous automake
996         # invocation might have created some directories.
997         $output_rules .= '      for subdir in $(SUBDIRS); do            \\
998           test -d $(distdir)/$$subdir           \\
999           || mkdir $(distdir)/$$subdir          \\
1000           || exit 1;                            \\
1001           chmod 777 $(distdir)/$$subdir;        \\
1002           (cd $$subdir && $(MAKE) dist) || exit 1; \\
1003         done
1005     }
1007     # Make verbatim copies of some subdirectories if required.  This
1008     # is a hack which might go away.
1009     if (defined $contents{'DIST_SUBDIRS'})
1010     {
1011         $output_rules .= '      @for dir in $(DIST_SUBDIRS); do         \\
1012           echo copying directory $$dir;         \\
1013           tar chf - $$dir | (cd $(distdir) && tar xBpf -); \\
1014         done
1016     }
1018     # Finalize.
1019     if ($relative_dir eq '.')
1020     {
1021         $output_rules .= '      chmod -R a+r $(distdir)' . "\n\t";
1022         if ($distshar)
1023         {
1024             $output_rules .= 'shar $(distdir) | gzip > $(distdir).shar.gz';
1025         }
1026         else
1027         {
1028             $output_rules .= 'tar chozf $(distdir).tar.gz $(distdir)';
1029         }
1030         $output_rules .= "\n\t" . 'rm -rf $(distdir)' . "\n";
1031     }
1033     push (@phony, $target);
1036 # Handle 'dist' target.
1037 sub handle_dist
1039     # Set up maint_charset.
1040     $local_maint_charset = $contents{'MAINT_CHARSET'}
1041         if defined $contents{'MAINT_CHARSET'};
1042     $maint_charset = $local_maint_charset
1043         if $relative_dir eq '.';
1045     if (defined $contents{'DIST_CHARSET'})
1046     {
1047         &am_line_error ('DIST_CHARSET',
1048                         "DIST_CHARSET defined but no MAINT_CHARSET defined")
1049             if ! $local_maint_charset;
1050         if ($relative_dir eq '.')
1051         {
1052             $dist_charset = $contents{'DIST_CHARSET'}
1053         }
1054         else
1055         {
1056             &am_line_error ('DIST_CHARSET',
1057                             "DIST_CHARSET can only be defined at top level");
1058         }
1059     }
1061     # Look for common files that should be included in distribution.
1062     local ($cfile);
1063     foreach $cfile (@common_files)
1064     {
1065         if (-f ($relative_dir . "/" . $cfile))
1066         {
1067             &push_dist_common ($cfile);
1068         }
1069     }
1071     # Keys of %dist_common are names of files to distributed.  We put
1072     # README first because it then becomes easier to make a
1073     # Usenet-compliant shar file (in these, README must be first).
1074     # FIXME do more ordering of files here.
1075     local (@coms);
1076     if (defined $dist_common{'README'})
1077     {
1078         push (@coms, 'README');
1079         undef $dist_common{'README'};
1080     }
1081     push (@coms, sort keys %dist_common);
1083     &pretty_print ("DIST_COMMON =", "", @coms);
1084     $output_vars .= "\n";
1086     # Some boilerplate.
1087     $output_vars .= &file_contents ('dist-vars');
1089     # Put these things in rules section so it is easier for whoever
1090     # reads Makefile.in.
1091     if ($relative_dir eq '.')
1092     {
1093         $output_rules .= "\n" . 'distdir = $(PACKAGE)-$(VERSION)' . "\n";
1094     }
1095     else
1096     {
1097         $output_rules .= ("\nsubdir = " . $relative_dir . "\n"
1098                           . 'distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)'
1099                           . "\n");
1100     }
1102     # Generate 'dist' target, and maybe dist-shar.
1103     &handle_dist_worker (0);
1104     &handle_dist_worker (1) if defined $options{'dist-shar'};
1107 # Handle auto-dependency code.
1108 sub handle_dependencies
1110     if ($use_dependencies)
1111     {
1112         # Include GNU-make-specific auto-dep code.
1113         if ($dir_holds_sources)
1114         {
1115             &pretty_print ('DEP_FILES =', "", sort keys %dep_files);
1116             $output_rules .= &file_contents ('depend');
1117         }
1118     }
1119     else
1120     {
1121         # Include any auto-generated deps that are present.
1122         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
1123         {
1124             local ($depfile);
1125             local ($gpat) = $relative_dir . "/.deps/*.P";
1127             foreach $depfile (<${gpat}>)
1128             {
1129                 if (! open (DEP_FILE, $depfile))
1130                 {
1131                     &am_error ("couldn't open $depfile", $!);
1132                     next;
1133                 }
1134                 print "automake: reading $depfile\n" if $verbose;
1136                 # Slurp entire file.
1137                 $output_rules .= join ('', <DEP_FILE>);
1139                 close (DEP_FILE);
1140             }
1142             $output_rules .= "\n";
1143         }
1144     }
1147 # Handle subdirectories.
1148 sub handle_subdirs
1150     if (! defined $contents{'SUBDIRS'})
1151     {
1152         &am_conf_error
1153             ("ud_GNU_GETTEXT in configure.in but SUBDIRS not defined")
1154                 if $seen_gettext && $relative_dir eq '.';
1155         return;
1156     }
1158     if ($seen_gettext)
1159     {
1160         &am_line_error
1161             ('SUBDIRS',
1162              "ud_GNU_GETTEXT in configure.in but \`po' not in SUBDIRS")
1163                 if $contents{'SUBDIRS'} !~ /\bpo\b/;
1164         &am_line_error
1165             ('SUBDIRS',
1166              "ud_GNU_GETTEXT in configure.in but \`intl' not in SUBDIRS")
1167                 if $contents{'SUBDIRS'} !~ /\bintl\b/;
1168     }
1170     &require_file ($NORMAL, 'ABOUT-NLS') if $seen_gettext;
1172     return if ! defined $contents{'SUBDIRS'};
1174     # Make sure each directory mentioned in SUBDIRS actually exists.
1175     local ($dir);
1176     foreach $dir (split (/\s+/, $contents{'SUBDIRS'}))
1177     {
1178         # Skip directories substituted by configure.
1179         next if $dir =~ /^\@.*\@$/;
1180         &am_line_error ('SUBDIRS',
1181                         "required directory $relative_dir/$dir does not exist")
1182             if ! -d $relative_dir . '/' . $dir;
1183     }
1185     $output_rules .= &file_contents ('subdirs');
1187     # Push a bunch of phony targets.
1188     local ($phonies);
1189     foreach $phonies ('-data', '-exec', 'dirs')
1190     {
1191         push (@phony, 'install' . $phonies . '-recursive');
1192         push (@phony, 'uninstall' . $phonies . '-recursive');
1193     }
1194     foreach $phonies ('all', 'check', 'installcheck', 'info', 'dvi')
1195     {
1196         push (@phony, $phonies . '-recursive');
1197     }
1198     &push_phony_cleaners ('recursive');
1200     push (@check, "check-recursive");
1201     push (@installcheck, "installcheck-recursive");
1202     push (@info, "info-recursive");
1203     push (@dvi, "dvi-recursive");
1205     $recursive_install = 1;
1208 # Handle remaking and configure stuff.
1209 sub handle_configure
1211     # If SUBDIRS defined, require AC_PROG_MAKE_SET.
1212     &am_line_error ('SUBDIRS', "AC_PROG_MAKE_SET must be used in configure.in")
1213         if defined $contents{'SUBDIRS'} && ! $seen_make_set;
1215     local ($top_reldir);
1216     if ($relative_dir ne '.')
1217     {
1218         # In subdirectory.
1219         $output_rules .= &file_contents ('remake-subd');
1220         $top_reldir = '../';
1221     }
1222     else
1223     {
1224         if (-f 'aclocal.m4')
1225         {
1226             $output_vars .= "ACLOCAL = aclocal.m4\n";
1227             &push_dist_common ('aclocal.m4');
1228         }
1229         $output_rules .= &file_contents ('remake');
1231         # Look for some files we need.
1232         &require_file ($NORMAL, 'install-sh', 'mkinstalldirs');
1234         &am_error
1235             ("\`install.sh' is an anachronism; use \`install-sh' instead")
1236                 if -f $relative_dir . '/install.sh';
1238         # If we have a configure header, require it.
1239         if ($config_header)
1240         {
1241             # FIXME this restriction should be lifted.
1242             # FIXME first see if it is even needed as-is.
1243             &am_line_error ($config_header_line,
1244                             "argument to AC_CONFIG_HEADER contains \`/'\n")
1245                 if ($config_header =~ /\//);
1247             &require_file ($NORMAL, $config_header);
1249             # Header defined and in this directory.
1250             if (-f 'acconfig.h')
1251             {
1252                 $output_vars .= "ACCONFIG = acconfig.h\n";
1253                 &push_dist_common ('acconfig.h');
1254             }
1255             if (-f $config_name . '.top')
1256             {
1257                 $output_vars .= "CONFIG_TOP = ${config_name}.top\n";
1258                 &push_dist_common ($config_name . '.top');
1259             }
1260             if (-f $config_name . '.bot')
1261             {
1262                 $output_vars .= "CONFIG_BOT = ${config_name}.bot\n";
1263                 &push_dist_common ($config_name . '.bot');
1264             }
1266             &push_dist_common ('stamp-h.in');
1268             $output_rules .= &file_contents ('remake-hdr');
1269             $output_vars .= "CONFIG_HEADER_IN = ${config_header}\n";
1270         }
1272         $top_reldir = '';
1273     }
1275     &am_line_error ('CONFIG_HEADER',
1276                     "\`CONFIG_HEADER' is an anachronism; now determined from \`configure.in'")
1277         if defined $contents{'CONFIG_HEADER'};
1279     # Generate CONFIG_HEADER define, and define interally.
1280     $output_vars .= "CONFIG_HEADER = ${top_builddir}/${config_name}\n"
1281         if $config_name;
1282     $contents{'CONFIG_HEADER'} = "${top_builddir}/${config_name}"
1283         if $config_name;
1285     # Now look for other files in this directory which must be remade
1286     # by config.status, and generate rules for them.
1287     local ($file, $local, $input);
1288     foreach $file (@other_input_files)
1289     {
1290         # Skip files not in this directory, any Makefile, and the
1291         # config header.  These last two must be handled specially.
1292         next unless &dirname ($file) eq $relative_dir;
1293         next if $file eq $top_builddir . '/' . $config_name;
1294         ($local = $file) =~ s/^.*\///;
1295         next if $local eq 'Makefile';
1297         if ($local =~ /^(.*):(.*)$/)
1298         {
1299             # This is the ":" syntax of AC_OUTPUT.
1300             $input = $2;
1301             $local = $1;
1302         }
1303         else
1304         {
1305             # Normal usage.
1306             $input = $local . '.in';
1307         }
1308         # FIXME when using autoconf ":" syntax, should we set CONFIG_FILES
1309         # to $local:$input?
1310         $output_rules .= ($local . ': '
1311                           . '$(top_builddir)/config.status ' . $input . "\n"
1312                           . "\t"
1313                           . 'cd $(top_builddir) && CONFIG_FILES='
1314                           . ($relative_dir eq '.' ? '' : '$(subdir)/')
1315                           . '$@ CONFIG_HEADERS= ./config.status'
1316                           . "\n");
1317         &require_file ($NORMAL, $local . '.in');
1318     }
1321 # Handle C headers.
1322 sub handle_headers
1324     &am_install_var ('header', 'HEADERS', 'include',
1325                      'oldinclude', 'pkginclude',
1326                      'noinst');
1329 # Handle footer elements.
1330 sub handle_footer
1332     if ($contents{'SOURCES'})
1333     {
1334         &pretty_print ('SOURCES =', "",
1335                        split (/\s+/, $contents{'SOURCES'}));
1336     }
1337     if ($contents{'OBJECTS'})
1338     {
1339         &pretty_print ('OBJECTS =', "",
1340                        split (/\s+/, $contents{'OBJECTS'}));
1341     }
1342     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
1343     {
1344         $output_vars .= "\n";
1345     }
1347     if (defined $contents{'SUFFIXES'})
1348     {
1349         push (@suffixes, '$(SUFFIXES)');
1350     }
1352     $output_trailer .= ".SUFFIXES:\n";
1353     if (@suffixes)
1354     {
1355         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
1356     }
1357     $output_trailer .= &file_contents ('footer');
1360 # Deal with installdirs target.
1361 sub handle_installdirs
1363     # GNU Makefile standards recommend this.
1364     $output_rules .= ("installdirs:"
1365                       . ($recursive_install
1366                          ? " installdirs-recursive\n"
1367                          : "\n"));
1368     push (@phony, 'installdirs');
1369     if (@installdirs)
1370     {
1371         &pretty_print_rule ("\t\$(top_srcdir)/mkinstalldirs ", "\t\t",
1372                             @installdirs);
1373     }
1374     $output_rules .= "\n";
1377 # There are several targets which need to be merged.  This is because
1378 # their complete definition is compiled from many parts.  Note that we
1379 # avoid double colon rules, otherwise we'd use them instead.
1380 sub handle_merge_targets
1382     push (@all, 'Makefile');
1383     push (@all, $config_name)
1384         if $config_name && &dirname ($config_name) eq $relative_dir;
1386     &do_one_merge_target ('info', @info);
1387     &do_one_merge_target ('dvi', @dvi);
1389     if (! defined $contents{'SUBDIRS'} || $relative_dir ne '.')
1390     {
1391         # 'check' must depend on 'all', but not at top level.
1392         # Ditto install.
1393         unshift (@check, 'all');
1394         unshift (@install, 'all');
1395     }
1396     &do_one_merge_target ('check', @check);
1397     &do_one_merge_target ('installcheck', @installcheck);
1399     # Handle the various install targets specially.  We do this so
1400     # that (eg) "make install-exec" will run "install-exec-recursive"
1401     # if required, but "make install" won't run it twice.  Step one is
1402     # to see if the user specified local versions of any of the
1403     # targets we handle.  "all" is treated as one of these since
1404     # "install" can run it.
1405     push (@install_exec, 'install-exec-local')
1406         if defined $contents{'install-exec-local'};
1407     push (@install_data, 'install-data-local')
1408         if defined $contents{'install-data-local'};
1409     push (@uninstall, 'uninstall-local')
1410         if defined $contents{'uninstall-local'};
1411     push (@all, 'all-local')
1412         if defined $contents{'all-local'};
1414     if (defined $contents{'install-local'})
1415     {
1416         &am_line_error ('install-local',
1417                         "use \`install-data' or \`install-exec', not \`install'");
1418     }
1420     # Step two: if we are doing recursive makes, write out the
1421     # appropriate rules.
1422     local (@install);
1423     if ($recursive_install)
1424     {
1425         push (@install, 'install-recursive');
1427         if (@all)
1428         {
1429             $output_rules .= ('all-am: '
1430                               . join (' ', @all)
1431                               . "\n\n");
1432             @all = ('all-recursive', 'all-am');
1433             push (@phony, 'all-am');
1434         }
1435         else
1436         {
1437             @all = ('all-recursive');
1438         }
1439         if (@install_exec)
1440         {
1441             $output_rules .= ('install-exec-am: '
1442                               . join (' ', @install_exec)
1443                               . "\n\n");
1444             @install_exec = ('install-exec-recursive', 'install-exec-am');
1445             push (@install, 'install-exec-am');
1446             push (@phony, 'install-exec-am');
1447         }
1448         else
1449         {
1450             @install_exec = ('install-exec-recursive');
1451         }
1452         if (@install_data)
1453         {
1454             $output_rules .= ('install-data-am: '
1455                               . join (' ', @install_data)
1456                               . "\n\n");
1457             @install_data = ('install-data-recursive', 'install-data-am');
1458             push (@install, 'install-data-am');
1459             push (@phony, 'install-data-am');
1460         }
1461         else
1462         {
1463             @install_data = ('install-data-recursive');
1464         }
1465         if (@uninstall)
1466         {
1467             $output_rules .= ('uninstall-am: '
1468                               . join (' ', @uninstall)
1469                               . "\n\n");
1470             @uninstall = ('uninstall-recursive', 'uninstall-am');
1471             push (@phony, 'uninstall-am');
1472         }
1473         else
1474         {
1475             @uninstall = ('uninstall-recursive');
1476         }
1477     }
1479     # Step three: print definitions users can use.
1480     $output_rules .= ("install-exec: "
1481                       . join (' ', @install_exec)
1482                       . "\n\n");
1483     push (@install, 'install-exec') if !$recursive_install;
1484     push (@phony, 'install-exec');
1486     $output_rules .= ("install-data: "
1487                       . join (' ', @install_data)
1488                       . "\n\n");
1489     push (@install, 'install-data') if !$recursive_install;
1490     push (@phony, 'install-data');
1492     # If no dependencies for 'install', add 'all'.  Why?  That way
1493     # "make install" at top level of distclean'd distribution won't
1494     # fail because stuff in 'lib' fails to build.
1495     push (@install, 'all') if ! @install;
1496     $output_rules .= ('install: '
1497                       . join (' ', @install)
1498                       # Use "@:" as empty command so nothing prints.
1499                       . "\n\t\@:"
1500                       . "\n\n"
1501                       . 'uninstall: '
1502                       . join (' ', @uninstall)
1503                       . "\n\n");
1504     push (@phony, 'install', 'uninstall');
1506     $output_rules .= ('all: '
1507                       . join (' ', @all)
1508                       . "\n\n");
1509     push (@phony, 'all');
1512 # Helper for handle_merge_targets.
1513 sub do_one_merge_target
1515     local ($name, @values) = @_;
1517     if (defined $contents{$name . '-local'})
1518     {
1519         # User defined local form of target.  So include it.
1520         push (@values, $name . '-local');
1521         push (@phony, $name . '-local');
1522     }
1524     $output_rules .= $name . ":";
1525     if (@values)
1526     {
1527         $output_rules .= ' ' . join (' ', @values);
1528     }
1529     $output_rules .= "\n\n";
1530     push (@phony, $name);
1533 # Handle all 'clean' targets.
1534 sub handle_clean
1536     push (@clean, 'generic');
1537     $output_rules .= &file_contents ('clean');
1538     &push_phony_cleaners ('generic');
1540     local ($target) = $recursive_install ? 'clean-am' : 'clean';
1541     &do_one_clean_target ($target, 'mostly', '', @clean);
1542     &do_one_clean_target ($target, '', 'mostly', @clean);
1543     &do_one_clean_target ($target, 'dist', '', @clean);
1544     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
1546     push (@phony, 'clean', 'mostlyclean', 'distclean', 'maintainer-clean');
1548     local (@deps);
1549     if ($recursive_install)
1550     {
1551         @deps = ('am', 'recursive');
1552         &do_one_clean_target ('', 'mostly', '', @deps);
1553         &do_one_clean_target ('', '', '', @deps);
1554         &do_one_clean_target ('', 'dist', '', @deps);
1555         &do_one_clean_target ('', 'maintainer-', '', @deps);
1556     }
1559 # Helper for handle_clean.
1560 sub do_one_clean_target
1562     local ($target, $name, $last_name, @deps) = @_;
1564     # Special case: if target not passed, then don't generate
1565     # dependency on next "lower" clean target (eg no
1566     # clean<-mostlyclean derivation).  In this case the target is
1567     # implicitly known to be 'clean'.
1568     local ($flag) = $target;
1569     $target = 'clean' if ! $flag;
1571     grep (($_ = $name . 'clean-' . $_) && 0, @deps);
1572     if ($flag)
1573     {
1574         if ($last_name || $name ne 'mostly')
1575         {
1576             push (@deps, $last_name . $target . " ");
1577         }
1578     }
1579     # FIXME not sure if I like the tabs here.
1580     &pretty_print_rule ($name . $target . ": ", "\t\t", @deps);
1582     # FIXME shouldn't we really print these messages before running
1583     # the dependencies?
1584     if ($name . $target eq 'maintainer-clean')
1585     {
1586         # Print a special warning.
1587         $output_rules .=
1588             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
1589              . "\t\@echo \"it deletes files that may require special "
1590              . "tools to rebuild.\"\n");
1592         $output_rules .= "\trm -f config.status\n"
1593             if $relative_dir eq '.';
1594     }
1595     elsif ($name . $target eq 'distclean')
1596     {
1597         $output_rules .= "\trm -f config.status\n";
1598     }
1599     $output_rules .= "\n";
1602 # Handle .PHONY target.
1603 sub handle_phony
1605     &pretty_print_rule ('.PHONY:', "", @phony);
1606     $output_rules .= "\n";
1609 # Handle TESTS variable.
1610 sub handle_tests
1612     return if ! defined $contents{'TESTS'};
1614     &push_dist_common (split (/\s+/, $contents{'TESTS'}));
1615     push (@check, 'check-TESTS');
1616     # FIXME use $(SHELL) here?  That is what Ulrich suggests.  Maybe a
1617     # new macro, $(TEST_SHELL), a la $(CONFIG_SHELL)?  For now we just
1618     # execute the file directly; this allows test files which are
1619     # compiled -- a possibly useful feature.
1620     $output_rules .= 'check-TESTS:
1621         @failed=0; all=0; \\
1622         srcdir=$(srcdir); export srcdir; \\
1623         for tst in $(TESTS); do \\
1624           all=`expr $$all + 1`; \\
1625           if test -f $$tst; then dir=.; \\
1626           else dir="$(srcdir)"; fi; \\
1627           if $$dir/$$tst; then \\
1628             echo "PASS: $$tst"; \\
1629           else \\
1630             failed=`expr $$failed + 1`; \\
1631             echo "FAIL: $$tst"; \\
1632           fi; \\
1633         done; \\
1634         if test "$$failed" -eq 0; then \\
1635           echo "========================"; \\
1636           echo "All $$all tests passed"; \\
1637           echo "========================"; \\
1638         else \\
1639           echo "$$failed of $$all tests failed"; \\
1640         fi
1644 ################################################################
1646 # Scan configure.in for interesting things.
1647 # FIXME ensure VERSION, PACKAGE are set.
1648 sub scan_configure
1650     open (CONFIGURE, 'configure.in')
1651         || die "automake: couldn't open configure.in: $!\n";
1652     print "automake: reading configure.in\n" if $verbose;
1654     # Reinitialize libsources here.  This isn't really necessary,
1655     # since we currently assume there is only one configure.in.  But
1656     # that won't always be the case.
1657     %libsources = ();
1659     local ($in_ac_output, @make_list) = 0;
1660     local ($seen_arg_prog) = 0;
1661     local ($libobj_iter);
1662     while (<CONFIGURE>)
1663     {
1664         # Remove comments from current line.
1665         s/\bdnl\b.*$//;
1666         s/\#.*$//;
1668         # Populate libobjs array.
1669         if (/AC_FUNC_ALLOCA/)
1670         {
1671             $libsources{'alloca.c'} = 1;
1672         }
1673         elsif (/AC_FUNC_GETLOADAVG/)
1674         {
1675             $libsources{'getloadavg.c'} = 1;
1676         }
1677         elsif (/AC_FUNC_MEMCMP/)
1678         {
1679             $libsources{'memcmp.c'} = 1;
1680         }
1681         elsif (/AC_STRUCT_ST_BLOCKS/)
1682         {
1683             $libsources{'fileblocks.c'} = 1;
1684         }
1685         elsif (/(AC|fp)_FUNC_FNMATCH/)
1686         {
1687             # AC_FUNC_FNMATCH is just wishful thinking at this point.
1688             $libsources{'fnmatch.c'} = 1;
1689         }
1690         elsif (/AC_REPLACE_FUNCS\s*\((.*)\)/)
1691         {
1692             foreach (split (/\s+/, $1))
1693             {
1694                 $libsources{$_ . '.c'} = 1;
1695             }
1696         }
1697         elsif (/LIBOBJS="(.*)\s+\$LIBOBJS"/
1698                || /LIBOBJS="\$LIBOBJS\s+(.*)"/)
1699         {
1700             foreach $libobj_iter (split (/\s+/, $1))
1701             {
1702                 if ($libobj_iter =~ /^(.*)\.o$/)
1703                 {
1704                     $libsources{$1 . '.c'} = 1;
1705                 }
1706             }
1707         }
1709         # Process the AC_OUTPUT macro.
1710         if (! $in_ac_output && s/AC_OUTPUT\s*\(\[?//)
1711         {
1712             $in_ac_output = 1;
1713         }
1714         if ($in_ac_output)
1715         {
1716             $in_ac_output = 0 if s/[\]\),].*$//;
1718             # Look at potential Makefile.am's.
1719             foreach (split)
1720             {
1721                 next if $_ eq "\\";
1722                 if (-f $_ . '.am')
1723                 {
1724                     push (@make_list, $_);
1725                 }
1726                 else
1727                 {
1728                     push (@other_input_files, $_);
1729                 }
1730             }
1731         }
1733         # Check for ansi2knr.
1734         $fp_c_prototypes = 1 if /fp_C_PROTOTYPES/;
1736         # Check for NLS support.
1737         if (/ud_GNU_GETTEXT/)
1738         {
1739             $seen_gettext = 1;
1740         }
1742         # Handle configuration headers.
1743         if (/AC_CONFIG_HEADER\s*\((.*)\)/)
1744         {
1745             $config_header_line = $.;
1746             $config_name = $1;
1747             if ($config_name =~ /^([^:]+):(.+)$/)
1748             {
1749                 $config_name = $1;
1750                 $config_header = $2;
1751             }
1752             else
1753             {
1754                 $config_header = $config_name . '.in';
1755             }
1756         }
1758         $seen_canonical = 1 if /AC_CANONICAL_(HOST|SYSTEM)/;
1759         $seen_canonical = 1 if /AC_CHECK_TOOL/;
1760         $seen_path_xtra = 1 if /AC_PATH_XTRA/;
1762         # Sometimes it is desirable to explicitly set YACC.  For
1763         # instance some people don't want to use bison.
1764         $seen_prog_yacc = 1 if (/AC_PROG_YACC/
1765                                 || /AC_SUBST\(YACC\)/
1766                                 || /AC_(PATH|CHECK)_PROGS?\(YACC/);
1768         # Some things required by Automake.  FIXME We only really
1769         # require AC_ARG_PROGRAM if any program is installed.
1770         $seen_make_set = 1 if /AC_PROG_MAKE_SET/;
1771         $seen_prog_install = 1 if ! $seen_prog_install && /AC_PROG_INSTALL/;
1772         $seen_prog_install = 2 if ! $seen_prog_install && /fp_PROG_INSTALL/;
1773         $seen_arg_prog = 1 if /AC_ARG_PROGRAM/;
1774         $seen_ranlib = 1 if /AC_PROG_RANLIB/;
1775     }
1777     # Set input files if not specified by user.
1778     @input_files = @make_list if (! @input_files);
1780     &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1781         unless $seen_arg_prog;
1783     close (CONFIGURE);
1786 ################################################################
1788 # Do any extra checking for GNU standards.
1789 sub check_gnu_standards
1791     &require_file ($GNU, 'ChangeLog');
1793     if ($relative_dir eq '.')
1794     {
1795         # In top level (or only) directory.
1796         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
1797                        'AUTHORS');
1798     }
1801 # Do any extra checking for GNITS standards.
1802 sub check_gnits_standards
1804     if ($strictness >= $GNITS && -f $relative_dir . '/COPYING.LIB')
1805     {
1806         &am_error
1807             ("\`${relative_dir}/COPYING.LIB' disallowed by Gnits standards");
1808     }
1810     if ($relative_dir eq '.')
1811     {
1812         # In top level (or only) directory.
1813         &require_file ($GNITS, 'THANKS');
1814     }
1817 ################################################################
1819 # Pretty-print something.  HEAD is what should be printed at the
1820 # beginning of the first line, FILL is what should be printed at the
1821 # beginning of every subsequent line.
1822 sub pretty_print_internal
1824     local ($head, $fill, @values) = @_;
1826     local ($column) = length ($head);
1827     local ($result) = $head;
1829     # Fill length is number of characters.  However, each Tab
1830     # character counts for eight.  So we count the number of Tabs and
1831     # multiply by 7.
1832     local ($fill_length) = length ($fill);
1833     $fill_length += 7 * ($fill =~ tr/\t/\t/d);
1835     local ($bol) = 0;
1836     foreach (@values)
1837     {
1838         # "71" because we also print a space.
1839         if ($column + length ($_) > 71)
1840         {
1841             $result .= " \\\n" . $fill;
1842             $column = $fill_length;
1843             $bol = 1;
1844         }
1846         $result .= ' ' unless ($bol);
1847         $result .= $_;
1848         $column += length ($_) + 1;
1849         $bol = 0;
1850     }
1852     $result .= "\n";
1853     return $result;
1856 # Pretty-print something and append to output_vars.
1857 sub pretty_print
1859     $output_vars .= &pretty_print_internal (@_);
1862 # Pretty-print something and append to output_rules.
1863 sub pretty_print_rule
1865     $output_rules .= &pretty_print_internal (@_);
1869 ################################################################
1871 # Read Makefile.am and set up %contents.  Simultaneously copy lines
1872 # from Makefile.am into $output_trailer or $output_vars as
1873 # appropriate.  NOTE we put rules in the trailer section.  We want
1874 # user rules to come after our generated stuff.
1875 sub read_am_file
1877     local ($amfile) = @_;
1879     # Compute relative location of the top object directory.
1880     local (@topdir) = ();
1881     foreach (split (/\//, $relative_dir))
1882     {
1883         next if $_ eq '.' || $_ eq '';
1884         if ($_ eq '..')
1885         {
1886             pop @topdir;
1887         }
1888         else
1889         {
1890             push (@topdir, '..');
1891         }
1892     }
1893     @topdir = ('.') if ! @topdir;
1895     $top_builddir = join ('/', @topdir);
1896     local ($build_rx);
1897     ($build_rx = $top_builddir) =~ s/(\W)/\\$1/g;
1898     local ($header_vars) =
1899         &file_contents_with_transform
1900             ('s/\@top_builddir\@/' . $build_rx . '/g',
1901              'header-vars');
1903     open (AM_FILE, $amfile) || die "automake: couldn't open $amfile: $!\n";
1904     print "automake: reading $amfile\n" if $verbose;
1906     $output_vars .= ("# Makefile.in generated automatically by automake "
1907                      . $VERSION . " from Makefile.am\n");
1909     # Generate copyright for generated Makefile.in.
1910     $output_vars .= $gen_copyright;
1912     local ($saw_bk) = 0;
1913     local ($was_rule) = 0;
1914     local ($spacing) = '';
1915     local ($comment) = '';
1916     local ($last_var_name) = '';
1918     while (<AM_FILE>)
1919     {
1920         if (/$IGNORE_PATTERN/o)
1921         {
1922             # Merely delete comments beginning with two hashes.
1923         }
1924         elsif (/$WHITE_PATTERN/o)
1925         {
1926             # Stick a single white line before the incoming macro or rule.
1927             $spacing = "\n";
1928         }
1929         elsif (/$COMMENT_PATTERN/o)
1930         {
1931             # Stick comments before the incoming macro or rule.
1932             $comment .= $spacing . $_;
1933             $spacing = '';
1934         }
1935         else
1936         {
1937             last;
1938         }
1939     }
1941     $output_vars .= $comment . "\n" . $header_vars;
1942     $comment = '';
1943     $spacing = "\n";
1945     while ($_)
1946     {
1947         if (/$IGNORE_PATTERN/o)
1948         {
1949             # Merely delete comments beginning with two hashes.
1950         }
1951         elsif (/$WHITE_PATTERN/o)
1952         {
1953             # Stick a single white line before the incoming macro or rule.
1954             $spacing = "\n";
1955         }
1956         elsif (/$COMMENT_PATTERN/o)
1957         {
1958             # Stick comments before the incoming macro or rule.
1959             $comment .= $spacing . $_;
1960             $spacing = '';
1961         }
1962         elsif ($saw_bk)
1963         {
1964             if ($was_rule)
1965             {
1966                 $output_trailer .= $_;
1967                 $saw_bk = /\\$/;
1968             }
1969             else
1970             {
1971                 $output_vars .= $_;
1972                 $saw_bk = /\\$/;
1973                 # Chop newline and backslash if this line is
1974                 # continued.  FIXME maybe ensure trailing whitespace
1975                 # exists?
1976                 chop if $saw_bk;
1977                 chop if $saw_bk;
1978                 $contents{$last_var_name} .= $_;
1979             }
1980         }
1981         elsif (/$RULE_PATTERN/o)
1982         {
1983             # warn "** Saw rule .$1.\n";
1984             # Found a rule.
1985             $was_rule = 1;
1986             # Value here doesn't matter; for targets we only note
1987             # existence.
1988             $contents{$1} = 1;
1989             $content_lines{$1} = $.;
1990             $output_trailer .= $comment . $spacing . $_;
1991             $comment = $spacing = '';
1992             $saw_bk = /\\$/;
1993         }
1994         elsif (/$MACRO_PATTERN/o)
1995         {
1996             # warn "** Saw macro .$1.\n";
1997             # Found a macro definition.
1998             $was_rule = 0;
1999             $last_var_name = $1;
2000             if (substr ($2, -1) eq "\\")
2001             {
2002                 $contents{$1} = substr ($2, 0, length ($2) - 1);
2003             }
2004             else
2005             {
2006                 $contents{$1} = $2;
2007             }
2008             $content_lines{$1} = $.;
2009             $output_vars .= $comment . $spacing . $_;
2010             $comment = $spacing = '';
2011             $saw_bk = /\\$/;
2012         }
2013         else
2014         {
2015             # This isn't an error; it is probably a continued rule.
2016             # In fact, this is what we assume.
2017             $was_rule = 1;
2018             $output_trailer .= $comment . $spacing . $_;
2019             $comment = $spacing = '';
2020             $saw_bk = /\\$/;
2021         }
2023         $_ = <AM_FILE>;
2024     }
2026     $output_trailer .= $comment;
2029 ################################################################
2031 sub initialize_global_constants
2033     # Associative array of standard directory names.  Entry is TRUE if
2034     # corresponding directory should be installed during
2035     # 'install-exec' phase.
2036     %exec_dir_p =
2037         ('bin', 1,
2038          'sbin', 1,
2039          'libexec', 1,
2040          'data', 0,
2041          'sysconf', 1,
2042          'localstate', 1,
2043          'lib', 1,
2044          'info', 0,
2045          'man', 0,
2046          'include', 0,
2047          'oldinclude', 0,
2048          'pkgdata', 0,
2049          'pkglib', 1,
2050          'pkginclude', 0
2051          );
2053     # Helper text for dealing with man pages.
2054     $install_man_format =
2055     '   @sect=@SECTION@;                                \\
2056         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
2057         echo installing @MAN@ as $(mandir)/man$$sect/$$inst; \\
2058         $(INSTALL_DATA) $(srcdir)/@MAN@ $(mandir)/man$$sect/$$inst
2061     $uninstall_man_format =
2062     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
2063         rm -f $(mandir)/man@SECTION@/$$inst
2066     # Commonly found files we look for and automatically include in
2067     # DISTFILES.
2068     @common_files =
2069         (
2070          "README", "THANKS", "TODO", "NEWS", "COPYING", "COPYING.LIB",
2071          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
2072          "config.guess", "config.sub", "AUTHORS", "BACKLOG", "ABOUT-GNU"
2073          );
2075     # Commonly used files we auto-include, but only sometimes.
2076     @common_sometimes =
2077         (
2078          "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
2079          "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
2080          "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
2081          );
2083     $USAGE = "\
2084   --amdir=DIR           directory storing config files
2085   --gnits               same as --strictness=gnits
2086   --gnu                 same as --strictness=gnu
2087   --help                print this help, then exit
2088   --include-deps        include generated dependencies in Makefile.in
2089   --install-missing     install missing standard files
2090   --output-dir=DIR      put generated Makefile.in's into DIR
2091   --strictness=LEVEL    set strictness level.  LEVEL is normal, gnu, gnits
2092   --verbose             verbosely list files processed
2093   --version             print version number, then exit\n";
2095     # Copyright on generated Makefile.ins.
2096     $gen_copyright = "\
2097 # Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
2098 # This Makefile.in is free software; the Free Software Foundation
2099 # gives unlimited permission to copy, distribute and modify it.
2103 # (Re)-Initialize per-Makefile.am variables.
2104 sub initialize_per_input
2106     # These two variables are used when generating each Makefile.in.
2107     # They hold the Makefile.in until it is ready to be printed.
2108     $output_rules = '';
2109     $output_vars = '';
2110     $output_trailer = '';
2112     # Suffixes found during a run.
2113     @suffixes = ();
2115     # This holds the contents of a Makefile.am, as parsed by
2116     # read_am_file.
2117     %contents = ();
2119     # This holds the line numbers at which various elements of
2120     # %contents are defined.
2121     %content_lines = ();
2123     # This holds the "relative directory" of the current Makefile.in.
2124     # Eg for src/Makefile.in, this is "src".
2125     $relative_dir = '';
2127     # Directory where output files go.  Actually, output files are
2128     # relative to this directory.
2129     $output_directory = '.';
2131     # This holds a list of files that are included in the
2132     # distribution.
2133     %dist_common = ();
2135     # List of dependencies for the obvious targets.
2136     @install_data = ();
2137     @install_exec = ();
2138     @uninstall = ();
2139     @installdirs = ();
2141     @info = ();
2142     @dvi = ();
2143     @all = ();
2144     @check = ();
2145     @installcheck = ();
2146     @clean = ();
2148     @phony = ();
2150     # These are pretty obvious, too.  They are used to define the
2151     # SOURCES and OBJECTS variables.
2152     @sources = ();
2153     @objects = ();
2155     # TRUE if current directory holds any C source files.  (Actually
2156     # holds object extension, but this information is encapsulated in
2157     # the function get_object_extension).
2158     $dir_holds_sources = '';
2160     # TRUE if install targets should work recursively.
2161     $recursive_install = 0;
2163     # All .P files.
2164     %dep_files = ();
2166     # Strictness levels.
2167     $strictness = $default_strictness;
2168     $strictness_name = $default_strictness_name;
2170     # Options from AUTOMAKE_OPTIONS.
2171     %options = ();
2173     # Whether or not dependencies are handled.  Can be further changed
2174     # in handle_options.
2175     $use_dependencies = $cmdline_use_dependencies;
2177     # Per Makefile.am.
2178     $local_maint_charset = $maint_charset;
2182 ################################################################
2184 # Return contents of a file from $am_dir, automatically skipping
2185 # macros or rules which are already known.  Runs command on each line
2186 # as it is read; this command can modify $_.
2187 sub file_contents_with_transform
2189     local ($command, $basename) = @_;
2190     local ($file) = $am_dir . '/' . $basename . '.am';
2192     open (FC_FILE, $file)
2193         || die "automake: installation error: cannot open \`$file'\n";
2194     # Looks stupid?
2195     # print "automake: reading $file\n" if $verbose;
2197     local ($was_rule) = 0;
2198     local ($result_vars) = '';
2199     local ($result_rules) = '';
2200     local ($comment) = '';
2201     local ($spacing) = "\n";
2202     local ($skipping) = 0;
2204     while (<FC_FILE>)
2205     {
2206         eval $command;
2208         if (/$IGNORE_PATTERN/o)
2209         {
2210             # Merely delete comments beginning with two hashes.
2211         }
2212         elsif (/$WHITE_PATTERN/o)
2213         {
2214             # Stick a single white line before the incoming macro or rule.
2215             $spacing = "\n";
2216         }
2217         elsif (/$COMMENT_PATTERN/o)
2218         {
2219             # Stick comments before the incoming macro or rule.
2220             $comment .= $spacing . $_;
2221             $spacing = '';
2222         }
2223         elsif ($saw_bk)
2224         {
2225             if ($was_rule)
2226             {
2227                 $result_rules .= $_ if ! $skipping;
2228             }
2229             else
2230             {
2231                 $result_vars .= $_ if ! $skipping;
2232             }
2233             $saw_bk = /\\$/;
2234         }
2235         elsif (/$RULE_PATTERN/o)
2236         {
2237             # warn "** Found rule .$1.\n";
2238             # Found a rule.
2239             $was_rule = 1;
2240             $skipping = defined $contents{$1};
2241             # warn "** Skip $skipping\n" if $skipping;
2242             $result_rules .= $comment . $spacing . $_ if ! $skipping;
2243             $comment = $spacing = '';
2244             $saw_bk = /\\$/;
2245         }
2246         elsif (/$MACRO_PATTERN/o)
2247         {
2248             # warn "** Found macro .$1.\n";
2249             # Found a variable reference.
2250             $was_rule = 0;
2251             $skipping = defined $contents{$1};
2252             # warn "** Skip $skipping\n" if $skipping;
2253             $result_vars .= $comment . $spacing . $_ if ! $skipping;
2254             $comment = $spacing = '';
2255             $saw_bk = /\\$/;
2256         }
2257         else
2258         {
2259             # This isn't an error; it is probably a continued rule.
2260             # In fact, this is what we assume.
2261             $was_rule = 1;
2262             $result_rules .= $comment . $spacing . $_ if ! $skipping;
2263             $comment = $spacing = '';
2264             $saw_bk = /\\$/;
2265         }
2266     }
2268     close (FC_FILE);
2269     return $result_vars . $result_rules . $comment;
2272 # Like file_contents_with_transform, but no transform.
2273 sub file_contents
2275     return &file_contents_with_transform ('', @_);
2278 # Handle `where_HOW' variable magic.  Does all lookups, generates
2279 # install code,and possibly generates code to define the primary
2280 # variable.  The first argument is the name of the .am file to munge,
2281 # the second argument is the primary variable (eg HEADERS), and all
2282 # subsequent arguments are possible installation locations.  Returns
2283 # list of all values of all _HOW targets.
2285 # FIXME this should be rewritten to be cleaner.  It should be broken
2286 # up into multiple functions.
2288 # Usage is: am_install_var (OPTION..., file, HOW, where...)
2289 sub am_install_var
2291     local (@args) = @_;
2293     local ($do_all, $do_clean) = (1, 0);
2294     while (@args)
2295     {
2296         if ($args[0] eq '-clean')
2297         {
2298             $do_clean = 1;
2299         }
2300         elsif ($args[0] eq '-no-all')
2301         {
2302             $do_all = 0;
2303         }
2304         elsif ($args[0] !~ /^-/)
2305         {
2306             last;
2307         }
2308         shift (@args);
2309     }
2310     local ($file, $primary, @prefixes) = @args;
2312     local (@used) = ();
2313     local (@result) = ();
2315     # Now that configure substitutions are allowed in where_HOW
2316     # variables, it is an error to actually define the primary.
2317     &am_line_error ($primary, "\`$primary' is an anachronism")
2318         if defined $contents{$primary};
2320     local ($clean_file) = $file . '-clean';
2321     local ($one_name);
2322     local ($X);
2323     foreach $X (@prefixes)
2324     {
2325         $one_name = $X . '_' . $primary;
2326         if (defined $contents{$one_name})
2327         {
2328             # Append actual contents to result.  Skip elements that
2329             # look like configure substitutions.
2330             push (@result, grep (! /^\@.*\@$/,
2331                                  split (/\s+/, $contents{$one_name})));
2333             if ($do_clean)
2334             {
2335                 $output_rules .=
2336                     &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
2337                                                    $clean_file);
2339                 push (@clean, $X . $primary);
2340                 &push_phony_cleaners ($X . $primary);
2341             }
2343             push (@used, '$(' . $one_name . ')');
2344             if ($X eq 'noinst')
2345             {
2346                 # Objects in noinst_FOO never get installed.
2347                 next;
2348             }
2350             $output_rules .=
2351                 &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
2352                                                $file);
2354             push (@uninstall, 'uninstall-' . $X . $primary);
2355             push (@phony, 'uninstall-' . $X . $primary);
2356             push (@installdirs, '$(' . $X . 'dir)');
2357             if ($exec_dir_p{$X})
2358             {
2359                 push (@install_exec, 'install-' . $X . $primary);
2360                 push (@phony, 'install-' . $X . $primary);
2361             }
2362             else
2363             {
2364                 push (@install_data, 'install-' . $X . $primary);
2365                 push (@phony, 'install-' . $X . $primary);
2366             }
2367         }
2368     }
2370     if (@used)
2371     {
2372         # Define it.
2373         &pretty_print ($primary . ' =', '', @used);
2374         $output_vars .= "\n";
2375     }
2377     # Push here because PRIMARY might be configure time determined.
2378     push (@all, '$(' . $primary . ')')
2379         if $do_all && @used;
2381     # Look for misspellings.  It is an error to have a variable ending
2382     # in a "reserved" suffix whose prefix is unknown, eg
2383     # "bni_PROGRAMS".
2384     local (%valid, $varname);
2385     grep ($valid{$_} = 0, @prefixes);
2386     $valid{'EXTRA'} = 0;
2387     foreach $varname (keys %contents)
2388     {
2389         if ($varname =~ /^(.*)_$primary$/ && ! defined $valid{$1})
2390         {
2391             &am_line_error ($varname, "invalid variable \"$varname\"");
2392         }
2393     }
2395     push (@result, split (/\s+/, $contents{'EXTRA_' . $primary}))
2396         if defined $contents{'EXTRA_' . $primary};
2398     return (@result);
2402 ################################################################
2404 # Verify that the file must exist in the current directory.
2405 # Usage: require_file (strictness, file)
2406 # strictness is the strictness level at which this file becomes
2407 # required.
2408 sub require_file
2410     local ($mystrict, @files) = @_;
2411     local ($file, $fullfile);
2413     foreach $file (@files)
2414     {
2415         $fullfile = $relative_dir . "/" . $file;
2417         if (-f $fullfile)
2418         {
2419             &push_dist_common ($file);
2420         }
2421         elsif ($strictness >= $mystrict)
2422         {
2423             # Only install missing files according to our desired
2424             # strictness level.
2425             if ($install_missing && -f ($am_dir . '/' . $file))
2426             {
2427                 # Install the missing file.  Symlink if we can, copy
2428                 # if we must.
2429                 if ($symlink_exists)
2430                 {
2431                     symlink ($am_dir . '/' . $file, $fullfile);
2432                 }
2433                 else
2434                 {
2435                     system ('cp', $am_dir . '/' . $file, $fullfile);
2436                 }
2438                 # FIXME this is a hack.  Should have am_warn.
2439                 local ($save) = $exit_status;
2440                 &am_error
2441                     ("required file \"$fullfile\" not found; installing");
2442                 $exit_status = $save;
2443             }
2444             else
2445             {
2446                 # Only an error if strictness constraint violated.
2447                 &am_error ("required file \"$fullfile\" not found");
2448             }
2449         }
2450     }
2453 # Push a list of files onto dist_common.
2454 sub push_dist_common
2456     local (@files) = @_;
2457     local ($file);
2459     foreach $file (@files)
2460     {
2461         $dist_common{$file} = 1;
2462     }
2465 # Push a list of clean targets onto phony.
2466 sub push_phony_cleaners
2468     local ($base) = @_;
2469     local ($target);
2470     foreach $target ('mostly', 'dist', '', 'maintainer-')
2471     {
2472         push (@phony, $target . 'clean-' . $base);
2473     }
2476 # Set strictness.
2477 sub set_strictness
2479     $strictness_name = $_[0];
2480     if ($strictness_name eq 'gnu')
2481     {
2482         $strictness = $GNU;
2483     }
2484     elsif ($strictness_name eq 'gnits')
2485     {
2486         $strictness = $GNITS;
2487     }
2488     elsif ($strictness_name eq 'normal')
2489     {
2490         $strictness = $NORMAL;
2491     }
2492     else
2493     {
2494         die "automake: level \`$strictness_name' not recognized\n";
2495     }
2499 ################################################################
2501 # Return directory name of file.
2502 sub dirname
2504     local ($file) = @_;
2505     local ($sub);
2507     ($sub = $file) =~ s,/+[^/]+$,,g;
2508     $sub = '.' if $sub eq $file;
2509     return $sub;
2512 # Make a directory.
2513 sub mkdir
2515     local ($dirname) = @_;
2516     system ("mkdir", $dirname);
2519 ################################################################
2521 # Print an error message and set exit status.
2522 sub am_error
2524     warn "automake: ${am_file}.am: ", join (' ', @_), "\n";
2525     $exit_status = 1;
2528 sub am_line_error
2530     local ($symbol, @args) = @_;
2532     # If SYMBOL not already a line number, look it up in Makefile.am.
2533     $symbol = $content_lines{$symbol} unless $symbol =~ /^\d+$/;
2534     warn "${am_file}.am:", $symbol, ": ", join (' ', @args),
2535          "\n";
2536     $exit_status = 1;
2539 # The same, but while scanning configure.in.
2540 sub am_conf_error
2542     # FIXME can run in subdirs.
2543     warn "automake: configure.in: ", join (' ', @_), "\n";
2544     $exit_status = 1;
2547 # Tell user where our aclocal.m4 is, but only once.
2548 sub keyed_aclocal_warning
2550     local ($key) = @_;
2551     warn "automake: macro \`$key' can be found in ${am_dir}/aclocal.m4\n";
2554 # Print usage information.
2555 sub usage
2557     print "Usage: automake [OPTION] ... [Makefile]...\n";
2558     print $USAGE;
2559     print "\nFiles which are automatically distributed, if found:\n";
2560     $~ = "USAGE_FORMAT";
2561     local (@lcomm) = sort ((@common_files, @common_sometimes));
2562     local ($one, $two, $three, $four);
2563     while (@lcomm > 0)
2564     {
2565         $one = shift @lcomm;
2566         $two = @lcomm ? shift @lcomm : '';
2567         $three = @lcomm ? shift @lcomm : '';
2568         $four = @lcomm ? shift @lcomm : '';
2569         write;
2570     }
2572     exit 0;
2575 format USAGE_FORMAT =
2576   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
2577   $one,               $two,               $three,             $four