46385d6349e5f3e5319b7e8be117ce7cc2f00c99
[platform/upstream/automake.git] / automake.in
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
4
5 eval 'exec @PERL@ -S $0 ${1+"$@"}'
6     if 0;
7
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
10
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.
15
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.
20
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.
25
26 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
27 # Perl reimplementation by Tom Tromey <tromey@cygnus.com>.
28
29
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 $PACKAGE = "@PACKAGE@";
34 $prefix = "@prefix@";
35 $am_dir = "@datadir@/@PACKAGE@";
36 $TAR = "@TAR@";
37
38 # String constants.
39 $IGNORE_PATTERN = "^##([^#].*)?\$";
40 $WHITE_PATTERN = "^[ \t]*\$";
41 $COMMENT_PATTERN = "^#";
42 $RULE_PATTERN = "^([\$a-zA-Z_.][-.a-zA-Z0-9_(){}/\$]*) *:([^=].*|)\$";
43 $SUFFIX_RULE_PATTERN = "^\\.([a-zA-Z]+)\\.([a-zA-Z]+)\$";
44 $MACRO_PATTERN = "^([A-Za-z][A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)\$";
45 $BOGUS_MACRO_PATTERN = "^([^ \t]*)[ \t]*:?=[ \t]*(.*)\$";
46 $GNITS_VERSION_PATTERN = "[0-9]+\\.[0-9]+([a-z]|\\.[0-9]+)?";
47 $IF_PATTERN = "^if[ \t]+\([A-Za-z][A-Za-z0-9_]*\)[ \t]*\(#.*\)?\$";
48 $ELSE_PATTERN = "^else[ \t]*\(#.*\)?\$";
49 $ENDIF_PATTERN = "^endif[ \t]*\(#.*\)?\$";
50
51 # Some regular expressions.  One reason to put them here is that it
52 # makes indentation work better in Emacs.
53 $AC_CONFIG_AUX_DIR_PATTERN = "AC_CONFIG_AUX_DIR\\(([^)]+)\\)";
54 $AM_INIT_AUTOMAKE_PATTERN = "AM_INIT_AUTOMAKE\\([^,]*,([^,)]+)[,)]";
55 $AM_PACKAGE_VERSION_PATTERN = "^\\s*\\[?([^]\\s]+)\\]?\\s*\$";
56 # Note that there is no AC_PATH_TOOL.  But we don't really care.
57 $AC_CHECK_PATTERN = "AC_(CHECK|PATH)_(PROG|PROGS|TOOL)\\(\\[?(\\w+)";
58 $AM_MISSING_PATTERN = "AM_MISSING_PROG\\(\\[?(\\w+)";
59 # Just check for alphanumeric in AC_SUBST.  If you do AC_SUBST(5),
60 # then too bad.
61 $AC_SUBST_PATTERN = "AC_SUBST\\(\\[?(\\w+)";
62 $AM_CONDITIONAL_PATTERN = "AM_CONDITIONAL\\((\\w+)";
63
64 # Constants to define the "strictness" level.
65 $FOREIGN = 0;
66 $GNU = 1;
67 $GNITS = 2;
68
69 \f
70
71 # Variables global to entire run.
72
73 # TRUE if we should always generate Makefile.in.
74 $force_generation = 1;
75
76 # Strictness level as set on command line.
77 $default_strictness = $GNU;
78
79 # Name of strictness level, as set on command line.
80 $default_strictness_name = 'gnu';
81
82 # This is TRUE if GNU make specific automatic dependency generation
83 # code should be included in generated Makefile.in.
84 $cmdline_use_dependencies = 1;
85
86 # This is the name of a dependency makefile bit (usually for inclusion in a
87 # SMakefile or similar); empty if not set.
88 $generate_deps = '';
89
90 # TRUE if in verbose mode.
91 $verbose = 0;
92
93 # This holds our (eventual) exit status.  We don't actually exit until
94 # we have processed all input files.
95 $exit_status = 0;
96
97 # From the Perl manual.
98 $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
99
100 # TRUE if missing standard files should be installed.
101 $add_missing = 0;
102
103 # Files found by scanning configure.in for LIBOBJS.
104 %libsources = ();
105
106 # True if AM_C_PROTOTYPES appears in configure.in.
107 $am_c_prototypes = 0;
108
109 # Names used in AC_CONFIG_HEADER call.  @config_fullnames holds the
110 # name which appears in AC_CONFIG_HEADER, colon and all.
111 # @config_names holds the file names.  @config_headers holds the '.in'
112 # files.  Ordinarily these are similar, but they can be different if
113 # the weird "NAME:FILE" syntax is used.
114 @config_fullnames = ();
115 @config_names = ();
116 @config_headers = ();
117 # Line number at which AC_CONFIG_HEADER appears in configure.in.
118 $config_header_line = 0;
119
120 # Directory where output files go.  Actually, output files are
121 # relative to this directory.
122 $output_directory = '.';
123
124 # Relative location of top build directory.
125 $top_builddir = '';
126
127 # Absolute location of top build directory.
128 $build_directory = '';
129
130 # Name of srcdir as given in build directory's Makefile.  For
131 # dependencies only.
132 $srcdir_name = '';
133
134 # List of Makefile.am's to process, and their corresponding outputs.
135 @input_files = ();
136 %output_files = ();
137
138 # List of files in AC_OUTPUT without Makefile.am, and their outputs.
139 @other_input_files = ();
140 # Line number at which AC_OUTPUT seen.
141 $ac_output_line = 0;
142
143 # List of directories to search for configure-required files.  This
144 # can be set by AC_CONFIG_AUX_DIR.
145 @config_aux_path = ('.', '..', '../..');
146 $config_aux_dir = '';
147
148 # Whether AC_PROG_MAKE_SET has been seen in configure.in.
149 $seen_make_set = 0;
150
151 # Whether AM_GNU_GETTEXT has been seen in configure.in.
152 $seen_gettext = 0;
153 # Line number at which AM_GNU_GETTEXT seen.
154 $ac_gettext_line = 0;
155
156 # Whether ALL_LINGUAS has been seen.
157 $seen_linguas = '';
158 # The actual text.
159 $all_linguas = '';
160 # Line number at which it appears.
161 $all_linguas_line = 0;
162
163 # 1 if AC_PROG_INSTALL seen, 2 if AM_PROG_INSTALL seen.
164 $seen_prog_install = 0;
165
166 # 1 if any scripts installed, 0 otherwise.
167 $scripts_installed = 0;
168
169 # Whether AC_PATH_XTRA has been seen in configure.in.
170 $seen_path_xtra = 0;
171
172 # TRUE if AC_DECL_YYTEXT was seen.
173 $seen_decl_yytext = 0;
174
175 # TRUE if we've seen AC_CANONICAL_(HOST|SYSTEM).  The presence of
176 # AC_CHECK_TOOL also sets this.
177 $seen_canonical = 0;
178
179 # TRUE if we've seen AC_ARG_PROGRAM.
180 $seen_arg_prog = 0;
181
182 # TRUE if we've seen AM_PROG_LIBTOOL.
183 $seen_libtool = 0;
184 $libtool_line = 0;
185
186 # Files installed by libtoolize.
187 @libtoolize_files = ('ltconfig', 'ltmain.sh', 'config.guess', 'config.sub');
188
189 # TRUE if we've seen AM_MAINTAINER_MODE.
190 $seen_maint_mode = 0;
191
192 # TRUE if we've seen PACKAGE and VERSION.
193 $seen_package = 0;
194 $seen_version = 0;
195
196 # Actual version we've seen.
197 $package_version = '';
198
199 # Line number where we saw version definition.
200 $package_version_line = 0;
201
202 # TRUE if we've seen AM_PATH_LISPDIR.
203 $seen_lispdir = 0;
204
205 # TRUE if we've seen AM_EXEEXT.
206 $seen_exeext = 0;
207
208 # Hash table of discovered configure substitutions.  Keys are names,
209 # values are meaningless.
210 %configure_vars = ();
211
212 # Charsets used by maintainer and in distribution.  MAINT_CHARSET is
213 # handled in a funny way: if seen in the top-level Makefile.am, it is
214 # used for every directory which does not specify a different value.
215 # The rationale here is that some directories (eg gettext) might be
216 # distributions of other packages, and thus require their own charset
217 # info.  However, the DIST_CHARSET must be the same for the entire
218 # package; it can only be set at top-level.
219 # FIXME: this yields bugs when rebuilding.  What to do?  Always
220 # read (and sometimes discard) top-level Makefile.am?
221 $maint_charset = '';
222 $dist_charset = 'utf8';         # recode doesn't support this yet.
223
224 # Name of input file ("Makefile.in") and output file ("Makefile.am").
225 # These have no directory components.
226 $am_file_name = '';
227 $in_file_name = '';
228
229 # TRUE if --cygnus seen.
230 $cygnus_mode = 0;
231
232 # Keys of this hash are names of dependency files to ignore.
233 %omit_dependencies = ();
234
235 # Hash table of AM_CONDITIONAL variables seen in configure.
236 %configure_cond = ();
237
238 # Map from obsolete macros to hints for new macros.
239 # FIXME complete the list so that there are no `0' hints.
240 %obsolete_macros =
241     (
242      'AC_FEATURE_CTYPE', "use \`AC_HEADER_STDC'",
243      'AC_FEATURE_ERRNO', "add \`strerror' to \`AC_REPLACE_FUNCS(...)'",
244      'AC_FEATURE_EXIT', 0,
245      'AC_SYSTEM_HEADER', 0,
246
247      # Note that we do not handle this one, because it is still run
248      # from AM_CONFIG_HEADER.  So we deal with it specially in
249      # scan_configure.
250      # 'AC_CONFIG_HEADER', "use \`AM_CONFIG_HEADER'",
251
252      'fp_C_PROTOTYPES', "use \`AM_C_PROTOTYPES'",
253      'fp_PROG_CC_STDC', "use \`AM_PROG_CC_STDC'",
254      'fp_PROG_INSTALL', "use \`AM_PROG_INSTALL'",
255      'fp_WITH_DMALLOC', "use \`AM_WITH_DMALLOC'",
256      'fp_WITH_REGEX', "use \`AM_WITH_REGEX'",
257      'gm_PROG_LIBTOOL', "use \`AM_PROG_LIBTOOL'",
258      'jm_MAINTAINER_MODE', "use \`AM_MAINTAINER_MODE'",
259      'md_TYPE_PTRDIFF_T', "use \`AM_TYPE_PTRDIFF_T'",
260      'ud_PATH_LISPDIR', "use \`AM_PATH_LISPDIR'",
261
262      # Now part of autoconf proper, under a different name.
263      'AM_FUNC_FNMATCH', "use \`AC_FUNC_FNMATCH'",
264      'fp_FUNC_FNMATCH', "use \`AC_FUNC_FNMATCH'",
265      'AM_SANITY_CHECK_CC', "automatically done by \`AC_PROG_CC'",
266
267 # These aren't quite obsolete.
268 #      'md_PATH_PROG',
269      );
270
271 # Regexp to match the above macros.
272 $obsolete_rx = '(' . join ('|', keys %obsolete_macros) . ')';
273
274 \f
275
276 &initialize_global_constants;
277
278 # Parse command line.
279 &parse_arguments (@ARGV);
280
281 # Do configure.in scan only once.
282 &scan_configure;
283
284 die "automake: no \`Makefile.am' found or specified\n"
285     if ! @input_files;
286
287 # If --generate-deps was given, we don't do anything else
288 #
289 if ($generate_deps)
290 {
291     die "automake: Must specify --include-deps (or -i) when generating\n"
292       if ($use_dependencies);
293     die "automake: Must provide --build-dir when generating\n"
294       if (!$build_directory);
295     die "automake: Must provide --srcdir-name when generating\n"
296       if (!$srcdir_name);
297
298     open(GDEP, ">$output_directory/.dep_segment")
299       || die "automake: Could not open `$output_directory/.dep_segment': $!\n";
300
301     &handle_dependencies;
302     print GDEP $output_rules;
303
304     close(GDEP);
305     exit $exit_status;
306 }
307
308 # Now do all the work on each file.
309 foreach $am_file (@input_files)
310 {
311     if (! -f ($am_file . '.am'))
312     {
313         &am_error ("\`" . $am_file . ".am' does not exist");
314     }
315     else
316     {
317         &generate_makefile ($output_files{$am_file}, $am_file);
318     }
319 }
320
321 if ($seen_prog_install <= $scripts_installed)
322 {
323     &am_conf_error (($scripts_installed ? 'AM_PROG_INSTALL' : 'AC_PROG_INSTALL')
324                     . " must be used in configure.in");
325     &keyed_aclocal_warning ('AM_PROG_INSTALL')
326         if $scripts_installed;
327 }
328
329 exit $exit_status;
330
331
332 ################################################################
333
334 # Parse command line.
335 sub parse_arguments
336 {
337     local (@arglist) = @_;
338
339     # Start off as gnu.
340     &set_strictness ('gnu');
341
342     while (@arglist)
343     {
344         if ($arglist[0] eq "--version")
345         {
346             print "automake (GNU $PACKAGE) $VERSION\n\n";
347             print "Copyright (C) 1998 Free Software Foundation, Inc.\n";
348             print "This is free software; see the source for copying conditions.  There is NO\n";
349             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
350             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
351
352             exit 0;
353         }
354         elsif ($arglist[0] eq "--help")
355         {
356             &usage;
357         }
358         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
359         {
360             $am_dir = $1;
361         }
362         elsif ($arglist[0] eq '--amdir')
363         {
364             &require_argument (@arglist);
365             shift (@arglist);
366             $am_dir = $arglist[0];
367         }
368         elsif ($arglist[0] =~ /^--build-dir=(.+)$/)
369         {
370             # Must end in /.
371             $build_directory = $1 . '/';
372         }
373         elsif ($arglist[0] eq '--build-dir')
374         {
375             &require_argument (@arglist);
376             shift (@arglist);
377             # Must end in /.
378             $build_directory = $arglist[0] . '/';
379         }
380         elsif ($arglist[0] =~ /^--srcdir-name=(.+)$/)
381         {
382             $srcdir_name = $1;
383         }
384         elsif ($arglist[0] eq '--srcdir-name')
385         {
386             &require_argument (@arglist);
387             shift (@arglist);
388             $srcdir_name = $arglist[0];
389         }
390         elsif ($arglist[0] eq '--gnu')
391         {
392             &set_strictness ('gnu');
393         }
394         elsif ($arglist[0] eq '--gnits')
395         {
396             &set_strictness ('gnits');
397         }
398         elsif ($arglist[0] eq '--cygnus')
399         {
400             $cygnus_mode = 1;
401         }
402         elsif ($arglist[0] eq '--foreign')
403         {
404             &set_strictness ('foreign');
405         }
406         elsif ($arglist[0] eq '--include-deps' || $arglist[0] eq '-i')
407         {
408             $cmdline_use_dependencies = 0;
409         }
410         elsif ($arglist[0] eq '--generate-deps')
411         {
412             $generate_deps = 1;
413         }
414         elsif ($arglist[0] eq '--no-force')
415         {
416             $force_generation = 0;
417         }
418         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
419         {
420             # Set output directory.
421             $output_directory = $1;
422         }
423         elsif ($arglist[0] eq '--output-dir' || $arglist[0] eq '-o')
424         {
425             &require_argument (@arglist);
426             shift (@arglist);
427             $output_directory = $arglist[0];
428         }
429         elsif ($arglist[0] eq '--add-missing' || $arglist[0] eq '-a')
430         {
431             $add_missing = 1;
432         }
433         elsif ($arglist[0] eq '--verbose' || $arglist[0] eq '-v')
434         {
435             $verbose = 1;
436         }
437         elsif ($arglist[0] eq '--')
438         {
439             # Stop option processing.
440             shift (@arglist);
441             push (@input_files, @arglist);
442             last;
443         }
444         elsif ($arglist[0] =~ /^-/)
445         {
446             die "automake: unrecognized option -- \`$arglist[0]'\nTry \`automake --help' for more information.\n";
447         }
448         else
449         {
450             # Handle $local:$input syntax.  Note that we only examine
451             # the first ":" file to see if it is automake input; the
452             # rest are just taken verbatim.  We still keep all the
453             # files around for dependency checking, however.
454             local ($local, $input, @rest) = split (/:/, $arglist[0]);
455             if (! $input)
456             {
457                 $input = $local;
458             }
459             else
460             {
461                 # Strip .in; later on .am is tacked on.  That is how
462                 # the automake input file is found.  Maybe not the
463                 # best way, but it is easy to explain.  FIXME: should
464                 # be error if .in is missing.
465                 $input =~ s/\.in$//;
466             }
467             push (@input_files, $input);
468             $output_files{$input} = join (':', ($local, @rest));
469         }
470
471         shift (@arglist);
472     }
473
474     # Take global strictness from whatever we currently have set.
475     $default_strictness = $strictness;
476     $default_strictness_name = $strictness_name;
477 }
478
479 # Ensure argument exists, or die.
480 sub require_argument
481 {
482     local ($arg, @arglist) = @_;
483     die "automake: no argument given for option \`$arg'\n"
484         if ! @arglist;
485 }
486
487 ################################################################
488
489 # Generate a Makefile.in given the name of the corresponding Makefile and
490 # the name of the file output by config.status.
491 sub generate_makefile
492 {
493     local ($output, $makefile) = @_;
494
495     ($am_file_name = $makefile) =~ s/^.*\///;
496     $in_file_name = $am_file_name . '.in';
497     $am_file_name .= '.am';
498
499     # $OUTPUT is encoded.  If it contains a ":" then the first element
500     # is the real output file, and all remaining elements are input
501     # files.  We don't scan or otherwise deal with these input file,
502     # other than to mark them as dependencies.  See scan_configure for
503     # details.
504     local (@secondary_inputs);
505     ($output, @secondary_inputs) = split (/:/, $output);
506
507     &initialize_per_input;
508     $relative_dir = &dirname ($output);
509     $am_relative_dir = &dirname ($makefile);
510
511     # At the toplevel directory, we might need config.guess, config.sub
512     # or libtool scripts (ltconfig and ltmain.sh).
513     if ($relative_dir eq '.')
514     {
515         # libtool requires some files.
516         &require_conf_file_with_conf_line ($libtool_line, $FOREIGN,
517                                            @libtoolize_files)
518             if $seen_libtool;
519
520         # AC_CANONICAL_HOST and AC_CANONICAL_SYSTEM need config.guess and
521         # config.sub.
522         &require_config_file ($FOREIGN, 'config.guess', 'config.sub')
523             if $seen_canonical;
524     }
525
526     # We still need Makefile.in here, because sometimes the `dist'
527     # target doesn't re-run automake.
528     if ($am_relative_dir eq $relative_dir)
529     {
530         # Only distribute the files if they are in the same subdir as
531         # the generated makefile.
532         &push_dist_common ($in_file_name, $am_file_name);
533     }
534     push (@sources, '$(SOURCES)')
535         if &variable_defined ('SOURCES');
536     push (@objects, '$(OBJECTS)')
537         if &variable_defined ('OBJECTS');
538
539     &read_am_file ($makefile . '.am');
540     if (&handle_options)
541     {
542         # Fatal error.  Just return, so we can continue with next file.
543         return;
544     }
545
546     # Check first, because we might modify some state.
547     &check_cygnus;
548     &check_gnu_standards;
549     &check_gnits_standards;
550
551     &handle_configure ($output, $makefile, @secondary_inputs);
552     &handle_gettext;
553     &handle_libraries;
554     &handle_ltlibraries;
555     &handle_programs;
556     &handle_scripts;
557
558     &handle_built_sources;
559
560     # This must be run after all the sources are scanned.
561     &handle_yacc_lex_cxx;
562
563     # Re-init SOURCES and OBJECTS.  FIXME: other code shouldn't depend
564     # on this (but currently does).
565     $contents{'SOURCES'} = join (' ', @sources);
566     $contents{'OBJECTS'} = join (' ', @objects);
567
568     &handle_texinfo;
569     &handle_emacs_lisp;
570     &handle_java;
571     &handle_man_pages;
572     &handle_data;
573     &handle_headers;
574     &handle_subdirs;
575     &handle_tags;
576     &handle_minor_options;
577     &handle_dist ($makefile);
578     &handle_dependencies;
579     &handle_tests;
580     &handle_footer;
581     &handle_merge_targets ($output);
582     &handle_installdirs;
583     &handle_clean;
584     &handle_phony;
585
586     &check_typos;
587
588     if (! -d ($output_directory . '/' . $am_relative_dir))
589     {
590         mkdir ($output_directory . '/' . $am_relative_dir, 0755);
591     }
592
593     local ($out_file) = $output_directory . '/' . $makefile . ".in";
594     if (! $force_generation && -e $out_file)
595     {
596         local ($am_time) = (stat ($makefile . '.am'))[9];
597         local ($in_time) = (stat ($out_file))[9];
598         # FIXME: should cache these times.
599         local ($conf_time) = (stat ('configure.in'))[9];
600         # FIXME: how to do unsigned comparison?
601         if ($am_time < $in_time || $am_time < $conf_time)
602         {
603             # No need to update.
604             return;
605         }
606         if (-f 'aclocal.m4')
607         {
608             local ($acl_time) = (stat _)[9];
609             return if ($am_time < $acl_time);
610         }
611     }
612
613     if (! open (GM_FILE, "> " . $out_file))
614     {
615         warn "automake: ${am_file}.in: cannot write: $!\n";
616         $exit_status = 1;
617         return;
618     }
619     print "automake: creating ", $makefile, ".in\n" if $verbose;
620
621     print GM_FILE $output_vars;
622     # We make sure that `all:' is the first target.
623     print GM_FILE $output_all;
624     print GM_FILE $output_header;
625     print GM_FILE $output_rules;
626     print GM_FILE $output_trailer;
627
628     close (GM_FILE);
629 }
630
631 ################################################################
632
633 # Handle AUTOMAKE_OPTIONS variable.  Return 1 on error, 0 otherwise.
634 sub handle_options
635 {
636     if (&variable_defined ('AUTOMAKE_OPTIONS'))
637     {
638         foreach (&variable_value_as_list ('AUTOMAKE_OPTIONS', ''))
639         {
640             $options{$_} = 1;
641             if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'foreign')
642             {
643                 &set_strictness ($_);
644             }
645             elsif ($_ eq 'cygnus')
646             {
647                 $cygnus_mode = 1;
648             }
649             elsif (/ansi2knr/)
650             {
651                 # An option like "../lib/ansi2knr" is allowed.  With
652                 # no path prefix, we assume the required programs are
653                 # in this directory.  We save the actual option for
654                 # later.
655                 $options{'ansi2knr'} = $_;
656             }
657             elsif ($_ eq 'no-installman' || $_ eq 'no-installinfo'
658                    || $_ eq 'dist-shar' || $_ eq 'dist-zip'
659                    || $_ eq 'dist-tarZ' || $_ eq 'dejagnu'
660                    || $_ eq 'no-texinfo.tex'
661                    || $_ eq 'readme-alpha' || $_ eq 'check-news')
662             {
663                 # Explicitly recognize these.
664             }
665             elsif ($_ eq 'no-dependencies')
666             {
667                 $use_dependencies = 0;
668             }
669             elsif (/([0-9]+)\.([0-9]+)([a-z])?/)
670             {
671                 # Got a version number.
672
673                 local ($rmajor, $rminor, $ralpha) = ($1, $2, $3);
674
675                 if ($VERSION !~ /([0-9]+)\.([0-9]+)([a-z])?/)
676                 {
677                     print STDERR
678                         "automake: programming error: version is incorrect\n";
679                     exit 1;
680                 }
681                 local ($tmajor, $tminor, $talpha) = ($1, $2, $3);
682
683                 # 2.0 is better than 1.0.
684                 # 1.2 is better than 1.1.
685                 # 1.2a is better than 1.2.
686                 if ($rmajor > $tmajor
687                     || ($rmajor == $tmajor && $rminor > $tminor)
688                     || ($rminor == $tminor && $rminor == $tminor
689                         && $ralpha gt $talpha))
690                 {
691                     &am_line_error ('AUTOMAKE_OPTIONS',
692                                     "require version $_, only have $VERSION");
693                     return 1;
694                 }
695             }
696             else
697             {
698                 &am_line_error ('AUTOMAKE_OPTIONS',
699                                 "option \`" . $_ . "\' not recognized");
700             }
701         }
702     }
703
704     if ($strictness == $GNITS)
705     {
706         $options{'readme-alpha'} = 1;
707         $options{'check-news'} = 1;
708     }
709
710     return 0;
711 }
712
713 # Return object extension.  Just once, put some code into the output.
714 # Argument is the name of the output file
715 sub get_object_extension
716 {
717     local ($out) = @_;
718
719     # Always set this.
720     $dir_holds_sources = 1;
721
722     # Maybe require libtool library object files.
723     local ($extension) = '.o';
724     $extension = '.lo' if ($out =~ /\.la$/);
725
726     if (! $included_generic_compile)
727     {
728         # Boilerplate.
729         local ($xform) = '';
730         if (&variable_defined ('CONFIG_HEADER'))
731         {
732             local ($one_hdr);
733             foreach $one_hdr (split (' ', &variable_value ('CONFIG_HEADER')))
734             {
735                 local ($var);
736                 ($var = &dirname ($one_hdr)) =~ s/(\W)/\\$1/g;
737                 $xform .= ' ' if $xform;
738                 $xform .= '-I' . $var;
739             }
740         }
741         $xform = 's/\@CONFIG_INCLUDE_SPEC\@/' . $xform . '/go;';
742         $output_vars .= &file_contents_with_transform ($xform,
743                                                        'comp-vars');
744         $output_rules .=
745             &file_contents_with_transform
746                 ($use_dependencies ? 's/^NOTDEPEND.*$//;' : 's/^NOTDEPEND//;',
747                  'compile');
748
749         &push_phony_cleaners ('compile');
750
751         # If using X, include some extra variable definitions.  NOTE
752         # we don't want to force these into CFLAGS or anything,
753         # because not all programs will necessarily use X.
754         if ($seen_path_xtra)
755         {
756             local ($var);
757             foreach $var ('X_CFLAGS', 'X_LIBS', 'X_EXTRA_LIBS', 'X_PRE_LIBS')
758             {
759                 &define_configure_variable ($var);
760             }
761         }
762
763         push (@suffixes, '.c', '.o', '.S', '.s');
764         push (@clean, 'compile');
765
766         $included_generic_compile = 1;
767     }
768
769     if ($seen_libtool && ! $included_libtool_compile)
770     {
771         # Output the libtool compilation rules.
772         $output_rules .=
773             &file_contents_with_transform
774                 ($use_dependencies ? 's/^NOTDEPEND.*$//;' : 's/^NOTDEPEND//;',
775                  'libtool');
776
777         &push_phony_cleaners ('libtool');
778
779         push (@suffixes, '.lo');
780         push (@clean, 'libtool');
781
782         $included_libtool_compile = 1;
783     }
784
785     # Check for automatic de-ANSI-fication.
786     if (defined $options{'ansi2knr'})
787     {
788         $extension = '$U' . $extension;
789         if (! $included_knr_compile)
790         {
791             if (! $am_c_prototypes)
792             {
793                 &am_line_error ('AUTOMAKE_OPTIONS',
794                                 "option \`ansi2knr' in use but \`AM_C_PROTOTYPES' not in configure.in");
795                 &keyed_aclocal_warning ('AM_C_PROTOTYPES');
796                 # Only give this error once.
797                 $am_c_prototypes = 1;
798             }
799
800             # Only require ansi2knr files if they should appear in
801             # this directory.
802             if ($options{'ansi2knr'} eq 'ansi2knr')
803             {
804                 &require_file_with_line ('AUTOMAKE_OPTIONS', $FOREIGN,
805                                          'ansi2knr.c', 'ansi2knr.1');
806                 $output_rules .= &file_contents ('kr-extra');
807                 push (@clean, 'krextra');
808                 &push_phony_cleaners ('krextra');
809             }
810
811             # Generate rules to build ansi2knr.  If it is in some
812             # other directory, then generate dependencies but have the
813             # rule just run elsewhere.
814             $output_rules .= ($options{'ansi2knr'} . ': '
815                               . $options{'ansi2knr'} . ".o\n");
816             if ($options{'ansi2knr'} eq 'ansi2knr')
817             {
818                 $output_rules .= ("\t\$(LINK) ansi2knr.o \$(LIBS)\n"
819                                   . "ansi2knr.o: \$(CONFIG_HEADER)\n\n");
820             }
821             else
822             {
823                 $output_rules .= ("\tcd " . &dirname ($options{'ansi2knr'})
824                                   . " && \$(MAKE) ansi2knr\n\n");
825                 # This is required for non-GNU makes.
826                 $output_rules .= ($options{'ansi2knr'} . ".o:\n");
827                 $output_rules .= ("\tcd " . &dirname ($options{'ansi2knr'})
828                                   . " && \$(MAKE) ansi2knr.o\n\n");
829             }
830
831             # Make sure ansi2knr can be found: if no path specified,
832             # specify "./".
833             if ($options{'ansi2knr'} eq 'ansi2knr')
834             {
835                 # Substitution from AM_C_PROTOTYPES.  This makes it be
836                 # built only when necessary.
837                 &define_configure_variable ('ANSI2KNR');
838                 # ansi2knr needs to be built before subdirs, so unshift it.
839                 unshift (@all, '$(ANSI2KNR)');
840             }
841             else
842             {
843                 # Found in another directory.
844                 &define_variable ("ANSI2KNR", $options{'ansi2knr'});
845             }
846
847             $output_rules .= &file_contents ('clean-kr');
848
849             push (@clean, 'kr');
850             &push_phony_cleaners ('kr');
851
852             $included_knr_compile = 1;
853         }
854     }
855
856     return $extension;
857 }
858
859 # Handle yacc and lex.
860 sub handle_yacc_lex_cxx
861 {
862     #
863     # First do yacc and lex.
864     #
865
866     local ($yacc_count) = scalar (keys %yacc_sources);
867     local ($lex_count) = scalar (keys %lex_sources);
868
869     if ($yacc_count)
870     {
871         local ($file, $base, $hname, $cname);
872         local (%seen_suffix) = ();
873         foreach $file (keys %yacc_sources)
874         {
875             $file =~ /(\..*)$/;
876             &output_yacc_build_rule ($1, $yacc_count > 1)
877                 if (! defined $seen_suffix{$1});
878             $seen_suffix{$1} = 1;
879
880             # Now generate rule to make the header file.  This should
881             # only be generated if `yacc -d' specified.  But right now
882             # there is no way to determine that.  FIXME: the
883             # AM_FOOFLAGS idea would suffice here.
884             $file =~ /^(.*)\.(y|yy|y\+\+|yxx)$/;
885             $base = $1;
886             ($hname = $2) =~ tr/y/h/;
887             ($cname = $2) =~ tr/y/c/;
888             $output_rules .= "${base}.${hname}: ${base}.${cname}\n";
889
890             # If the files are built in the build directory, then we
891             # want to remove them with `make clean'.  If they are in
892             # srcdir they shouldn't be touched.  However, we can't
893             # determine this statically, and the GNU rules say that
894             # yacc/lex output files should be removed by
895             # maintainer-clean.  So that's what we do.
896             push (@maintainer_clean_files, $base . $hname, $base . $cname);
897         }
898         $output_rules .= "\n";
899
900         if (! defined $configure_vars{'YACC'})
901         {
902             &am_error ("yacc source seen but \`YACC' not defined in \`configure.in'");
903         }
904         if (&variable_defined ('YACCFLAGS'))
905         {
906             &am_line_error ('YACCFLAGS',
907                             "\`YACCFLAGS' obsolete; use \`YFLAGS' instead");
908         }
909     }
910     if ($lex_count)
911     {
912         local (%seen_suffix) = ();
913         local ($file, $cname);
914         foreach $file (keys %lex_sources)
915         {
916             $file =~ /(\..*)$/;
917             &output_lex_build_rule ($1, $lex_count > 1)
918                 if (! defined $seen_suffix{$1});
919             $seen_suffix{$1} = 1;
920
921             # If the files are built in the build directory, then we
922             # want to remove them with `make clean'.  If they are in
923             # srcdir they shouldn't be touched.  However, we can't
924             # determine this statically, and the GNU rules say that
925             # yacc/lex output files should be removed by
926             # maintainer-clean.  So that's what we do.
927             $file =~ /^(.*)\.(l|ll|l\+\+|lxx)$/;
928             ($cname = $2) =~ tr/y/c/;
929             push (@maintainer_clean_files, $1 . $cname);
930         }
931
932         if (! defined $configure_vars{'LEX'})
933         {
934             &am_error ("lex source seen but \`LEX' not defined in \`configure.in'");
935         }
936         if (! $seen_decl_yytext)
937         {
938             &am_error ("lex source seen but \`AC_DECL_YYTEXT' not in \`configure.in'");
939         }
940     }
941
942     if ($yacc_count > 1 || $lex_count > 1)
943     {
944         # If there is more than one distinct yacc (resp lex) source
945         # file in a given directory, then the `ylwrap' program is
946         # required to allow parallel builds to work correctly.  FIXME:
947         # for now, no line number.
948         &require_config_file ($FOREIGN, 'ylwrap');
949         if ($config_aux_dir ne '.' && $config_aux_dir ne '')
950         {
951                 &define_variable ('YLWRAP', $config_aux_dir . "/ylwrap");
952         }
953         else
954         {
955                 &define_variable ('YLWRAP', '$(srcdir)/ylwrap');
956         }
957     }
958
959     #
960     # Handle libtool.
961     #
962     local ($ltcompile, $ltlink) = ('', '');
963     if ($seen_libtool)
964     {
965         &define_configure_variable ("LIBTOOL");
966         $ltcompile = '$(LIBTOOL) --mode=compile ';
967         $ltlink = '$(LIBTOOL) --mode=link ';
968     }
969
970     #
971     # Now handle C++.
972     #
973     local (@cxx_list) = keys %cxx_extensions;
974     local ($cxx_count) = scalar @cxx_list;
975     if ($cxx_count)
976     {
977         push (@suffixes, @cxx_list);
978
979         &define_configure_variable ("CXXFLAGS");
980         &define_variable ('CXXCOMPILE', '$(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)');
981         &define_variable ('LTCXXCOMPILE',
982                           $ltcompile . '$(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)')
983             if ($seen_libtool);
984
985         &define_variable ('CXXLINK', $ltlink . '$(CXX) $(AM_CXXFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@');
986
987         local ($ext);
988         foreach $ext (@cxx_list)
989         {
990             $output_rules .= ("$ext.o:\n"
991                               . "\t\$(CXXCOMPILE) -c \$<\n");
992             $output_rules .= ("$ext.lo:\n"
993                               . "\t\$(LTCXXCOMPILE) -c \$<\n")
994                 if ($seen_libtool);
995         }
996
997         if (! defined $configure_vars{'CXX'})
998         {
999             &am_error ("C++ source seen but \`CXX' not defined in \`configure.in'");
1000         }
1001     }
1002
1003     #
1004     # Handle some ansi2knr cleanup.
1005     #
1006
1007     # Push all libobjs files onto de_ansi_files.  We actually only
1008     # push files which exist in the current directory, and which are
1009     # genuine source files.  This is moderately lame.  FIXME.
1010     local ($file);
1011     foreach $file (keys %libsources)
1012     {
1013         if ($file =~ /^(.*)\.[cly]$/ && -f "$relative_dir/$file")
1014         {
1015             $de_ansi_files{$1} = 1;
1016         }
1017     }
1018
1019     if (defined $options{'ansi2knr'} && keys %de_ansi_files)
1020     {
1021         # Make all _.c files depend on their corresponding .c files.
1022         local ($base, @objects);
1023         foreach $base (sort (keys %de_ansi_files))
1024         {
1025             # Each _.c file must depend on ansi2knr; otherwise it
1026             # might be used in a parallel build before it is built.
1027             # We need to support files in the srcdir and in the build
1028             # dir (because these files might be auto-generated.  But
1029             # we can't use $< -- some makes only define $< during a
1030             # suffix rule.
1031             $output_rules .= ($base . "_.c: $base.c \$(ANSI2KNR)\n\t"
1032                               . '$(CPP) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) '
1033                               . '`if test -f $(srcdir)/' . $base . '.c'
1034                               . '; then echo $(srcdir)/' . $base . '.c'
1035                               . '; else echo ' . $base . '.c; fi` '
1036                               . '| $(ANSI2KNR) ' . $base . "_.c\n");
1037             push (@objects, $base . '_.o');
1038             push (@objects, $base . '_.lo') if $seen_libtool;
1039         }
1040
1041         # Make all _.o (and _.lo) files depend on ansi2knr.
1042         # Use a sneaky little hack to make it print nicely.
1043         &pretty_print_rule ('', '', @objects, ':', '$(ANSI2KNR)');
1044     }
1045
1046     #
1047     # Last, handle some C cleanup.
1048     #
1049     if ($seen_any_source)
1050     {
1051         &define_configure_variable ('CFLAGS');
1052         &define_variable ('COMPILE',
1053                           '$(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)');
1054         &define_variable ('LTCOMPILE',
1055                           $ltcompile .
1056                           '$(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)')
1057             if ($seen_libtool);
1058         &define_variable ('LINK', $ltlink . '$(CC) $(AM_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@');
1059     }
1060
1061     if ($seen_c_source && ! defined $configure_vars{'CC'})
1062     {
1063         &am_line_error ($seen_c_source,
1064                         "C source seen but \`CC' not defined in \`configure.in'");
1065     }
1066 }
1067
1068
1069 # Output a rule to build from a YACC source.  The output from YACC is
1070 # compiled with C or C++, depending on the extension of the YACC file.
1071 sub output_yacc_build_rule
1072 {
1073     local ($yacc_suffix, $use_ylwrap, $c_suffix) = @_;
1074
1075     local ($suffix);
1076     ($suffix = $yacc_suffix) =~ tr/y/c/;
1077     push (@suffixes, $yacc_suffix, $suffix);
1078
1079     # Generate rule for c/c++.
1080     $output_rules .= "$yacc_suffix$suffix:\n\t";
1081
1082     if ($use_ylwrap)
1083     {
1084         $output_rules .= ('$(SHELL) $(YLWRAP)'
1085                           . ' "$(YACC)" $< y.tab.c $*' . $suffix
1086                           . ' y.tab.h $*.h -- $(AM_YFLAGS) $(YFLAGS)');
1087     }
1088     else
1089     {
1090         $output_rules .= ('$(YACC) $(AM_YFLAGS) $(YFLAGS) $< && mv y.tab.c $*'
1091                           . $suffix . "\n"
1092                           . "\tif test -f y.tab.h; then \\\n"
1093                           . "\tif cmp -s y.tab.h \$*.h; then rm -f y.tab.h; else mv y.tab.h \$*.h; fi; \\\n"
1094                           . "\telse :; fi");
1095     }
1096     $output_rules .= "\n";
1097 }
1098
1099 sub output_lex_build_rule
1100 {
1101     local ($lex_suffix, $use_ylwrap) = @_;
1102     local ($c_suffix);
1103
1104     ($c_suffix = $lex_suffix) =~ tr/l/c/;
1105     push (@suffixes, $lex_suffix);
1106     &define_configure_variable ('LEX_OUTPUT_ROOT');
1107     &define_configure_variable ('LEXLIB');
1108     $output_rules .= "$lex_suffix$c_suffix:\n\t";
1109
1110     if ($use_ylwrap)
1111     {
1112         # Is the $@ correct here?  If so, why not use it in the ylwrap
1113         # build rule for yacc above?
1114         $output_rules .= '$(SHELL) $(YLWRAP)'
1115             . ' "$(LEX)" $< $(LEX_OUTPUT_ROOT).c $@ -- $(AM_LFLAGS) $(LFLAGS)';
1116     }
1117     else
1118     {
1119         $output_rules .= '$(LEX) $(AM_LFLAGS) $(LFLAGS) $< && mv $(LEX_OUTPUT_ROOT).c $@';
1120     }
1121     $output_rules .= "\n";
1122 }
1123
1124
1125 # Check to make sure a source defined in LIBOBJS is not explicitly
1126 # mentioned.  This is a separate function (as opposed to being inlined
1127 # in handle_source_transform) because it isn't always appropriate to
1128 # do this check.
1129 sub check_libobjs_sources
1130 {
1131     local ($one_file, $unxformed) = @_;
1132
1133     local ($prefix, $file, @files);
1134     foreach $prefix ('', 'EXTRA_')
1135     {
1136         if (&variable_defined ($prefix . $one_file . '_SOURCES'))
1137         {
1138             @files = &variable_value_as_list (($prefix
1139                                                . $one_file . '_SOURCES'),
1140                                               'all');
1141         }
1142         elsif ($prefix eq '')
1143         {
1144             @files = ($unxformed . '.c');
1145         }
1146         else
1147         {
1148             next;
1149         }
1150
1151         foreach $file (@files)
1152         {
1153             if (defined $libsources{$file})
1154             {
1155                 &am_line_error ($prefix . $one_file . '_SOURCES',
1156                                 "automatically discovered file \`$file' should not be explicitly mentioned");
1157             }
1158         }
1159     }
1160 }
1161
1162 # Does much of the actual work for handle_source_transform.
1163 # Arguments are:
1164 #   pattern matching object extension (e.g., .o)
1165 #   list of source files to transform
1166 # Result is a list
1167 #   first element is name of linker to use (empty string for default linker)
1168 #   remaining elements are names of `.o's
1169 sub handle_single_transform_list
1170 {
1171     local ($objpat, @files) = @_;
1172     local ($linker) = '';
1173     local (@result) = ();
1174     local ($nonansi_obj) = $obj;
1175     $nonansi_obj =~ s/_//g;
1176     if (@files > 0)
1177     {
1178         # Turn sources into objects.
1179         foreach (@files)
1180         {
1181             # Skip header files, including C++-ish ones.  The list
1182             # of C++ header extensions comes from Emacs 19.32
1183             # etags.
1184             next if /\.[hH]$/;
1185             next if /\.hxx$/;
1186             next if /\.h\+\+$/;
1187             next if /\.hh$/;
1188             next if /\.hpp$/;
1189             # Skip things that look like configure substitutions.
1190             next if /^\@.*\@$/;
1191
1192             # Include appropriate file for lex or yacc source in
1193             # distribution.  If the extension is the regular '.y' or
1194             # '.l', we assume C compilation, and the generated file
1195             # has exension .c.  Otherwise, we compile with C++, and
1196             # make the following association: (yy -> cc, y++ -> c++,
1197             # yxx -> cxx), similarly for .ll, etc.
1198             if (/^(.*)\.(y|yy|y\+\+|yxx)$/)
1199             {
1200                 # Yacc source.
1201                 local ($ext) = $2;
1202                 $ext =~ tr/y/c/;
1203                 &push_dist_common ("$1.$ext");
1204                 $yacc_sources{$_} = 1;
1205             }
1206             elsif (/^(.*)\.(l|ll|l\+\+|lxx)$/)
1207             {
1208                 # Lex source.
1209                 local ($ext) = $2;
1210                 $ext =~ tr/l/c/;
1211                 &push_dist_common ("$1.$ext");
1212                 $lex_sources{$_} = 1;
1213             }
1214
1215             # Strip any directory prefix.
1216             $_ = &basename ($_);
1217
1218             local ($is_cxx) = 0;
1219             # Transform source files into .o files.  List of C++
1220             # extensions comes from Emacs 19.34 etags.
1221             if (s/\.(c\+\+|cc|cpp|cxx|C)$/$nonansi_obj/)
1222             {
1223                 $cxx_extensions{'.' . $1} = 1;
1224                 $linker = 'CXXLINK';
1225                 $is_cxx = 1;
1226             }
1227             elsif (s/\.(yy|y\+\+|yxx|ll|l\+\+|lxx)$/$nonansi_obj/)
1228             {
1229                 # Compiling lex or yacc with C++
1230                 local ($ext) = $1;
1231                 $ext =~ tr/ly/cc/;
1232                 $cxx_extensions{".$ext"} = 1;
1233                 $linker = 'CXXLINK';
1234             }
1235             elsif (s/\.([Ff]|f90|for)$/$nonansi_obj/)
1236             {
1237                 # FORTRAN support.  FIXME: not finished.
1238             }
1239             elsif (s/\.[sS]$/$nonansi_obj/)
1240             {
1241                 # .s is assembly.  Just rewrite it.  FIXME: not finished.
1242             }
1243             elsif (defined ($options{'ansi2knr'}) && s/_\.[cly]$/_.c/)
1244             {
1245                 # FIXME include line number in error.
1246                 &am_error ("C source file \`$_' would be deleted by ansi2knr rules");
1247             }
1248             elsif (s/\.[cly]$//)
1249             {
1250                 # .c is C.  .l is lex.  .y is yacc.
1251
1252                 # Note: first we rewrite (eg) foo.c to foo and push
1253                 # the file onto the list of deansified files.  Then we add
1254                 # $obj, which can either be `_.o', or simply `.o' if
1255                 # deansification is not required.
1256                 $de_ansi_files{$_} = 1;
1257                 $_ .= $obj;
1258                 $seen_c_source = 1;
1259             }
1260             elsif (/\.$source_suffix_pattern$/) 
1261             {
1262                 # We just rewrite it.  Maybe we should do more.
1263                 s//.$suffix_rules{$1}/;
1264                 $objpat = "\\." . $suffix_rules{$1};
1265             }
1266             else
1267             {
1268                 # No error message here.  Used to have one, but it was
1269                 # very unpopular.
1270                 next;
1271             }
1272
1273             $seen_any_source = 1 unless $is_cxx;
1274             push (@result, $_);
1275
1276             # Transform .o or $o file into .P file (for automatic
1277             # dependency code).
1278             s/$objpat$/.P/g;
1279             $dep_files{'.deps/' . $_} = 1;
1280         }
1281     }
1282
1283     return ($linker, @result);
1284 }
1285
1286 # Handle SOURCE->OBJECT transform for one program or library.
1287 # Arguments are:
1288 #   canonical (transformed) name of object to build
1289 #   actual name of object to build
1290 #   object extension (ie either `.o' or `$o'.
1291 # Return result is name of linker variable that must be used.
1292 # Empty return means just use `LINK'.
1293 sub handle_source_transform
1294 {
1295     # one_file is canonical name.  unxformed is given name.  obj is
1296     # object extension.
1297     local ($one_file, $unxformed, $obj) = @_;
1298     local ($objpat) = $obj;
1299     $objpat =~ s/(\W)/\\$1/g;
1300     # Handle explicit `.o' as well as whatever we're passed.
1301     $objpat = '(' . $objpat . "|\\.o)";
1302
1303     local ($linker) = '';
1304
1305     if (&variable_defined ($one_file . "_OBJECTS"))
1306     {
1307         &am_line_error ($one_file . '_OBJECTS',
1308                         $one_file . '_OBJECTS', 'should not be defined');
1309         # No point in continuing.
1310         return;
1311     }
1312
1313     local (@files, @result, $prefix, $temp);
1314     foreach $prefix ('', 'EXTRA_')
1315     {
1316         @files = ();
1317         local ($var) = $prefix . $one_file . "_SOURCES";
1318         if (&variable_defined ($var))
1319         {
1320             push (@sources, '$(' . $prefix . $one_file . "_SOURCES)");
1321             push (@objects, '$(' . $prefix . $one_file . "_OBJECTS)")
1322                 unless $prefix eq 'EXTRA_';
1323             local (@conds) = &variable_conditions ($var);
1324             if (! @conds)
1325             {
1326                 @files = &variable_value_as_list ($var, '');
1327             }
1328             else
1329             {
1330                 local ($cond, $cond_var_name);
1331                 local ($count) = 0;
1332                 local (@namelist) = ();
1333                 foreach $cond (@conds)
1334                 {
1335                     @files = &variable_value_as_list ($var, $cond);
1336                     ($temp, @result) = &handle_single_transform_list ($objpat,
1337                                                                       @files);
1338                     $linker = $temp if $linker eq '';
1339
1340                     # We have to have a new name for each such
1341                     # variable.  Then we define the _OBJECTS variable
1342                     # as the union of all such variables.  Hopefully
1343                     # this is the Right Thing.
1344                     $cond_var_name = 'cond' . $count . $one_file . '_OBJECTS';
1345                     &define_pretty_variable ($cond_var_name, $cond, @result)
1346                         unless $prefix eq 'EXTRA_';
1347                     push (@namelist, '$(' . $cond_var_name . ')');
1348                     ++$count;
1349                 }
1350
1351                 &define_pretty_variable ($one_file . '_OBJECTS', '',
1352                                          @namelist);
1353
1354                 next;
1355             }
1356         }
1357         elsif ($prefix eq '')
1358         {
1359             &define_variable ($one_file . "_SOURCES", $unxformed . ".c");
1360             push (@sources, $unxformed . '.c');
1361             push (@objects, $unxformed . $obj);
1362             push (@files, $unxformed . '.c');
1363         }
1364
1365         ($temp, @result) = &handle_single_transform_list ($objpat, @files);
1366         $linker = $temp if $linker eq '';
1367         &define_pretty_variable ($one_file . "_OBJECTS", '', @result)
1368             unless $prefix eq 'EXTRA_';
1369     }
1370
1371     return $linker;
1372 }
1373
1374 # Handle the BUILT_SOURCES variable.
1375 sub handle_built_sources
1376 {
1377     return unless &variable_defined ('BUILT_SOURCES');
1378
1379     local (@sources) = &variable_value_as_list ('BUILT_SOURCES', 'all');
1380     local ($s);
1381     foreach $s (@sources)
1382     {
1383         if (/^\@.*\@$/)
1384         {
1385             # FIXME: is this really the right thing to do?
1386             &am_line_error ('BUILT_SOURCES',
1387                             "\`BUILT_SOURCES' should not contain a configure substitution");
1388             last;
1389         }
1390     }
1391
1392     # We don't care about the return value of this function.  We just
1393     # want to make sure to update %dep_files with the contents of
1394     # BUILT_SOURCES.
1395     &handle_single_transform_list ("\\.o", @sources);
1396 }
1397
1398 # Special-case @ALLOCA@ and @LIBOBJS@ in _LDADD or _LIBADD variables.
1399 # Also, generate _DEPENDENCIES variable if appropriate.
1400 # Arguments are:
1401 #   transformed name of object being built, or empty string if no object
1402 #   name of _LDADD/_LIBADD-type variable to examine
1403 #   boolean (lex_seen) which is true if a lex source file was seen in this
1404 #     object.  valid only for LDADDs, not LIBADDs.
1405 # Returns 1 if LIBOBJS seen, 0 otherwise.
1406 sub handle_lib_objects
1407 {
1408     local ($xname, $var, $lex_seen) = @_;
1409     local ($ret);
1410
1411     die "automake: programming error 1 in handle_lib_objects\n"
1412         if ! &variable_defined ($var);
1413
1414     die "automake: programming error 2 in handle_lib_objects\n"
1415         if $lex_seen && $var =~ /LIBADD/;
1416
1417     local (@conds) = &variable_conditions ($var);
1418     if (! @conds)
1419     {
1420         $ret = &handle_lib_objects_cond ($xname, $var, $lex_seen, '');
1421     }
1422     else
1423     {
1424         local ($cond);
1425         $ret = 0;
1426         foreach $cond (@conds)
1427         {
1428             if (&handle_lib_objects_cond ($xname, $var, $lex_seen, $cond))
1429             {
1430                 $ret = 1;
1431             }
1432         }
1433     }
1434
1435     return $ret;
1436 }
1437
1438 # Subroutine of handle_lib_objects: handle a particular condition.
1439 sub handle_lib_objects_cond
1440 {
1441     local ($xname, $var, $lex_seen, $cond) = @_;
1442
1443     # We recognize certain things that are commonly put in LIBADD or
1444     # LDADD.
1445     local ($lsearch);
1446     local (@dep_list) = ();
1447
1448     local ($seen_libobjs) = 0;
1449     local ($flagvar) = 0;
1450
1451     foreach $lsearch (&variable_value_as_list ($var, $cond))
1452     {
1453         # Skip -lfoo and -Ldir; these are explicitly allowed.
1454         next if $lsearch =~ /^-[lL]/;
1455         if (! $flagvar && $lsearch =~ /^-/)
1456         {
1457             if ($var =~ /^(.*)LDADD$/)
1458             {
1459                 &am_line_error ($var, "linker flags such as \`$lsearch' belong in \`${1}LDFLAGS");
1460             }
1461             else
1462             {
1463                 # Only get this error once.
1464                 $flagvar = 1;
1465                 &am_line_error ($var, "you cannot use linker flags such as \`$lsearch' in \`$var'");
1466             }
1467         }
1468
1469         # Assume we have a file of some sort, and push it onto the
1470         # dependency list.  Autoconf substitutions are not pushed;
1471         # rarely is a new dependency substituted into (eg) foo_LDADD
1472         # -- but "bad things (eg -lX11) are routinely substituted.
1473         # Note that LIBOBJS and ALLOCA are exceptions to this rule,
1474         # and handled specially below.
1475         push (@dep_list, $lsearch)
1476             unless $lsearch =~ /^\@.*\@$/;
1477
1478         # Automatically handle @LIBOBJS@ and @ALLOCA@.  Basically this
1479         # means adding entries to dep_files.
1480         if ($lsearch =~ /^\@(LT)?LIBOBJS\@$/)
1481         {
1482             push (@dep_list, $lsearch);
1483             $seen_libobjs = 1;
1484             if (! keys %libsources)
1485             {
1486                 &am_line_error ($var, "\@$1" . "LIBOBJS\@ seen but never set in \`configure.in'");
1487             }
1488
1489             local ($iter, $rewrite);
1490             foreach $iter (keys %libsources)
1491             {
1492                 if ($iter =~ /\.[cly]$/)
1493                 {
1494                     $seen_c_source = $var;
1495                     $seen_any_source = 1;
1496                 }
1497
1498                 if ($iter =~ /\.h$/)
1499                 {
1500                     &require_file_with_line ($var, $FOREIGN, $iter);
1501                 }
1502                 elsif ($iter ne 'alloca.c')
1503                 {
1504                     ($rewrite = $iter) =~ s/\.c$/.P/;
1505                     $dep_files{'.deps/' . $rewrite} = 1;
1506                     &require_file_with_line ($var, $FOREIGN, $iter);
1507                 }
1508             }
1509         }
1510         elsif ($lsearch =~ /^\@(LT)?ALLOCA\@$/)
1511         {
1512             push (@dep_list, $lsearch);
1513             &am_line_error ($var,
1514                             "\@$1" . "ALLOCA\@ seen but \`AC_FUNC_ALLOCA' not in \`configure.in'")
1515                 if ! defined $libsources{'alloca.c'};
1516             $dep_files{'.deps/alloca.P'} = 1;
1517             &require_file_with_line ($var, $FOREIGN, 'alloca.c');
1518             $seen_c_source = $var;
1519             $seen_any_source = 1;
1520         }
1521     }
1522
1523     if ($xname ne '' && ! &variable_defined ($xname . '_DEPENDENCIES', $cond))
1524     {
1525         &define_pretty_variable ($xname . '_DEPENDENCIES', $cond, @dep_list);
1526     }
1527
1528     return $seen_libobjs;
1529 }
1530
1531 # Canonicalize a name, and check to make sure the non-canonical name
1532 # is never used.  Returns canonical name.  Arguments are name and a
1533 # list of suffixes to check for.
1534 sub check_canonical_spelling
1535 {
1536     local ($name, @suffixes) = @_;
1537     local ($xname, $xt);
1538
1539     ($xname = $name) =~ tr/A-Za-z0-9_/_/c;
1540     if ($xname ne $name)
1541     {
1542         local ($xt);
1543         foreach $xt (@suffixes)
1544         {
1545             &am_line_error ($name . $xt,
1546                             "invalid variable \`" . $name . $xt
1547                             . "'; should be \`" . $xname . $xt . "'")
1548                 if &variable_defined ($name . $xt);
1549         }
1550     }
1551
1552     return $xname;
1553 }
1554
1555 # Handle C programs.
1556 sub handle_programs
1557 {
1558     local (@proglist) = &am_install_var ('-clean',
1559                                          'progs', 'PROGRAMS',
1560                                          'bin', 'sbin', 'libexec', 'pkglib',
1561                                          'noinst', 'check');
1562     return if ! @proglist;
1563
1564     # If a program is installed, this is required.  We only want this
1565     # error to appear once.
1566     &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1567         unless $seen_arg_prog;
1568     $seen_arg_prog = 1;
1569
1570     local ($one_file, $xname, $munge);
1571
1572     local ($seen_libobjs) = 0;
1573     foreach $one_file (@proglist)
1574     {
1575         local ($obj) = &get_object_extension ($one_file);
1576
1577         # Canonicalize names and check for misspellings.
1578         $xname = &check_canonical_spelling ($one_file, '_LDADD', '_LDFLAGS',
1579                                             '_SOURCES', '_OBJECTS',
1580                                             '_DEPENDENCIES');
1581
1582         # FIXME: Using a trick to figure out if any lex sources appear
1583         # in our program; should use some cleaner method.
1584         local ($lex_num) = scalar (keys %lex_sources);
1585         local ($linker) = &handle_source_transform ($xname, $one_file, $obj);
1586         local ($lex_file_seen) = (scalar (keys %lex_sources) > $lex_num);
1587
1588         local ($xt) = '';
1589         if (&variable_defined ($xname . "_LDADD"))
1590         {
1591             if (&handle_lib_objects ($xname, $xname . '_LDADD',
1592                                      $lex_file_seen))
1593             {
1594                 $seen_libobjs = 1;
1595             }
1596             $lex_file_seen = 0;
1597             $xt = '_LDADD';
1598         }
1599         else
1600         {
1601             # User didn't define prog_LDADD override.  So do it.
1602             &define_variable ($xname . '_LDADD', '$(LDADD)');
1603
1604             # This does a bit too much work.  But we need it to
1605             # generate _DEPENDENCIES when appropriate.
1606             if (&variable_defined ('LDADD'))
1607             {
1608                 if (&handle_lib_objects ($xname, 'LDADD', $lex_file_seen))
1609                 {
1610                     $seen_libobjs = 1;
1611                 }
1612                 $lex_file_seen = 0;
1613             }
1614             elsif (! &variable_defined ($xname . '_DEPENDENCIES'))
1615             {
1616                 &define_variable ($xname . '_DEPENDENCIES', '');
1617             }
1618             $xt = '_SOURCES'
1619         }
1620
1621         if (&variable_defined ($xname . '_LIBADD'))
1622         {
1623             &am_line_error ($xname . '_LIBADD',
1624                             "use \`" . $xname . "_LDADD', not \`"
1625                             . $xname . "_LIBADD'");
1626         }
1627
1628         if (! &variable_defined ($xname . '_LDFLAGS'))
1629         {
1630             # Define the prog_LDFLAGS variable.
1631             &define_variable ($xname . '_LDFLAGS', '');
1632         }
1633
1634         # Determine program to use for link.
1635         local ($xlink);
1636         if (&variable_defined ($xname . '_LINK'))
1637         {
1638             $xlink = $xname . '_LINK';
1639         }
1640         else
1641         {
1642             $xlink = $linker ? $linker : 'LINK';
1643         }
1644
1645         local ($xexe);
1646         if ($seen_exeext && $one_file !~ /\./)
1647         {
1648             $xexe = 's/\@EXEEXT\@/\$(EXEEXT)/g;';
1649         }
1650         else
1651         {
1652             $xexe = 's/\@EXEEXT\@//g;';
1653         }
1654
1655         $output_rules .=
1656             &file_contents_with_transform
1657                 ('s/\@PROGRAM\@/' . $one_file . '/go;'
1658                  . 's/\@XPROGRAM\@/' . $xname . '/go;'
1659                  . 's/\@XLINK\@/' . $xlink . '/go;'
1660                  . $xexe,
1661                  'program');
1662     }
1663
1664     if (&variable_defined ('LDADD') && &handle_lib_objects ('', 'LDADD', 0))
1665     {
1666         $seen_libobjs = 1;
1667     }
1668
1669     if ($seen_libobjs)
1670     {
1671         foreach $one_file (@proglist)
1672         {
1673             # Canonicalize names.
1674             ($xname = $one_file) =~ tr/A-Za-z0-9_/_/c;
1675
1676             if (&variable_defined ($xname . '_LDADD'))
1677             {
1678                 &check_libobjs_sources ($xname, $xname . '_LDADD');
1679             }
1680             elsif (&variable_defined ('LDADD'))
1681             {
1682                 &check_libobjs_sources ($xname, 'LDADD');
1683             }
1684         }
1685     }
1686 }
1687
1688
1689 # Handle libraries.
1690 sub handle_libraries
1691 {
1692     local (@liblist) = &am_install_var ('-clean',
1693                                         'libs', 'LIBRARIES',
1694                                         'lib', 'pkglib', 'noinst', 'check');
1695     return if ! @liblist;
1696
1697     local (%valid) = &am_primary_prefixes ('LIBRARIES', 'lib', 'pkglib',
1698                                            'noinst', 'check');
1699     if (! defined $configure_vars{'RANLIB'})
1700     {
1701         local ($key);
1702         foreach $key (keys %valid)
1703         {
1704             if (&variable_defined ($key . '_LIBRARIES'))
1705             {
1706                 &am_line_error ($key . '_LIBRARIES', "library used but \`RANLIB' not defined in \`configure.in'");
1707                 # Only get this error once.
1708                 $configure_vars{'RANLIB'} = 1;
1709                 last;
1710             }
1711         }
1712     }
1713
1714     local ($onelib);
1715     local ($munge);
1716     local ($xlib);
1717     local ($seen_libobjs) = 0;
1718     foreach $onelib (@liblist)
1719     {
1720         # Check that the library fits the standard naming convention.
1721         if ($onelib !~ /^lib.*\.a$/)
1722         {
1723             # FIXME should put line number here.  That means mapping
1724             # from library name back to variable name.
1725             &am_error ("\`$onelib' is not a standard library name");
1726         }
1727
1728         local ($obj) = &get_object_extension ($onelib);
1729
1730         # Canonicalize names and check for misspellings.
1731         $xlib = &check_canonical_spelling ($onelib, '_LIBADD', '_SOURCES',
1732                                            '_OBJECTS', '_DEPENDENCIES');
1733
1734         if (&variable_defined ($xlib . '_LIBADD'))
1735         {
1736             if (&handle_lib_objects ($xlib, $xlib . '_LIBADD', 0))
1737             {
1738                 $seen_libobjs = 1;
1739             }
1740         }
1741         else
1742         {
1743             # Generate support for conditional object inclusion in
1744             # libraries.
1745             &define_variable ($xlib . "_LIBADD", '');
1746         }
1747
1748         if (&variable_defined ($xlib . '_LDADD'))
1749         {
1750             &am_line_error ($xlib . '_LDADD',
1751                             "use \`" . $xlib . "_LIBADD', not \`"
1752                             . $xlib . "_LDADD'");
1753         }
1754
1755         # Make sure we at look at this.
1756         &examine_variable ($xlib . '_DEPENDENCIES');
1757
1758         &handle_source_transform ($xlib, $onelib, $obj);
1759
1760         $output_rules .=
1761             &file_contents_with_transform ('s/\@LIBRARY\@/' . $onelib . '/go;'
1762                                            . 's/\@XLIBRARY\@/'
1763                                            . $xlib . '/go;',
1764                                            'library');
1765     }
1766
1767     if ($seen_libobjs)
1768     {
1769         foreach $onelib (@liblist)
1770         {
1771             # Canonicalize names.
1772             ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1773             if (&variable_defined ($xlib . '_LIBADD'))
1774             {
1775                 &check_libobjs_sources ($xlib, $xlib . '_LIBADD');
1776             }
1777         }
1778     }
1779
1780     &define_variable ('AR', 'ar');
1781     &define_configure_variable ('RANLIB');
1782 }
1783
1784 # Handle shared libraries.
1785 sub handle_ltlibraries
1786 {
1787     local (@liblist) = &am_install_var ('-clean',
1788                                         'ltlib', 'LTLIBRARIES',
1789                                         'lib', 'pkglib');
1790     return if ! @liblist;
1791
1792     local (%instdirs);
1793     local (%valid) = &am_primary_prefixes ('LTLIBRARIES', 'lib', 'pkglib');
1794
1795     local ($key);
1796     foreach $key (keys %valid)
1797     {
1798         if (&variable_defined ($key . '_LTLIBRARIES'))
1799         {
1800             if (!$seen_libtool)
1801             {
1802                 &am_line_error ($key . '_LTLIBRARIES', "library used but \`LIBTOOL' not defined in \`configure.in'");
1803                 # Only get this error once.
1804                 $configure_vars{'LIBTOOL'} = 1;
1805                 $seen_libtool = 1;
1806             }
1807
1808             # Get the installation directory of each library.
1809             for (&variable_value_as_list ($key . '_LTLIBRARIES', 'all'))
1810             {
1811                 if ($instdirs{$_})
1812                 {
1813                     &am_error ("\`$_' is already going to be installed in \`$instdirs{$_}'");
1814                 }
1815                 else
1816                 {
1817                     $instdirs{$_} = $key;
1818                 }
1819             }
1820         }
1821     }
1822
1823     local ($onelib);
1824     local ($munge);
1825     local ($xlib);
1826     local ($seen_libobjs) = 0;
1827     foreach $onelib (@liblist)
1828     {
1829         # Check that the library fits the standard naming convention.
1830         if ($onelib !~ /^lib.*\.la$/)
1831         {
1832             # FIXME this should only be a warning for foreign packages
1833             # FIXME should put line number here.  That means mapping
1834             # from library name back to variable name.
1835             &am_error ("\`$onelib' is not a standard libtool library name");
1836         }
1837
1838         local ($obj) = &get_object_extension ($onelib);
1839
1840         # Canonicalize names and check for misspellings.
1841         $xlib = &check_canonical_spelling ($onelib, '_LIBADD', '_LDFLAGS',
1842                                            '_SOURCES', '_OBJECTS',
1843                                            '_DEPENDENCIES');
1844
1845         if (! &variable_defined ($xlib . '_LDFLAGS'))
1846         {
1847             # Define the lib_LDFLAGS variable.
1848             &define_variable ($xlib . '_LDFLAGS', '');
1849         }
1850
1851         if (&variable_defined ($xlib . '_LIBADD'))
1852         {
1853             if (&handle_lib_objects ($xlib, $xlib . '_LIBADD', 0))
1854             {
1855                 $seen_libobjs = 1;
1856             }
1857         }
1858         else
1859         {
1860             # Generate support for conditional object inclusion in
1861             # libraries.
1862             &define_variable ($xlib . "_LIBADD", '');
1863         }
1864
1865         if (&variable_defined ($xlib . '_LDADD'))
1866         {
1867             &am_line_error ($xlib . '_LDADD',
1868                             "use \`" . $xlib . "_LIBADD', not \`"
1869                             . $xlib . "_LDADD'");
1870         }
1871
1872         # Make sure we at look at this.
1873         &examine_variable ($xlib . '_DEPENDENCIES');
1874
1875         local ($linker) = &handle_source_transform ($xlib, $onelib, $obj);
1876
1877         # Determine program to use for link.
1878         local ($xlink);
1879         if (&variable_defined ($xlib . '_LINK'))
1880         {
1881             $xlink = $xlib . '_LINK';
1882         }
1883         else
1884         {
1885             $xlink = $linker ? $linker : 'LINK';
1886         }
1887
1888         local ($rpath);
1889         if ($instdirs{$onelib} eq 'EXTRA')
1890         {
1891             # It's an EXTRA_ library, so we can't specify -rpath.
1892             # Yuck.
1893             $rpath = 's/\@RPATH\@//go;';
1894         }
1895         else
1896         {
1897             $rpath = ('s/\@RPATH\@/-rpath \$(' . $instdirs{$onelib}
1898                       . 'dir)/go;');
1899         }
1900
1901         $output_rules .=
1902             &file_contents_with_transform ('s/\@LTLIBRARY\@/'
1903                                            . $onelib . '/go;'
1904                                            . 's/\@XLTLIBRARY\@/'
1905                                            . $xlib . '/go;'
1906                                            . $rpath
1907                                            . 's/\@XLINK\@/' . $xlink . '/go;',
1908                                            'ltlibrary');
1909     }
1910
1911     if ($seen_libobjs)
1912     {
1913         foreach $onelib (@liblist)
1914         {
1915             # Canonicalize names.
1916             ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1917             if (&variable_defined ($xlib . '_LIBADD'))
1918             {
1919                 &check_libobjs_sources ($xlib, $xlib . '_LIBADD');
1920             }
1921         }
1922     }
1923 }
1924
1925 # See if any _SOURCES variable were misspelled.  Also, make sure that
1926 # EXTRA_ variables don't contain configure substitutions.
1927 sub check_typos
1928 {
1929     local ($varname, $primary);
1930     foreach $varname (keys %contents)
1931     {
1932         foreach $primary ('_SOURCES', '_LIBADD', '_LDADD', '_LDFLAGS',
1933                           '_DEPENDENCIES')
1934         {
1935             if ($varname =~ /$primary$/ && ! $content_seen{$varname})
1936             {
1937                 &am_line_error ($varname,
1938                                 "invalid unused variable name: \`$varname'");
1939             }
1940         }
1941     }
1942 }
1943
1944 # Handle scripts.
1945 sub handle_scripts
1946 {
1947     # NOTE we no longer automatically clean SCRIPTS, because it is
1948     # useful to sometimes distribute scripts verbatim.  This happens
1949     # eg in Automake itself.
1950     &am_install_var ('scripts', 'SCRIPTS',
1951                      'bin', 'sbin', 'libexec', 'pkgdata',
1952                      'noinst', 'check');
1953
1954     # Set $scripts_installed if appropriate.  Make sure we only find
1955     # scripts which are actually installed -- this is why we can't
1956     # simply use the return value of am_install_var.
1957     local (%valid) = &am_primary_prefixes ('SCRIPTS', 'bin', 'sbin',
1958                                            'libexec', 'pkgdata',
1959                                            'noinst', 'check');
1960     local ($key);
1961     foreach $key (keys %valid)
1962     {
1963         if ($key ne 'noinst'
1964             && $key ne 'check'
1965             && &variable_defined ($key . '_SCRIPTS'))
1966         {
1967             $scripts_installed = 1;
1968             # push (@check_tests, 'check-' . $key . 'SCRIPTS');
1969         }
1970     }
1971
1972     if ($scripts_installed)
1973     {
1974         # If a program is installed, this is required.  We only want this
1975         # error to appear once.
1976         &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1977             unless $seen_arg_prog;
1978         $seen_arg_prog = 1;
1979     }
1980 }
1981
1982 # Search a file for a "version.texi" Texinfo include.  Return the name
1983 # of the include file if found, or the empty string if not.  A
1984 # "version.texi" file is actually any file whose name matches
1985 # "vers*.texi".
1986 sub scan_texinfo_file
1987 {
1988     local ($filename) = @_;
1989
1990     if (! open (TEXI, $filename))
1991     {
1992         &am_error ("couldn't open \`$filename': $!");
1993         return '';
1994     }
1995     print "automake: reading $filename\n" if $verbose;
1996
1997     local ($vfile, $outfile);
1998     while (<TEXI>)
1999     {
2000         if (/^\@setfilename +(\S+)/)
2001         {
2002             $outfile = $1;
2003             last if ($vfile);
2004         }
2005
2006         if (/^\@include\s+(vers[^.]*\.texi)\s*$/)
2007         {
2008             # Found version.texi include.
2009             $vfile = $1;
2010             last if $outfile;
2011         }
2012     }
2013
2014     close (TEXI);
2015     return ($outfile, $vfile);
2016 }
2017
2018 # Handle all Texinfo source.
2019 sub handle_texinfo
2020 {
2021     &am_line_error ('TEXINFOS',
2022                     "\`TEXINFOS' is an anachronism; use \`info_TEXINFOS'")
2023         if &variable_defined ('TEXINFOS');
2024     return if (! &variable_defined ('info_TEXINFOS')
2025                && ! &variable_defined ('html_TEXINFOS'));
2026
2027     local (@texis) = &variable_value_as_list ('info_TEXINFOS', 'all');
2028
2029     local (@info_deps_list, @dvis_list, @texi_deps);
2030     local ($infobase, $info_cursor);
2031     local (%versions);
2032     local ($done) = 0;
2033     local ($vti);
2034     local ($tc_cursor, @texi_cleans);
2035     local ($canonical);
2036
2037     foreach $info_cursor (@texis)
2038     {
2039         # FIXME: This is mildly hacky, since it recognizes "txinfo".
2040         # I don't feel like making it right.
2041         ($infobase = $info_cursor) =~ s/\.te?xi(nfo)?$//;
2042
2043         # If 'version.texi' is referenced by input file, then include
2044         # automatic versioning capability.
2045         local ($out_file, $vtexi) = &scan_texinfo_file ($relative_dir
2046                                                         . "/" . $info_cursor);
2047
2048         if ($out_file eq '')
2049         {
2050             &am_error ("\`$info_cursor' missing \@setfilename");
2051             next;
2052         }
2053
2054         if ($out_file =~ /\.(.+)$/ && $1 ne 'info')
2055         {
2056             # FIXME should report line number in input file.
2057             &am_error ("output of \`$info_cursor', \`$out_file', has unrecognized extension");
2058             next;
2059         }
2060
2061         if ($vtexi)
2062         {
2063             &am_error ("\`$vtexi', included in \`$info_cursor', also included in \`$versions{$vtexi}'")
2064                 if (defined $versions{$vtexi});
2065             $versions{$vtexi} = $info_cursor;
2066
2067             # We number the stamp-vti files.  This is doable since the
2068             # actual names don't matter much.  We only number starting
2069             # with the second one, so that the common case looks nice.
2070             $vti = 'vti' . ($done ? $done : '');
2071             &push_dist_common ($vtexi, 'stamp-' . $vti);
2072             push (@clean, $vti);
2073
2074             # Only require once.
2075             &require_conf_file_with_line ('info_TEXINFOS', $FOREIGN,
2076                                           'mdate-sh')
2077                 if ! $done;
2078             ++$done;
2079
2080             local ($conf_pat);
2081             ($conf_pat = $config_aux_dir) =~ s/(\W)/\\$1/g;
2082             $output_rules .=
2083                 &file_contents_with_transform
2084                     ('s/\@TEXI\@/' . $info_cursor . '/g; '
2085                      . 's/\@VTI\@/' . $vti . '/g; '
2086                      . 's/\@VTEXI\@/' . $vtexi . '/g;'
2087                      . 's,\@MDDIR\@,' . $conf_pat . ',g;',
2088                      'texi-vers');
2089
2090             &push_phony_cleaners ($vti);
2091         }
2092
2093         # If user specified file_TEXINFOS, then use that as explicit
2094         # dependency list.
2095         @texi_deps = ();
2096         push (@texi_deps, $info_cursor);
2097         push (@texi_deps, $vtexi) if $vtexi;
2098
2099         # Canonicalize name first.
2100         ($canonical = $infobase) =~ tr/A-Za-z0-9_/_/c;
2101         if (&variable_defined ($canonical . "_TEXINFOS"))
2102         {
2103             push (@texi_deps, '$(' . $canonical . '_TEXINFOS)');
2104             &push_dist_common ('$(' . $canonical . '_TEXINFOS)');
2105         }
2106
2107         $output_rules .= ("\n" . $out_file . ": "
2108                           . join (' ', @texi_deps)
2109                           . "\n" . $infobase . ".dvi: "
2110                           . join (' ', @texi_deps)
2111                           . "\n\n");
2112
2113         push (@info_deps_list, $out_file);
2114         push (@dvis_list, $infobase . '.dvi');
2115
2116         # Generate list of things to clean for this target.  We do
2117         # this explicitly because otherwise too many things could be
2118         # removed.  In particular the ".log" extension might
2119         # reasonably be used in other contexts by the user.
2120         foreach $tc_cursor ('aux', 'cp', 'cps', 'dvi', 'fn', 'fns',
2121                             'ky', 'kys', 'ps', 'log', 'pg', 'toc', 'tp', 'tps',
2122                             'vr', 'vrs', 'op', 'tr', 'cv', 'cn')
2123         {
2124             push (@texi_cleans, $infobase . '.' . $tc_cursor);
2125         }
2126     }
2127
2128     # Find these programs wherever they may lie.  Yes, this has
2129     # intimate knowledge of the structure of the texinfo distribution.
2130     &define_program_variable ('MAKEINFO', 'build', 'texinfo/makeinfo',
2131                               'makeinfo',
2132                               # Circumlocution to avoid accidental
2133                               # configure substitution.
2134                               '@MAKE' . 'INFO@');
2135     &define_program_variable ('TEXI2DVI', 'src', 'texinfo/util',
2136                               'texi2dvi');
2137
2138     # Set transform for including texinfos.am.  First, handle --cygnus
2139     # stuff.
2140     local ($xform);
2141     if ($cygnus_mode)
2142     {
2143         $xform = 's/^NOTCYGNUS.*$//; s/^CYGNUS//;';
2144     }
2145     else
2146     {
2147         $xform = 's/^CYGNUS.*$//; s/^NOTCYGNUS//;';
2148     }
2149
2150     # Handle location of texinfo.tex.
2151     local ($need_texi_file) = 0;
2152     if ($cygnus_mode)
2153     {
2154         &define_variable ('TEXINFO_TEX',
2155                           '$(top_srcdir)/../texinfo/texinfo.tex');
2156     }
2157     elsif ($config_aux_dir ne '.' && $config_aux_dir ne '')
2158     {
2159         &define_variable ('TEXINFO_TEX', $config_aux_dir . '/texinfo.tex');
2160     }
2161     elsif (! &variable_defined ('TEXINFO_TEX'))
2162     {
2163         &define_variable ('TEXINFO_TEX', '$(srcdir)/texinfo.tex');
2164     }
2165     else
2166     {
2167         $need_texi_file = 1;
2168     }
2169     local ($xxform) = &dirname (&variable_value ('TEXINFO_TEX'));
2170     $xxform =~ s/(\W)/\\$1/g;
2171     $xform .= ' s/\@TEXINFODIR\@/' . $xxform . '/g;';
2172
2173     $output_rules .= &file_contents_with_transform ($xform, 'texinfos');
2174     push (@phony, 'install-info-am', 'uninstall-info');
2175     push (@dist_targets, 'dist-info');
2176
2177     # How to clean.  The funny name is due to --cygnus influence; in
2178     # Cygnus mode, `clean-info' is a target that users can use.
2179     $output_rules .= "\nmostlyclean-aminfo:\n";
2180     &pretty_print_rule ("\t-rm -f", "\t  ", @texi_cleans);
2181     $output_rules .= ("\nclean-aminfo:\n\ndistclean-aminfo:\n\n"
2182                       . "maintainer-clean-aminfo:\n\t"
2183                       # Eww.  But how else can we find all the output
2184                       # files from makeinfo?
2185                       . 'for i in $(INFO_DEPS); do' . " \\\n"
2186                       . "\t" . '  rm -f $$i;' . " \\\n"
2187                       . "\t" . '  if test "`echo $$i-[0-9]*`" != "$$i-[0-9]*"; then' . " \\\n"
2188                       . "\t" . '    rm -f $$i-[0-9]*;' . " \\\n"
2189                       . "\t" . '  fi;' . " \\\n"
2190                       . "\tdone\n");
2191     &push_phony_cleaners ('aminfo');
2192     if ($cygnus_mode)
2193     {
2194         $output_rules .= "clean-info: mostlyclean-aminfo\n";
2195     }
2196
2197     push (@suffixes, '.texi', '.texinfo', '.txi', '.info', '.dvi', '.ps');
2198
2199     if (! defined $options{'no-installinfo'})
2200     {
2201         push (@uninstall, 'uninstall-info');
2202         push (@installdirs, '$(DESTDIR)$(infodir)');
2203         unshift (@install_data, 'install-info-am');
2204
2205         # Make sure documentation is made and installed first.  Use
2206         # $(INFO_DEPS), not 'info', because otherwise recursive makes
2207         # get run twice during "make all".
2208         unshift (@all, '$(INFO_DEPS)');
2209     }
2210     push (@clean, 'aminfo');
2211     push (@info, '$(INFO_DEPS)');
2212     push (@dvi, '$(DVIS)');
2213
2214     &define_variable ("INFO_DEPS", join (' ', @info_deps_list));
2215     &define_variable ("DVIS", join (' ', @dvis_list));
2216     # This next isn't strictly needed now -- the places that look here
2217     # could easily be changed to look in info_TEXINFOS.  But this is
2218     # probably better, in case noinst_TEXINFOS is ever supported.
2219     &define_variable ("TEXINFOS", &variable_value ('info_TEXINFOS'));
2220
2221     # Do some error checking.  Note that this file is not required
2222     # when in Cygnus mode; instead we defined TEXINFO_TEX explicitly
2223     # up above.
2224     &require_file_with_line ('info_TEXINFOS', $FOREIGN, 'texinfo.tex')
2225         if $need_texi_file && ! defined $options{'no-texinfo.tex'};
2226 }
2227
2228 # Handle any man pages.
2229 sub handle_man_pages
2230 {
2231     &am_line_error ('MANS', "\`MANS' is an anachronism; use \`man_MANS'")
2232         if &variable_defined ('MANS');
2233     return if ! &variable_defined ('man_MANS');
2234
2235     # Find all the sections in use.  We do this by first looking for
2236     # "standard" sections, and then looking for any additional
2237     # sections used in man_MANS.
2238     local ($sect, %sections, %vlist);
2239     # Add more sections as needed.
2240     foreach $sect ('0'..'9', 'n', 'l')
2241     {
2242         if (&variable_defined ('man' . $sect . '_MANS'))
2243         {
2244             $sections{$sect} = 1;
2245             $vlist{'$(man' . $sect . '_MANS)'} = 1;
2246         }
2247     }
2248
2249     if (&variable_defined ('man_MANS'))
2250     {
2251         $vlist{'$(man_MANS)'} = 1;
2252         foreach (&variable_value_as_list ('man_MANS', 'all'))
2253         {
2254             # A page like `foo.1c' goes into man1dir.
2255             if (/\.([0-9a-z])([a-z]*)$/)
2256             {
2257                 $sections{$1} = 1;
2258             }
2259         }
2260     }
2261
2262
2263     # Now for each section, generate an install and unintall rule.
2264     # Sort sections so output is deterministic.
2265     local (@namelist);
2266     foreach $sect (sort keys %sections)
2267     {
2268         &define_variable ('man' . $sect . 'dir', '$(mandir)/man' . $sect);
2269         push (@installdirs, '$(DESTDIR)$(mandir)/man' . $sect)
2270             unless defined $options{'no-installman'};
2271         $output_rules .= &file_contents_with_transform ('s/\@SECTION\@/'
2272                                                         . $sect . '/g;',
2273                                                         'mans');
2274         push (@phony, 'install-man' . $sect, 'uninstall-man' . $sect);
2275         push (@namelist, 'install-man' . $sect);
2276     }
2277
2278     # We don't really need this, but we use it in case we ever want to
2279     # support noinst_MANS.
2280     &define_variable ("MANS", join (' ', sort keys %vlist));
2281
2282     # Generate list of install dirs.
2283     $output_rules .= ("install-man: \$(MANS)\n"
2284                       . "\t\@\$(NORMAL_INSTALL)\n");
2285     &pretty_print_rule ("\t\$(MAKE)", "\t ", @namelist);
2286     push (@phony, 'install-man');
2287
2288     $output_rules .= "uninstall-man:\n\t\@\$(NORMAL_UNINSTALL)\n";
2289     grep ($_ = 'un' . $_, @namelist);
2290     &pretty_print_rule ("\t\$(MAKE)", "\t ", @namelist);
2291     push (@phony, 'uninstall-man');
2292
2293     $output_vars .= &file_contents ('mans-vars');
2294
2295     if (! defined $options{'no-installman'})
2296     {
2297         push (@install_data, 'install-man');
2298         push (@uninstall, 'uninstall-man');
2299         push (@all, '$(MANS)');
2300     }
2301 }
2302
2303 # Handle DATA variables.
2304 sub handle_data
2305 {
2306     &am_install_var ('data', 'DATA', 'data', 'sysconf',
2307                      'sharedstate', 'localstate', 'pkgdata',
2308                      'noinst', 'check');
2309 }
2310
2311 # Handle TAGS.
2312 sub handle_tags
2313 {
2314     push (@phony, 'tags');
2315     local (@tag_deps) = ();
2316     if (&variable_defined ('SUBDIRS'))
2317     {
2318         $output_rules .= ("tags-recursive:\n"
2319                           . "\tlist=\'\$(SUBDIRS)\'; for subdir in \$\$list; do \\\n"
2320                           # Never fail here if a subdir fails; it
2321                           # isn't important.
2322                           . "\t  (cd \$\$subdir && \$(MAKE) tags); \\\n"
2323                           . "\tdone\n");
2324         push (@tag_deps, 'tags-recursive');
2325         push (@phony, 'tags-recursive');
2326     }
2327
2328     if ($dir_holds_sources
2329         || $dir_holds_headers
2330         || &variable_defined ('ETAGS_ARGS')
2331         || @tag_deps)
2332     {
2333         local ($xform) = '';
2334         local ($one_hdr);
2335         foreach $one_hdr (@config_headers)
2336         {
2337             if ($relative_dir eq &dirname ($one_hdr))
2338             {
2339                 # The config header is in this directory.  So require it.
2340                 local ($var);
2341                 ($var = &basename ($one_hdr)) =~ s/(\W)/\\$1/g;
2342                 $xform .= ' ' if $xform;
2343                 $xform .= $var;
2344             }
2345         }
2346         $xform = ('s/\@CONFIG\@/' . $xform . '/;'
2347                   . 's/\@DIRS\@/' . join (' ', @tag_deps) . '/;');
2348
2349         if (&variable_defined ('SUBDIRS'))
2350         {
2351             $xform .= 's/^SUBDIRS//;';
2352         }
2353         else
2354         {
2355             $xform .= 's/^SUBDIRS.*$//;';
2356         }
2357
2358         $output_rules .= &file_contents_with_transform ($xform, 'tags');
2359         $output_rules .= &file_contents ('tags-clean');
2360         push (@clean, 'tags');
2361         &push_phony_cleaners ('tags');
2362         &examine_variable ('TAGS_DEPENDENCIES');
2363     }
2364     elsif (&variable_defined ('TAGS_DEPENDENCIES'))
2365     {
2366         &am_line_error ('TAGS_DEPENDENCIES',
2367                         "doesn't make sense to define \`TAGS_DEPENDENCIES' without sources or \`ETAGS_ARGS'");
2368     }
2369     else
2370     {
2371         # Every Makefile must define some sort of TAGS rule.
2372         # Otherwise, it would be possible for a top-level "make TAGS"
2373         # to fail because some subdirectory failed.
2374         $output_rules .= "tags: TAGS\nTAGS:\n\n";
2375     }
2376 }
2377
2378 # Worker for handle_dist.
2379 sub handle_dist_worker
2380 {
2381     local ($makefile) = @_;
2382
2383     $output_rules .= 'distdir: $(DISTFILES)' . "\n";
2384
2385     # Initialization; only at top level.
2386     if ($relative_dir eq '.')
2387     {
2388         if (defined $options{'check-news'})
2389         {
2390             # For Gnits users, this is pretty handy.  Look at 15 lines
2391             # in case some explanatory text is desirable.
2392             $output_rules .= '  @if sed 15q $(srcdir)/NEWS | fgrep -e "$(VERSION)" > /dev/null; then :; else \\
2393           echo "NEWS not updated; not releasing" 1>&2; \\
2394           exit 1; \\
2395         fi
2396 ';
2397         }
2398
2399
2400         # Create dist directory.
2401         $output_rules .= ("\t-rm -rf \$(distdir)\n"
2402                           . "\tmkdir \$(distdir)\n"
2403                           . "\t-chmod 777 \$(distdir)\n");
2404     }
2405
2406     # Only run automake in `dist' target if --include-deps and
2407     # `no-dependencies' not specified.  That way the recipient of a
2408     # distribution can run "make dist" and not need Automake.  You
2409     # might be wondering why we run automake once for each directory
2410     # we distribute, instead of running it once at the top level.  The
2411     # answer is that we want to run automake after the dependencies
2412     # have been generated.  This occurs when "make" is run in the
2413     # subdir.  So automake must be run after make has updated the
2414     # Makefile, which means that it must run once per directory.
2415     if ($use_dependencies)
2416     {
2417         $output_rules .=
2418             (
2419              # There are several directories we need to know about
2420              # when rebuilding the Makefile.ins.  They are:
2421              #   here - The absolute path to our topmost build directory.
2422              #   top_distdir - The absolute path to the top of our dist
2423              #                 hierarchy.
2424              #   distdir - The path to our sub-part of the dist hierarchy.
2425              # If this directory is the topmost directory, we set
2426              # top_distdir from distdir; that lets us pass in distdir
2427              # from an enclosing package.
2428              "\t" . 'here=`cd $(top_builddir) && pwd`; ' . "\\\n"
2429              . "\t" . 'top_distdir=`cd $('
2430              . (($relative_dir eq '.') ? 'distdir' : 'top_distdir')
2431              . ') && pwd`; ' . "\\\n"
2432              . "\t" . 'distdir=`cd $(distdir) && pwd`; ' . "\\\n"
2433              . "\tcd \$(top_srcdir) \\\n"
2434              . "\t  && \$(AUTOMAKE) --include-deps --build-dir=\$\$here --srcdir-name=\$(top_srcdir) --output-dir=\$\$top_distdir "
2435              # Set strictness of output.
2436              . ($cygnus_mode ? '--cygnus' : ('--' . $strictness_name))
2437              . ($cmdline_use_dependencies ? '' : ' --include-deps')
2438              . " " . $makefile . "\n"
2439              );
2440     }
2441
2442     # Scan EXTRA_DIST to see if we need to distribute anything from a
2443     # subdir.  If so, add it to the list.  I didn't want to do this
2444     # originally, but there were so many requests that I finally
2445     # relented.
2446     local (@dist_dirs);
2447     if (&variable_defined ('EXTRA_DIST'))
2448     {
2449         # FIXME: This should be fixed to work with conditionals.  That
2450         # will require only making the entries in @dist_dirs under the
2451         # appropriate condition.  This is meaningful if the nature of
2452         # the distribution should depend upon the configure options
2453         # used.
2454         foreach (&variable_value_as_list ('EXTRA_DIST', ''))
2455         {
2456             next if /^\@.*\@$/;
2457             next unless s,/+[^/]+$,,;
2458             push (@dist_dirs, $_)
2459                 unless $_ eq '.';
2460         }
2461     }
2462     if (@dist_dirs)
2463     {
2464         # Prepend $(distdir) to each directory given.  Doing it via a
2465         # hash lets us ensure that each directory is used only once.
2466         local (%dhash);
2467         grep ($dhash{'$(distdir)/' . $_} = 1, @dist_dirs);
2468         $output_rules .= "\t";
2469         &pretty_print_rule ('$(mkinstalldirs)', "\t   ", sort keys %dhash);
2470     }
2471
2472     # In loop, test for file existence because sometimes a file gets
2473     # included in DISTFILES twice.  For example this happens when a
2474     # single source file is used in building more than one program.
2475     # Also, there are situations in which "ln" can fail.  For instance
2476     # a file to distribute could actually be a cross-filesystem
2477     # symlink -- this can easily happen if "gettextize" was run on the
2478     # distribution.
2479     $output_rules .= "\t\@for file in \$(DISTFILES); do \\\n";
2480     if ($cygnus_mode)
2481     {
2482         $output_rules .= "\t  if test -f \$\$file; then d=.; else d=\$(srcdir); fi; \\\n";
2483     }
2484     else
2485     {
2486         $output_rules .= "\t  d=\$(srcdir); \\\n";
2487     }
2488     $output_rules .= '    test -f $(distdir)/$$file \\
2489           || ln $$d/$$file $(distdir)/$$file 2> /dev/null \\
2490           || cp -p $$d/$$file $(distdir)/$$file; \\
2491         done
2492 ';
2493
2494     # If we have SUBDIRS, create all dist subdirectories and do
2495     # recursive build.
2496     if (&variable_defined ('SUBDIRS'))
2497     {
2498         # If SUBDIRS is conditionally defined, then set DIST_SUBDIRS
2499         # to all possible directories, and use it.
2500         if (! &variable_conditions ('SUBDIRS'))
2501         {
2502             $dist_subdir_name = 'SUBDIRS';
2503         }
2504         else
2505         {
2506             $dist_subdir_name = 'DIST_SUBDIRS';
2507             if (! &variable_defined ('DIST_SUBDIRS'))
2508             {
2509                 &define_pretty_variable ('DIST_SUBDIRS', '',
2510                                          &variable_value_as_list ('SUBDIRS',
2511                                                                   'all'));
2512             }
2513         }
2514
2515         # Test for directory existence here because previous automake
2516         # invocation might have created some directories.  Note that
2517         # we explicitly set distdir for the subdir make; that lets us
2518         # mix-n-match many automake-using packages into one large
2519         # package, and have "dist" at the top level do the right
2520         # thing.  If we're in the topmost directory, then we use
2521         # `distdir' instead of `top_distdir'; this lets us work
2522         # correctly with an enclosing package.
2523         $output_rules .= 
2524             ("\t" . 'for subdir in $(' . $dist_subdir_name . '); do ' . "\\\n"
2525              . "\t" . '  test -d $(distdir)/$$subdir ' . "\\\n"
2526              . "\t" . '  || mkdir $(distdir)/$$subdir ' . "\\\n"
2527              . "\t" . '  || exit 1; ' . "\\\n"
2528              . "\t" . '  chmod 777 $(distdir)/$$subdir; ' . "\\\n"
2529              . "\t" . '  (cd $$subdir && $(MAKE) top_distdir=../$('
2530              . (($relative_dir eq '.') ? 'distdir' : 'top_distdir')
2531              . ') distdir=../$(distdir)/$$subdir distdir) ' . "\\\n"
2532              . "\t" . '    || exit 1; ' . "\\\n"
2533              . "\tdone\n");
2534     }
2535
2536     # If the target `dist-hook' exists, make sure it is run.  This
2537     # allows users to do random weird things to the distribution
2538     # before it is packaged up.
2539     push (@dist_targets, 'dist-hook') if defined $contents{'dist-hook'};
2540
2541     local ($targ);
2542     foreach $targ (@dist_targets)
2543     {
2544         # We must explicitly set distdir and top_distdir for these
2545         # sub-makes.
2546         $output_rules .= "\t\$(MAKE) top_distdir=\"\$(top_distdir)\" distdir=\"\$(distdir)\" $targ\n";
2547     }
2548
2549     push (@phony, 'distdir');
2550 }
2551
2552 # Handle 'dist' target.
2553 sub handle_dist
2554 {
2555     local ($makefile) = @_;
2556
2557     # Set up maint_charset.
2558     $local_maint_charset = &variable_value ('MAINT_CHARSET')
2559         if &variable_defined ('MAINT_CHARSET');
2560     $maint_charset = $local_maint_charset
2561         if $relative_dir eq '.';
2562
2563     if (&variable_defined ('DIST_CHARSET'))
2564     {
2565         &am_line_error ('DIST_CHARSET',
2566                         "DIST_CHARSET defined but no MAINT_CHARSET defined")
2567             if ! $local_maint_charset;
2568         if ($relative_dir eq '.')
2569         {
2570             $dist_charset = &variable_value ('DIST_CHARSET')
2571         }
2572         else
2573         {
2574             &am_line_error ('DIST_CHARSET',
2575                             "DIST_CHARSET can only be defined at top level");
2576         }
2577     }
2578
2579     # Look for common files that should be included in distribution.
2580     local ($cfile);
2581     foreach $cfile (@common_files)
2582     {
2583         if (-f ($relative_dir . "/" . $cfile))
2584         {
2585             &push_dist_common ($cfile);
2586         }
2587     }
2588
2589     # Keys of %dist_common are names of files to distributed.  We put
2590     # README first because it then becomes easier to make a
2591     # Usenet-compliant shar file (in these, README must be first).
2592     # FIXME: do more ordering of files here.
2593     local (@coms);
2594     if (defined $dist_common{'README'})
2595     {
2596         push (@coms, 'README');
2597         delete $dist_common{'README'};
2598     }
2599     push (@coms, sort keys %dist_common);
2600
2601     &define_pretty_variable ("DIST_COMMON", '', @coms);
2602     $output_vars .= "\n";
2603
2604     # Some boilerplate.
2605     $output_vars .= &file_contents ('dist-vars') . "\n";
2606     &define_variable ('TAR', $TAR);
2607     &define_variable ('GZIP', '--best');
2608
2609     # Put these things in rules section so it is easier for whoever
2610     # reads Makefile.in.
2611     if (! &variable_defined ('distdir'))
2612     {
2613         if ($relative_dir eq '.')
2614         {
2615             $output_rules .= "\n" . 'distdir = $(PACKAGE)-$(VERSION)' . "\n";
2616         }
2617         else
2618         {
2619             $output_rules .= ("\n"
2620                               . 'distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)'
2621                               . "\n");
2622         }
2623     }
2624     if ($relative_dir eq '.')
2625     {
2626         $output_rules .= "top_distdir = \$(distdir)\n\n";
2627     }
2628     else
2629     {
2630         $output_rules .= "\nsubdir = " . $relative_dir . "\n\n";
2631     }
2632
2633     # Generate 'dist' target, and maybe dist-shar / dist-zip / dist-tarZ.
2634     if ($relative_dir eq '.')
2635     {
2636         # Rule to check whether a distribution is viable.
2637         $output_rules .= ('# This target untars the dist file and tries a VPATH configuration.  Then
2638 # it guarantees that the distribution is self-contained by making another
2639 # tarfile.
2640 distcheck: dist
2641         -rm -rf $(distdir)
2642         GZIP=$(GZIP) $(TAR) zxf $(distdir).tar.gz
2643         mkdir $(distdir)/=build
2644         mkdir $(distdir)/=inst
2645         dc_install_base=`cd $(distdir)/=inst && pwd`; \\'
2646                           . (defined $contents{'distcheck-hook'}
2647                              ? "\t\$(MAKE) distcheck-hook"
2648                              : '')
2649                           . '
2650         cd $(distdir)/=build \\
2651           && ../configure '
2652
2653                           . ($seen_gettext ? '--with-included-gettext ' : '')
2654                           . '--srcdir=.. --prefix=$$dc_install_base \\
2655           && $(MAKE) \\
2656           && $(MAKE) dvi \\
2657           && $(MAKE) check \\
2658           && $(MAKE) install \\
2659           && $(MAKE) installcheck \\
2660           && $(MAKE) dist
2661         -rm -rf $(distdir)
2662         @echo "========================"; \\
2663         echo "$(distdir).tar.gz is ready for distribution"; \\
2664         echo "========================"
2665 ');
2666
2667         local ($dist_all) = ('dist-all: distdir' . "\n"
2668                              . $dist_header);
2669         local ($curs);
2670         foreach $curs ('dist', 'dist-shar', 'dist-zip', 'dist-tarZ')
2671         {
2672             if (defined $options{$curs} || $curs eq 'dist')
2673             {
2674                 $output_rules .= ($curs . ': distdir' . "\n"
2675                                   . $dist_header
2676                                   . $dist{$curs}
2677                                   . $dist_trailer);
2678                 $dist_all .= $dist{$curs};
2679             }
2680         }
2681         $output_rules .= $dist_all . $dist_trailer;
2682     }
2683
2684     # Generate distdir target.
2685     &handle_dist_worker ($makefile);
2686 }
2687
2688 # Scan a single dependency file and rewrite the dependencies as
2689 # appropriate.  Essentially this means:
2690 # * Clean out absolute dependencies which are not desirable.
2691 # * Rewrite other dependencies to be relative to $(top_srcdir).
2692 sub scan_dependency_file
2693 {
2694     local ($depfile) = @_;
2695
2696     if (! open (DEP_FILE, $depfile))
2697     {
2698         &am_error ("couldn't open \`$depfile': $!");
2699         return;
2700     }
2701     print "automake: reading $depfile\n" if $verbose;
2702
2703     # Sometimes it is necessary to omit some dependencies.
2704     local (%omit) = %omit_dependencies;
2705     if (&variable_defined ('OMIT_DEPENDENCIES'))
2706     {
2707         # FIXME: Doesn't work with conditionals.  I'm not sure if this
2708         # matters.
2709         grep ($omit{$_} = 1,
2710               &variable_value_as_list ('OMIT_DEPENDENCIES', ''));
2711     }
2712
2713     local ($first_line) = 1;
2714     local ($last_line) = 0;
2715     local ($target, @dependencies);
2716     local ($one_dep, $xform);
2717     local ($just_file);
2718
2719     local ($srcdir_rx, $fixup_rx);
2720     ($fixup_rx = $srcdir_name . '/' . $relative_dir . '/')
2721         =~ s/(\W)/\\$1/g;
2722     ($srcdir_rx = $srcdir_name . '/') =~ s/(\W)/\\$1/g;
2723
2724     local ($rewrite_builddir) = (($top_builddir eq '.')
2725                                  ? ''
2726                                  : $top_builddir . '/');
2727
2728     while (<DEP_FILE>)
2729     {
2730         if ($last_line)
2731         {
2732             # If LAST_LINE set then we've already seen what we thought
2733             # was the last line.
2734             goto bad_format;
2735         }
2736         next if (/$WHITE_PATTERN/o);
2737         chop;
2738         if (! s/\\$//)
2739         {
2740             # No trailing "\" means this should be the last line.
2741             $last_line = 1;
2742         }
2743
2744         if ($first_line)
2745         {
2746             if (! /^([^:]+:)(.+)$/)
2747             {
2748               bad_format:
2749                 &am_error ("\`$depfile' has incorrect format");
2750                 close (DEP_FILE);
2751                 return;
2752             }
2753
2754             $_ = $2;
2755             # Make sure to strip the .P file from the target.
2756             ($target = $1) =~ s, *\.deps/[^.]+\.P,,;
2757
2758             $first_line = 0;
2759         }
2760
2761         foreach $one_dep (split (' ', $_))
2762         {
2763             ($just_file = $one_dep) =~ s,^.*/,,;
2764             next if defined $omit{$just_file};
2765
2766             if ($one_dep =~ /^$fixup_rx/)
2767             {
2768                 # The dependency points to the current directory in
2769                 # some way.
2770                 ($xform = $one_dep) =~ s/^$fixup_rx//;
2771                 push (@dependencies, $xform);
2772             }
2773             elsif ($one_dep =~ /^$srcdir_rx/)
2774             {
2775                 # The dependency is in some other directory in the package.
2776                 ($xform = $one_dep) =~ s/^$srcdir_rx/$rewrite_builddir/;
2777                 push (@dependencies, $xform);
2778             }
2779             elsif ($one_dep =~ /^\//)
2780             {
2781                 # Absolute path; ignore.
2782             }
2783             else
2784             {
2785                 # Anything else is assumed to be correct.
2786                 push (@dependencies, $one_dep);
2787             }
2788         }
2789     }
2790
2791     &pretty_print_rule ($target, "\t", @dependencies);
2792
2793     close (DEP_FILE);
2794 }
2795
2796 # Handle auto-dependency code.
2797 sub handle_dependencies
2798 {
2799     # Make sure this variable is always marked as used.
2800     &examine_variable ('OMIT_DEPENDENCIES');
2801
2802     if ($use_dependencies)
2803     {
2804         # Include GNU-make-specific auto-dep code.  Don't include it
2805         # if DEP_FILES would be empty.
2806         if ($dir_holds_sources && keys %dep_files)
2807         {
2808             &define_pretty_variable ('DEP_FILES', '', sort keys %dep_files);
2809             $output_rules .= &file_contents ('depend');
2810             push (@clean, 'depend');
2811             &push_phony_cleaners ('depend');
2812             $output_rules .=
2813                 &file_contents_with_transform ('s/\@EXT\@/.c/g;'
2814                                                . 's/\@PFX\@//g;',
2815                                                'depend2');
2816             local ($ext);
2817             foreach $ext (sort keys %cxx_extensions)
2818             {
2819                 $output_rules .=
2820                     &file_contents_with_transform ('s/\@EXT\@/' . $ext .'/g;'
2821                                                    . 's/\@PFX\@/CXX/g;',
2822                                                    'depend2');
2823             }
2824         }
2825     }
2826     elsif ($build_directory ne '')
2827     {
2828         # Include any auto-generated deps that are present.  Note that
2829         # $build_directory ends in a "/".
2830         if (-d ($build_directory . $relative_dir . "/.deps"))
2831         {
2832             local ($depfile);
2833
2834             foreach $depfile (&my_glob ($build_directory
2835                                         . $relative_dir . "/.deps/*.P"))
2836             {
2837                 &scan_dependency_file ($depfile);
2838             }
2839
2840             $output_rules .= "\n";
2841         }
2842     }
2843 }
2844
2845 # Handle subdirectories.
2846 sub handle_subdirs
2847 {
2848     return if ! &variable_defined ('SUBDIRS');
2849
2850     # Make sure each directory mentioned in SUBDIRS actually exists.
2851     local ($dir);
2852     foreach $dir (&variable_value_as_list ('SUBDIRS', 'all'))
2853     {
2854         # Skip directories substituted by configure.
2855         next if $dir =~ /^\@.*\@$/;
2856
2857         if (! -d $am_relative_dir . '/' . $dir)
2858         {
2859             &am_line_error ('SUBDIRS',
2860                             "required directory $am_relative_dir/$dir does not exist");
2861             next;
2862         }
2863
2864         &am_line_error ('SUBDIRS', "directory should not contain \`/'")
2865             if $dir =~ /\//;
2866     }
2867
2868     local ($xform) = ('s/\@INSTALLINFO\@/' .
2869                       (defined $options{'no-installinfo'}
2870                        ? 'install-info-recursive'
2871                        : '')
2872                       . '/;');
2873     $output_rules .= &file_contents_with_transform ($xform, 'subdirs');
2874
2875     # Push a bunch of phony targets.
2876     local ($phonies);
2877     foreach $phonies ('-data', '-exec', 'dirs')
2878     {
2879         push (@phony, 'install' . $phonies . '-recursive');
2880         push (@phony, 'uninstall' . $phonies . '-recursive');
2881     }
2882     foreach $phonies ('all', 'check', 'installcheck', 'info', 'dvi')
2883     {
2884         push (@phony, $phonies . '-recursive');
2885     }
2886     &push_phony_cleaners ('recursive');
2887
2888     push (@check_tests, "check-recursive");
2889     push (@installcheck, "installcheck-recursive");
2890     push (@info, "info-recursive");
2891     push (@dvi, "dvi-recursive");
2892
2893     $recursive_install = 1;
2894 }
2895
2896 # Handle aclocal.m4.
2897 sub handle_aclocal_m4
2898 {
2899     local ($regen_aclocal) = 0;
2900     if (-f 'aclocal.m4')
2901     {
2902         &define_variable ("ACLOCAL_M4", '$(top_srcdir)/aclocal.m4');
2903         &push_dist_common ('aclocal.m4');
2904
2905         if (open (ACLOCAL, '< aclocal.m4'))
2906         {
2907             local ($line);
2908             $line = <ACLOCAL>;
2909             close (ACLOCAL);
2910
2911             if ($line =~ 'generated automatically by aclocal')
2912             {
2913                 $regen_aclocal = 1;
2914             }
2915         }
2916     }
2917
2918     local ($acinclude) = 0;
2919     if (-f 'acinclude.m4')
2920     {
2921         $regen_aclocal = 1;
2922         $acinclude = 1;
2923     }
2924
2925     # Note that it might be possible that aclocal.m4 doesn't exist but
2926     # should be auto-generated.  This case probably isn't very
2927     # important.
2928     if ($regen_aclocal)
2929     {
2930         local (@ac_deps) = (
2931                             ($seen_maint_mode ? "\@MAINT\@" : "") ,
2932                             "configure.in",
2933                             ($acinclude ? ' acinclude.m4' : '')
2934                             );
2935
2936         # Scan all -I directories for m4 files.  These are our
2937         # dependencies.
2938         if (&variable_defined ('ACLOCAL_AMFLAGS'))
2939         {
2940             local ($examine_next, $amdir) = 0;
2941             foreach $amdir (&variable_value_as_list ('ACLOCAL_AMFLAGS', ''))
2942             {
2943                 if ($examine_next)
2944                 {
2945                     $examine_next = 0;
2946                     if ($amdir !~ /^\// && -d $amdir)
2947                     {
2948                         push (@ac_deps, &my_glob ($amdir . '/*.m4'));
2949                     }
2950                 }
2951                 elsif ($amdir eq '-I')
2952                 {
2953                     $examine_next = 1;
2954                 }
2955             }
2956         }
2957
2958         &pretty_print_rule ("\$(ACLOCAL_M4):", "\t\t", @ac_deps);
2959
2960         $output_rules .=  ("\t"
2961                            . 'cd $(srcdir) && $(ACLOCAL)'
2962                            . (&variable_defined ('ACLOCAL_AMFLAGS')
2963                               ? ' $(ACLOCAL_AMFLAGS)' : '')
2964                            . "\n");
2965     }
2966 }
2967
2968 # Rewrite a list of input files into a form suitable to put on a
2969 # dependency list.  The idea is that if an input file has a directory
2970 # part the same as the current directory, then the directory part is
2971 # simply removed.  But if the directory part is different, then
2972 # $(top_srcdir) is prepended.  Among other things, this is used to
2973 # generate the dependency list for the output files generated by
2974 # AC_OUTPUT.  Consider what the dependencies should look like in this
2975 # case:
2976 #   AC_OUTPUT(src/out:src/in1:lib/in2)
2977 sub rewrite_inputs_into_dependencies
2978 {
2979     local (@inputs) = @_;
2980     local ($single, @newinputs);
2981
2982     foreach $single (@inputs)
2983     {
2984         if (&dirname ($single) eq $relative_dir)
2985         {
2986             push (@newinputs, &basename ($single));
2987         }
2988         else
2989         {
2990             push (@newinputs, '$(top_srcdir)/' . $single);
2991         }
2992     }
2993
2994     return @newinputs;
2995 }
2996
2997 # Handle remaking and configure stuff.
2998 # We need the name of the input file, to do proper remaking rules.
2999 sub handle_configure
3000 {
3001     local ($local, $input, @secondary_inputs) = @_;
3002
3003     # If SUBDIRS defined, require AC_PROG_MAKE_SET.
3004     &am_line_error ('SUBDIRS', "AC_PROG_MAKE_SET must be used in configure.in")
3005         if &variable_defined ('SUBDIRS') && ! $seen_make_set;
3006
3007     local ($top_reldir);
3008
3009     local ($input_base) = &basename ($input);
3010     local ($local_base) = &basename ($local);
3011
3012     local ($amfile) = $input_base . '.am';
3013     # We know we can always add '.in' because it really should be an
3014     # error if the .in was missing originally.
3015     local ($infile) = '$(srcdir)/' . $input_base . '.in';
3016     local ($colon_infile);
3017     if ($local ne $input || @secondary_inputs)
3018     {
3019         $colon_infile = ':' . $input . '.in';
3020     }
3021     $colon_infile .= ':' . join (':', @secondary_inputs)
3022         if @secondary_inputs;
3023
3024     local (@rewritten) = &rewrite_inputs_into_dependencies (@secondary_inputs);
3025     # This rule remakes the Makefile.in.  Note use of @MAINT@ forces
3026     # us to abandon pretty-printing.  Sigh.
3027     $output_rules .= ($infile
3028                       # NOTE perl 5.003 (with -w) gives a
3029                       # uninitialized value error on the next line.
3030                       # Don't know why.
3031                       . ': '
3032                       . ($seen_maint_mode ? '@MAINT@ ' : '')
3033                       . $amfile . ' '
3034                       . '$(top_srcdir)/configure.in $(ACLOCAL_M4)' . "\n"
3035                       . "\tcd \$(top_srcdir) && \$(AUTOMAKE) "
3036                       . ($cygnus_mode ? '--cygnus' : ('--' . $strictness_name))
3037                       . ($cmdline_use_dependencies ? '' : ' --include-deps')
3038                       . ' ' . $input . $colon_infile . "\n\n");
3039
3040     # This rule remakes the Makefile.
3041     $output_rules .= ($local_base
3042                       # NOTE: bogus uninit value error on next line;
3043                       # see comment above.
3044                       . ': '
3045                       . $infile . ' '
3046                       . join (' ', @rewritten)
3047                       . ' $(top_builddir)/config.status'
3048                       # NOTE: Makefile only depends on BUILT_SOURCES
3049                       # when dependencies are being computed.  This is
3050                       # a workaround for an obscure bug with
3051                       # AC_LINK_FILES.  Anyway, when dependencies are
3052                       # turned off, this shouldn't matter.
3053                       . ($use_dependencies ? ' $(BUILT_SOURCES)' : '')
3054                       . "\n"
3055                       . "\tcd \$(top_builddir) \\\n"
3056                       . "\t  && CONFIG_FILES="
3057                       . (($relative_dir eq '.') ? '$@' : '$(subdir)/$@')
3058                       . $colon_infile
3059                       . ' CONFIG_HEADERS= $(SHELL) ./config.status'
3060                       . "\n\n");
3061
3062     if ($relative_dir ne '.')
3063     {
3064         # In subdirectory.
3065         $top_reldir = '../';
3066     }
3067     else
3068     {
3069         &handle_aclocal_m4;
3070         $output_rules .= &file_contents ('remake');
3071         &examine_variable ('CONFIGURE_DEPENDENCIES');
3072         $top_reldir = '';
3073     }
3074
3075     # If we have a configure header, require it.
3076     local ($one_hdr);
3077     local (@local_fullnames) = @config_fullnames;
3078     local (@local_names) = @config_names;
3079     local ($hdr_index) = 0;
3080     local ($distclean_config) = '';
3081     foreach $one_hdr (@config_headers)
3082     {
3083         local ($one_fullname) = shift (@local_fullnames);
3084         local ($one_name) = shift (@local_names);
3085         $hdr_index += 1;
3086         if ($relative_dir eq &dirname ($one_hdr))
3087         {
3088             local ($ch_sans_dir) = &basename ($one_hdr);
3089             local ($cn_sans_dir) = &basename ($one_name);
3090
3091             &require_file_with_conf_line ($config_header_line,
3092                                           $FOREIGN, $ch_sans_dir);
3093
3094             # Header defined and in this directory.
3095             local (@files);
3096             if (-f $relative_dir . '/acconfig.h')
3097             {
3098                 push (@files, 'acconfig.h');
3099             }
3100             if (-f $one_name . '.top')
3101             {
3102                 push (@files, "${cn_sans_dir}.top");
3103             }
3104             if (-f $one_name . '.bot')
3105             {
3106                 push (@files, "${cn_sans_dir}.bot");
3107             }
3108
3109             &push_dist_common (@files);
3110
3111             local ($stamp_name) = 'stamp-h';
3112             $stamp_name .= "${hdr_index}" if scalar (@config_headers) > 1;
3113
3114             local ($xform) = '';
3115
3116             $xform = 's,\@FILES\@,' . join (' ', @files) . ',;';
3117             $xform .= 's,\@CONFIG_HEADER\@,' . "${cn_sans_dir}" . ',;';
3118             $xform .= 's,\@CONFIG_HEADER_IN\@,' . "${ch_sans_dir}" . ',;';
3119             $xform .= 's,\@CONFIG_HEADER_FULL\@,' . "${one_fullname}" . ',;';
3120             $xform .= 's,\@STAMP\@,' . "${stamp_name}" . ',;';
3121
3122             $output_rules .= &file_contents_with_transform ($xform,
3123                                                             'remake-hdr');
3124
3125             &touch ($relative_dir . "/${stamp_name}.in");
3126             &require_file_with_conf_line ($config_header_line, $FOREIGN,
3127                                           "${stamp_name}.in");
3128
3129             $distclean_config .= ' ' if $distclean_config;
3130             $distclean_config .= $cn_sans_dir;
3131         }
3132     }
3133
3134     if ($distclean_config)
3135     {
3136         $output_rules .= &file_contents_with_transform ('s,\@FILES\@,'
3137                                                         . $distclean_config
3138                                                         . ',;',
3139                                                         'clean-hdr');
3140         push (@clean, 'hdr');
3141         &push_phony_cleaners ('hdr');
3142     }
3143
3144     # Set location of mkinstalldirs.
3145     if ($config_aux_dir ne '.' && $config_aux_dir ne '')
3146     {
3147         &define_variable ('mkinstalldirs', ('$(SHELL) ' . $config_aux_dir
3148                                             . '/mkinstalldirs'));
3149     }
3150     else
3151     {
3152         &define_variable ('mkinstalldirs',
3153                           '$(SHELL) $(top_srcdir)/mkinstalldirs');
3154     }
3155
3156     &am_line_error ('CONFIG_HEADER',
3157                     "\`CONFIG_HEADER' is an anachronism; now determined from \`configure.in'")
3158         if &variable_defined ('CONFIG_HEADER');
3159
3160     local ($one_name);
3161     local ($config_header) = '';
3162     foreach $one_name (@config_names)
3163     {
3164         # Generate CONFIG_HEADER define.
3165         local ($one_hdr);
3166         if ($relative_dir eq &dirname ($one_name))
3167         {
3168             $one_hdr = &basename ($one_name);
3169         }
3170         else
3171         {
3172             $one_hdr = "${top_builddir}/${one_name}";
3173         }
3174
3175         $config_header .= ' ' if $config_header;
3176         $config_header .= $one_hdr;
3177     }
3178     if ($config_header)
3179     {
3180         &define_variable ("CONFIG_HEADER", $config_header);
3181     }
3182
3183     # Now look for other files in this directory which must be remade
3184     # by config.status, and generate rules for them.
3185     local (@actual_other_files) = ();
3186     local ($file, $local);
3187     local (@inputs, @rewritten_inputs, $single);
3188     local ($need_rewritten);
3189     foreach $file (@other_input_files)
3190     {
3191         if ($file =~ /^([^:]*):(.*)$/)
3192         {
3193             # This is the ":" syntax of AC_OUTPUT.
3194             $file = $1;
3195             $local = &basename ($file);
3196             @inputs = split (':', $2);
3197             @rewritten_inputs = &rewrite_inputs_into_dependencies (@inputs);
3198             $need_rewritten = 1;
3199         }
3200         else
3201         {
3202             # Normal usage.
3203             $local = &basename ($file);
3204             @inputs = ($local . '.in');
3205             @rewritten_inputs =
3206                 &rewrite_inputs_into_dependencies ($file . '.in');
3207             $need_rewritten = 0;
3208         }
3209
3210         # Skip files not in this directory.
3211         next unless &dirname ($file) eq $relative_dir;
3212
3213         # Skip any file that is an automake input.
3214         next if -f $file . '.am';
3215
3216         # Some users have been tempted to put `stamp-h' in the
3217         # AC_OUTPUT line.  This won't do the right thing, so we
3218         # explicitly fail here.
3219         if ($local eq 'stamp-h')
3220         {
3221             # FIXME: allow real filename.
3222             &am_conf_error ('configure.in', $ac_output_line,
3223                             'stamp-h should not appear in AC_OUTPUT');
3224             next;
3225         }
3226
3227         $output_rules .= ($local . ': '
3228                           . '$(top_builddir)/config.status '
3229                           . join (' ', @rewritten_inputs) . "\n"
3230                           . "\t"
3231                           . 'cd $(top_builddir) && CONFIG_FILES='
3232                           . ($relative_dir eq '.' ? '' : '$(subdir)/')
3233                           . '$@' . ($need_rewritten
3234                                     ? (':' . join (':', @rewritten_inputs))
3235                                     : '')
3236                           . ' CONFIG_HEADERS= $(SHELL) ./config.status'
3237                           . "\n");
3238         push (@actual_other_files, $local);
3239
3240         # Require all input files.
3241         &require_file_with_conf_line ($ac_output_line, $FOREIGN,
3242                                       @inputs);
3243     }
3244
3245     # These files get removed by "make clean".
3246     &define_pretty_variable ('CONFIG_CLEAN_FILES', '', @actual_other_files);
3247 }
3248
3249 # Handle C headers.
3250 sub handle_headers
3251 {
3252     $dir_holds_headers = &am_install_var ('header', 'HEADERS', 'include',
3253                                           'oldinclude', 'pkginclude',
3254                                           'noinst', 'check');
3255 }
3256
3257 sub handle_gettext
3258 {
3259     return if ! $seen_gettext || $relative_dir ne '.';
3260
3261     if (! &variable_defined ('SUBDIRS'))
3262     {
3263         &am_conf_error
3264             ("AM_GNU_GETTEXT in configure.in but SUBDIRS not defined");
3265         return;
3266     }
3267
3268     &require_file_with_conf_line ($ac_gettext_line, $FOREIGN, 'ABOUT-NLS')
3269         if $seen_gettext;
3270
3271     if (&variable_defined ('SUBDIRS'))
3272     {
3273         &am_line_error
3274             ('SUBDIRS',
3275              "AM_GNU_GETTEXT in configure.in but \`po' not in SUBDIRS")
3276                 if $contents{'SUBDIRS'} !~ /\bpo\b/;
3277         &am_line_error
3278             ('SUBDIRS',
3279              "AM_GNU_GETTEXT in configure.in but \`intl' not in SUBDIRS")
3280                 if $contents{'SUBDIRS'} !~ /\bintl\b/;
3281     }
3282
3283     # Ensure that each language in ALL_LINGUAS has a .po file, and
3284     # each po file is mentioned in ALL_LINGUAS.
3285     if ($seen_linguas)
3286     {
3287         local (%linguas) = ();
3288         grep ($linguas{$_} = 1, split (' ', $all_linguas));
3289
3290         foreach (<po/*.po>)
3291         {
3292             s/^po\///;
3293             s/\.po$//;
3294
3295             &am_line_error ($all_linguas_line,
3296                             ("po/$_.po exists but \`$_' not in \`ALL_LINGUAS'"))
3297                 if ! $linguas{$_};
3298         }
3299
3300         foreach (keys %linguas)
3301         {
3302             &am_line_error ($all_linguas_line,
3303                             "$_ in \`ALL_LINGUAS' but po/$_.po does not exist")
3304                 if ! -f "po/$_.po";
3305         }
3306     }
3307     else
3308     {
3309         &am_error ("AM_GNU_GETTEXT in configure.in but \`ALL_LINGUAS' not defined");
3310     }
3311 }
3312
3313 # Handle footer elements.
3314 sub handle_footer
3315 {
3316     if ($contents{'SOURCES'})
3317     {
3318         # NOTE don't use define_pretty_variable here, because
3319         # $contents{...} is already defined.
3320         $output_vars .= 'SOURCES = ' . $contents{'SOURCES'} . "\n";
3321     }
3322     if ($contents{'OBJECTS'})
3323     {
3324         # NOTE don't use define_pretty_variable here, because
3325         # $contents{...} is already defined.
3326         $output_vars .= 'OBJECTS = ' . $contents{'OBJECTS'} . "\n";
3327     }
3328     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
3329     {
3330         $output_vars .= "\n";
3331     }
3332
3333     if (&variable_defined ('SUFFIXES'))
3334     {
3335         # Push actual suffixes, and not $(SUFFIXES).  Some versions of
3336         # make do not like variable substitutions on the .SUFFIXES
3337         # line.
3338         push (@suffixes, &variable_value_as_list ('SUFFIXES', ''));
3339     }
3340     if (&target_defined ('.SUFFIXES'))
3341     {
3342         &am_line_error ('.SUFFIXES',
3343                         "use variable \`SUFFIXES', not target \`.SUFFIXES'");
3344     }
3345
3346     # Note: AIX 4.1 /bin/make will fail if any suffix rule appears
3347     # before .SUFFIXES.  So we make sure that .SUFFIXES appears before
3348     # anything else, by sticking it right after the default: target.
3349     $output_header .= ".SUFFIXES:\n";
3350     if (@suffixes)
3351     {
3352
3353         # Make sure suffixes has unique elements.  Sort them to ensure
3354         # the output remains consistent.
3355         local (%suffixes);
3356
3357         grep ($suffixes{$_} = 1, @suffixes);
3358
3359         $output_header .= (".SUFFIXES: "
3360                            . join (' ', sort keys %suffixes)
3361                            . "\n");
3362     }
3363     $output_trailer .= &file_contents ('footer');
3364 }
3365
3366 # Deal with installdirs target.
3367 sub handle_installdirs
3368 {
3369     # GNU Makefile standards recommend this.
3370     $output_rules .= ("installdirs:"
3371                       . ($recursive_install
3372                          ? " installdirs-recursive\n"
3373                          : "\n"));
3374     push (@phony, 'installdirs');
3375     if (@installdirs)
3376     {
3377         &pretty_print_rule ("\t" . '$(mkinstalldirs) ', "\t\t",
3378                             @installdirs);
3379     }
3380     $output_rules .= "\n";
3381 }
3382
3383 # There are several targets which need to be merged.  This is because
3384 # their complete definition is compiled from many parts.  Note that we
3385 # avoid double colon rules, otherwise we'd use them instead.
3386 sub handle_merge_targets
3387 {
3388     local ($makefile) = @_;
3389
3390     # There are a few install-related variables that you should not define.
3391     local ($var);
3392     foreach $var ('PRE_INSTALL', 'POST_INSTALL', 'NORMAL_INSTALL')
3393     {
3394         if (&variable_defined ($var))
3395         {
3396             &am_line_error ($var, "\`$var' should not be defined");
3397         }
3398     }
3399
3400     # Put this at the beginning for the sake of non-GNU makes.  This
3401     # is still wrong if these makes can run parallel jobs.  But it is
3402     # right enough.
3403     unshift (@all, &basename ($makefile));
3404
3405     local ($one_name);
3406     foreach $one_name (@config_names)
3407     {
3408         push (@all, &basename ($one_name))
3409             if &dirname ($one_name) eq $relative_dir;
3410     }
3411
3412     &do_one_merge_target ('info', @info);
3413     &do_one_merge_target ('dvi', @dvi);
3414     &do_check_merge_target;
3415     &do_one_merge_target ('installcheck', @installcheck);
3416
3417     if (defined $options{'no-installinfo'})
3418     {
3419         # FIXME: this is kind of a hack; should find another way to
3420         # know that this is required.
3421         local (@dirs);
3422         if (grep ($_ eq 'install-info-am', @phony))
3423         {
3424             push (@dirs, 'install-info-am');
3425         }
3426         if (&variable_defined ('SUBDIRS'))
3427         {
3428             push (@dirs, 'install-info-recursive');
3429         }
3430         &do_one_merge_target ('install-info', @dirs);
3431     }
3432
3433     # Handle the various install targets specially.  We do this so
3434     # that (eg) "make install-exec" will run "install-exec-recursive"
3435     # if required, but "make install" won't run it twice.  Step one is
3436     # to see if the user specified local versions of any of the
3437     # targets we handle.  "all" is treated as one of these since
3438     # "install" can run it.
3439     push (@install_exec, 'install-exec-local')
3440         if defined $contents{'install-exec-local'};
3441     push (@install_data, 'install-data-local')
3442         if defined $contents{'install-data-local'};
3443     push (@uninstall, 'uninstall-local')
3444         if defined $contents{'uninstall-local'};
3445     local ($utarg);
3446     foreach $utarg ('uninstall-data-local', 'uninstall-data-hook',
3447                     'uninstall-exec-local', 'uninstall-exec-hook')
3448     {
3449         if (defined $contents{$utarg})
3450         {
3451             local ($x);
3452             ($x = $utarg) =~ s/(data|exec)-//;
3453             &am_line_error ($utarg, "use \`$x', not \`$utarg'");
3454         }
3455     }
3456     push (@all, 'all-local')
3457         if defined $contents{'all-local'};
3458
3459     if (defined $contents{'install-local'})
3460     {
3461         &am_line_error ('install-local',
3462                         "use \`install-data' or \`install-exec', not \`install'");
3463     }
3464
3465     # Step two: if we are doing recursive makes, write out the
3466     # appropriate rules.
3467     local (@install);
3468     if ($recursive_install)
3469     {
3470         push (@install, 'install-recursive');
3471
3472         if (@all)
3473         {
3474             local (@hackall) = ();
3475             local ($one_name);
3476             local ($local_headers) = '';
3477             foreach $one_name (@config_names)
3478             {
3479                 if (&dirname ($one_name) eq $relative_dir)
3480                 {
3481                     $local_headers .= ' ' if $local_headers;
3482                     $local_headers .= &basename ($one_name);
3483                 }
3484             }
3485             if ($local_headers)
3486             {
3487
3488                 # This is kind of a hack, but I couldn't see a better
3489                 # way to handle it.  In this particular case, we need
3490                 # to make sure config.h is built before we recurse.
3491                 # We can't do this by changing the order of
3492                 # dependencies to the "all" because that breaks when
3493                 # using parallel makes.  Instead we handle things
3494                 # explicitly.
3495                 $output_rules .= ("all-recursive-am: ${local_headers}"
3496                                   . "\n\t" . '$(MAKE) all-recursive'
3497                                   . "\n\n");
3498                 push (@hackall, 'all-recursive-am');
3499                 push (@phony, 'all-recursive-am');
3500             }
3501             else
3502             {
3503                 push (@hackall, 'all-recursive');
3504             }
3505
3506             $output_rules .= ('all-am: '
3507                               . join (' ', @all)
3508                               . "\n\n");
3509             @all = @hackall;
3510             push (@all, 'all-am');
3511             push (@phony, 'all-am');
3512         }
3513         else
3514         {
3515             @all = ('all-recursive');
3516
3517             # Must always generate `all-am' target, so it can be
3518             # referred to elsewhere.
3519             $output_rules .= "all-am:\n";
3520         }
3521         if (@install_exec)
3522         {
3523             $output_rules .= ('install-exec-am: '
3524                               . join (' ', @install_exec)
3525                               . "\n\n");
3526             @install_exec = ('install-exec-recursive', 'install-exec-am');
3527             push (@install, 'install-exec-am');
3528             push (@phony, 'install-exec-am');
3529         }
3530         else
3531         {
3532             @install_exec = ('install-exec-recursive');
3533         }
3534         if (@install_data)
3535         {
3536             $output_rules .= ('install-data-am: '
3537                               . join (' ', @install_data)
3538                               . "\n\n");
3539             @install_data = ('install-data-recursive', 'install-data-am');
3540             push (@install, 'install-data-am');
3541             push (@phony, 'install-data-am');
3542         }
3543         else
3544         {
3545             @install_data = ('install-data-recursive');
3546         }
3547         if (@uninstall)
3548         {
3549             $output_rules .= ('uninstall-am: '
3550                               . join (' ', @uninstall)
3551                               . "\n\n");
3552             @uninstall = ('uninstall-recursive', 'uninstall-am');
3553             push (@phony, 'uninstall-am');
3554         }
3555         else
3556         {
3557             @uninstall = ('uninstall-recursive');
3558         }
3559     }
3560
3561     # Step three: print definitions users can use.  Code below knows
3562     # that install-exec is done before install-data, beware.
3563     $output_rules .= ("install-exec: "
3564                       . join (' ', @install_exec)
3565                       . "\n");
3566     $output_rules .= "\t\@\$(NORMAL_INSTALL)\n";
3567     if (defined $contents{'install-exec-hook'})
3568     {
3569         $output_rules .= "\t" . '$(MAKE) install-exec-hook' . "\n";
3570     }
3571     $output_rules .= "\n";
3572     push (@install, 'install-exec') if !$recursive_install;
3573     push (@phony, 'install-exec');
3574
3575     $output_rules .= ("install-data: "
3576                       . join (' ', @install_data)
3577                       . "\n");
3578     $output_rules .= "\t\@\$(NORMAL_INSTALL)\n";
3579     if (defined $contents{'install-data-hook'})
3580     {
3581         $output_rules .= "\t" . '$(MAKE) install-data-hook' . "\n";
3582     }
3583     $output_rules .= "\n";
3584     push (@install, 'install-data') if !$recursive_install;
3585     push (@phony, 'install-data');
3586
3587     # If no dependencies for 'install', add 'all'.  Why?  That way
3588     # "make install" at top level of distclean'd distribution won't
3589     # fail because stuff in 'lib' fails to build.
3590     if (! @install || (scalar (@install) == 2
3591                        && $install[0] eq 'install-exec'
3592                        && $install[1] eq 'install-data'))
3593     {
3594         push (@install, 'all');
3595     }
3596     $output_rules .= ('install: '
3597                       . join (' ', @install)
3598                       # Use "@:" as empty command so nothing prints.
3599                       . "\n\t\@:"
3600                       . "\n\n"
3601                       . 'uninstall: '
3602                       . join (' ', @uninstall)
3603                       . "\n\n");
3604     push (@phony, 'install', 'uninstall');
3605
3606     $output_all = 'all: ' . join (' ', @all) . "\n\n";
3607     push (@phony, 'all');
3608
3609     # Generate the new 'install-strip' target.  Must set
3610     # INSTALL_SCRIPT to avoid stripping scripts.
3611     $output_rules .= ("install-strip:\n\t"
3612                       . '$(MAKE) INSTALL_PROGRAM=\'$(INSTALL_PROGRAM) -s\' INSTALL_SCRIPT=\'$(INSTALL_PROGRAM)\' install'
3613                       . "\n");
3614 }
3615
3616 # Helper for handle_merge_targets.
3617 sub do_one_merge_target
3618 {
3619     local ($name, @values) = @_;
3620
3621     if (defined $contents{$name . '-local'})
3622     {
3623         # User defined local form of target.  So include it.
3624         push (@values, $name . '-local');
3625         push (@phony, $name . '-local');
3626     }
3627
3628     &pretty_print_rule ($name . ":", "\t\t", @values);
3629     push (@phony, $name);
3630 }
3631
3632 # Handle check merge target specially.
3633 sub do_check_merge_target
3634 {
3635     if (defined $contents{'check-local'})
3636     {
3637         # User defined local form of target.  So include it.
3638         push (@check_tests, 'check-local');
3639         push (@phony, 'check-local');
3640     }
3641
3642     # In --cygnus mode, check doesn't depend on all.
3643     if (! $cygnus_mode)
3644     {
3645         if (! &variable_defined ('SUBDIRS'))
3646         {
3647             # 'check' must depend on `all', but not when doing
3648             # recursive build.
3649             unshift (@check, 'all');
3650         }
3651         else
3652         {
3653             # When subdirs are used, do the `all' build and then do
3654             # all the recursive stuff.  Actually use `all-am' because
3655             # it doesn't recurse; we rely on the check target in the
3656             # subdirs to do the required builds there.
3657             unshift (@check, 'all-am');
3658         }
3659     }
3660
3661     # The check target must depend on the local equivalent of `all',
3662     # to ensure all the primary targets are built.  Also it must
3663     # depend on the test code named in @check.
3664     &pretty_print_rule ('check:', "\t\t", @check);
3665
3666     # Now the check rules must explicitly run anything named in
3667     # @check_tests.  This is done via a separate make invocation to
3668     # avoid problems with parallel makes.  Every time I write code
3669     # like this I wonder: how could you invent a parallel make and not
3670     # provide any real synchronization facilities?  The official
3671     # answer is that you can push the dependencies out to the leaves
3672     # of the dependency tree.  That seems fairly gross to me.
3673     &pretty_print_rule ("\t\$(MAKE)", "\t  ", @check_tests)
3674         if @check_tests;
3675 }
3676
3677 # Handle all 'clean' targets.
3678 sub handle_clean
3679 {
3680     local ($xform) = '';
3681     local ($name);
3682
3683     # Don't include `MAINTAINER'; it is handled specially below.
3684     foreach $name ('MOSTLY', '', 'DIST')
3685     {
3686         if (! &variable_defined ($name . 'CLEANFILES'))
3687         {
3688             $xform .= 's/^' . $name . 'CLEAN.*$//;';
3689         }
3690         else
3691         {
3692             $xform .= 's/^' . $name . 'CLEAN//;';
3693         }
3694     }
3695
3696     # Built sources are automatically removed by maintainer-clean.
3697     push (@maintainer_clean_files, '$(BUILT_SOURCES')
3698         if &variable_defined ('BUILT_SOURCES');
3699     push (@maintainer_clean_files, '$(MAINTAINERCLEANFILES)')
3700         if &variable_defined ('MAINTAINERCLEANFILES');
3701     if (! @maintainer_clean_files)
3702     {
3703         $xform .= 's/^MAINTAINERCLEAN.*$//;';
3704     }
3705     else
3706     {
3707         $xform .= ('s/^MAINTAINERCLEAN//;'
3708                    # Join with no space to avoid spurious `test -z'
3709                    # success at runtime.
3710                    . 's,\@MCFILES\@,' . join ('', @maintainer_clean_files)
3711                    . ',;'
3712                    # A space is required in the join here.
3713                    . 's,\@MFILES\@,' . join (' ', @maintainer_clean_files)
3714                    . ',;');
3715     }
3716
3717     $output_rules .= &file_contents_with_transform ($xform, 'clean');
3718
3719     push (@clean, 'generic');
3720     &push_phony_cleaners ('generic');
3721
3722     local ($target) = $recursive_install ? 'clean-am' : 'clean';
3723     &do_one_clean_target ($target, 'mostly', '', @clean);
3724     &do_one_clean_target ($target, '', 'mostly', @clean);
3725     &do_one_clean_target ($target, 'dist', '', @clean);
3726     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
3727
3728     push (@phony, 'clean', 'mostlyclean', 'distclean', 'maintainer-clean');
3729
3730     local (@deps);
3731     if ($recursive_install)
3732     {
3733         # Do -recursive before -am.  If you aren't doing a parallel
3734         # make, this can be nicer.
3735         @deps = ('recursive', 'am');
3736         &do_one_clean_target ('', 'mostly', '', @deps);
3737         &do_one_clean_target ('', '', '', @deps);
3738         &do_one_clean_target ('', 'dist', '', @deps);
3739         &do_one_clean_target ('', 'maintainer-', '', @deps);
3740     }
3741 }
3742
3743 # Helper for handle_clean.
3744 sub do_one_clean_target
3745 {
3746     local ($target, $name, $last_name, @deps) = @_;
3747
3748     # Special case: if target not passed, then don't generate
3749     # dependency on next "lower" clean target (eg no
3750     # clean<-mostlyclean derivation).  In this case the target is
3751     # implicitly known to be 'clean'.
3752     local ($flag) = $target;
3753     $target = 'clean' if ! $flag;
3754
3755     grep (($_ = $name . 'clean-' . $_) && 0, @deps);
3756     if ($flag)
3757     {
3758         if ($last_name || $name ne 'mostly')
3759         {
3760             push (@deps, $last_name . $target);
3761         }
3762     }
3763
3764     # If a -local version of the rule is given, add it to the list.
3765     if (defined $contents{$name . $target . '-local'})
3766     {
3767         push (@deps, $name . $target . '-local');
3768     }
3769
3770     # Print the target and the dependencies.
3771     &pretty_print_rule ($name . $target . ": ", "\t\t", @deps);
3772
3773     # FIXME: shouldn't we really print these messages before running
3774     # the dependencies?
3775     if ($name . $target eq 'maintainer-clean')
3776     {
3777         # Print a special warning.
3778         $output_rules .=
3779             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
3780              . "\t\@echo \"it deletes files that may require special "
3781              . "tools to rebuild.\"\n");
3782
3783         $output_rules .= "\t-rm -f config.status\n"
3784             if $relative_dir eq '.';
3785     }
3786     elsif ($name . $target eq 'distclean')
3787     {
3788         $output_rules .= "\t-rm -f config.status\n";
3789         $output_rules .= "\t-rm -f libtool\n" if $seen_libtool;
3790     }
3791     $output_rules .= "\n";
3792 }
3793
3794 # Handle .PHONY target.
3795 sub handle_phony
3796 {
3797     &pretty_print_rule ('.PHONY:', "", @phony);
3798     $output_rules .= "\n";
3799 }
3800
3801 # Handle TESTS variable and other checks.
3802 sub handle_tests
3803 {
3804     if (defined $options{'dejagnu'})
3805     {
3806         push (@check_tests, 'check-DEJAGNU');
3807         push (@phony, 'check-DEJAGNU');
3808
3809         local ($xform);
3810         if ($cygnus_mode)
3811         {
3812             $xform = 's/^CYGNUS//;';
3813         }
3814         else
3815         {
3816             $xform = 's/^CYGNUS.*$//;';
3817         }
3818         $output_rules .= &file_contents_with_transform ($xform, 'dejagnu');
3819
3820         # In Cygnus mode, these are found in the build tree.
3821         # Otherwise they are looked for in $PATH.
3822         &define_program_variable ('EXPECT', 'build', 'expect', 'expect');
3823         &define_program_variable ('RUNTEST', 'src', 'dejagnu', 'runtest');
3824
3825         # Note that in the rule we don't directly generate site.exp to
3826         # avoid the possibility of a corrupted site.exp if make is
3827         # interrupted.  Jim Meyering has some useful text on this
3828         # topic.
3829         $output_rules .= ("site.exp: Makefile\n"
3830                           . "\t\@echo 'Making a new site.exp file...'\n"
3831                           . "\t-\@rm -f site.bak\n"
3832                           . "\t\@echo '## these variables are automatically generated by make ##' > \$\@-t\n"
3833                           . "\t\@echo '# Do not edit here.  If you wish to override these values' >> \$\@-t\n"
3834                           . "\t\@echo '# edit the last section' >> \$\@-t\n"
3835                           . "\t\@echo 'set tool \$(DEJATOOL)' >> \$\@-t\n"
3836                           . "\t\@echo 'set srcdir \$(srcdir)' >> \$\@-t\n"
3837                           . "\t\@echo 'set objdir' \`pwd\` >> \$\@-t\n");
3838
3839         # Extra stuff for AC_CANONICAL_*
3840         local (@whatlist) = ();
3841         if ($seen_canonical)
3842         {
3843             push (@whatlist, 'host')
3844         }
3845
3846         # Extra stuff only for AC_CANONICAL_SYSTEM.
3847         if ($seen_canonical == $AC_CANONICAL_SYSTEM)
3848         {
3849             push (@whatlist, 'target', 'build');
3850         }
3851
3852         local ($c1, $c2);
3853         foreach $c1 (@whatlist)
3854         {
3855             foreach $c2 ('alias', 'triplet')
3856             {
3857                 $output_rules .= "\t\@echo 'set ${c1}_${c2} \$(${c1}_${c2})' >> \$\@-t\n";
3858             }
3859         }
3860
3861         $output_rules .= ("\t\@echo '## All variables above are generated by configure. Do Not Edit ##' >> \$\@-t\n"
3862                           . "\t-\@sed '1,/^## All variables above are.*##/ d' site.bak >> \$\@-t\n"
3863                           . "\t-\@mv site.exp site.bak\n"
3864                           . "\t\@mv \$\@-t site.exp\n");
3865     }
3866     else
3867     {
3868         local ($c);
3869         foreach $c ('DEJATOOL', 'RUNTEST', 'RUNTESTFLAGS')
3870         {
3871             if (&variable_defined ($c))
3872             {
3873                 &am_line_error ($c, "\`$c' defined but \`dejagnu' not in \`AUTOMAKE_OPTIONS'");
3874             }
3875         }
3876     }
3877
3878     if (&variable_defined ('TESTS'))
3879     {
3880         push (@check_tests, 'check-TESTS');
3881         push (@phony, 'check-TESTS');
3882
3883         $output_rules .= 'check-TESTS: $(TESTS)
3884         @failed=0; all=0; \\
3885         srcdir=$(srcdir); export srcdir; \\
3886         for tst in $(TESTS); do \\
3887           if test -f $$tst; then dir=.; \\
3888           else dir="$(srcdir)"; fi; \\
3889           if $(TESTS_ENVIRONMENT) $$dir/$$tst; then \\
3890             all=`expr $$all + 1`; \\
3891             echo "PASS: $$tst"; \\
3892           elif test $$? -ne 77; then \\
3893             all=`expr $$all + 1`; \\
3894             failed=`expr $$failed + 1`; \\
3895             echo "FAIL: $$tst"; \\
3896           fi; \\
3897         done; \\
3898         if test "$$failed" -eq 0; then \\
3899           banner="All $$all tests passed"; \\
3900         else \\
3901           banner="$$failed of $$all tests failed"; \\
3902         fi; \\
3903         dashes=`echo "$$banner" | sed s/./=/g`; \\
3904         echo "$$dashes"; \\
3905         echo "$$banner"; \\
3906         echo "$$dashes"; \\
3907         test "$$failed" -eq 0
3908 ';
3909     }
3910 }
3911
3912 # Handle Emacs Lisp.
3913 sub handle_emacs_lisp
3914 {
3915     local (@elfiles) = &am_install_var ('lisp', 'LISP', 'lisp', 'noinst');
3916
3917     if (@elfiles)
3918     {
3919         # Found some lisp.
3920         &define_configure_variable ('lispdir');
3921         &define_configure_variable ('EMACS');
3922         $output_rules .= (".el.elc:\n"
3923                           . "\t\@echo 'WARNING: Warnings can be ignored. :-)'\n"
3924                           . "\tif test \$(EMACS) != no; then \\\n"
3925                           . "\t  EMACS=\$(EMACS) \$(SHELL) \$(srcdir)/elisp-comp \$<; \\\n"
3926                           . "\tfi\n");
3927         push (@suffixes, '.el', '.elc');
3928
3929         # Generate .elc files.
3930         grep ($_ .= 'c', @elfiles);
3931         &define_pretty_variable ('ELCFILES', '', @elfiles);
3932
3933         $output_rules .= &file_contents ('lisp-clean');
3934         push (@clean, 'lisp');
3935         &push_phony_cleaners ('lisp');
3936
3937         push (@all, '$(ELCFILES)');
3938
3939         local ($varname);
3940         if (&variable_defined ('lisp_LISP'))
3941         {
3942             $varname = 'lisp_LISP';
3943             &am_error ("\`lisp_LISP' defined but \`AM_PATH_LISPDIR' not in \`configure.in'")
3944                 if ! $seen_lispdir;
3945         }
3946         else
3947         {
3948             $varname = 'noinst_LISP';
3949         }
3950
3951         &require_file_with_line ($varname, $FOREIGN, 'elisp-comp');
3952     }
3953 }
3954
3955 # Handle Java.
3956 sub handle_java
3957 {
3958     local (@sourcelist) = &am_install_var ('-clean', 'java', 'JAVA',
3959                                            'java', 'noinst', 'check');
3960     return if ! @sourcelist;
3961
3962     &define_variable ('JAVAC', 'javac');
3963     &define_variable ('JAVACFLAGS', '');
3964     &define_variable ('CLASSPATH_ENV',
3965                       'CLASSPATH=$(JAVAROOT):$(srcdir)/$(JAVAROOT):$$CLASSPATH');
3966     &define_variable ('JAVAROOT', '$(top_builddir)');
3967
3968     local (%valid) = &am_primary_prefixes ('JAVA', 'java', 'noinst', 'check');
3969
3970     local ($dir, $curs);
3971     foreach $curs (keys %valid)
3972     {
3973         if (! &variable_defined ($curs . '_JAVA') || $curs eq 'noinst'
3974             || $curs eq 'EXTRA')
3975         {
3976             next;
3977         }
3978
3979         if (defined $dir)
3980         {
3981             print STDERR "got $curs\n";
3982             &am_line_error ($curs . '_JAVA',
3983                             "multiple _JAVA primaries in use");
3984         }
3985         $dir = $curs;
3986     }
3987
3988     $output_rules .= ('class' . $dir . '.stamp: $(' . $dir . '_JAVA)' . "\n"
3989                       . "\t" . '$(CLASSPATH_ENV) $(JAVAC) -d $(JAVAROOT) '
3990                       . '$(JAVACFLAGS) $?' . "\n"
3991                       . "\t" . 'echo timestamp > class' . $dir . '.stamp'
3992                       . "\n");
3993     push (@all, 'class' . $dir . '.stamp');
3994     &push_dist_common ('$(' . $dir . '_JAVA)');
3995 }
3996
3997 # Handle some of the minor options.
3998 sub handle_minor_options
3999 {
4000     if (defined $options{'readme-alpha'})
4001     {
4002         if ($relative_dir eq '.')
4003         {
4004             if ($package_version !~ /^$GNITS_VERSION_PATTERN$/)
4005             {
4006                 # FIXME: allow real filename.
4007                 &am_conf_line_error ('configure.in',
4008                                      $package_version_line,
4009                                      "version \`$package_version' doesn't follow Gnits standards");
4010             }
4011             elsif (defined $1 && -f 'README-alpha')
4012             {
4013                 # This means we have an alpha release.  See
4014                 # GNITS_VERSION_PATTERN for details.
4015                 &require_file ($FOREIGN, 'README-alpha');
4016             }
4017         }
4018     }
4019 }
4020
4021 ################################################################
4022
4023 # Scan one file for interesting things.  Subroutine of scan_configure.
4024 sub scan_one_configure_file
4025 {
4026     local ($filename) = @_;
4027
4028     open (CONFIGURE, $filename)
4029         || die "automake: couldn't open \`$filename': $!\n";
4030     print "automake: reading $filename\n" if $verbose;
4031
4032     while (<CONFIGURE>)
4033     {
4034         # Remove comments from current line.
4035         s/\bdnl\b.*$//;
4036         s/\#.*$//;
4037
4038         # Skip macro definitions.  Otherwise we might be confused into
4039         # thinking that a macro that was only defined was actually
4040         # used.
4041         next if /AC_DEFUN/;
4042
4043         # Follow includes.  This is a weirdness commonly in use at
4044         # Cygnus and hopefully nowhere else.
4045         if (/sinclude\((.*)\)/ && -f $1)
4046         {
4047             &scan_one_configure_file ($1);
4048         }
4049
4050         # Populate libobjs array.
4051         if (/AC_FUNC_ALLOCA/)
4052         {
4053             $libsources{'alloca.c'} = 1;
4054         }
4055         elsif (/AC_FUNC_GETLOADAVG/)
4056         {
4057             $libsources{'getloadavg.c'} = 1;
4058         }
4059         elsif (/AC_FUNC_MEMCMP/)
4060         {
4061             $libsources{'memcmp.c'} = 1;
4062         }
4063         elsif (/AC_STRUCT_ST_BLOCKS/)
4064         {
4065             $libsources{'fileblocks.c'} = 1;
4066         }
4067         elsif (/A[CM]_REPLACE_GNU_GETOPT/)
4068         {
4069             $libsources{'getopt.c'} = 1;
4070             $libsources{'getopt1.c'} = 1;
4071         }
4072         elsif (/AM_FUNC_STRTOD/)
4073         {
4074             $libsources{'strtod.c'} = 1;
4075         }
4076         elsif (/AM_WITH_REGEX/)
4077         {
4078             $libsources{'rx.c'} = 1;
4079             $libsources{'rx.h'} = 1;
4080             $libsources{'regex.c'} = 1;
4081             $libsources{'regex.h'} = 1;
4082             $omit_dependencies{'rx.h'} = 1;
4083             $omit_dependencies{'regex.h'} = 1;
4084         }
4085         elsif (/AM_FUNC_MKTIME/)
4086         {
4087             $libsources{'mktime.c'} = 1;
4088         }
4089         elsif (/AM_FUNC_ERROR_AT_LINE/)
4090         {
4091             $libsources{'error.c'} = 1;
4092             $libsources{'error.h'} = 1;
4093         }
4094         elsif (/AM_FUNC_OBSTACK/)
4095         {
4096             $libsources{'obstack.c'} = 1;
4097             $libsources{'obstack.h'} = 1;
4098         }
4099         elsif (/LIBOBJS="(.*)\s+\$LIBOBJS"/
4100                || /LIBOBJS="\$LIBOBJS\s+(.*)"/)
4101         {
4102             foreach $libobj_iter (split (' ', $1))
4103             {
4104                 if ($libobj_iter =~ /^(.*)\.o$/)
4105                 {
4106                     $libsources{$1 . '.c'} = 1;
4107                 }
4108             }
4109         }
4110
4111         if (! $in_ac_replace && s/AC_REPLACE_FUNCS\s*\(\[?//)
4112         {
4113             $in_ac_replace = 1;
4114         }
4115         if ($in_ac_replace)
4116         {
4117             $in_ac_replace = 0 if s/[\]\)].*$//;
4118             # Remove trailing backslash.
4119             s/\\$//;
4120             foreach (split)
4121             {
4122                 # Need to skip empty elements for Perl 4.
4123                 next if $_ eq '';
4124                 $libsources{$_ . '.c'} = 1;
4125             }
4126         }
4127
4128         if (/$obsolete_rx/o)
4129         {
4130             local ($hint) = '';
4131             if ($obsolete_macros{$1})
4132             {
4133                 $hint = '; ' . $obsolete_macros{$1};
4134             }
4135             &am_conf_line_error ($filename, $., "\`$1' is obsolete$hint");
4136         }
4137
4138         # Process the AC_OUTPUT macro.
4139         if (! $in_ac_output && s/AC_OUTPUT\s*\(\[?//)
4140         {
4141             $in_ac_output = 1;
4142             $ac_output_line = $.;
4143         }
4144         if ($in_ac_output)
4145         {
4146             local ($closing) = 0;
4147             if (s/[\]\),].*$//)
4148             {
4149                 $in_ac_output = 0;
4150                 $closing = 1;
4151             }
4152
4153             # Look at potential Makefile.am's.
4154             foreach (split)
4155             {
4156                 # Must skip empty string for Perl 4.
4157                 next if $_ eq "\\" || $_ eq '';
4158
4159                 # Handle $local:$input syntax.  Note that we ignore
4160                 # every input file past the first, though we keep
4161                 # those around for later.
4162                 local ($local, $input, @rest) = split (/:/);
4163                 if (! $input)
4164                 {
4165                     $input = $local;
4166                 }
4167                 else
4168                 {
4169                     # FIXME: should be error if .in is missing.
4170                     $input =~ s/\.in$//;
4171                 }
4172
4173                 if (-f $input . '.am')
4174                 {
4175                     # We have a file that automake should generate.
4176                     push (@make_input_list, $input);
4177                     $make_list{$input} = join (':', ($local, @rest));
4178                 }
4179                 else
4180                 {
4181                     # We have a file that automake should cause to be
4182                     # rebuilt, but shouldn't generate itself.
4183                     push (@other_input_files, $_);
4184                 }
4185             }
4186
4187             if ($closing && @make_input_list == 0 && @other_input_files == 0)
4188             {
4189                 &am_conf_line_error ($filename, $ac_output_line,
4190                                      "No files mentioned in \`AC_OUTPUT'");
4191                 exit 1;
4192             }
4193         }
4194
4195         if (/$AC_CONFIG_AUX_DIR_PATTERN/o)
4196         {
4197             @config_aux_path = $1;
4198         }
4199
4200         # Check for ansi2knr.
4201         $am_c_prototypes = 1 if /AM_C_PROTOTYPES/;
4202
4203         # Check for exe extension stuff.
4204         if (/AM_EXEEXT/)
4205         {
4206             $seen_exeext = 1;
4207             $configure_vars{'EXEEXT'} = 1;
4208         }
4209
4210         # Check for NLS support.
4211         if (/AM_GNU_GETTEXT/)
4212         {
4213             $seen_gettext = 1;
4214             $ac_gettext_line = $.;
4215             $omit_dependencies{'libintl.h'} = 1;
4216         }
4217
4218         # Look for ALL_LINGUAS.
4219         if (/ALL_LINGUAS="(.*)"$/ || /ALL_LINGUAS=(.*)$/)
4220         {
4221             $seen_linguas = 1;
4222             $all_linguas = $1;
4223             $all_linguas_line = $.;
4224         }
4225
4226         # Handle configuration headers.  A config header of `[$1]'
4227         # means we are actually scanning AM_CONFIG_HEADER from
4228         # aclocal.m4.
4229         if (/A([CM])_CONFIG_HEADER\s*\((.*)\)/
4230             && $2 ne '[$1]')
4231         {
4232             &am_conf_line_error
4233                 ($filename, $.,
4234                  "\`AC_CONFIG_HEADER' is obsolete; use \`AM_CONFIG_HEADER'")
4235                     if $1 eq 'C';
4236
4237             $config_header_line = $.;
4238             local ($one_hdr);
4239             foreach $one_hdr (split (' ', $2))
4240             {
4241                 push (@config_fullnames, $one_hdr);
4242                 if ($one_hdr =~ /^([^:]+):(.+)$/)
4243                 {
4244                     push (@config_names, $1);
4245                     push (@config_headers, $2);
4246                 }
4247                 else
4248                 {
4249                     push (@config_names, $one_hdr);
4250                     push (@config_headers, $one_hdr . '.in');
4251                 }
4252             }
4253         }
4254
4255         # Handle AC_CANONICAL_*.  Always allow upgrading to
4256         # AC_CANONICAL_SYSTEM, but never downgrading.
4257         $seen_canonical = $AC_CANONICAL_HOST
4258             if ! $seen_canonical
4259                 && (/AC_CANONICAL_HOST/ || /AC_CHECK_TOOL/);
4260         $seen_canonical = $AC_CANONICAL_SYSTEM if /AC_CANONICAL_SYSTEM/;
4261
4262         $seen_path_xtra = 1 if /AC_PATH_XTRA/;
4263
4264         # This macro handles several different things.
4265         if (/$AM_INIT_AUTOMAKE_PATTERN/o)
4266         {
4267             $seen_make_set = 1;
4268             $seen_package = 1;
4269             $seen_version = 1;
4270             $seen_arg_prog = 1;
4271             $seen_prog_install = 2;
4272             ($package_version = $1) =~ s/$AM_PACKAGE_VERSION_PATTERN/$1/o;
4273             $package_version_line = $.;
4274         }
4275
4276         # This is like AM_INIT_AUTOMAKE, but doesn't actually set the
4277         # package and version number.  (This might change in the
4278         # future).  Yes, I'm not above hacking Automake so it works
4279         # well with other GNU tools -- that is actually the point.
4280         if (/AM_INIT_GUILE_MODULE/)
4281         {
4282             $seen_make_set = 1;
4283             $seen_package = 1;
4284             $seen_version = 1;
4285             $seen_arg_prog = 1;
4286             $seen_prog_install = 2;
4287             @config_aux_path = ('..');
4288         }
4289
4290         # Some things required by Automake.
4291         $seen_make_set = 1 if /AC_PROG_MAKE_SET/;
4292         $seen_arg_prog = 1 if /AC_ARG_PROGRAM/;
4293
4294         if (/AM_PROG_LEX/)
4295         {
4296             $configure_vars{'LEX'} = 1;
4297             $seen_decl_yytext = 1;
4298         }
4299         if (/AC_DECL_YYTEXT/ && $filename =~ /configure\.in$/)
4300         {
4301             &am_conf_line_warning ($filename, $.,
4302                                    "\`AC_DECL_YYTEXT' is obsolete; use \`AM_PROG_LEX'");
4303         }
4304         if (/AC_PROG_LEX/ && $filename =~ /configure\.in$/)
4305         {
4306             &am_conf_line_warning ($filename, $.,
4307                                    "\`AC_PROG_LEX' is obsolete; use \`AM_PROG_LEX'");
4308         }
4309
4310         if (/AC_PROG_(YACC|RANLIB|CC|CXXCPP|CXX|LEX|AWK|CPP|LN_S)/)
4311         {
4312             $configure_vars{$1} = 1;
4313         }
4314         if (/$AC_CHECK_PATTERN/o)
4315         {
4316             $configure_vars{$3} = 1;
4317         }
4318         if (/$AM_MISSING_PATTERN/o
4319             && $1 ne 'ACLOCAL'
4320             && $1 ne 'AUTOCONF'
4321             && $1 ne 'AUTOMAKE'
4322             && $1 ne 'AUTOHEADER')
4323         {
4324             $configure_vars{$1} = 1;
4325         }
4326
4327         # Explicitly avoid ANSI2KNR -- we AC_SUBST that in protos.m4,
4328         # but later define it elsewhere.  This is pretty hacky.  We
4329         # also explicitly avoid INSTALL_SCRIPT and some other
4330         # variables because they are defined in header-vars.am.
4331         # FIXME.
4332         if (/$AC_SUBST_PATTERN/o
4333             && $1 ne 'ANSI2KNR'
4334             && $1 ne 'INSTALL_SCRIPT'
4335             && $1 ne 'INSTALL_DATA')
4336         {
4337             $configure_vars{$1} = 1;
4338         }
4339
4340         $seen_decl_yytext = 1 if /AC_DECL_YYTEXT/;
4341         $seen_maint_mode = 1 if /AM_MAINTAINER_MODE/;
4342         $seen_package = 1 if /PACKAGE=/;
4343
4344         # Skip VERSION of [$2]; that is from AM_INIT_AUTOMAKE.
4345         if (/\bVERSION=(\S+)/ && $1 ne '[$2]')
4346         {
4347             $seen_version = 1;
4348             $package_version = $1;
4349             $package_version_line = $.;
4350         }
4351         elsif (/VERSION=/)
4352         {
4353             $seen_version = 1;
4354         }
4355
4356         # Weird conditionals here because it is always allowed to
4357         # upgrade to AM_PROG_INSTALL but never to downgrade to
4358         # AC_PROG_INSTALL.
4359         $seen_prog_install = 1 if ! $seen_prog_install && /AC_PROG_INSTALL/;
4360         $seen_prog_install = 2 if /AM_PROG_INSTALL/;
4361
4362         $seen_lispdir = 1 if /AM_PATH_LISPDIR/;
4363
4364         if (/AM_PROG_LIBTOOL/)
4365         {
4366             $seen_libtool = 1;
4367             $libtool_line = $.;
4368             $configure_vars{'LIBTOOL'} = 1;
4369             $configure_vars{'RANLIB'} = 1;
4370             $configure_vars{'CC'} = 1;
4371             # AM_PROG_LIBTOOL runs AC_CANONICAL_HOST.  Make sure we
4372             # never downgrade (if we've seen AC_CANONICAL_SYSTEM).
4373             $seen_canonical = $AC_CANONICAL_HOST if ! $seen_canonical;
4374         }
4375
4376         if (/$AM_CONDITIONAL_PATTERN/o)
4377         {
4378             $configure_cond{$1} = 1;
4379         }
4380     }
4381
4382     close (CONFIGURE);
4383 }
4384
4385 # Scan configure.in and aclocal.m4 for interesting things.  We must
4386 # scan aclocal.m4 because there might be AC_SUBSTs and such there.
4387 sub scan_configure
4388 {
4389     # Reinitialize libsources here.  This isn't really necessary,
4390     # since we currently assume there is only one configure.in.  But
4391     # that won't always be the case.
4392     %libsources = ();
4393
4394     local ($in_ac_output, $in_ac_replace) = (0, 0);
4395     local (%make_list, @make_input_list);
4396     local ($libobj_iter);
4397
4398     &scan_one_configure_file ('configure.in');
4399     &scan_one_configure_file ('aclocal.m4')
4400         if -f 'aclocal.m4';
4401
4402     # Set input and output files if not specified by user.
4403     if (! @input_files)
4404     {
4405         @input_files = @make_input_list;
4406         %output_files = %make_list;
4407     }
4408
4409     &am_conf_error ("\`PACKAGE' not defined in configure.in")
4410         if ! $seen_package;
4411     &am_conf_error ("\`VERSION' not defined in configure.in")
4412         if ! $seen_version;
4413
4414     # Look for some files we need.  Always check for these.  This
4415     # check must be done for every run, even those where we are only
4416     # looking at a subdir Makefile.  We must set relative_dir so that
4417     # the file-finding machinery works.
4418     local ($relative_dir) = '.';
4419     &require_config_file ($FOREIGN, 'install-sh', 'mkinstalldirs', 'missing');
4420     &am_error ("\`install.sh' is an anachronism; use \`install-sh' instead")
4421         if -f $config_aux_path[0] . '/install.sh';
4422 }
4423
4424 ################################################################
4425
4426 # Set up for Cygnus mode.
4427 sub check_cygnus
4428 {
4429     return unless $cygnus_mode;
4430
4431     &set_strictness ('foreign');
4432     $options{'no-installinfo'} = 1;
4433     $options{'no-dependencies'} = 1;
4434     $use_dependencies = 0;
4435
4436     if (! $seen_maint_mode)
4437     {
4438         &am_conf_error ("\`AM_MAINTAINER_MODE' required when --cygnus specified");
4439     }
4440
4441     if (! $seen_exeext)
4442     {
4443         &am_conf_error ("\`AM_EXEEXT' required when --cygnus specified");
4444     }
4445 }
4446
4447 # Do any extra checking for GNU standards.
4448 sub check_gnu_standards
4449 {
4450     if ($relative_dir eq '.')
4451     {
4452         # In top level (or only) directory.
4453         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
4454                        'AUTHORS', 'ChangeLog');
4455     }
4456
4457     if ($strictness >= $GNU)
4458     {
4459         if (defined $options{'no-installman'})
4460         {
4461             &am_line_error ('AUTOMAKE_OPTIONS',
4462                             "option \`no-installman' disallowed by GNU standards");
4463         }
4464
4465         if (defined $options{'no-installinfo'})
4466         {
4467             &am_line_error ('AUTOMAKE_OPTIONS',
4468                             "option \`no-installinfo' disallowed by GNU standards");
4469         }
4470     }
4471 }
4472
4473 # Do any extra checking for GNITS standards.
4474 sub check_gnits_standards
4475 {
4476     if ($strictness >= $GNITS)
4477     {
4478         if (-f $relative_dir . '/COPYING.LIB')
4479         {
4480             &am_error ("\`${relative_dir}/COPYING.LIB' disallowed by Gnits standards");
4481         }
4482     }
4483
4484     if ($relative_dir eq '.')
4485     {
4486         # In top level (or only) directory.
4487         &require_file ($GNITS, 'THANKS');
4488     }
4489 }
4490
4491 ################################################################
4492
4493 # Pretty-print something.  HEAD is what should be printed at the
4494 # beginning of the first line, FILL is what should be printed at the
4495 # beginning of every subsequent line.
4496 sub pretty_print_internal
4497 {
4498     local ($head, $fill, @values) = @_;
4499
4500     local ($column) = length ($head);
4501     local ($result) = $head;
4502
4503     # Fill length is number of characters.  However, each Tab
4504     # character counts for eight.  So we count the number of Tabs and
4505     # multiply by 7.
4506     local ($fill_length) = length ($fill);
4507     $fill_length += 7 * ($fill =~ tr/\t/\t/d);
4508
4509     local ($bol) = ($head eq '');
4510     foreach (@values)
4511     {
4512         # "71" because we also print a space.
4513         if ($column + length ($_) > 71)
4514         {
4515             $result .= " \\\n" . $fill;
4516             $column = $fill_length;
4517             $bol = 1;
4518         }
4519
4520         $result .= ' ' unless ($bol);
4521         $result .= $_;
4522         $column += length ($_) + 1;
4523         $bol = 0;
4524     }
4525
4526     $result .= "\n";
4527     return $result;
4528 }
4529
4530 # Pretty-print something and append to output_vars.
4531 sub pretty_print
4532 {
4533     $output_vars .= &pretty_print_internal (@_);
4534 }
4535
4536 # Pretty-print something and append to output_rules.
4537 sub pretty_print_rule
4538 {
4539     $output_rules .= &pretty_print_internal (@_);
4540 }
4541
4542
4543 ################################################################
4544
4545 # See if a target exists.
4546 sub target_defined
4547 {
4548     local ($target) = @_;
4549     return defined $targets{$target};
4550 }
4551
4552 # See if two conditionals are the same.
4553 sub conditional_same
4554 {
4555     local ($cond1, $cond2) = @_;
4556
4557     return (&conditional_true_when ($cond1, $cond2)
4558             && &conditional_true_when ($cond2, $cond1));
4559 }
4560
4561 # See if a conditional is true.  Both arguments are conditional
4562 # strings.  This returns true if the first conditional is true when
4563 # the second conditional is true.
4564 sub conditional_true_when
4565 {
4566     local ($cond, $when) = @_;
4567
4568     # Check the easy case first.
4569     if ($cond eq $when)
4570     {
4571         return 1;
4572     }
4573
4574     # Check each component of $cond, which looks @COND1@@COND2@.
4575     foreach $comp (split ('@', $cond))
4576     {
4577         # The way we split will give null strings between each
4578         # condition.
4579         next if ! $comp;
4580
4581         if (index ($when, '@' . $comp . '@') == -1)
4582         {
4583             return 0;
4584         }
4585     }
4586
4587     return 1;
4588 }
4589
4590 # Check for an ambiguous conditional.  This is called when a variable
4591 # or target is being defined conditionally.  If we already know about
4592 # a definition that is true under the same conditions, then we have an
4593 # ambiguity.
4594 sub check_ambiguous_conditional
4595 {
4596     local ($var_name, $cond) = @_;
4597     local (@cond_vals) = split (' ', $conditional{$var_name});
4598     while (@cond_vals)
4599     {
4600         local ($vcond) = shift (@cond_vals);
4601         shift (@cond_vals);
4602         if (&conditional_true_when ($vcond, $cond)
4603             || &conditional_true_when ($cond, $vcond))
4604         {
4605             &am_line_error ($var_name,
4606                             "$var_name multiply defined in condition");
4607         }
4608     }
4609 }
4610
4611 # See if a variable exists.  The first argument is the variable name,
4612 # and the optional second argument is the condition which we should
4613 # check.  If no condition is given, we currently return true if the
4614 # variable is defined under any condition.
4615 sub variable_defined
4616 {
4617     local ($var, $cond) = @_;
4618     if (defined $targets{$var})
4619     {
4620         &am_line_error ($var, "\`$var' is target; expected variable");
4621         return 0;
4622     }
4623     elsif (defined $contents{$var})
4624     {
4625         if ($cond && $conditional{$var})
4626         {
4627             # We have been asked to check for a particular condition,
4628             # and the variable is defined conditionally.  We need to
4629             # look through the conditions under which the variable is
4630             # defined, and see if any of them match the conditional we
4631             # have been asked to check.
4632             local (@cond_vars) = split (' ', $conditional{$var});
4633             while (@cond_vars)
4634             {
4635                 if (&conditional_same ($cond, shift (@cond_vars)))
4636                 {
4637                     # Even a conditional examination is good enough
4638                     # for us.  FIXME: really should maintain examined
4639                     # status on a per-condition basis.
4640                     $content_seen{$var} = 1;
4641                     return 1;
4642                 }
4643                 shift (@cond_vars);
4644             }
4645
4646             # The variable is not defined for the given condition.
4647             return 0;
4648         }
4649
4650         $content_seen{$var} = 1;
4651         return 1;
4652     }
4653     return 0;
4654 }
4655
4656 # Mark a variable as examined.
4657 sub examine_variable
4658 {
4659     local ($var) = @_;
4660     &variable_defined ($var);
4661 }
4662
4663 # Quote a value in order to put it in $conditional.  We need to quote
4664 # spaces, and we need to handle null strings, so that we can later
4665 # retrieve values by splitting on space.
4666 sub quote_cond_val
4667 {
4668     local ($val) = @_;
4669     $val =~ s/ /\001/g;
4670     $val = '\002' if $val eq '';
4671     return $val;
4672 }
4673
4674 # Unquote a value in $conditional.
4675 sub unquote_cond_val
4676 {
4677     local ($val) = @_;
4678     $val =~ s/\001/ /g;
4679     $val = '' if $val eq '\002';
4680     return $val;
4681 }
4682
4683 # Return the set of conditions for which a variable is defined.
4684
4685 # If the variable is not defined conditionally, and is not defined in
4686 # terms of any variables which are defined conditionally, then this
4687 # returns the empty list.
4688
4689 # If the variable is defined conditionally, but is not defined in
4690 # terms of any variables which are defined conditionally, then this
4691 # returns the list of conditions for which the variable is defined.
4692
4693 # If the variable is defined in terms of any variables which are
4694 # defined conditionally, then this returns a full set of permutations
4695 # of the subvariable conditions.  For example, if the variable is
4696 # defined in terms of a variable which is defined for @COND_TRUE@,
4697 # then this returns both @COND_TRUE@ and @COND_FALSE@.  This is
4698 # because we will need to define the variable under both conditions.
4699
4700 sub variable_conditions
4701 {
4702     local ($var) = @_;
4703     local (%uniqify);
4704     local ($cond);
4705
4706     foreach $cond (&variable_conditions_sub ($var, '', ()))
4707     {
4708         $uniqify{$cond} = 1;
4709     }
4710
4711     return keys %uniqify;
4712 }
4713
4714 # A subroutine of variable_conditions.  We only return conditions
4715 # which are true for all the conditions in @PARENT_CONDS.
4716 sub variable_conditions_sub
4717 {
4718     local ($var, $parent, @parent_conds) = @_;
4719     local (@new_conds) = ();
4720
4721     if (! $conditional{$var})
4722     {
4723         foreach (split (' ', $contents{$var}))
4724         {
4725             # If a comment seen, just leave.
4726             last if /^#/;
4727
4728             # Handle variable substitutions.
4729             if (/^\$\{(.*)\}$/ || /^\$\((.*)\)$/)
4730             {
4731                 push (@new_conds,
4732                       &variable_conditions_sub ($1, $var, @parent_conds));
4733             }
4734         }
4735
4736         return &variable_conditions_reduce (@new_conds);
4737     }
4738
4739     local (@this_conds) = ();
4740     local (@condvals) = split (' ', $conditional{$var});
4741     while (@condvals)
4742     {
4743         local ($cond) = shift (@condvals);
4744         local ($val) = &unquote_cond_val (shift (@condvals));
4745
4746         if (@parent_conds)
4747         {
4748             local ($ok) = 1;
4749             local ($parent_cond);
4750             foreach $parent_cond (@parent_conds)
4751             {
4752                 if (! &conditional_true_when ($parent_cond, $cond))
4753                 {
4754                     $ok = 0;
4755                     last;
4756                 }
4757             }
4758
4759             next if ! $ok;
4760         }
4761
4762         push (@this_conds, $cond);
4763
4764         push (@parent_conds, $cond);
4765         local (@subvar_conds) = ();
4766         foreach (split (' ', $val))
4767         {
4768             # If a comment seen, just leave.
4769             last if /^#/;
4770
4771             # Handle variable substitutions.
4772             if (/^\$\{(.*)\}$/ || /^\$\((.*)\)$/)
4773             {
4774                 push (@subvar_conds,
4775                       &variable_conditions_sub ($1, $var, @parent_conds));
4776             }
4777         }
4778         pop (@parent_conds);
4779
4780         # If there are no conditional subvariables, then we want to
4781         # return this condition.  Otherwise, we want to return the
4782         # permutations of the subvariables.
4783         if (! @subvar_conds)
4784         {
4785             push (@new_conds, $cond);
4786         }
4787         else
4788         {
4789             push (@new_conds, &variable_conditions_reduce (@subvar_conds));
4790         }
4791     }
4792
4793     return @new_conds
4794         if ! $parent;
4795
4796     # If we are being called on behalf of another variable, we need to
4797     # return all possible permutations of the conditions.  We have
4798     # already handled everything in @this_conds along with their
4799     # subvariables.  We now need to add any permutations that are not
4800     # in @this_conds.
4801     local ($this_cond);
4802     foreach $this_cond (@this_conds)
4803     {
4804         local (@perms) =
4805             &variable_conditions_permutations (split('@', $this_cond));
4806         local ($perm);
4807         foreach $perm (@perms)
4808         {
4809             local ($scan);
4810             local ($ok) = 1;
4811             foreach $scan (@this_conds)
4812             {
4813                 if (&conditional_true_when ($perm, $scan)
4814                     || &conditional_true_when ($scan, $perm))
4815                 {
4816                     $ok = 0;
4817                     last;
4818                 }
4819             }
4820             next if ! $ok;
4821
4822             if (@parent_conds)
4823             {
4824                 local ($ok) = 1;
4825                 local ($parent_cond);
4826                 foreach $parent_cond (@parent_conds)
4827                 {
4828                     if (! &conditional_true_when ($parent_cond, $perm))
4829                     {
4830                         $ok = 0;
4831                         last;
4832                     }
4833                 }
4834
4835                 next if ! $ok;
4836             }
4837
4838             # This permutation was not already handled, and is valid
4839             # for the parents.
4840             push (@new_conds, $perm);
4841         }
4842     }
4843
4844     return @new_conds;
4845 }
4846
4847 # Subroutine for variable_conditions_sort
4848 sub variable_conditions_cmp
4849 {
4850     local ($as) = $a;
4851     $as =~ s/[^@]//g;
4852     local ($bs) = $b;
4853     $bs =~ s/[^@]//g;
4854     return (length ($as) <=> length ($bs)
4855             || $a cmp $b);
4856 }
4857
4858 # Sort a list of conditionals so that only the exclusive ones are
4859 # retained.  For example, if both @COND1_TRUE@@COND2_TRUE@ and
4860 # @COND1_TRUE@ are in the list, discard the latter.
4861 sub variable_conditions_reduce
4862 {
4863     local (@conds) = @_;
4864     local (@ret) = ();
4865     local ($cond);
4866     foreach $cond (sort variable_conditions_cmp @conds)
4867     {
4868         local ($ok) = 1;
4869         local ($scan);
4870         foreach $scan (@ret)
4871         {
4872             if (&conditional_true_when ($cond, $scan))
4873             {
4874                 $ok = 0;
4875                 last;
4876             }
4877         }
4878         next if ! $ok;
4879         push (@ret, $cond);
4880     }
4881
4882     return @ret;
4883 }
4884
4885 # Return a list of permutations of a conditional string.
4886 sub variable_conditions_permutations
4887 {
4888     local (@comps) = @_;
4889     return ()
4890         if ! @comps;
4891     local ($comp) = shift (@comps);
4892     return &variable_conditions_permutations (@comps)
4893         if $comp eq '';
4894     local ($neg) = $comp;
4895     $neg =~ s/TRUE$/TRUEO/;
4896     $neg =~ s/FALSE$/TRUE/;
4897     $neg =~ s/TRUEO$/FALSE/;
4898     local (@ret);
4899     local ($sub);
4900     foreach $sub (&variable_conditions_permutations (@comps))
4901     {
4902         push (@ret, '@' . $comp . '@' . $sub);
4903         push (@ret, '@' . $neg . '@' . $sub);
4904     }
4905     if (! @ret)
4906     {
4907         push (@ret, '@' . $comp . '@');
4908         push (@ret, '@' . $neg . '@');
4909     }
4910     return @ret;
4911 }
4912
4913 # Warn if a variable is conditionally defined.  This is called if we
4914 # are using the value of a variable.
4915 sub variable_conditionally_defined
4916 {
4917     local ($var, $parent) = @_;
4918     if ($conditional{$var})
4919     {
4920         if ($parent)
4921         {
4922             &am_line_error ($parent,
4923                             "warning: automake does not support conditional definition of $var in $parent");
4924         }
4925         else
4926         {
4927             &am_line_error ($parent,
4928                             "warning: automake does not support $var being defined conditionally")
4929         }
4930     }
4931 }
4932
4933 # Get the value of a variable.  This just returns $contents, but warns
4934 # if the variable is conditionally defined.
4935 sub variable_value
4936 {
4937     local ($var) = @_;
4938     &variable_conditionally_defined ($var);
4939     return $contents{$var};
4940 }
4941
4942 # Convert a variable value to a list, split as whitespace.  This will
4943 # recursively follow $(...) and ${...} inclusions.  It preserves @...@
4944 # substitutions.  If COND is 'all', then all values under all
4945 # conditions should be returned; if COND is a particular condition
4946 # (all conditions are surrounded by @...@) then only the value for
4947 # that condition should be returned; otherwise, warn if VAR is
4948 # conditionally defined.
4949 sub value_to_list
4950 {
4951     local ($var, $val, $cond) = @_;
4952     local (@result);
4953
4954     foreach (split (' ', $val))
4955     {
4956         # If a comment seen, just leave.
4957         last if /^#/;
4958
4959         # Handle variable substitutions.
4960         if (/^\$\{([^}]*)\}$/ || /^\$\(([^)]*)\)$/)
4961         {
4962             local ($varname) = $1;
4963
4964             # If the user uses a losing variable name, just ignore it.
4965             # This isn't ideal, but people have requested it.
4966             next if ($varname =~ /\@.*\@/);
4967
4968             local ($from, $to);
4969             local (@temp_list);
4970             if ($varname =~ /^([^:]*):([^=]*)=(.*)$/)
4971             {
4972                 $varname = $1;
4973                 $to = $3;
4974                 ($from = $2) =~ s/(\W)/\\$1/g;
4975             }
4976
4977             # Find the value.
4978             @temp_list = &variable_value_as_list ($1, $cond, $var);
4979
4980             # Now rewrite the value if appropriate.
4981             if ($from)
4982             {
4983                 grep (s/$from$/$to/, @temp_list);
4984             }
4985
4986             push (@result, @temp_list);
4987         }
4988         else
4989         {
4990             push (@result, $_);
4991         }
4992     }
4993
4994     return @result;
4995 }
4996
4997 # Return contents of variable as list, split as whitespace.  This will
4998 # recursively follow $(...) and ${...} inclusions.  It preserves @...@
4999 # substitutions.  If COND is 'all', then all values under all
5000 # conditions should be returned; if COND is a particular condition
5001 # (all conditions are surrounded by @...@) then only the value for
5002 # that condition should be returned; otherwise, warn if VAR is
5003 # conditionally defined.  If PARENT is specified, it is the name of
5004 # the including variable; this is only used for error reports.
5005 sub variable_value_as_list
5006 {
5007     local ($var, $cond, $parent) = @_;
5008     local (@result);
5009
5010     if (defined $targets{$var})
5011     {
5012         &am_line_error ($var, "\`$var' is target; expected variable");
5013     }
5014     elsif (! defined $contents{$var})
5015     {
5016         &am_line_error ($parent, "variable \`$var' not defined");
5017     }
5018     elsif ($cond eq 'all' && $conditional{$var})
5019     {
5020         local (@condvals) = split (' ', $conditional{$var});
5021         while (@condvals)
5022         {
5023             shift (@condvals);
5024             local ($val) = &unquote_cond_val (shift (@condvals));
5025             push (@result, &value_to_list ($var, $val, $cond));
5026         }
5027     }
5028     elsif ($cond && $conditional{$var})
5029     {
5030         local (@condvals) = split (' ', $conditional{$var});
5031         local ($onceflag);
5032         while (@condvals)
5033         {
5034             local ($vcond) = shift (@condvals);
5035             local ($val) = &unquote_cond_val (shift (@condvals));
5036             if (&conditional_true_when ($vcond, $cond))
5037             {
5038                 # Warn if we have an ambiguity.  It's hard to know how
5039                 # to handle this case correctly.
5040                 &variable_conditionally_defined ($var, $parent)
5041                     if $onceflag;
5042                 $onceflag = 1;
5043                 push (@result, &value_to_list ($var, $val, $cond));
5044             }
5045         }
5046     }
5047     else
5048     {
5049         &variable_conditionally_defined ($var, $parent);
5050         $content_seen{$var} = 1;
5051         push (@result, &value_to_list ($var, $contents{$var}, $cond));
5052     }
5053
5054     return @result;
5055 }
5056
5057 # Define a new variable, but only if not already defined.
5058 sub define_variable
5059 {
5060     local ($var, $value) = @_;
5061
5062     if (! defined $contents{$var})
5063     {
5064         $output_vars .= $var . ' = ' . $value . "\n";
5065         $contents{$var} = $value;
5066         $content_seen{$var} = 1;
5067     }
5068 }
5069
5070 # Like define_variable, but the value is a list, and the variable may
5071 # be defined conditionally.  The second argument is the conditional
5072 # under which the value should be defined; this should be the empty
5073 # string to define the variable unconditionally.  The third argument
5074 # is a list holding the values to use for the variable.  The value is
5075 # pretty printed in the output file.
5076 sub define_pretty_variable
5077 {
5078     local ($var, $cond, @value) = @_;
5079     if (! defined $contents{$var}
5080         || ($cond && ! &variable_defined ($var, $cond)))
5081     {
5082         $contents{$var} = join (' ', @value);
5083         if ($cond)
5084         {
5085             if ($conditional{$var})
5086             {
5087                 $conditional{$var} .= ' ';
5088             }
5089             else
5090             {
5091                 $conditional{$var} = '';
5092             }
5093             $conditional{$var} .= ($cond
5094                                    . ' '
5095                                    . &quote_cond_val ($contents{$var}));
5096         }
5097         &pretty_print ($cond . $var . ' = ', $cond, @value);
5098         $content_seen{$var} = 1;
5099     }
5100 }
5101
5102 # Like define_variable, but define a variable to be the configure
5103 # substitution by the same name.
5104 sub define_configure_variable
5105 {
5106     local ($var) = @_;
5107     local ($value) = '@' . $var . '@';
5108     &define_variable ($var, $value);
5109 }
5110
5111 # Define a variable that represents a program to run.  If in Cygnus
5112 # mode, the program is searched for in the build (or source) tree.
5113 # Otherwise no searching is done at all.  Arguments are:
5114 # * VAR      Name of variable to define
5115 # * WHATDIR  Either `src' or `build', depending on where program should
5116 #            be found.  (runtest is in srcdir!)
5117 # * SUBDIR   Subdir of top-level dir
5118 # * PROGRAM  Name of program
5119 # * OVERRIDE If specified, the name of the program to use when not in
5120 #            Cygnus mode.  Defaults to PROGRAM.
5121 sub define_program_variable
5122 {
5123     local ($var, $whatdir, $subdir, $program, $override) = @_;
5124
5125     if (! $override)
5126     {
5127         $override = $program;
5128     }
5129
5130     if ($cygnus_mode)
5131     {
5132         local ($full) = ('$(top_' . $whatdir . 'dir)/../'
5133                          . $subdir . '/' . $program);
5134         &define_variable ($var, ('`if test -f ' . $full
5135                                  . '; then echo ' . $full . '; else echo '
5136                                  . $program . '; fi`'));
5137     }
5138     else
5139     {
5140         &define_variable ($var, $override);
5141     }
5142 }
5143
5144
5145 ################################################################
5146
5147 # Read Makefile.am and set up %contents.  Simultaneously copy lines
5148 # from Makefile.am into $output_trailer or $output_vars as
5149 # appropriate.  NOTE we put rules in the trailer section.  We want
5150 # user rules to come after our generated stuff.
5151 sub read_am_file
5152 {
5153     local ($amfile) = @_;
5154
5155     open (AM_FILE, $amfile) || die "automake: couldn't open \`$amfile': $!\n";
5156     print "automake: reading $amfile\n" if $verbose;
5157
5158     $output_vars = ("# $in_file_name generated automatically by automake "
5159                     . $VERSION . " from $am_file_name\n");
5160
5161     # Generate copyright for generated Makefile.in.
5162     $output_vars .= $gen_copyright;
5163
5164     local ($saw_bk) = 0;
5165     local ($was_rule) = 0;
5166     local ($spacing) = '';
5167     local ($comment) = '';
5168     local ($last_var_name) = '';
5169     local ($blank) = 0;
5170
5171     while (<AM_FILE>)
5172     {
5173         if (/$IGNORE_PATTERN/o)
5174         {
5175             # Merely delete comments beginning with two hashes.
5176         }
5177         elsif (/$WHITE_PATTERN/o)
5178         {
5179             # Stick a single white line before the incoming macro or rule.
5180             $spacing = "\n";
5181             $blank = 1;
5182         }
5183         elsif (/$COMMENT_PATTERN/o)
5184         {
5185             # Stick comments before the incoming macro or rule.  Make
5186             # sure a blank line preceeds first block of comments.
5187             $spacing = "\n" unless $blank;
5188             $blank = 1;
5189             $comment .= $spacing . $_;
5190             $spacing = '';
5191         }
5192         else
5193         {
5194             last;
5195         }
5196     }
5197
5198     $output_vars .= $comment . "\n";
5199     $comment = '';
5200     $spacing = "\n";
5201     local ($am_vars) = '';
5202
5203     local ($is_ok_macro);
5204     while ($_)
5205     {
5206         $_ .= "\n"
5207             unless substr ($_, -1, 1) eq "\n";
5208
5209         $_ =~ s/\@MAINT\@//g
5210             unless $seen_maint_mode;
5211
5212         if (/$IGNORE_PATTERN/o)
5213         {
5214             # Merely delete comments beginning with two hashes.
5215         }
5216         elsif (/$WHITE_PATTERN/o)
5217         {
5218             # Stick a single white line before the incoming macro or rule.
5219             $spacing = "\n";
5220         }
5221         elsif (/$COMMENT_PATTERN/o)
5222         {
5223             # Stick comments before the incoming macro or rule.
5224             $comment .= $spacing . $_;
5225             $spacing = '';
5226         }
5227         elsif ($saw_bk)
5228         {
5229             if ($was_rule)
5230             {
5231                 $output_trailer .= join ('', @conditional_stack) . $_;
5232                 $saw_bk = /\\$/;
5233             }
5234             else
5235             {
5236                 $am_vars .= join ('', @conditional_stack) . $_;
5237                 $saw_bk = /\\$/;
5238                 # Chop newline and backslash if this line is
5239                 # continued.  FIXME: maybe ensure trailing whitespace
5240                 # exists?
5241                 chop if $saw_bk;
5242                 chop if $saw_bk;
5243                 $contents{$last_var_name} .= $_;
5244                 if (@conditional_stack)
5245                 {
5246                     $conditional{$last_var_name} .= &quote_cond_val ($_);
5247                 }
5248             }
5249         }
5250         elsif (/$IF_PATTERN/o)
5251         {
5252             &am_line_error ($., "$1 does not appear in AM_CONDITIONAL")
5253                 if (! $configure_cond{$1});
5254             push (@conditional_stack, "\@" . $1 . "_TRUE\@");
5255         }
5256         elsif (/$ELSE_PATTERN/o)
5257         {
5258             if (! @conditional_stack)
5259             {
5260                 &am_line_error ($., "else without if");
5261             }
5262             elsif ($conditional_stack[$#conditional_stack] =~ /_FALSE\@$/)
5263             {
5264                 &am_line_error ($., "else after else");
5265             }
5266             else
5267             {
5268                 $conditional_stack[$#conditional_stack]
5269                     =~ s/_TRUE\@$/_FALSE\@/;
5270             }
5271         }
5272         elsif (/$ENDIF_PATTERN/o)
5273         {
5274             if (! @conditional_stack)
5275             {
5276                 &am_line_error ($., "endif without if");
5277             }
5278             else
5279             {
5280                 pop @conditional_stack;
5281             }
5282         }
5283         elsif (/$RULE_PATTERN/o)
5284         {
5285             # Found a rule.
5286             $was_rule = 1;
5287             if (defined $contents{$1}
5288                 && (@conditional_stack
5289                     ? ! defined $conditional{$1}
5290                     : defined $conditional{$1}))
5291             {
5292                 &am_line_error ($1,
5293                                 "$1 defined both conditionally and unconditionally");
5294             }
5295             # Value here doesn't matter; for targets we only note
5296             # existence.
5297             $contents{$1} = 1;
5298             $targets{$1} = 1;
5299             local ($cond_string) = join ('', @conditional_stack);
5300             if (@conditional_stack)
5301             {
5302                 if ($conditional{$1})
5303                 {
5304                     &check_ambiguous_conditional ($1, $cond_string);
5305                     $conditional{$1} .= ' ';
5306                 }
5307                 else
5308                 {
5309                     $conditional{$1} = '';
5310                 }
5311                 $conditional{$1} .= $cond_string . ' 1';
5312             }
5313             $content_lines{$1} = $.;
5314             $output_trailer .= $comment . $spacing . $cond_string . $_;
5315             $comment = $spacing = '';
5316             $saw_bk = /\\$/;
5317
5318             # Check the rule for being a suffix rule. If so, store in
5319             # a hash.
5320
5321             local ($source_suffix);
5322             local ($object_suffix);
5323
5324             if (($source_suffix, $object_suffix) = ($1 =~ $SUFFIX_RULE_PATTERN)) 
5325             {
5326               $suffix_rules{$source_suffix} = $object_suffix;
5327               print "Sources ending in .$source_suffix become .$object_suffix\n" if $verbose;
5328               $source_suffix_pattern = "(" . join('|', keys %suffix_rules) . ")";
5329             }
5330
5331             # FIXME: make sure both suffixes are in SUFFIXES? Or set
5332             # SUFFIXES from suffix_rules?
5333         }
5334         elsif (($is_ok_macro = /$MACRO_PATTERN/o)
5335                || /$BOGUS_MACRO_PATTERN/o)
5336         {
5337             # Found a macro definition.
5338             $was_rule = 0;
5339             $last_var_name = $1;
5340             if (defined $contents{$1}
5341                 && (@conditional_stack
5342                     ? ! defined $conditional{$1}
5343                     : defined $conditional{$1}))
5344             {
5345                 &am_line_error ($1,
5346                                 "$1 defined both conditionally and unconditionally");
5347             }
5348             if ($2 ne '' && substr ($2, -1) eq "\\")
5349             {
5350                 $contents{$last_var_name} = substr ($2, 0, length ($2) - 1);
5351             }
5352             else
5353             {
5354                 $contents{$last_var_name} = $2;
5355             }
5356             local ($cond_string) = join ('', @conditional_stack);
5357             if (@conditional_stack)
5358             {
5359                 if ($conditional{$last_var_name})
5360                 {
5361                     &check_ambiguous_conditional ($last_var_name,
5362                                                   $cond_string);
5363                     $conditional{$last_var_name} .= ' ';
5364                 }
5365                 else
5366                 {
5367                     $conditional{$last_var_name} = '';
5368                 }
5369                 local ($val) = $contents{$last_var_name};
5370                 $conditional{$last_var_name} .= ($cond_string
5371                                                  . ' '
5372                                                  . &quote_cond_val ($val));
5373             }
5374             $content_lines{$last_var_name} = $.;
5375             $am_vars .= $comment . $spacing . $cond_string . $_;
5376             $comment = $spacing = '';
5377             $saw_bk = /\\$/;
5378
5379             # Error if bogus.
5380             &am_line_error ($., "bad macro name \`$last_var_name'")
5381                 if ! $is_ok_macro;
5382         }
5383         else
5384         {
5385             # This isn't an error; it is probably a continued rule.
5386             # In fact, this is what we assume.
5387             $was_rule = 1;
5388             $output_trailer .= ($comment . $spacing
5389                                 . join ('', @conditional_stack) . $_);
5390             $comment = $spacing = '';
5391             $saw_bk = /\\$/;
5392         }
5393
5394         $_ = <AM_FILE>;
5395     }
5396
5397     $output_trailer .= $comment;
5398
5399     &am_error ("unterminated conditionals: " . join (' ', @conditional_stack))
5400         if (@conditional_stack);
5401
5402     # Compute relative location of the top object directory.
5403     local (@topdir) = ();
5404     foreach (split (/\//, $relative_dir))
5405     {
5406         next if $_ eq '.' || $_ eq '';
5407         if ($_ eq '..')
5408         {
5409             pop @topdir;
5410         }
5411         else
5412         {
5413             push (@topdir, '..');
5414         }
5415     }
5416     @topdir = ('.') if ! @topdir;
5417
5418     $top_builddir = join ('/', @topdir);
5419     local ($build_rx);
5420     ($build_rx = $top_builddir) =~ s/(\W)/\\$1/g;
5421     $output_vars .= &file_contents_with_transform
5422                         ('s/\@top_builddir\@/' . $build_rx . '/g;',
5423                          'header-vars');
5424
5425     # Generate some useful variables when AC_CANONICAL_* used.  FIXME:
5426     # this should use generic %configure_vars method.
5427     if ($seen_canonical)
5428     {
5429         local ($curs, %vars);
5430         $vars{'host_alias'} = 'host_alias';
5431         $vars{'host_triplet'} = 'host';
5432         if ($seen_canonical == $AC_CANONICAL_SYSTEM)
5433         {
5434             $vars{'build_alias'} = 'build_alias';
5435             $vars{'build_triplet'} = 'build';
5436             $vars{'target_alias'} = 'target_alias';
5437             $vars{'target_triplet'} = 'target';
5438         }
5439         foreach $curs (sort keys %vars)
5440         {
5441             $output_vars .= "$curs = \@$vars{$curs}\@\n";
5442             $contents{$curs} = "\@$vars{$curs}\@";
5443         }
5444     }
5445
5446     local ($curs);
5447     foreach $curs (sort keys %configure_vars)
5448     {
5449         &define_configure_variable ($curs);
5450     }
5451
5452     $output_vars .= $am_vars;
5453 }
5454
5455 ################################################################
5456
5457 sub initialize_global_constants
5458 {
5459     # Values for AC_CANONICAL_*
5460     $AC_CANONICAL_HOST = 1;
5461     $AC_CANONICAL_SYSTEM = 2;
5462
5463     # Associative array of standard directory names.  Entry is TRUE if
5464     # corresponding directory should be installed during
5465     # 'install-exec' phase.
5466     %exec_dir_p =
5467         ('bin', 1,
5468          'sbin', 1,
5469          'libexec', 1,
5470          'data', 0,
5471          'sysconf', 1,
5472          'localstate', 1,
5473          'lib', 1,
5474          'info', 0,
5475          'man', 0,
5476          'include', 0,
5477          'oldinclude', 0,
5478          'pkgdata', 0,
5479          'pkglib', 1,
5480          'pkginclude', 0
5481          );
5482
5483     # Commonly found files we look for and automatically include in
5484     # DISTFILES.
5485     @common_files =
5486         (
5487          "README", "THANKS", "TODO", "NEWS", "COPYING", "COPYING.LIB",
5488          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
5489          "config.guess", "config.sub", "AUTHORS", "BACKLOG", "ABOUT-GNU",
5490          "libversion.in", "mdate-sh", "mkinstalldirs", "install-sh",
5491          'texinfo.tex', "ansi2knr.c", "ansi2knr.1", 'elisp-comp',
5492          'ylwrap', 'acinclude.m4', @libtoolize_files,
5493          'missing'
5494          );
5495
5496     # Commonly used files we auto-include, but only sometimes.
5497     @common_sometimes =
5498         (
5499          "aclocal.m4", "acconfig.h", "config.h.top",
5500          "config.h.bot", "stamp-h.in", 'stamp-vti'
5501          );
5502
5503     $USAGE = "\
5504   -a, --add-missing     add missing standard files to package
5505   --amdir=DIR           directory storing config files
5506   --build-dir=DIR       directory where build being done (for dependencies)
5507   --cygnus              assume program is part of Cygnus-style tree
5508   --foreign             set strictness to foreign
5509   --gnits               set strictness to gnits
5510   --gnu                 set strictness to gnu
5511   --help                print this help, then exit
5512   -i, --include-deps    include generated dependencies in Makefile.in
5513   --no-force            only update Makefile.in's that are out of date
5514   -o DIR, --output-dir=DIR
5515                         put generated Makefile.in's into DIR
5516   --srcdir-name=DIR     name used for srcdir (for dependencies)
5517   -v, --verbose         verbosely list files processed
5518   --version             print version number, then exit\n";
5519
5520     # Copyright on generated Makefile.ins.
5521     $gen_copyright = "\
5522 # Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
5523 # This Makefile.in is free software; the Free Software Foundation
5524 # gives unlimited permission to copy and/or distribute it,
5525 # with or without modifications, as long as this notice is preserved.
5526
5527 # This program is distributed in the hope that it will be useful,
5528 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
5529 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
5530 # PARTICULAR PURPOSE.
5531 ";
5532
5533     # Ignore return result from chmod, because it might give an error
5534     # if we chmod a symlink.
5535     $dist_header = "\t" . '-chmod -R a+r $(distdir)' . "\n";
5536     $dist{'dist-tarZ'} = ("\t"
5537                      . '$(TAR) chf - $(distdir) | compress -c > $(distdir).tar.Z'
5538                      . "\n");
5539     $dist{'dist-shar'} = ("\t"
5540                      . 'shar $(distdir) | gzip > $(distdir).shar.gz'
5541                      . "\n");
5542     $dist{'dist-zip'} = "\t" . 'zip -rq $(distdir).zip $(distdir)' . "\n";
5543     $dist{'dist'} = "\t" .  'GZIP=$(GZIP) $(TAR) chozf $(distdir).tar.gz $(distdir)' . "\n";
5544     $dist_trailer = "\t" . '-rm -rf $(distdir)' . "\n";
5545 }
5546
5547 # (Re)-Initialize per-Makefile.am variables.
5548 sub initialize_per_input
5549 {
5550     # These two variables are used when generating each Makefile.in.
5551     # They hold the Makefile.in until it is ready to be printed.
5552     $output_rules = '';
5553     $output_vars = '';
5554     $output_trailer = '';
5555     $output_all = '';
5556     $output_header = '';
5557
5558     # Suffixes found during a run.
5559     @suffixes = ();
5560
5561     # This holds the contents of a Makefile.am, as parsed by
5562     # read_am_file.
5563     %contents = ();
5564
5565     # This holds the names which are targets.  These also appear in
5566     # %contents.
5567     %targets = ();
5568
5569     # For a variable or target which is defined conditionally, this
5570     # holds an array of the conditional values.  The array is composed
5571     # of pairs of condition strings (the variables which configure
5572     # will substitute) and values (the value of a target is
5573     # meaningless).  For an unconditional variable, this is empty.
5574     %conditional = ();
5575
5576     # This holds the line numbers at which various elements of
5577     # %contents are defined.
5578     %content_lines = ();
5579
5580     # This holds a 1 if a particular variable was examined.
5581     %content_seen = ();
5582
5583     # This is the conditional stack.
5584     @conditional_stack = ();
5585
5586     # This holds the "relative directory" of the current Makefile.in.
5587     # Eg for src/Makefile.in, this is "src".
5588     $relative_dir = '';
5589
5590     # This holds a list of files that are included in the
5591     # distribution.
5592     %dist_common = ();
5593
5594     # List of dependencies for the obvious targets.
5595     @install_data = ();
5596     @install_exec = ();
5597     @uninstall = ();
5598     @installdirs = ();
5599
5600     @info = ();
5601     @dvi = ();
5602     @all = ();
5603     @check = ();
5604     @check_tests = ();
5605     @installcheck = ();
5606     @clean = ();
5607
5608     @phony = ();
5609
5610     # A list of files deleted by `maintainer-clean'.
5611     @maintainer_clean_files = ();
5612
5613     # These are pretty obvious, too.  They are used to define the
5614     # SOURCES and OBJECTS variables.
5615     @sources = ();
5616     @objects = ();
5617
5618     # TRUE if current directory holds any C source files.
5619     $dir_holds_sources = 0;
5620
5621     # These variables track inclusion of various compile-related .am
5622     # files.  $included_generic_compile is TRUE if the basic code has
5623     # been included.  $included_knr_compile is TRUE if the ansi2knr
5624     # code has been included.  $included_libtool_compile is TRUE if
5625     # libtool support has been included.
5626     $included_generic_compile = 0;
5627     $included_knr_compile = 0;
5628     $included_libtool_compile = 0;
5629
5630     # TRUE if current directory holds any headers.
5631     $dir_holds_headers = 0;
5632
5633     # TRUE if install targets should work recursively.
5634     $recursive_install = 0;
5635
5636     # All .P files.
5637     %dep_files = ();
5638
5639     # Strictness levels.
5640     $strictness = $default_strictness;
5641     $strictness_name = $default_strictness_name;
5642
5643     # Options from AUTOMAKE_OPTIONS.
5644     %options = ();
5645
5646     # Whether or not dependencies are handled.  Can be further changed
5647     # in handle_options.
5648     $use_dependencies = $cmdline_use_dependencies;
5649
5650     # Per Makefile.am.
5651     $local_maint_charset = $maint_charset;
5652
5653     # All yacc and lex source filenames for this directory.  Use
5654     # filenames instead of raw count so that multiple instances are
5655     # counted correctly (eg one yacc file can appear in multiple
5656     # programs without harm).
5657     %yacc_sources = ();
5658     %lex_sources = ();
5659
5660     # C++ source extensions we've seen.
5661     %cxx_extensions = ();
5662
5663     # TRUE if we've seen any non-C++ sources.  This actually holds a
5664     # line number or the name of a symbol corresponding to a line
5665     # number where the C sources were seen.  If it is -1 then it means
5666     # we couldn't (easily) figure out which line of the Makefile.am
5667     # mentioned the sources.
5668     $seen_c_source = 0;
5669
5670     # TRUE if we've seen any sources at all.
5671     $seen_any_source = 0;
5672
5673     # This is a list of all targets to run during "make dist".
5674     @dist_targets = ();
5675
5676     # Keys in this hash are the basenames of files which must depend
5677     # on ansi2knr.
5678     %de_ansi_files = ();
5679
5680     # This maps the source extension of a suffix rule to its
5681     # corresponding output extension.
5682     %suffix_rules = ();
5683 }
5684
5685
5686 ################################################################
5687
5688 # Return contents of a file from $am_dir, automatically skipping
5689 # macros or rules which are already known.  Runs command on each line
5690 # as it is read; this command can modify $_.
5691 sub file_contents_with_transform
5692 {
5693     local ($command, $basename) = @_;
5694     local ($file) = $am_dir . '/' . $basename . '.am';
5695
5696     if ($command ne '' && substr ($command, -1) ne ';')
5697     {
5698         die "automake: programming error in file_contents_with_transform: $command\n";
5699     }
5700
5701     open (FC_FILE, $file)
5702         || die "automake: installation error: cannot open \`$file'\n";
5703     # Looks stupid?
5704     # print "automake: reading $file\n" if $verbose;
5705
5706     local ($was_rule) = 0;
5707     local ($result_vars) = '';
5708     local ($result_rules) = '';
5709     local ($comment) = '';
5710     local ($spacing) = "\n";
5711     local ($skipping) = 0;
5712     local ($had_chars);
5713
5714     while (<FC_FILE>)
5715     {
5716         $_ =~ s/\@MAINT\@//g
5717             unless $seen_maint_mode;
5718
5719         $had_chars = length ($_) && $_ ne "\n";
5720         eval $command;
5721         # If the transform caused all the characters to go away, then
5722         # ignore the line.  Why do this?  Because in Perl 4, a "next"
5723         # inside of an eval doesn't affect a loop outside the eval.
5724         # So we can't pass in a "transform" that uses next.  We used
5725         # to do this.  "Empty" also means consisting of a single
5726         # newline.
5727         next if $had_chars && ($_ eq '' || $_ eq "\n");
5728
5729         if (/$IGNORE_PATTERN/o)
5730         {
5731             # Merely delete comments beginning with two hashes.
5732         }
5733         elsif (/$WHITE_PATTERN/o)
5734         {
5735             # Stick a single white line before the incoming macro or rule.
5736             $spacing = "\n";
5737         }
5738         elsif (/$COMMENT_PATTERN/o)
5739         {
5740             # Stick comments before the incoming macro or rule.
5741             $comment .= $spacing . $_;
5742             $spacing = '';
5743         }
5744         elsif ($saw_bk)
5745         {
5746             if ($was_rule)
5747             {
5748                 $result_rules .= $_ if ! $skipping;
5749             }
5750             else
5751             {
5752                 $result_vars .= $_ if ! $skipping;
5753             }
5754             $saw_bk = /\\$/;
5755         }
5756         elsif (/$RULE_PATTERN/o)
5757         {
5758             # Found a rule.
5759             $was_rule = 1;
5760             $skipping = defined $contents{$1};
5761             $result_rules .= $comment . $spacing . $_ if ! $skipping;
5762             $comment = $spacing = '';
5763             $saw_bk = /\\$/;
5764         }
5765         elsif (/$MACRO_PATTERN/o)
5766         {
5767             # Found a variable reference.
5768             $was_rule = 0;
5769             $skipping = defined $contents{$1};
5770             $result_vars .= $comment . $spacing . $_ if ! $skipping;
5771             $comment = $spacing = '';
5772             $saw_bk = /\\$/;
5773         }
5774         else
5775         {
5776             # This isn't an error; it is probably a continued rule.
5777             # In fact, this is what we assume.
5778             $was_rule = 1;
5779             $result_rules .= $comment . $spacing . $_ if ! $skipping;
5780             $comment = $spacing = '';
5781             $saw_bk = /\\$/;
5782         }
5783     }
5784
5785     close (FC_FILE);
5786     return $result_vars . $result_rules . $comment;
5787 }
5788
5789 # Like file_contents_with_transform, but no transform.
5790 sub file_contents
5791 {
5792     return &file_contents_with_transform ('', @_);
5793 }
5794
5795 # Find all variable prefixes that are used for install directories.  A
5796 # prefix `zar' qualifies iff:
5797 # * `zardir' is a variable.
5798 # * `zar_PRIMARY' is a variable.
5799 sub am_primary_prefixes
5800 {
5801     local ($primary, @prefixes) = @_;
5802
5803     local (%valid, $varname);
5804     grep ($valid{$_} = 0, @prefixes);
5805     $valid{'EXTRA'} = 0;
5806     foreach $varname (keys %contents)
5807     {
5808         if ($varname =~ /^(.*)_$primary$/)
5809         {
5810             if (! defined $valid{$1}
5811                 && ! &variable_defined ($1 . 'dir')
5812                 # Note that a configure variable is always legitimate.
5813                 # It is natural to name such variables after the
5814                 # primary, so we explicitly allow it.
5815                 && ! defined $configure_vars{$varname})
5816             {
5817                 &am_line_error ($varname, "invalid variable \`$varname'");
5818             }
5819             else
5820             {
5821                 # Ensure all extended prefixes are actually used.
5822                 $valid{$1} = 1;
5823             }
5824         }
5825     }
5826
5827     return %valid;
5828 }
5829
5830 # Handle `where_HOW' variable magic.  Does all lookups, generates
5831 # install code, and possibly generates code to define the primary
5832 # variable.  The first argument is the name of the .am file to munge,
5833 # the second argument is the primary variable (eg HEADERS), and all
5834 # subsequent arguments are possible installation locations.  Returns
5835 # list of all values of all _HOW targets.
5836 #
5837 # FIXME: this should be rewritten to be cleaner.  It should be broken
5838 # up into multiple functions.
5839 #
5840 # Usage is: am_install_var (OPTION..., file, HOW, where...)
5841 sub am_install_var
5842 {
5843     local (@args) = @_;
5844
5845     local ($do_clean) = 0;
5846
5847     local ($ltxform);
5848     if (defined $configure_vars{'LIBTOOL'})
5849     {
5850         # Transform '@LIBTOOL ...@' to '$(LIBTOOL) ...'
5851         $ltxform = 's/\@LIBTOOL([^\@]*)\@/\$(LIBTOOL) $1/;';
5852     }
5853     else
5854     {
5855         # Delete '@LIBTOOL ...@'
5856         $ltxform = 's/\@LIBTOOL([^\@]*)\@//;';
5857     }
5858
5859     local ($cygxform);
5860     if (! $seen_exeext)
5861     {
5862         $cygxform = 's/\@EXEEXT\@//g;';
5863     }
5864     else
5865     {
5866         $cygxform = 's/\@EXEEXT\@/\$(EXEEXT)/g;';
5867     }
5868
5869     while (@args)
5870     {
5871         if ($args[0] eq '-clean')
5872         {
5873             $do_clean = 1;
5874         }
5875         elsif ($args[0] !~ /^-/)
5876         {
5877             last;
5878         }
5879         shift (@args);
5880     }
5881     local ($file, $primary, @prefixes) = @args;
5882
5883     local (@used) = ();
5884     local (@result) = ();
5885
5886     # Now that configure substitutions are allowed in where_HOW
5887     # variables, it is an error to actually define the primary.  We
5888     # allow `JAVA', as it is customarily used to mean the Java
5889     # interpreter.  This is but one of several Java hacks.
5890     &am_line_error ($primary, "\`$primary' is an anachronism")
5891         if &variable_defined ($primary) && $primary ne 'JAVA';
5892
5893
5894     # Look for misspellings.  It is an error to have a variable ending
5895     # in a "reserved" suffix whose prefix is unknown, eg
5896     # "bni_PROGRAMS".  However, unusual prefixes are allowed if a
5897     # variable of the same name (with "dir" appended) exists.  For
5898     # instance, if the variable "zardir" is defined, then
5899     # "zar_PROGRAMS" becomes valid.  This is to provide a little extra
5900     # flexibility in those cases which need it.  Perhaps it should be
5901     # disallowed in the Gnits case?  The problem is, sometimes it is
5902     # useful to put things in a subdir of eg pkgdatadir, perhaps even
5903     # for Gnitsoids.
5904     local (%valid) = &am_primary_prefixes ($primary, @prefixes);
5905
5906     # If a primary includes a configure substitution, then the EXTRA_
5907     # form is required.  Otherwise we can't properly do our job.
5908     local ($require_extra);
5909     local ($warned_about_extra) = 0;
5910
5911     local ($clean_file) = $file . '-clean';
5912     local ($one_name);
5913     local ($X);
5914     foreach $X (sort keys %valid)
5915     {
5916         $one_name = $X . '_' . $primary;
5917         if (&variable_defined ($one_name))
5918         {
5919             # Append actual contents of where_PRIMARY variable to
5920             # result.
5921             local ($rcurs);
5922             foreach $rcurs (&variable_value_as_list ($one_name, 'all'))
5923             {
5924                 # Skip configure substitutions.  Possibly bogus.
5925                 if ($rcurs =~ /^\@.*\@$/)
5926                 {
5927                     if ($X eq 'EXTRA')
5928                     {
5929                         if (! $warned_about_extra)
5930                         {
5931                             $warned_about_extra = 1;
5932                             &am_line_error ($one_name,
5933                                             "\`$one_name' contains configure substitution, but shouldn't");
5934                         }
5935                     }
5936                     # Check here to make sure variables defined in
5937                     # configure.in do not imply that EXTRA_PRIMARY
5938                     # must be defined.
5939                     elsif (! defined $configure_vars{$one_name})
5940                     {
5941                         $require_extra = $one_name;
5942                     }
5943
5944                     next;
5945                 }
5946
5947                 push (@result, $rcurs);
5948             }
5949
5950             # "EXTRA" shouldn't be used when generating clean targets,
5951             # all, or install targets.
5952             next if $X eq 'EXTRA';
5953
5954             # A blatant hack: we rewrite each _PROGRAMS primary to
5955             # include EXEEXT when in Cygwin32 mode.
5956             if ($seen_exeext && $primary eq 'PROGRAMS')
5957             {
5958                 local (@conds) = &variable_conditions ($one_name);
5959                 local (@one_binlist);
5960
5961                 # FIXME: this definitely loses aesthetically; it
5962                 # redefines $ONE_NAME.  Instead we should arrange for
5963                 # variable definitions to be output later, instead of
5964                 # at scan time.
5965
5966                 if (! @conds)
5967                 {
5968                     @one_binlist = ();
5969                     foreach $rcurs (&variable_value_as_list ($one_name, ''))
5970                     {
5971                         if ($rcurs =~ /\./ || $rcurs =~ /^\@.*\@$/)
5972                         {
5973                             push (@one_binlist, $rcurs);
5974                         }
5975                         else
5976                         {
5977                             push (@one_binlist, $rcurs . '$(EXEEXT)');
5978                         }
5979                     }
5980
5981                     delete $contents{$one_name};
5982                     &define_pretty_variable ($one_name, '', @one_binlist);
5983                 }
5984                 else
5985                 {
5986                     local ($cond);
5987                     local ($condvals) = '';
5988                     foreach $cond (@conds)
5989                     {
5990                         @one_binlist = ();
5991                         local (@condval) = &variable_value_as_list ($one_name,
5992                                                                     $cond);
5993                         foreach $rcurs (@condval)
5994                         {
5995                             if ($rcurs =~ /\./ || $rcurs =~ /^\@.*\@$/)
5996                             {
5997                                 push (@one_binlist, $rcurs);
5998                             }
5999                             else
6000                             {
6001                                 push (@one_binlist, $rcurs . '$(EXEEXT)');
6002                             }
6003                         }
6004
6005                         push (@condvals, $cond);
6006                         push (@condvals, join (' ', @one_binlist));
6007                     }
6008
6009                     delete $contents{$one_name};
6010
6011                     while (@condvals)
6012                     {
6013                         $cond = shift (@condvals);
6014                         local (@val) = split (' ', shift (@condvals));
6015                         &define_pretty_variable ($one_name, $cond, @val);
6016                     }
6017                 }
6018             }
6019
6020             if ($do_clean)
6021             {
6022                 $output_rules .=
6023                     &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go;'
6024                                                    . $cygxform,
6025                                                    $clean_file);
6026
6027                 push (@clean, $X . $primary);
6028                 &push_phony_cleaners ($X . $primary);
6029             }
6030
6031             if ($X eq 'check')
6032             {
6033                 push (@check, '$(' . $one_name . ')');
6034             }
6035             else
6036             {
6037                 push (@used, '$(' . $one_name . ')');
6038             }
6039             if ($X eq 'noinst' || $X eq 'check')
6040             {
6041                 # Objects which don't get installed by default.
6042                 next;
6043             }
6044
6045             $output_rules .=
6046                 &file_contents_with_transform ('s/\@DIR\@/' . $X . '/g;'
6047                                                . $ltxform . $cygxform,
6048                                                $file);
6049
6050             push (@uninstall, 'uninstall-' . $X . $primary);
6051             push (@phony, 'uninstall-' . $X . $primary);
6052             push (@installdirs, '$(DATADIR)$(' . $X . 'dir)');
6053             if (defined $exec_dir_p{$X} ? $exec_dir_p{$X} : ($X =~ /exec/))
6054             {
6055                 push (@install_exec, 'install-' . $X . $primary);
6056                 push (@phony, 'install-' . $X . $primary);
6057             }
6058             else
6059             {
6060                 push (@install_data, 'install-' . $X . $primary);
6061                 push (@phony, 'install-' . $X . $primary);
6062             }
6063         }
6064     }
6065
6066     # The JAVA variable is used as the name of the Java interpreter.
6067     if (@used && $primary ne 'JAVA')
6068     {
6069         # Define it.
6070         &define_pretty_variable ($primary, '', @used);
6071         $output_vars .= "\n";
6072     }
6073
6074     if ($require_extra && ! &variable_defined ('EXTRA_' . $primary))
6075     {
6076         &am_line_error ($require_extra,
6077                         "\`$require_extra' contains configure substitution, but \`EXTRA_$primary' not defined");
6078     }
6079
6080     # Push here because PRIMARY might be configure time determined.
6081     push (@all, '$(' . $primary . ')')
6082         if @used && $primary ne 'JAVA';
6083
6084     return (@result);
6085 }
6086
6087
6088 ################################################################
6089
6090 # This variable is local to the "require file" set of functions.
6091 @require_file_paths = ();
6092
6093 # Verify that the file must exist in the current directory.  Usage:
6094 # require_file (isconfigure, line_number, strictness, file) strictness
6095 # is the strictness level at which this file becomes required.  Must
6096 # set require_file_paths before calling this function.
6097 # require_file_paths is set to hold a single directory (the one in
6098 # which the first file was found) before return.
6099 sub require_file_internal
6100 {
6101     local ($is_configure, $line, $mystrict, @files) = @_;
6102     local ($file, $fullfile);
6103     local ($found_it, $errfile, $errdir);
6104     local ($save_dir);
6105
6106     foreach $file (@files)
6107     {
6108         $found_it = 0;
6109         foreach $dir (@require_file_paths)
6110         {
6111             if ($dir eq '.')
6112             {
6113                 $fullfile = $relative_dir . "/" . $file;
6114                 $errdir = $relative_dir unless $errdir;
6115             }
6116             else
6117             {
6118                 $fullfile = $dir . "/" . $file;
6119                 $errdir = $dir unless $errdir;
6120             }
6121
6122             # Use different name for "error filename".  Otherwise on
6123             # an error the bad file will be reported as eg
6124             # `../../install-sh' when using the default
6125             # config_aux_path.
6126             $errfile = $errdir . '/' . $file;
6127
6128             if (-f $fullfile)
6129             {
6130                 $found_it = 1;
6131                 # FIXME: Once again, special-case `.'.
6132                 &push_dist_common ($file)
6133                     if $dir eq $relative_dir || $dir eq '.';
6134                 $save_dir = $dir;
6135                 last;
6136             }
6137         }
6138
6139         if ($found_it)
6140         {
6141             # Prune the path list.
6142             @require_file_paths = $save_dir;
6143         }
6144         else
6145         {
6146             if ($strictness >= $mystrict)
6147             {
6148                 local ($trailer) = '';
6149                 local ($suppress) = 0;
6150
6151                 # Only install missing files according to our desired
6152                 # strictness level.
6153                 local ($message) = "required file \`$errfile' not found";
6154                 if ($add_missing)
6155                 {
6156                     $suppress = 1;
6157
6158                     # Maybe run libtoolize.
6159                     if ($seen_libtool
6160                         && grep ($_ eq $file, @libtoolize_files)
6161                         && system ('libtoolize', '--automake'))
6162                     {
6163                         $message = "installing \`$errfile'";
6164                         $suppress = 0;
6165                         $trailer = "; cannot run \`libtoolize': $!";
6166                     }
6167                     elsif (-f ($am_dir . '/' . $file))
6168                     {
6169                         # Install the missing file.  Symlink if we
6170                         # can, copy if we must.  Note: delete the file
6171                         # first, in case it is a dangling symlink.
6172                         $message = "installing \`$errfile'";
6173                         unlink ($errfile);
6174                         if ($symlink_exists)
6175                         {
6176                             if (! symlink ($am_dir . '/' . $file, $errfile))
6177                             {
6178                                 $suppress = 0;
6179                                 $trailer = "; error while making link: $!\n";
6180                             }
6181                         }
6182                         elsif (! system ('cp', $am_dir . '/' . $file, $errfile))
6183                         {
6184                             $suppress = 0;
6185                             $trailer = "\n    error while making link\n";
6186                         }
6187                     }
6188                 }
6189
6190                 local ($save) = $exit_status;
6191                 if ($is_configure)
6192                 {
6193                     # FIXME: allow actual file to be specified.
6194                     &am_conf_line_error ('configure.in', $line,
6195                                          "$message$trailer");
6196                 }
6197                 else
6198                 {
6199                     &am_line_error ($line, "$message$trailer");
6200                 }
6201                 $exit_status = $save if $suppress;
6202             }
6203         }
6204     }
6205 }
6206
6207 # Like require_file_with_line, but error messages refer to
6208 # configure.in, not the current Makefile.am.
6209 sub require_file_with_conf_line
6210 {
6211     @require_file_paths = '.';
6212     &require_file_internal (1, @_);
6213 }
6214
6215 sub require_file_with_line
6216 {
6217     @require_file_paths = '.';
6218     &require_file_internal (0, @_);
6219 }
6220
6221 sub require_file
6222 {
6223     @require_file_paths = '.';
6224     &require_file_internal (0, '', @_);
6225 }
6226
6227 # Require a file that is also required by Autoconf.  Looks in
6228 # configuration path, as specified by AC_CONFIG_AUX_DIR.
6229 sub require_config_file
6230 {
6231     @require_file_paths = @config_aux_path;
6232     &require_file_internal (1, '', @_);
6233     local ($dir) = $require_file_paths[0];
6234     @config_aux_path = @require_file_paths;
6235     if ($dir eq '.')
6236     {
6237         $config_aux_dir = '.';
6238     }
6239     else
6240     {
6241         $config_aux_dir = '$(top_srcdir)/' . $dir;
6242     }
6243 }
6244
6245 # Assumes that the line number is in Makefile.am.
6246 sub require_conf_file_with_line
6247 {
6248     @require_file_paths = @config_aux_path;
6249     &require_file_internal (0, @_);
6250     local ($dir) = $require_file_paths[0];
6251     @config_aux_path = @require_file_paths;
6252     if ($dir eq '.')
6253     {
6254         $config_aux_dir = '.';
6255     }
6256     else
6257     {
6258         $config_aux_dir = '$(top_srcdir)/' . $dir;
6259     }
6260 }
6261
6262 # Assumes that the line number is in Makefile.am.
6263 sub require_conf_file_with_conf_line
6264 {
6265     @require_file_paths = @config_aux_path;
6266     &require_file_internal (1, @_);
6267     local ($dir) = $require_file_paths[0];
6268     @config_aux_path = @require_file_paths;
6269     if ($dir eq '.')
6270     {
6271         $config_aux_dir = '.';
6272     }
6273     else
6274     {
6275         $config_aux_dir = '$(top_srcdir)/' . $dir;
6276     }
6277 }
6278
6279 ################################################################
6280
6281 # Push a list of files onto dist_common.
6282 sub push_dist_common
6283 {
6284     local (@files) = @_;
6285     local ($file);
6286
6287     foreach $file (@files)
6288     {
6289         $dist_common{$file} = 1;
6290     }
6291 }
6292
6293 # Push a list of clean targets onto phony.
6294 sub push_phony_cleaners
6295 {
6296     local ($base) = @_;
6297     local ($target);
6298     foreach $target ('mostly', 'dist', '', 'maintainer-')
6299     {
6300         push (@phony, $target . 'clean-' . $base);
6301     }
6302 }
6303
6304 # Set strictness.
6305 sub set_strictness
6306 {
6307     $strictness_name = $_[0];
6308     if ($strictness_name eq 'gnu')
6309     {
6310         $strictness = $GNU;
6311     }
6312     elsif ($strictness_name eq 'gnits')
6313     {
6314         $strictness = $GNITS;
6315     }
6316     elsif ($strictness_name eq 'foreign')
6317     {
6318         $strictness = $FOREIGN;
6319     }
6320     else
6321     {
6322         die "automake: level \`$strictness_name' not recognized\n";
6323     }
6324 }
6325
6326
6327 ################################################################
6328
6329 # Return directory name of file.
6330 sub dirname
6331 {
6332     local ($file) = @_;
6333     local ($sub);
6334
6335     ($sub = $file) =~ s,/+[^/]+$,,g;
6336     $sub = '.' if $sub eq $file;
6337     return $sub;
6338 }
6339
6340 # Return file name of a file.
6341 sub basename
6342 {
6343     local ($file) = @_;
6344     local ($sub);
6345
6346     ($sub = $file) =~s,^.*/+,,g;
6347     return $sub;
6348 }
6349
6350 # Touch a file.
6351 sub touch
6352 {
6353     local ($file) = @_;
6354
6355     open (TOUCH, ">> $file");
6356     close (TOUCH);
6357 }
6358
6359 # Glob something.  Do this to avoid indentation screwups everywhere we
6360 # want to glob.  Gross!
6361 sub my_glob
6362 {
6363     local ($pat) = @_;
6364     return <${pat}>;
6365 }
6366
6367 ################################################################
6368
6369 # Print an error message and set exit status.
6370 sub am_error
6371 {
6372     warn "automake: ${am_file}.am: ", join (' ', @_), "\n";
6373     $exit_status = 1;
6374 }
6375
6376 sub am_line_error
6377 {
6378     local ($symbol, @args) = @_;
6379
6380     if ($symbol && "$symbol" ne '-1')
6381     {
6382         # If SYMBOL not already a line number, look it up in Makefile.am.
6383         if ($symbol =~ /^\d+$/)
6384         {
6385             $symbol .= ': ';
6386         }
6387         elsif (defined $content_lines{$symbol})
6388         {
6389             $symbol = $content_lines{$symbol} . ': ';
6390         }
6391         else
6392         {
6393             # A single space, to provide nice separation.
6394             $symbol = ' ';
6395         }
6396         warn "${am_file}.am:", $symbol, join (' ', @args), "\n";
6397         $exit_status = 1;
6398     }
6399     else
6400     {
6401         &am_error (@args);
6402     }
6403 }
6404
6405 # Like am_error, but while scanning configure.in.
6406 sub am_conf_error
6407 {
6408     # FIXME: can run in subdirs.
6409     warn "automake: configure.in: ", join (' ', @_), "\n";
6410     $exit_status = 1;
6411 }
6412
6413 # Error message with line number referring to configure.in.
6414 sub am_conf_line_error
6415 {
6416     local ($file, $line, @args) = @_;
6417
6418     if ($line)
6419     {
6420         warn "$file: $line: ", join (' ', @args), "\n";
6421         $exit_status = 1;
6422     }
6423     else
6424     {
6425         &am_conf_error (@args);
6426     }
6427 }
6428
6429 # Warning message with line number referring to configure.in.
6430 # Does not affect exit_status
6431 sub am_conf_line_warning
6432 {
6433     local ($saved_exit_status) = $exit_status;
6434     &am_conf_line_error (@_);
6435     $exit_status = $saved_exit_status;
6436 }
6437
6438 # Tell user where our aclocal.m4 is, but only once.
6439 sub keyed_aclocal_warning
6440 {
6441     local ($key) = @_;
6442     warn "automake: macro \`$key' can be generated by \`aclocal'\n";
6443 }
6444
6445 # Print usage information.
6446 sub usage
6447 {
6448     print "Usage: automake [OPTION] ... [Makefile]...\n\n";
6449     print "Generate Makefile.in for autoconf from Makefile.am\n";
6450     print $USAGE;
6451     print "\nFiles which are automatically distributed, if found:\n";
6452     $~ = "USAGE_FORMAT";
6453     local (@lcomm) = sort ((@common_files, @common_sometimes));
6454     local ($one, $two, $three, $four);
6455     while (@lcomm > 0)
6456     {
6457         $one = shift @lcomm;
6458         $two = @lcomm ? shift @lcomm : '';
6459         $three = @lcomm ? shift @lcomm : '';
6460         $four = @lcomm ? shift @lcomm : '';
6461         write;
6462     }
6463
6464     print "\nReport bugs to <automake-bugs\@gnu.org>.\n";
6465
6466     exit 0;
6467 }
6468
6469 format USAGE_FORMAT =
6470   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
6471   $one,               $two,               $three,             $four
6472 .