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