8dd9b6fb80f87f2897de234e60014fe860cbe3f3
[platform/upstream/automake.git] / automake.in
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
4
5 eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
6     if $running_under_some_shell;
7
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994, 1995, 1996 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@drip.colorado.edu>.
28
29
30 # Parameters set by configure.  Not to be changed.
31 $VERSION = "@VERSION@";
32 $prefix = "@prefix@";
33 $am_dir = "@datadir@/@PACKAGE@";
34
35 # String constants.
36 $IGNORE_PATTERN = "^##([^#].*)?\$";
37 $WHITE_PATTERN = "^[ \t]*\$";
38 $COMMENT_PATTERN = "^#";
39 $RULE_PATTERN = "^([a-zA-Z_.][-.a-zA-Z0-9_.]*) *:";
40 $MACRO_PATTERN = "^([A-Za-z][A-Za-z0-9_]*)[ \t]*=[ \t]*(.*)\$";
41
42
43 # Constants to define the "strictness" level.
44 $NORMAL = 0;
45 $GNU = 1;
46 $GNITS = 2;
47
48 \f
49
50 # Variables global to entire run.
51
52 # Strictness level.
53 $strictness = $NORMAL;
54
55 # Name of strictness level.
56 $strictness_name = 'normal';
57
58 # This is TRUE if GNU make specific automatic dependency generation
59 # code should be included in generated Makefile.in.
60 $use_dependencies = 1;
61
62 # This holds our (eventual) exit status.  We don't actually exit until
63 # we have processed all input files.
64 $exit_status = 0;
65
66 # From the Perl manual.
67 $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
68
69 # TRUE if missing standard files should be installed.
70 $install_missing = 0;
71
72 \f
73
74 &initialize_global_constants;
75
76 # Parse command line.
77 @input_files = &parse_arguments (@ARGV);
78
79 # Now do all the work on each file.
80 foreach $am_file (@input_files)
81 {
82     if (! -f ($am_file . '.am'))
83     {
84         &am_error ('no such file');
85     }
86     else
87     {
88         &generate_makefile ($am_file);
89     }
90 }
91
92 exit $exit_status;
93
94
95 ################################################################
96
97 # Parse command line.
98 sub parse_arguments
99 {
100     local (@arglist) = @_;
101     local (@make_list);
102
103     while (@arglist)
104     {
105         if ($arglist[0] eq "--version")
106         {
107             print "Automake version $VERSION\n";
108             exit 0;
109         }
110         elsif ($arglist[0] eq "--help")
111         {
112             &usage;
113         }
114         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
115         {
116             $am_dir = $1;
117         }
118         elsif ($arglist[0] eq '--amdir')
119         {
120             &require_argument (@arglist);
121             shift (@arglist);
122             $am_dir = $arglist[0];
123         }
124         elsif ($arglist[0] =~ /^--strictness=(.+)$/)
125         {
126             &set_strictness ($1);
127         }
128         elsif ($arglist[0] eq '--gnu')
129         {
130             &set_strictness ('gnu');
131         }
132         elsif ($arglist[0] eq '--gnits')
133         {
134             &set_strictness ('gnits');
135         }
136         elsif ($arglist[0] eq '--strictness')
137         {
138             &require_argument (@arglist);
139             shift (@arglist);
140             &set_strictness ($arglist[0]);
141         }
142         elsif ($arglist[0] eq '--include-deps')
143         {
144             $use_dependencies = 0;
145         }
146         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
147         {
148             # Set output directory.
149             $output_directory = $1;
150         }
151         elsif ($arglist[0] eq '--output-dir')
152         {
153             &require_argument (@arglist);
154             shift (@arglist);
155             $output_directory = $arglist[0];
156         }
157         elsif ($arglist[0] eq '--install-missing')
158         {
159             $install_missing = 1;
160         }
161         elsif ($arglist[0] eq '--')
162         {
163             # Stop option processing.
164             shift (@arglist);
165             push (@make_list, @arglist);
166             last;
167         }
168         elsif ($arglist[0] =~ /^-/)
169         {
170             die "automake: unrecognized option -- \`$arglist[0]'\n";
171         }
172         else
173         {
174             push (@make_list, $arglist[0]);
175         }
176
177         shift (@arglist);
178     }
179
180     if (! @make_list)
181     {
182         # Look around for some files.
183         push (@make_list, 'Makefile') if -f 'Makefile.am';
184
185         foreach (<*/Makefile.am>)
186         {
187             s/\.am$//;
188             push (@make_list, $_);
189         }
190
191         die "automake: no \"Makefile.am\" found or specified\n"
192             if ! @make_list;
193     }
194
195     return (@make_list);
196 }
197
198 # Ensure argument exists, or die.
199 sub require_argument
200 {
201     local ($arg, @arglist) = @_;
202     die "automake: no argument given for option \`$arg'\n"
203         if ! @arglist;
204 }
205
206 ################################################################
207
208 # Generate a Makefile.in given the name of the corresponding Makefile.
209 sub generate_makefile
210 {
211     local ($makefile) = @_;
212
213     print "creating ", $makefile, ".in\n";
214
215     &initialize_per_input;
216     $relative_dir = &dirname ($makefile);
217     # FIXME with new 'dist' target, don't need Makefile.in.  Probably
218     # should remove it here.
219     &push_dist_common ('Makefile.in', 'Makefile.am');
220     push (@sources, '$(SOURCES)') if defined $contents{'SOURCES'};
221     push (@objects, '$(OBJECTS)') if defined $contents{'OBJECTS'};
222
223     # This is always the default target.  This gives us freedom to do
224     # things in whatever order is convenient.
225     $output_rules .= "default: all\n\n";
226     push (@phony, 'default');
227
228     &read_am_file ($makefile . '.am');
229
230     # Check first, because we might modify some state.
231     &check_gnu_standards;
232     &check_gnits_standards;
233
234     &handle_libraries;
235     &handle_programs;
236     &handle_scripts;
237
238     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
239     # on this (but currently does).
240     $contents{'SOURCES'} = join (' ', @sources);
241     $contents{'OBJECTS'} = join (' ', @objects);
242
243     &handle_texinfo;
244     &handle_man_pages;
245     &handle_data;
246     &handle_headers;
247     &handle_subdirs;
248     &handle_configure;
249     &handle_tags;
250     &handle_dist;
251     &handle_dependencies;
252     &handle_footer;
253     &handle_merge_targets;
254     &handle_installdirs;
255     &handle_clean;
256     &handle_phony;
257
258     if (! -d ($output_directory . '/' . $relative_dir))
259     {
260         &mkdir ($output_directory . '/' . $relative_dir);
261     }
262     if (! open (GM_FILE, "> " . $output_directory . '/' . $makefile . ".in"))
263     {
264         warn "automake: ${am_file}.in: cannot open: $!\n";
265         $exit_status = 1;
266         return;
267     }
268
269     print GM_FILE $output_vars;
270     print GM_FILE $output_rules;
271     print GM_FILE $output_trailer;
272
273     close (GM_FILE);
274 }
275
276 ################################################################
277
278 # Return object extension.  Just once, put some code into the output.
279 sub get_object_extension
280 {
281     if (! $dir_holds_sources)
282     {
283
284         # Boilerplate.
285         $output_vars .= &file_contents ('compile-vars');
286         $output_rules .= &file_contents ('compile');
287         &push_phony_cleaners ('compile');
288
289         # Check for automatic de-ANSI-fication.
290         $dir_holds_sources = '.o';
291         push (@suffixes, '.c', '.o');
292         push (@clean, 'compile');
293
294         if (defined $contents{'@kr@'})
295         {
296             $dir_holds_sources = '.$(kr)o';
297             push (@suffixes, '._c', '._o');
298
299             &require_file ($NORMAL, 'ansi2knr.c', 'ansi2knr.1');
300
301             $output_vars .= &file_contents ('kr-vars');
302             $output_rules .= &file_contents ('compile-kr');
303             $output_rules .= &file_contents ('clean-kr');
304
305             push (@clean, 'kr');
306             &push_phony_cleaners ('kr');
307         }
308     }
309     return $dir_holds_sources;
310 }
311
312 # Handle SOURCE->OBJECT transform for one program or library.
313 sub handle_source_transform
314 {
315     local ($one_file, $obj) = @_;
316
317     # Look for file_SOURCES and file_OBJECTS.
318     if (defined $contents{$one_file . "_SOURCES"})
319     {
320         if (! defined $contents{$one_file . "_OBJECTS"})
321         {
322             # Turn sources into objects.
323             local (@files) = split (/\s+/, $contents{$one_file . "_SOURCES"});
324             # Ugh: Perl syntax vs Emacs.
325             local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
326             local (@result) = ();
327             foreach (@files)
328             {
329                 if (/^(.*)\.[yl]$/)
330                 {
331                     # Automatically include generated .c file in
332                     # distribution.
333                     &push_dist_common ($1 . '.c');
334                 }
335
336                 # Transform source files into .o files.
337                 s/\.cc$/$obj/g;
338                 s/$krc1$/$obj/g;
339                 s/$krc2$/$obj/g;
340                 s/\.[cCmylfs]$/$obj/g;
341
342                 push (@result, $_);
343             }
344
345             &pretty_print ($one_file . "_OBJECTS =", '', @result);
346         }
347         else
348         {
349             &am_error ($one_file . '_OBJECTS', 'should not be defined');
350         }
351
352         push (@sources, '$(' . $one_file . "_SOURCES)");
353         push (@objects, '$(' . $one_file . "_OBJECTS)");
354     }
355     else
356     {
357         $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
358                          . $one_file . "_OBJECTS = ". $one_file
359                          . $obj . "\n");
360         push (@sources, $one_file . '.c');
361         push (@objects, $one_file . $obj);
362     }
363
364     if (defined $contents{'CONFIG_HEADER'})
365     {
366         $output_rules .= ('$(' . $one_file . "_OBJECTS): "
367                           . $contents{'CONFIG_HEADER'} . "\n");
368     }
369
370     return @result;
371 }
372
373 # Handle C programs.
374 sub handle_programs
375 {
376     local (@proglist) = &am_install_var ('-clean',
377                                          'programs', 'PROGRAMS',
378                                          'bin', 'sbin', 'libexec', 'noinst');
379     # FIXME error if PROGRAMS defined but no blah_PROGRAMS defined.
380     return if ! @proglist;
381
382     local ($obj) = &get_object_extension;
383     local ($one_file, $munge);
384
385     foreach $one_file (@proglist)
386     {
387         &handle_source_transform ($one_file, $obj);
388
389         if (! defined $contents{$one_file . "_LDADD"})
390         {
391             # User didn't define prog_LDADD override.  So do it.
392             $output_vars .= $one_file . '_LDADD = $(LDADD)' . "\n";
393         }
394
395         $output_rules .=
396             &file_contents_with_transform ('s/\@PROGRAM\@/' . $one_file
397                                            . '/go', 'program');
398     }
399 }
400
401 # Handle libraries.
402 sub handle_libraries
403 {
404     local (@liblist) = &am_install_var ('-no-all', '-clean',
405                                         'libraries', 'LIBRARIES',
406                                         'lib', 'pkglib', 'noinst');
407     # FIXME error if LIBRARIES defined but no blah_LIBRARIES defined.
408     return if ! @liblist;
409
410     # Generate _LIBFILES variables.  Too bad we can't do this in
411     # am_install_var.
412     local ($onedir, $onelib);
413     local (@outlist);
414     foreach $onedir ('lib', 'pkglib', 'noinst')
415     {
416         if (defined $contents{$onedir . '_LIBRARIES'})
417         {
418             @outlist = ();
419             foreach $onelib (split (/\s+/, $contents{$onedir . '_LIBRARIES'}))
420             {
421                 push (@outlist, 'lib' . $onelib . '.a');
422             }
423             &pretty_print ($onedir . '_LIBFILES =', '', @outlist);
424         }
425     }
426     push (@all, '$(LIBFILES)');
427
428     local ($obj) = &get_object_extension;
429     local ($munge);
430     foreach $onelib (@liblist)
431     {
432         if (! defined $contents{$onelib . '_LIBADD'})
433         {
434             # Generate support for conditional object inclusion in
435             # libraries.
436             $output_vars .= $onelib . "_LIBADD =\n";
437         }
438
439         &handle_source_transform ($onelib, $obj);
440
441         $output_rules .=
442             &file_contents_with_transform ('s/\@LIBRARY\@/' . $onelib . '/go',
443                                            'library');
444     }
445
446     # Turn "foo" into "libfoo.a" and include macro definition.
447     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
448
449     if (! defined $contents{'LIBFILES'})
450     {
451         &pretty_print ('LIBFILES = ', '', @liblist);
452     }
453     $output_vars .= &file_contents ('libraries-vars');
454 }
455
456 # Handle scripts.
457 sub handle_scripts
458 {
459     &am_install_var ('-clean',
460                      'scripts', 'SCRIPTS',
461                      'bin', 'sbin', 'libexec', 'noinst');
462 }
463
464 # Handle all Texinfo source.
465 sub handle_texinfo
466 {
467     local ($texis) = &am_variable ('TEXINFOS');
468     return if ! $texis;
469
470     local (@texis) = split (/\s+/, $texis);
471     if (@texis > 1)
472     {
473         &am_error ('sorry, only one file allowed in `TEXINFOS\'');
474         return;
475     }
476
477     local ($infobase);
478     ($infobase = $texis[0]) =~ s/\.texi$//;
479
480     # If 'version.texi' is referenced by input file, then include
481     # automatic versioning capability.
482     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
483             . " > /dev/null 2>&1");
484     if (! ($? >> 8))
485     {
486         # Got a hit.
487         push (@texis, 'version.texi');
488         &push_dist_common ('version.texi', 'stamp-vti');
489         push (@clean, 'vti');
490
491         $output_rules .=
492             &file_contents_with_transform ('s/\@TEXI\@/' . $texis[0] . '/go',
493                                            'texi-version');
494         &push_phony_cleaners ('vti');
495
496         &require_file ($NORMAL, 'mdate-sh');
497     }
498
499     # If user specified file_TEXINFOS, then use that as explicit
500     # dependency list.
501     if (defined $contents{$infobase . "_TEXINFOS"})
502     {
503         push (@texis, "\$" . $infobase . '_TEXINFOS');
504         &push_dist_common ("\$" . $infobase . '_TEXINFOS');
505     }
506
507     if (@texis)
508     {
509         $output_rules .= ($infobase . ".info: "
510                           . join (' ', @texis) . "\n\n");
511     }
512
513     # Some boilerplate.
514     $output_vars .= &file_contents ('texinfos-vars');
515     $output_rules .= &file_contents ('texinfos');
516     push (@phony, 'install-info', 'uninstall-info');
517
518     # How to clean.
519     $output_rules .=
520         &file_contents_with_transform ('s/\@TEXI\@/' . $infobase . '/go',
521                                        'texi-clean');
522     &push_phony_cleaners ('info');
523
524     push (@suffixes, '.texi', '.info', '.dvi');
525     push (@uninstall, 'uninstall-info');
526     push (@clean, 'info');
527     push (@info, '$(INFO_DEPS)');
528     push (@dvi, '$(DVIS)');
529     push (@installdirs, '$(infodir)');
530     # Make sure documentation is made and installed first.
531     unshift (@install_data, 'install-info');
532     unshift (@all, 'info');
533
534     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
535                      . "INFO_DEPS = " . $infobase . ".info\n"
536                      . "DVIS = " . $infobase . ".dvi\n\n");
537
538     # Do some error checking.
539     &require_file ($NORMAL, 'texinfo.tex');
540 }
541
542 # Handle any man pages.
543 sub handle_man_pages
544 {
545     return if ! defined $contents{'MANS'};
546
547     # We generate the manpage install code by hand to avoid the use of
548     # basename in the generated Makefile.
549     local (@mans) = split (/\s+/, $contents{'MANS'});
550     local (%sections, %inames, %secmap, %fullsecmap);
551     foreach (@mans)
552     {
553         # FIXME: statement without effect:
554         /^(.*)\.([0-9])([a-z]*)$/;
555         $sections{$2} = 1;
556         $inames{$1} = $_;
557         $secmap{$1} = $2;
558         $fullsecmap{$1} = $2 . $3;
559     }
560
561     # Generate list of install dirs.
562     $output_rules .= "install-man:\n";
563     foreach (keys %sections)
564     {
565         push (@installdirs, '$(mandir)/man' . $_);
566         $output_rules .= ("\t" . '$(top_srcdir)/mkinstalldirs $(mandir)/man'
567                           . $_ . "\n");
568     }
569     push (@phony, 'install-man');
570
571     # Generate install target.
572     local ($key);
573     foreach $key (keys %inames)
574     {
575         $_ = $install_man_format;
576         s/\@SECTION\@/$secmap{$key}/g;
577         s/\@MAN\@/$inames{$key}/g;
578         s/\@FULLSECT\@/$fullsecmap{$key}/g;
579         s/\@MANBASE\@/$key/g;
580         $output_rules .= $_;
581     }
582     $output_rules .= "\n";
583
584     $output_rules .= "uninstall-man:\n";
585     foreach $key (keys %inames)
586     {
587         $_ = $uninstall_man_format;
588         s/\@SECTION\@/$secmap{$key}/g;
589         s/\@MAN\@/$inames{$key}/g;
590         s/\@FULLSECT\@/$fullsecmap{$key}/g;
591         s/\@MANBASE\@/$key/g;
592         $output_rules .= $_;
593     }
594     $output_rules .= "\n";
595     push (@phony, 'uninstall-man');
596
597     $output_vars .= &file_contents ('mans-vars');
598
599     push (@install_data, 'install-man');
600     push (@uninstall, 'uninstall-man');
601     push (@all, '$(MANS)');
602 }
603
604 # Handle DATA variables.
605 sub handle_data
606 {
607     &am_install_var ('data', 'DATA', 'data', 'sysconf',
608                      'sharedstate', 'localstate', 'pkgdata',
609                      'noinst');
610 }
611
612 # Handle TAGS.
613 sub handle_tags
614 {
615     local ($tagging) = 0;
616
617     push (@phony, 'tags');
618     if (defined $contents{'SUBDIRS'})
619     {
620         $output_rules .= &file_contents ('tags');
621         $tagging = 1;
622     }
623     elsif ($dir_holds_sources || defined $contents{'ETAGS_ARGS'})
624     {
625         $output_rules .= &file_contents ('tags-subd');
626         push (@phony, 'id');
627         $tagging = 1;
628     }
629
630     if ($tagging)
631     {
632         $output_rules .= &file_contents ('tags-clean');
633         push (@clean, 'tags');
634         &push_phony_cleaners ('tags');
635     }
636     else
637     {
638         # Every Makefile must define some sort of TAGS rule.
639         # Otherwise, it would be possible for a top-level "make TAGS"
640         # to fail because some subdirectory failed.
641         $output_rules .= "tags: TAGS\nTAGS:\n\n";
642     }
643 }
644
645 # Handle 'dist' target.
646 sub handle_dist
647 {
648     # Look for common files that should be included in distribution.
649     local ($cfile);
650     foreach $cfile (@common_files)
651     {
652         if (-f ($relative_dir . "/" . $cfile))
653         {
654             &push_dist_common ($cfile);
655         }
656     }
657
658     # Keys of %dist_common are names of files to distributed.  We put
659     # README first because it then becomes easier to make a
660     # Usenet-compliant shar file (in these, README must be first).
661     # FIXME do more ordering of files here.
662     local (@coms);
663     if (defined $dist_common{'README'})
664     {
665         push (@coms, 'README');
666         undef $dist_common{'README'};
667     }
668     push (@coms, keys %dist_common);
669
670     &pretty_print ("DIST_COMMON =", '', @coms);
671     $output_vars .= "\n";
672
673     # Some boilerplate.
674     $output_vars .= &file_contents ('dist-vars');
675
676     # Initialization; only at top level.
677     if ($relative_dir eq '.')
678     {
679         $output_rules .=
680             (
681              # Some boilerplate.
682              '
683 distdir = $(PACKAGE)-$(VERSION)
684 dist: $(DISTFILES)
685 '
686              # Create dist directory.
687              . '        rm -rf $(distdir)
688         mkdir $(distdir)
689 '
690              # We need an absolute path for --output-dir.  Thus the
691              # weirdness.
692              . '        distdir=`cd $(distdir) && pwd` \\
693           && cd $(srcdir) \\
694           && automake --include-deps --output-dir=$$distdir --strictness='
695              # Set strictness of output.
696              . $strictness_name . "\n"
697              );
698     }
699     else
700     {
701         $output_rules .=
702             (
703              # Some boilerplate.
704              "\nsubdir = " . $relative_dir . "\n"
705              . 'distdir = $(PACKAGE)-$(VERSION)/$(subdir)
706 dist: $(DISTFILES)
707 '
708              );
709     }
710
711     # In loop, test for file existence because sometimes a file gets
712     # included in DISTFILES twice.  For example this happens when a
713     # single source file is used in building more than one program.
714     # Also, there are situations in which "ln" can fail.  For instance
715     # a file to distribute could actually be a cross-filesystem
716     # symlink -- this can easily happen if "gettextize" was run on the
717     # distribution.
718     $output_rules .= '  @for file in $(DISTFILES); do           \\
719           test -f $(distdir)/$$file \\
720           || ln $(srcdir)/$$file $(distdir)/$$file \\
721           || cp -p $(srcdir)/$$file $(distdir)/$$file; \\
722         done
723 ';
724
725     # If top level, create all dist subdirectories and do recursive
726     # build.
727     if ($relative_dir eq '.')
728     {
729         # Test for directory existence here because previous automake
730         # invocation might have created some directories.
731         $output_rules .= '      for subdir in $(SUBDIRS); do            \\
732           test -d $(distdir)/$$subdir           \\
733           || mkdir $(distdir)/$$subdir          \\
734           || exit 1;                            \\
735           chmod 777 $(distdir)/$$subdir;        \\
736           (cd $$subdir && $(MAKE) $@) || exit 1; \\
737         done
738 ';
739     }
740
741     # Make verbatim copies of some subdirectories if required.  This
742     # is a hack which might go away.
743     if (defined $contents{'DIST_SUBDIRS'})
744     {
745         $output_rules .= '      @for dir in $(DIST_SUBDIRS); do         \\
746           echo copying directory $$dir;         \\
747           tar -chf - $$dir | (cd $(distdir) && tar -xBpf -); \\
748         done
749 ';
750     }
751
752     # Finalize.
753     if ($relative_dir eq '.')
754     {
755         $output_rules .= '      chmod -R a+r $(distdir)
756         tar -chozf $(distdir).tar.gz $(distdir)
757         rm -rf $(distdir)
758 ';
759     }
760
761     push (@phony, 'dist');
762 }
763
764 # Handle auto-dependency code.
765 sub handle_dependencies
766 {
767     if ($use_dependencies)
768     {
769         # Include GNU-make-specific auto-dep code.
770         if ($dir_holds_sources)
771         {
772             $output_rules .= &file_contents ('depend');
773         }
774     }
775     else
776     {
777         # Include any auto-generated deps that are present.
778         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
779         {
780             local ($depfile);
781             local ($gpat) = $relative_dir . "/.deps/*.P";
782
783             foreach $depfile (<${gpat}>)
784             {
785                 if (! open (DEP_FILE, $depfile))
786                 {
787                     &am_error ("couldn't open $depfile", $!);
788                     next;
789                 }
790
791                 # Slurp entire file.
792                 $output_rules .= join ('', <DEP_FILE>);
793
794                 close (DEP_FILE);
795             }
796
797             $output_rules .= "\n";
798         }
799     }
800 }
801
802 # Handle subdirectories.
803 sub handle_subdirs
804 {
805     return if ! defined $contents{'SUBDIRS'};
806
807     $output_rules .= &file_contents ('subdirs');
808
809     # Push a bunch of phony targets.
810     local ($phonies);
811     foreach $phonies ('-data', '-exec', 'dirs')
812     {
813         push (@phony, 'install' . $phonies . '-recursive');
814         push (@phony, 'uninstall' . $phonies . '-recursive');
815     }
816     foreach $phonies ('all', 'check', 'installcheck', 'info', 'dvi')
817     {
818         push (@phony, $phonies . '-recursive');
819     }
820     &push_phony_cleaners ('recursive');
821
822     push (@all, "all-recursive");
823     push (@check, "check-recursive");
824     push (@installcheck, "installcheck-recursive");
825     push (@info, "info-recursive");
826     push (@dvi, "dvi-recursive");
827
828     $recursive_install = 1;
829 }
830
831 # Handle remaking and configure stuff.
832 sub handle_configure
833 {
834     if ($relative_dir ne '.')
835     {
836         # In subdirectory.
837         $output_rules .= &file_contents ('remake-subd');
838     }
839     else
840     {
841         &require_file ($NORMAL, 'configure.in');
842         # FIXME require 'configure'?  What if autoconf hasn't been run
843         # yet?
844
845         if (defined $contents{'SUBDIRS'})
846         {
847             # We required AC_PROG_MAKE_SET.
848             system ("grep AC_PROG_MAKE_SET configure.in > /dev/null 2>&1");
849             if ($? >> 8)
850             {
851                 # Nope.
852                 &am_error ("AC_PROG_MAKE_SET must be used in configure.in");
853             }
854         }
855
856         if (-f 'aclocal.m4')
857         {
858             $output_vars .= "ACLOCAL = aclocal.m4\n";
859             &push_dist_common ('aclocal.m4');
860         }
861         $output_rules .= &file_contents ('remake');
862
863         # Look for some files we need.
864         &require_file ($NORMAL, 'install-sh', 'mkinstalldirs');
865     }
866
867     if (defined $contents{'CONFIG_HEADER'}
868         && $contents{'CONFIG_HEADER'} !~ /\//)
869     {
870         # Header defined and in this directory.
871         if (-f 'acconfig.h')
872         {
873             $output_vars .= "ACCONFIG = acconfig.h\n";
874             &push_dist_common ('acconfig.h');
875         }
876         if (-f 'config.h.top')
877         {
878             $output_vars .= "CONFIG_TOP = config.h.top\n";
879             &push_dist_common ('config.h.top');
880         }
881         if (-f 'config.h.bot')
882         {
883             $output_vars .= "CONFIG_BOT = config.h.bot\n";
884             &push_dist_common ('config.h.bot');
885         }
886
887         &push_dist_common ('stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
888
889         $output_rules .= &file_contents ('remake-hdr');
890     }
891 }
892
893 # Handle C headers.
894 sub handle_headers
895 {
896     &am_install_var ('data', 'HEADERS', 'include',
897                      'oldinclude', 'pkginclude',
898                      'noinst');
899 }
900
901 # Handle footer elements.
902 sub handle_footer
903 {
904     if ($contents{'SOURCES'})
905     {
906         &pretty_print ('SOURCES =', '', split (/\s+/, $contents{'SOURCES'}));
907     }
908     if ($contents{'OBJECTS'})
909     {
910         &pretty_print ('OBJECTS =', '', split (/\s+/, $contents{'OBJECTS'}));
911     }
912     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
913     {
914         $output_vars .= "\n";
915     }
916
917     if (defined $contents{'SUFFIXES'})
918     {
919         push (@suffixes, '$(SUFFIXES)');
920     }
921
922     $output_trailer .= ".SUFFIXES:\n";
923     if (@suffixes)
924     {
925         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
926     }
927     $output_trailer .= &file_contents ('footer');
928 }
929
930 # Deal with installdirs target.
931 sub handle_installdirs
932 {
933     # GNU Makefile standards recommend this.
934     $output_rules .= ("installdirs:"
935                       . ($recursive_install
936                          ? " installdirs-recursive\n"
937                          : "\n"));
938     push (@phony, 'installdirs');
939     if (@installdirs)
940     {
941         &pretty_print_rule ("\t\$(top_srcdir)/mkinstalldirs ", "\t\t",
942                             @installdirs);
943     }
944     $output_rules .= "\n";
945 }
946
947 # There are several targets which need to be merged.  This is because
948 # their complete definition is compiled from many parts.  Note that we
949 # avoid double colon rules, otherwise we'd use them instead.
950 sub handle_merge_targets
951 {
952     &do_one_merge_target ('all', @all);
953     &do_one_merge_target ('info', @info);
954     &do_one_merge_target ('dvi', @dvi);
955
956     if (! defined $contents{'SUBDIRS'} || $relative_dir ne '.')
957     {
958         # 'check' must depend on 'all', but not at top level.
959         push (@check, 'all');
960     }
961     &do_one_merge_target ('check', @check);
962     &do_one_merge_target ('installcheck', @installcheck);
963
964     # Handle the various install targets specially.  We do this so
965     # that (eg) "make install-exec" will run "install-exec-recursive"
966     # if required, but "make install" won't run it twice.  Step one is
967     # to see if the user specified local versions of any of the
968     # targets we handle.
969     if (defined $contents{'install-exec-local'})
970     {
971         push (@install_exec, 'install-exec-local');
972     }
973     if (defined $contents{'install-data-local'})
974     {
975         push (@install_data, 'install-data-local');
976     }
977     if (defined $contents{'uninstall-local'})
978     {
979         push (@uninstall, 'uninstall-local');
980     }
981
982     if (defined $contents{'install-local'})
983     {
984         &am_error ("use \`install-data' or \`install-exec', not \`install'");
985     }
986
987     # Step two: if we are doing recursive makes, write out the
988     # appropriate rules.
989     local (@install);
990     if ($recursive_install)
991     {
992         push (@install, 'install-recursive');
993         push (@uninstall, 'uninstall-recursive');
994
995         if (@install_exec)
996         {
997             $output_rules .= ('install-exec-am: '
998                               . join (' ', @install_exec)
999                               . "\n\n");
1000             @install_exec = ('install-exec-recursive', 'install-exec-am');
1001             push (@install, 'install-exec-am');
1002             push (@phony, 'install-exec-am');
1003         }
1004         if (@install_data)
1005         {
1006             $output_rules .= ('install-data-am: '
1007                               . join (' ', @install_data)
1008                               . "\n\n");
1009             @install_data = ('install-data-recursive', 'install-data-am');
1010             push (@install, 'install-data-am');
1011             push (@phony, 'install-data-am');
1012         }
1013         if (@uninstall)
1014         {
1015             $output_rules .= ('uninstall-am: '
1016                               . join (' ', @uninstall)
1017                               . "\n\n");
1018             @uninstall = ('uninstall-recursive', 'uninstall-am');
1019             push (@phony, 'uninstall-am');
1020         }
1021     }
1022
1023     # Step three: print definitions users can use.
1024     if (@install_exec)
1025     {
1026         $output_rules .= ("install-exec: "
1027                           . join (' ', @install_exec)
1028                           . "\n\n");
1029         push (@install, 'install-exec') if !$recursive_install;
1030         push (@phony, 'install-exec');
1031     }
1032     if (@install_data)
1033     {
1034         $output_rules .= ("install-data: "
1035                           . join (' ', @install_data)
1036                           . "\n\n");
1037         push (@install, 'install-data') if !$recursive_install;
1038         push (@phony, 'install-data');
1039     }
1040
1041     # If no dependencies for 'install', add 'all'.  Why?  That way
1042     # "make install" at top level of distclean'd distribution won't
1043     # fail because stuff in 'lib' fails to build.
1044     push (@install, 'all') if ! @install;
1045     $output_rules .= ('install: '
1046                       . join (' ', @install)
1047                       # Use "@:" as empty command so nothing prints.
1048                       . "\n\t\@:"
1049                       . "\n\n"
1050                       . 'uninstall: '
1051                       . join (' ', @uninstall)
1052                       . "\n\n");
1053     push (@phony, 'install', 'uninstall');
1054 }
1055
1056 # Helper for handle_merge_targets.
1057 sub do_one_merge_target
1058 {
1059     local ($name, @values) = @_;
1060
1061     if (defined $contents{$name . '-local'})
1062     {
1063         # User defined local form of target.  So include it.
1064         push (@values, $name . '-local');
1065         push (@phony, $name . '-local');
1066     }
1067
1068     $output_rules .= $name . ":";
1069     if (@values)
1070     {
1071         $output_rules .= ' ' . join (' ', @values);
1072     }
1073     $output_rules .= "\n\n";
1074     push (@phony, $name);
1075 }
1076
1077 # Handle all 'clean' targets.
1078 sub handle_clean
1079 {
1080     push (@clean, 'generic');
1081     $output_rules .= &file_contents ('clean');
1082     &push_phony_cleaners ('generic');
1083
1084     local ($target) = $recursive_install ? 'clean-am' : 'clean';
1085     &do_one_clean_target ($target, 'mostly', '', @clean);
1086     &do_one_clean_target ($target, '', 'mostly', @clean);
1087     &do_one_clean_target ($target, 'dist', '', @clean);
1088     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
1089
1090     push (@phony, 'clean', 'mostlyclean', 'distclean', 'maintainer-clean');
1091
1092     local (@deps);
1093     if ($recursive_install)
1094     {
1095         @deps = ('am', 'recursive');
1096         &do_one_clean_target ('', 'mostly', '', @deps);
1097         &do_one_clean_target ('', '', '', @deps);
1098         &do_one_clean_target ('', 'dist', '', @deps);
1099         &do_one_clean_target ('', 'maintainer-', '', @deps);
1100     }
1101 }
1102
1103 # Helper for handle_clean.
1104 sub do_one_clean_target
1105 {
1106     local ($target, $name, $last_name, @deps) = @_;
1107
1108     # Special case: if target not passed, then don't generate
1109     # dependency on next "lower" clean target (eg no
1110     # clean<-mostlyclean derivation).  In this case the target is
1111     # implicitly known to be 'clean'.
1112     local ($flag) = $target;
1113     $target = 'clean' if ! $flag;
1114
1115     grep (($_ = $name . 'clean-' . $_) && 0, @deps);
1116     if ($flag)
1117     {
1118         if ($last_name || $name ne 'mostly')
1119         {
1120             push (@deps, $last_name . $target . " ");
1121         }
1122     }
1123     &pretty_print_rule ($name . $target . ": ", '', @deps);
1124
1125     # FIXME shouldn't we really print these messages before running
1126     # the dependencies?
1127     if ($name . $target eq 'maintainer-clean')
1128     {
1129         # Print a special warning.
1130         $output_rules .=
1131             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
1132              . "\t\@echo \"it deletes files that may require special "
1133              . "tools to rebuild.\"\n"
1134              . "\trm -f config.status\n");
1135     }
1136     elsif ($name . $target eq 'distclean')
1137     {
1138         $output_rules .= "\trm -f config.status\n";
1139     }
1140     $output_rules .= "\n";
1141 }
1142
1143 # Handle .PHONY target.
1144 sub handle_phony
1145 {
1146     &pretty_print_rule ('.PHONY:', '', @phony);
1147     $output_rules .= "\n";
1148 }
1149
1150 ################################################################
1151
1152 # Do any extra checking for GNU standards.
1153 sub check_gnu_standards
1154 {
1155     &require_file ($GNU, 'ChangeLog');
1156
1157     if ($relative_dir eq '.')
1158     {
1159         # In top level (or only) directory.
1160         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
1161                        'AUTHORS');
1162     }
1163 }
1164
1165 # Do any extra checking for GNITS standards.
1166 sub check_gnits_standards
1167 {
1168     if ($strictness >= $GNITS && -f $relative_dir . '/COPYING.LIB')
1169     {
1170         &am_error
1171             ("\`${relative_dir}/COPYING.LIB' disallowed by Gnits standards");
1172     }
1173
1174     if ($relative_dir eq '.')
1175     {
1176         # In top level (or only) directory.
1177         &require_file ($GNITS, 'THANKS');
1178     }
1179 }
1180
1181 ################################################################
1182
1183 # Pretty-print something.  HEAD is what should be printed at the
1184 # beginning of the first line, FILL is what should be printed at the
1185 # beginning of every subsequent line.
1186 sub pretty_print_internal
1187 {
1188     local ($head, $fill, @values) = @_;
1189
1190     local ($column) = length ($head);
1191     local ($result) = $head;
1192
1193     local ($bol) = 0;
1194     foreach (@values)
1195     {
1196         # "71" because we also print a space.
1197         if ($column + length ($_) > 71)
1198         {
1199             $result .= " \\\n" . $fill;
1200             $column = length ($fill);
1201             $bol = 1;
1202         }
1203
1204         $result .= ' ' unless ($bol);
1205         $result .= $_;
1206         $column += length ($_) + 1;
1207         $bol = 0;
1208     }
1209
1210     $result .= "\n";
1211     return $result;
1212 }
1213
1214 # Pretty-print something and append to output_vars.
1215 sub pretty_print
1216 {
1217     $output_vars .= &pretty_print_internal (@_);
1218 }
1219
1220 # Pretty-print something and append to output_rules.
1221 sub pretty_print_rule
1222 {
1223     $output_rules .= &pretty_print_internal (@_);
1224 }
1225
1226
1227 ################################################################
1228
1229 # Read Makefile.am and set up %contents.  Simultaneously copy lines
1230 # from Makefile.am into $output_trailer or $output_vars as
1231 # appropriate.  NOTE we put rules in the trailer section.  We want
1232 # user rules to come after our generated stuff.
1233 sub read_am_file
1234 {
1235     local ($amfile) = @_;
1236
1237     local ($header_vars) = &file_contents ('header-vars');
1238
1239     open (AM_FILE, $amfile) || die "automake: couldn't open $amfile: $!\n";
1240
1241     $output_vars .= ("# Makefile.in generated automatically by automake "
1242                      . $VERSION . " from Makefile.am\n");
1243
1244     # Generate copyright for generated Makefile.in.
1245     $output_vars .= $gen_copyright;
1246
1247     local ($saw_bk) = 0;
1248     local ($was_rule) = 0;
1249     local ($spacing) = '';
1250     local ($comment) = '';
1251     local ($last_var_name) = '';
1252
1253     while (<AM_FILE>)
1254     {
1255         if (/$IGNORE_PATTERN/o)
1256         {
1257             # Merely delete comments beginning with two hashes.
1258         }
1259         elsif (/$WHITE_PATTERN/o)
1260         {
1261             # Stick a single white line before the incoming macro or rule.
1262             $spacing = "\n";
1263         }
1264         elsif (/$COMMENT_PATTERN/o)
1265         {
1266             # Stick comments before the incoming macro or rule.
1267             $comment .= $spacing . $_;
1268             $spacing = '';
1269         }
1270         else
1271         {
1272             last;
1273         }
1274     }
1275
1276     $output_vars .= $comment . "\n" . $header_vars;
1277     $comment = '';
1278     $spacing = "\n";
1279
1280     while ($_)
1281     {
1282         if (/$IGNORE_PATTERN/o)
1283         {
1284             # Merely delete comments beginning with two hashes.
1285         }
1286         elsif (/$WHITE_PATTERN/o)
1287         {
1288             # Stick a single white line before the incoming macro or rule.
1289             $spacing = "\n";
1290         }
1291         elsif (/$COMMENT_PATTERN/o)
1292         {
1293             # Stick comments before the incoming macro or rule.
1294             $comment .= $spacing . $_;
1295             $spacing = '';
1296         }
1297         elsif ($saw_bk)
1298         {
1299             if ($was_rule)
1300             {
1301                 $output_trailer .= $_;
1302                 $saw_bk = /\\$/;
1303             }
1304             else
1305             {
1306                 $output_vars .= $_;
1307                 $saw_bk = /\\$/;
1308                 # Chop newline and backslash if this line is
1309                 # continued.  FIXME maybe ensure trailing whitespace
1310                 # exists?
1311                 chop if $saw_bk;
1312                 chop if $saw_bk;
1313                 $contents{$last_var_name} .= $_;
1314             }
1315         }
1316         elsif (/$RULE_PATTERN/o)
1317         {
1318             # warn "** Saw rule .$1.\n";
1319             # Found a rule.
1320             $was_rule = 1;
1321             # Value here doesn't matter; for targets we only note
1322             # existence.
1323             $contents{$1} = 1;
1324             $output_trailer .= $comment . $spacing . $_;
1325             $comment = $spacing = '';
1326             $saw_bk = /\\$/;
1327         }
1328         elsif (/$MACRO_PATTERN/o)
1329         {
1330             # warn "** Saw macro .$1.\n";
1331             # Found a macro definition.
1332             $was_rule = 0;
1333             $last_var_name = $1;
1334             if (substr ($2, -1) eq "\\")
1335             {
1336                 $contents{$1} = substr ($2, 0, length ($2) - 1);
1337             }
1338             else
1339             {
1340                 $contents{$1} = $2;
1341             }
1342             $output_vars .= $comment . $spacing . $_;
1343             $comment = $spacing = '';
1344             $saw_bk = /\\$/;
1345         }
1346         elsif ($_ eq "\@kr\@\n")
1347         {
1348             # Special case: this means we want automatic
1349             # de-ANSI-fication.  FIXME think of a better way.
1350             $contents{'@kr@'} = 1;
1351         }
1352         else
1353         {
1354             # This isn't an error; it is probably a continued rule.
1355             # In fact, this is what we assume.
1356             $was_rule = 1;
1357             $output_trailer .= $comment . $spacing . $_;
1358             $comment = $spacing = '';
1359             $saw_bk = /\\$/;
1360         }
1361
1362         $_ = <AM_FILE>;
1363     }
1364
1365     $output_trailer .= $comment;
1366 }
1367
1368 ################################################################
1369
1370 sub initialize_global_constants
1371 {
1372     # Associative array of standard directory names.  Entry is TRUE if
1373     # corresponding directory should be installed during
1374     # 'install-exec' phase.
1375     %exec_dir_p =
1376         ('bin', 1,
1377          'sbin', 1,
1378          'libexec', 1,
1379          'data', 0,
1380          'sysconf', 1,
1381          'localstate', 1,
1382          'lib', 1,
1383          'info', 0,
1384          'man', 0,
1385          'include', 0,
1386          'oldinclude', 0,
1387          'pkgdata', 0,
1388          'pkglib', 1,
1389          'pkginclude', 0
1390          );
1391
1392     # Helper text for dealing with man pages.
1393     $install_man_format =
1394     '   @sect=@SECTION@;                                \\
1395         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1396         echo installing @MAN@ as $(mandir)/man$$sect/$$inst; \\
1397         $(INSTALL_DATA) $(srcdir)/@MAN@ $(mandir)/man$$sect/$$inst
1398 ';
1399
1400     $uninstall_man_format =
1401     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1402         rm -f $(mandir)/man@SECTION@/$$inst
1403 ';
1404
1405     # Commonly found files we look for and automatically include in
1406     # DISTFILES.
1407     @common_files =
1408         (
1409          "README", "THANKS", "TODO", "NEWS", "COPYING", "COPYING.LIB",
1410          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
1411          "config.guess", "config.sub", "AUTHORS", "BACKLOG", "ABOUT-GNU"
1412          );
1413
1414     # Commonly used files we auto-include, but only sometimes.
1415     @common_sometimes =
1416         (
1417          "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
1418          "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
1419          "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
1420          );
1421
1422     $USAGE = "\
1423   --amdir=DIR           directory storing config files
1424   --gnits               same as --strictness=gnits
1425   --gnu                 same as --strictness=gnu
1426   --help                print this help, then exit
1427   --include-deps        include generated dependencies in Makefile.in
1428   --install-missing     install missing standard files
1429   --output-dir=DIR      put generated Makefile.in's into DIR
1430   --strictness=LEVEL    set strictness level.  LEVEL is normal, gnu, gnits
1431   --version             print version number, then exit\n";
1432
1433     # Copyright on generated Makefile.ins.
1434     $gen_copyright = "\
1435 # Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
1436 # This Makefile.in is free software; the Free Software Foundation
1437 # gives unlimited permission to copy, distribute and modify it.
1438 "
1439 }
1440
1441 # (Re)-Initialize per-Makefile.am variables.
1442 sub initialize_per_input
1443 {
1444     # These two variables are used when generating each Makefile.in.
1445     # They hold the Makefile.in until it is ready to be printed.
1446     $output_rules = '';
1447     $output_vars = '';
1448     $output_trailer = '';
1449
1450     # Suffixes found during a run.
1451     @suffixes = ();
1452
1453     # This holds the contents of a Makefile.am, as parsed by
1454     # read_am_file.
1455     %contents = ();
1456
1457     # This holds the "relative directory" of the current Makefile.in.
1458     # Eg for src/Makefile.in, this is "src".
1459     $relative_dir = '';
1460
1461     # Directory where output files go.  Actually, output files are
1462     # relative to this directory.
1463     $output_directory = '.';
1464
1465     # This holds a list of files that are included in the
1466     # distribution.
1467     %dist_common = ();
1468
1469     # List of dependencies for the obvious targets.
1470     @install_data = ();
1471     @install_exec = ();
1472     @uninstall = ();
1473     @installdirs = ();
1474
1475     @info = ();
1476     @dvi = ();
1477     @all = ();
1478     @check = ();
1479     @installcheck = ();
1480     @clean = ();
1481
1482     @phony = ();
1483
1484     # These are pretty obvious, too.  They are used to define the
1485     # SOURCES and OBJECTS variables.
1486     @sources = ();
1487     @objects = ();
1488
1489     # TRUE if current directory holds any C source files.  (Actually
1490     # holds object extension, but this information is encapsulated in
1491     # the function get_object_extension).
1492     $dir_holds_sources = '';
1493
1494     # TRUE if install targets should work recursively.
1495     $recursive_install = 0;
1496 }
1497
1498
1499 ################################################################
1500
1501 # Return contents of a file from $am_dir, automatically skipping
1502 # macros or rules which are already known.  Runs command on each line
1503 # as it is read; this command can modify $_.
1504 sub file_contents_with_transform
1505 {
1506     local ($command, $basename) = @_;
1507     local ($file) = $am_dir . '/' . $basename . '.am';
1508
1509     open (FC_FILE, $file)
1510         || die "automake: installation error: cannot open \"$file\"\n";
1511
1512     local ($was_rule) = 0;
1513     local ($result_vars) = '';
1514     local ($result_rules) = '';
1515     local ($comment) = '';
1516     local ($spacing) = "\n";
1517     local ($skipping) = 0;
1518
1519     while (<FC_FILE>)
1520     {
1521         eval $command;
1522
1523         if (/$IGNORE_PATTERN/o)
1524         {
1525             # Merely delete comments beginning with two hashes.
1526         }
1527         elsif (/$WHITE_PATTERN/o)
1528         {
1529             # Stick a single white line before the incoming macro or rule.
1530             $spacing = "\n";
1531         }
1532         elsif (/$COMMENT_PATTERN/o)
1533         {
1534             # Stick comments before the incoming macro or rule.
1535             $comment .= $spacing . $_;
1536             $spacing = '';
1537         }
1538         elsif ($saw_bk)
1539         {
1540             if ($was_rule)
1541             {
1542                 $result_rules .= $_ if ! $skipping;
1543             }
1544             else
1545             {
1546                 $result_vars .= $_ if ! $skipping;
1547             }
1548             $saw_bk = /\\$/;
1549         }
1550         elsif (/$RULE_PATTERN/o)
1551         {
1552             # warn "** Found rule .$1.\n";
1553             # Found a rule.
1554             $was_rule = 1;
1555             $skipping = defined $contents{$1};
1556             # warn "** Skip $skipping\n" if $skipping;
1557             $result_rules .= $comment . $spacing . $_ if ! $skipping;
1558             $comment = $spacing = '';
1559             $saw_bk = /\\$/;
1560         }
1561         elsif (/$MACRO_PATTERN/o)
1562         {
1563             # warn "** Found macro .$1.\n";
1564             # Found a variable reference.
1565             $was_rule = 0;
1566             $skipping = defined $contents{$1};
1567             # warn "** Skip $skipping\n" if $skipping;
1568             $result_vars .= $comment . $spacing . $_ if ! $skipping;
1569             $comment = $spacing = '';
1570             $saw_bk = /\\$/;
1571         }
1572         else
1573         {
1574             # This isn't an error; it is probably a continued rule.
1575             # In fact, this is what we assume.
1576             $was_rule = 1;
1577             $result_rules .= $comment . $spacing . $_ if ! $skipping;
1578             $comment = $spacing = '';
1579             $saw_bk = /\\$/;
1580         }
1581     }
1582
1583     close (FC_FILE);
1584     return $result_vars . $result_rules . $comment;
1585 }
1586
1587 # Like file_contents_with_transform, but no transform.
1588 sub file_contents
1589 {
1590     return &file_contents_with_transform ('', @_);
1591 }
1592
1593 # Return contents of some Makefile.am variable.  Allow for AM_ style
1594 # overrides.
1595 sub am_variable
1596 {
1597     local ($varname) = @_;
1598
1599     return (defined $contents{'AM_' . $varname}
1600             ? $contents{'AM_' . $varname}
1601             : $contents{$varname});
1602 }
1603
1604 # Handle `where_HOW' variable magic.  Does all lookups, generates
1605 # install code,and possibly generates code to define the primary
1606 # variable.  The first argument is the name of the .am file to munge,
1607 # the second argument is the primary variable (eg HEADERS), and all
1608 # subsequent arguments are possible installation locations.  Returns
1609 # list of all values of all _HOW targets.
1610 #
1611 # FIXME this should be rewritten to be cleaner.  It should be broken
1612 # up into multiple functions.
1613 #
1614 # Usage is: am_install_var (OPTION..., file, HOW, where...)
1615 sub am_install_var
1616 {
1617     local (@args) = @_;
1618
1619     local ($do_all, $do_clean) = (1, 0);
1620     while (@args)
1621     {
1622         if ($args[0] eq '-clean')
1623         {
1624             $do_clean = 1;
1625         }
1626         elsif ($args[0] eq '-no-all')
1627         {
1628             $do_all = 0;
1629         }
1630         elsif ($args[0] !~ /^-/)
1631         {
1632             last;
1633         }
1634         shift (@args);
1635     }
1636     local ($file, $primary, @prefixes) = @args;
1637
1638     local (@used) = ();
1639     local (@result) = ();
1640
1641     local ($clean_file) = $file . '-clean';
1642     local ($one_name);
1643     local ($X);
1644     foreach $X (@prefixes)
1645     {
1646         $one_name = $X . '_' . $primary;
1647         if (defined $contents{$one_name})
1648         {
1649             # Append actual contents to result.
1650             push (@result, split (/\s+/, $contents{$one_name}));
1651
1652             if ($do_clean)
1653             {
1654                 $output_rules .=
1655                     &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
1656                                                    $clean_file);
1657
1658                 push (@clean, $X . $primary);
1659                 &push_phony_cleaners ($X . $primary);
1660             }
1661
1662             push (@used, '$(' . $one_name . ')');
1663             if ($X eq 'noinst')
1664             {
1665                 # Objects in noinst_FOO never get installed.
1666                 next;
1667             }
1668
1669             $output_rules .=
1670                 &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
1671                                                $file);
1672
1673             push (@uninstall, 'uninstall-' . $X . $primary);
1674             push (@phony, 'uninstall-' . $X . $primary);
1675             push (@installdirs, '$(' . $X . 'dir)');
1676             if ($exec_dir_p{$X})
1677             {
1678                 push (@install_exec, 'install-' . $X . $primary);
1679             }
1680             else
1681             {
1682                 push (@install_data, 'install-' . $X . $primary);
1683             }
1684         }
1685     }
1686
1687     if (! defined $contents{$primary} && @used)
1688     {
1689         # Define it.
1690         &pretty_print ($primary . ' =', '', @used);
1691         $output_vars .= "\n";
1692     }
1693
1694     # Push here because PRIMARY might be configure time determined.
1695     push (@all, '$(' . $primary . ')') if ($do_all && @used);
1696
1697     # Look for misspellings.  It is an error to have a variable ending
1698     # in a "reserved" suffix whose prefix is unknown, eg
1699     # "bni_PROGRAMS".
1700     local (%valid, $varname);
1701     grep ($valid{$_} = 0, @prefixes);
1702     foreach $varname (keys %contents)
1703     {
1704         if ($varname =~ /^(.*)_$primary$/o && ! defined $valid{$1})
1705         {
1706             &am_error ("invalid variable \"$varname\"");
1707         }
1708     }
1709
1710     return (@result);
1711 }
1712
1713
1714 ################################################################
1715
1716 # Verify that the file must exist in the current directory.
1717 # Usage: require_file (strictness, file)
1718 # strictness is the strictness level at which this file becomes
1719 # required.
1720 sub require_file
1721 {
1722     local ($mystrict, @files) = @_;
1723     local ($file, $fullfile);
1724
1725     foreach $file (@files)
1726     {
1727         $fullfile = $relative_dir . "/" . $file;
1728
1729         if (-f $fullfile)
1730         {
1731             &push_dist_common ($file);
1732         }
1733         elsif ($install_missing && -f ($am_dir . '/' . $file))
1734         {
1735             # Install the missing file.  Symlink if we can, copy if we must.
1736             if ($symlink_exists)
1737             {
1738                 symlink ($am_dir . '/' . $file, $fullfile);
1739             }
1740             else
1741             {
1742                 system ('cp', $am_dir . '/' . $file, $fullfile);
1743             }
1744             &am_error ("required file \"$fullfile\" not found; installing");
1745         }
1746         elsif ($strictness >= $mystrict)
1747         {
1748             # Only an error if strictness constraint violated.
1749             &am_error ("required file \"$fullfile\" not found");
1750         }
1751     }
1752 }
1753
1754 # Push a list of files onto dist_common.
1755 sub push_dist_common
1756 {
1757     local (@files) = @_;
1758     local ($file);
1759
1760     foreach $file (@files)
1761     {
1762         $dist_common{$file} = 1;
1763     }
1764 }
1765
1766 # Push a list of clean targets onto phony.
1767 sub push_phony_cleaners
1768 {
1769     local ($base) = @_;
1770     local ($target);
1771     foreach $target ('mostly', 'dist', '', 'maintainer-')
1772     {
1773         push (@phony, $target . 'clean-' . $base);
1774     }
1775 }
1776
1777 # Set strictness.
1778 sub set_strictness
1779 {
1780     $strictness_name = $_[0];
1781     if ($strictness_name eq 'gnu')
1782     {
1783         $strictness = $GNU;
1784     }
1785     elsif ($strictness_name eq 'gnits')
1786     {
1787         $strictness = $GNITS;
1788     }
1789     elsif ($strictness_name eq 'normal')
1790     {
1791         $strictness = $NORMAL;
1792     }
1793     else
1794     {
1795         die "automake: level \`$strictness_name' not recognized\n";
1796     }
1797 }
1798
1799
1800 ################################################################
1801
1802 # Return directory name of file.
1803 sub dirname
1804 {
1805     local ($file) = @_;
1806     local ($sub);
1807
1808     ($sub = $file) =~ s,/+[^/]+,,g;
1809     $sub = '.' if $sub eq $file;
1810     return $sub;
1811 }
1812
1813 # Make a directory.
1814 sub mkdir
1815 {
1816     local ($dirname) = @_;
1817     system ("mkdir", $dirname);
1818 }
1819
1820 ################################################################
1821
1822 # Print an error message and set exit status.
1823 sub am_error
1824 {
1825     warn "automake: ${am_file}.am: ", join (' ', @_), "\n";
1826     $exit_status = 1;
1827 }
1828
1829 # Print usage information.
1830 sub usage
1831 {
1832     print "Usage: automake [OPTION] ... [Makefile]...\n";
1833     print $USAGE;
1834     print "\nFiles which are automatically distributed, if found:\n";
1835     $~ = "USAGE_FORMAT";
1836     local (@lcomm) = sort ((@common_files, @common_sometimes));
1837     local ($one, $two, $three, $four);
1838     while (@lcomm > 0)
1839     {
1840         $one = shift @lcomm;
1841         $two = @lcomm ? shift @lcomm : '';
1842         $three = @lcomm ? shift @lcomm : '';
1843         $four = @lcomm ? shift @lcomm : '';
1844         write;
1845     }
1846
1847     exit 0;
1848 }
1849
1850 format USAGE_FORMAT =
1851   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
1852   $one,               $two,               $three,             $four
1853 .