($(OBJECTS)): Depend on $(ANSI2KNR), not ansi2knr.
[automake.git] / automake.in
blobd8deb605cf9e74d9b5810fe53f7f749ee009f14b
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval "exec /usr/local/bin/perl -S $0 $*"
6     if $running_under_some_shell;
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994 Free Software Foundation, Inc.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
26 # Perl reimplementation by Tom Tromey <tromey@drip.colorado.edu>.
29 # Parameters set by configure.  Not to be changed.
30 $PACKAGE = "@PACKAGE@";
31 $VERSION = "@VERSION@";
32 $prefix = "@prefix@";
33 $am_dir = "@datadir@/@PACKAGE@";
37 # This is TRUE if GNU make specific automatic dependency generation
38 # code should be included in generated Makefile.in.
39 $use_dependencies = 1;
41 # This holds our (eventual) exit status.  We don't actually exit until
42 # we have processed all input files.
43 $exit_status = 0;
45 # These two variables are used when generating each Makefile.in.  They
46 # hold the Makefile.in until it is ready to be printed.
47 $output_rules = '';
48 $output_vars = '';
49 $output_trailer = '';
51 # Suffixes found during a run.
52 @suffixes = ();
54 # This holds the contents of a Makefile.am, as parsed by read_am_file.
55 %contents = ();
57 # This holds the "relative directory" of the current Makefile.in.  Eg
58 # for src/Makefile.in, this is "src".
59 $relative_dir = '';
61 # Directory where output files go.  Actually, output files are
62 # relative to this directory.
63 $output_directory = '.';
65 # This holds a list of files that are included in the distribution.
66 @dist_common = ();
68 # List of dependencies for the obvious targets.
69 @install_data = ();
70 @install_exec = ();
71 @uninstall = ();
72 @installdirs = ();
74 @info = ();
75 @dvi = ();
76 @all = ('${ALL}');
77 @check = ();
78 @clean = ();
80 # TRUE if current directory holds any C source files.
81 $dir_holds_sources = 0;
83 # TRUE if install targets should work recursively.
84 $recursive_install = 0;
88 &init_globals;
90 # Parse command line.
91 @input_files = &parse_arguments (@ARGV);
93 # Now do all the work on each file.
94 foreach $am_file (@input_files)
96     if (! -f ($am_file . '.am'))
97     {
98         print STDERR "automake: $am_file" . ".am: no such file\n";
99         $exit_status = 1;
100     }
101     else
102     {
103         &generate_makefile ($am_file);
104     }
107 exit $exit_status;
110 ################################################################
112 # Parse command line.
113 sub parse_arguments
115     local (@arglist) = @_;
116     local (@make_list);
118     while ($#arglist >= 0)
119     {
120         if ($arglist[0] eq "--version")
121         {
122             print "Automake version $VERSION\n";
123             exit 0;
124         }
125         elsif ($arglist[0] eq "--help")
126         {
127             &usage;
128         }
129         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
130         {
131             $am_dir = $1;
132         }
133         elsif ($arglist[0] eq '--amdir')
134         {
135             if ($#arglist == 0)
136             {
137                 print STDERR
138                     "automake: no argument given for option \`$arglist[0]'\n";
139                 exit 1;
140             }
141             shift (@arglist);
142             $am_dir = $arglist[0];
143         }
144         elsif ($arglist[0] eq '--include-deps')
145         {
146             $use_dependencies = 0;
147         }
148         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
149         {
150             # Set output directory.
151             $output_directory = $1;
152         }
153         elsif ($arglist[0] eq '--output-dir')
154         {
155             if ($#arglist == 0)
156             {
157                 print STDERR
158                     "automake: no argument given for option \`$arglist[0]'\n";
159                 exit 1;
160             }
161             shift (@arglist);
162             $output_directory = $arglist[0];
163         }
164         elsif ($arglist[0] eq '--')
165         {
166             # Stop option processing.
167             shift (@arglist);
168             push (@make_list, @arglist);
169             last;
170         }
171         elsif ($arglist[0] =~ /^-/)
172         {
173             print STDERR "automake: unrecognized option -- \`$arglist[0]'\n";
174             exit 1;
175         }
176         else
177         {
178             push (@make_list, $arglist[0]);
179         }
181         shift (@arglist);
182     }
184     if ($#make_list < 0)
185     {
186         # Look around for some files.
187         push (@make_list, 'Makefile') if -f 'Makefile.am';
189         foreach (<*/Makefile.am>)
190         {
191             s/\.am$//;
192             push (@make_list, $_);
193         }
195         if ($#make_list >= 0)
196         {
197             print "automake: using ", join (' ', @make_list), "\n";
198         }
199         else
200         {
201             print STDERR "automake: no \"Makefile.am\" found or specified\n";
202             exit 1;
203         }
204     }
206     return (@make_list);
209 ################################################################
211 # Generate a Makefile.in given the name of the corresponding Makefile.
212 sub generate_makefile
214     local ($makefile) = @_;
215     
216     print "creating ", $makefile, ".in\n";
218     $relative_dir = &dirname ($makefile);
219     $output_rules = '';
220     $output_vars = '';
221     $output_trailer = '';
222     @suffixes = ();
223     %contents = ();
224     # FIXME with new 'dist' target, don't need Makefile.in.  Probably
225     # should remove it here.
226     @dist_common = ('Makefile.in', 'Makefile.am');
227     @install_data = ();
228     @install_exec = ();
229     @uninstall = ();
230     @installdirs = ();
231     $dir_holds_sources = 0;
232     $recursive_install = 0;
233     @info = ();
234     @dvi = ();
235     @all = ('${ALL}');
236     @check = ();
237     @clean = ();
239     # Generate header before reading .am file.  The header must come
240     # before anything else, and read_am_file copies code into the
241     # output.
242     &generate_header;
244     # This is always the default target.  This gives us freedom to do
245     # things in whatever order is convenient.
246     $output_rules .= "default: all\n\n";
248     &read_am_file ($makefile . '.am');
250     # Program stuff.
251     local ($programs) = (defined ($contents{'AM_PROGRAMS'})
252                          ? $contents{'AM_PROGRAMS'}
253                          : $contents{'PROGRAMS'});
254     local ($libprograms) = (defined ($contents{'AM_LIBPROGRAMS'})
255                             ? $contents{'AM_LIBPROGRAMS'}
256                             : $contents{'LIBPROGRAMS'});
257     local ($libraries) = (defined ($contents{'AM_LIBRARIES'})
258                           ? $contents{'AM_LIBRARIES'}
259                           : $contents{'LIBRARIES'});
260     local ($scripts) = (defined ($contents{'AM_SCRIPTS'})
261                         ? $contents{'AM_SCRIPTS'}
262                         : $contents{'SCRIPTS'});
263     local ($libscripts) = (defined ($contents{'AM_LIBSCRIPTS'})
264                            ? $contents{'AM_LIBSCRIPTS'}
265                            : $contents{'LIBSCRIPTS'});
267     &handle_programs ($programs, $libprograms, $libraries);
268     &handle_scripts ($scripts, $libscripts);
269     &handle_libraries ($libraries);
271     &handle_texinfo;
272     &handle_man_pages;
273     &handle_data;
274     &handle_subdirs;
275     &handle_configure;
276     &handle_tags;
277     &handle_dist;
278     &handle_dependencies;
279     &handle_footer;
280     &handle_merge_targets;
281     &handle_installdirs;
282     &handle_clean;
284     if (! open (GM_FILE, "> " $output_directory . '/' . $makefile . ".in"))
285     {
286         print STDERR "automake: cannot open ", $makefile, ".in: ", $!, "\n";
287         $exit_status = 1;
288         return;
289     }
291     print GM_FILE $output_vars;
292     print GM_FILE $output_rules;
293     print GM_FILE $output_trailer;
295     close (GM_FILE);
298 ################################################################
300 # Generate header of Makefile.in.
301 sub generate_header
303     $output_vars =
304         ($output_vars
305          . "# Makefile.in generated automatically by automake "
306          . $VERSION
307          . " from Makefile.am\n");
309     $output_vars = $output_vars . &file_contents ('header-vars');
312 # Handle C programs and libraries.
313 sub handle_programs
315     local ($programs, $libprograms, $libraries) = @_;
317     if (!$programs && !$libprograms && !$libraries)
318     {
319         # None exist.
320         return;
321     }
322     $dir_holds_sources = 1;
324     # Boilerplate.
325     $output_vars .= &file_contents ('compile-vars');
326     $output_rules .= &file_contents ('compile');
328     # Check for automatic de-ANSI-fication.
329     local ($obj) = '.o';
330     push (@suffixes, '.c', '.o');
331     push (@clean, 'compile');
333     if (defined $contents{'@kr@'})
334     {
335         $obj = '.${kr}o';
336         push (@suffixes, '._c', '._o');
338         &require_file ('ansi2knr.c');
339         &require_file ('ansi2knr.1');
341         $output_vars .= &file_contents ('kr-vars');
342         $output_rules .= &file_contents ('compile-kr');
343         $output_rules .= &file_contents ('clean-kr');
345         push (@clean, 'kr');
346     }
348     local (@sources, @objects);
349     push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
350     push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
352     local ($one_file);
353     foreach $one_file (split (' ', ($programs . ' '
354                                     . $libprograms . ' '
355                                     . $libraries)))
356     {
357         # Look for file_SOURCES and file_OBJECTS.  FIXME file_OBJECTS
358         # should probably not be used(?)
359         if (defined $contents{$one_file . "_SOURCES"})
360         {
361             if (! defined $contents{$one_file . "_OBJECTS"})
362             {
363                 # Turn sources into objects.
364                 $_ = $contents{$one_file . "_SOURCES"};
366                 # Ugh: Perl syntax vs Emacs.
367                 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
369                 s/\.cc/$obj/g;
370                 s/$krc1/$obj/g;
371                 s/$krc2/$obj/g;
372                 s/\.[cCmylfs]/$obj/g;
374                 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
375             }
377             push (@sources, '${' . $one_file . "_SOURCES}");
378             push (@objects, '${' . $one_file . "_OBJECTS}");
379         }
380         else
381         {
382             $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
383                              . $one_file . "_OBJECTS = ". $one_file
384                              . $obj . "\n");
385             push (@sources, $one_file . '.c');
386             push (@objects, $one_file . $obj);
387         }
389         if (defined $contents{'CONFIG_HEADER'})
390         {
391             $output_rules .= ('$(' . $one_file . "_OBJECTS): "
392                               . $contents{'CONFIG_HEADER'} . "\n");
393         }
394     }
396     $output_vars .= "\n";
398     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
399     # on this.
400     $contents{'SOURCES'} = join (' ', @sources);
401     $contents{'OBJECTS'} = join (' ', @objects);
403     # Some boilerplate, and install rules.
404     if ($programs)
405     {
406         $output_rules .= &file_contents ('programs');
407         push (@install_exec, "install-programs");
408         push (@uninstall, 'uninstall-programs');
409         push (@clean, 'programs');
410         push (@installdirs, '$(bindir)');
411     }
412     if ($libprograms)
413     {
414         $output_rules .= &file_contents ('libprograms');
415         push (@install_exec, 'install-libprograms');
416         push (@uninstall, 'uninstall-libprograms');
417         push (@clean, 'libprograms');
418         push (@installdirs, '$(libexecdir)');
419     }
421     # Handle linking.
422     if ($programs || $libprograms)
423     {
424         local ($fcont) = &file_contents ('program');
425         local ($munge);
426         foreach $one_file (split (' ', $programs . ' ' . $libprograms))
427         {
428             if (! defined $contents{$one_file . "_LDADD"})
429             {
430                 # User didn't define prog_LDADD override.  So do it.
431                 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
432             }
434             ($munge = $fcont) =~ s/@PROGRAM@/$one_file/g;
435             $output_rules .= $munge;
436         }
437     }
440 # Handle libraries.
441 sub handle_libraries
443     local ($libraries) = @_;
445     return if (!$libraries);
447     local (@liblist) = split (' ', $libraries);
449     $output_rules .= &file_contents ('libraries');
450     local ($onefile) = &file_contents ('library');
451     local ($onelib, $munge);
452     foreach $onelib (@liblist)
453     {
454         ($munge = $onefile) =~ s/@LIBRARY@/$onelib/g;
455         $output_rules .= $munge;
456     }
458     # Turn "foo" into "libfoo.a" and include macro definition.
459     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
460     $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
461                      . &file_contents ('libraries-vars'));
463     push (@install_exec, 'install-libraries');
464     push (@uninstall, 'uninstall-libraries');
465     push (@clean, 'libraries');
468 # Handle scripts.
469 sub handle_scripts
471     local ($scripts, $libscripts) = @_;
473     if ($scripts)
474     {
475         $output_rules .= &file_contents ('scripts');
476         push (@install_exec, 'install-scripts');
477         push (@uninstall, 'uninstall-scripts');
478         push (@clean, 'scripts');
479         push (@installdirs, '$(bindir)');
480     }
482     if ($libscripts)
483     {
484         $output_rules .= &file_contents ('libscripts');
485         push (@install_exec, 'install-libscripts');
486         push (@uninstall, 'uninstall-libscripts');
487         push (@clean, 'libscripts');
488         push (@installdirs, '$(libexecdir');
489     }
492 # Handle all Texinfo source.
493 sub handle_texinfo
495     return if (! defined $contents{TEXINFOS});
497     local (@texis) = split (' ', $contents{TEXINFOS});
498     if ($#texis > 0)
499     {
500         print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
501         $exit_status = 1;
502         return;
503     }
505     local ($infobase);
506     ($infobase = $texis[0]) =~ s/\.texi$//;
508     # If 'version.texi' is referenced by input file, then include
509     # automatic versioning capability.
510     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
511             . " > /dev/null 2>&1");
512     if (!$?)
513     {
514         # Got a hit.
515         push (@texis, 'version.texi');
516         push (@dist_common, 'version.texi', 'stamp-vti');
517         push (@clean, 'vti');
519         local ($tfile);
520         ($tfile = &file_contents ('texi-version')) =~ s/@TEXI@/$texis[0]/g;
521         $output_rules = $output_rules . $tfile;
523         &require_file ('mdate-sh');
524     }
526     # If user specified file_TEXINFOS, then use that as explicit
527     # dependency list.
528     if (defined $contents{$infobase . "_TEXINFOS"})
529     {
530         push (@texis, "\$" . $infobase . '_TEXINFOS');
531         push (@dist_common, "\$" . $infobase . '_TEXINFOS');
532     }
534     if ($#texis >= 0)
535     {
536         $output_rules = ($output_rules . $infobase . ".info: "
537                          . join (' ', @texis) . "\n\n");
538     }
540     # Some boilerplate.
541     $output_vars = $output_vars . &file_contents ('texinfos-vars');
542     $output_rules = $output_rules . &file_contents ('texinfos');
544     push (@suffixes, '.texi', '.info', '.dvi');
545     push (@uninstall, 'uninstall-info');
546     push (@clean, 'info');
547     push (@info, '$(INFO_DEPS)');
548     push (@dvi, '$(DVIS)');
549     push (@installdirs, '$(infodir)');
550     # Make sure documentation is made and installed first.
551     unshift (@install_data, 'install-info');
552     unshift (@all, 'info');
554     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
555                      . "INFO_DEPS = " . $infobase . ".info\n"
556                      . "DVIS = " . $infobase . ".dvi\n\n");
558     # Do some error checking.
559     &require_file ('texinfo.tex');
562 # Handle any man pages.
563 sub handle_man_pages
565     return if (! defined $contents{'MANS'});
567     # We generate the manpage install code by hand to avoid the use of
568     # basename in the generated Makefile.
569     local (@mans) = split (' ', $contents{'MANS'});
570     local (%sections, %inames, %secmap, %fullsecmap);
571     foreach (@mans)
572     {
573         m/^(.*)\.([0-9])([a-z]*)$/;
574         $sections{$2} = 1;
575         $inames{$1} = $_;
576         $secmap{$1} = $2;
577         $fullsecmap{$1} = $2 . $3;
578     }
580     # Generate list of install dirs.
581     $output_rules .= "install-man:\n";
582     foreach (keys %sections)
583     {
584         push (@installdirs, '$(mandir)/man' . $_);
585         $output_rules .= ("\t" . '$(top_srcdir)/mkinstalldirs $(mandir)/man'
586                           . $_ . "\n");
587     }
589     # Generate install target.
590     local ($key);
591     foreach $key (keys %inames)
592     {
593         $_ = $install_man_format;
594         s/@SECTION@/$secmap{$key}/g;
595         s/@MAN@/$inames{$key}/g;
596         s/@FULLSECT@/$fullsecmap{$key}/g;
597         s/@MANBASE@/$key/g;
598         $output_rules .= $_;
599     }
600     $output_rules .= "\n";
602     $output_rules .= "uninstall-man:\n";
603     foreach $key (keys %inames)
604     {
605         $_ = $uninstall_man_format;
606         s/@SECTION@/$secmap{$key}/g;
607         s/@MAN@/$inames{$key}/g;
608         s/@FULLSECT@/$fullsecmap{$key}/g;
609         s/@MANBASE@/$key/g;
610         $output_rules .= $_;
611     }
612     $output_rules .= "\n";
614     $output_vars .= &file_contents ('mans-vars');
616     push (@install_data, 'install-man');
617     push (@uninstall, 'uninstall-man');
618     push (@all, '$(MANS)');
621 # Handle DATA and PACKAGEDATA.
622 sub handle_data
624     if (defined $contents{'DATA'})
625     {
626         $output_rules .= &file_contents ('data');
627         push (@install_data, 'install-ddata');
628         push (@uninstall, 'uninstall-ddata');
629         push (@installdirs, '$(datadir)');
630     }
632     if (defined $contents{'PACKAGEDATA'})
633     {
634         $output_rules .= &file_contents ('packagedata');
635         push (@install_data, 'install-pdata');
636         push (@uninstall, 'uninstall-pdata');
637         push (@installdirs, '$(datadir)/$(PACKAGE)');
638     }
641 # Handle TAGS.
642 sub handle_tags
644     local ($tagging) = 0;
646     if (defined ($contents{'SUBDIRS'}))
647     {
648         $output_rules .= &file_contents ('tags');
649         $tagging = 1;
650     }
651     elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
652     {
653         $output_rules .= &file_contents ('tags-subd');
654         $tagging = 1;
655     }
657     if ($tagging)
658     {
659         $output_rules .= &file_contents ('tags-clean');
660         push (@clean, 'tags');
661     }
664 # Handle 'dist' target.
665 sub handle_dist
667     # Look for common files that should be included in distribution.
668     local ($cfile);
669     foreach $cfile (@common_files)
670     {
671         if (-f ($relative_dir . "/" . $cfile))
672         {
673             push (@dist_common, $cfile);
674         }
675     }
677     $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
679     # Some boilerplate.
680     $output_vars .= &file_contents ('dist-vars');
681     if ($relative_dir ne '.')
682     {
683         # In a subdirectory.
684         $output_vars .= "subdir = " . $relative_dir . "\n\n";
685         $output_rules .= &file_contents ('dist-subd');
686     }
687     else
688     {
689         $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
690                                          ? 'dist-subd-top'
691                                          : 'dist');
692     }
695 # Handle auto-dependency code.
696 sub handle_dependencies
698     if ($use_dependencies)
699     {
700         # Include GNU-make-specific auto-dep code.
701         if ($dir_holds_sources)
702         {
703             $output_rules .= &file_contents ('depend');
704         }
705     }
706     else
707     {
708         # Include any auto-generated deps that are present.
709         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
710         {
711             local ($depfile);
712             local ($gpat) = $relative_dir . "/.deps/*.P";
714             foreach $depfile (<${gpat}>)
715             {
716                 if (! open (DEP_FILE, $depfile))
717                 {
718                     print STDERR "automake: couldn't open $depfile: $!\n";
719                     next;
720                 }
722                 # Slurp entire file.
723                 $output_rules .= join ('', <DEP_FILE>);
725                 close (DEP_FILE);
726             }
728             $output_rules .= "\n";
729         }       
730     }
733 # Handle subdirectories.
734 sub handle_subdirs
736     return if (! defined ($contents{'SUBDIRS'}));
738     $output_rules .= &file_contents ('subdirs');
740     push (@all, "all-recursive");
741     push (@check, "check-recursive");
742     push (@info, "info-recursive");
743     push (@dvi, "dvi-recursive");
745     $recursive_install = 1;
748 # Handle remaking and configure stuff.
749 sub handle_configure
751     if ($relative_dir ne '.')
752     {
753         # In subdirectory.
754         $output_rules .= &file_contents ('remake-subd');
755     }
756     else
757     {
758         if (-f 'aclocal.m4')
759         {
760             $output_vars .= "ACLOCAL = aclocal.m4\n";
761             push (@dist_common, 'aclocal.m4');
762         }
763         $output_rules .= &file_contents ('remake');
765         # Look for some files we need.
766         &require_file ('install-sh');
767         &require_file ('mkinstalldirs');
768     }
770     if (defined ($contents{'CONFIG_HEADER'})
771         && $contents{'CONFIG_HEADER'} !~ m,/,)
772     {
773         # Header defined and in this directory.
774         if (-f 'acconfig.h')
775         {
776             $output_vars .= "ACCONFIG = acconfig.h\n";
777             push (@dist_common, 'acconfig.h');
778         }
779         if (-f 'config.h.top')
780         {
781             $output_vars .= "CONFIG_TOP = config.h.top\n";
782             push (@dist_common, 'config.h.top');
783         }
784         if (-f 'config.h.bot')
785         {
786             $output_vars .= "CONFIG_BOT = config.h.bot\n";
787             push (@dist_common, 'config.h.bot');
788         }
790         push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
792         $output_rules .= &file_contents ('remake-hdr');
793     }
796 # Handle footer elements.
797 sub handle_footer
799     if ($contents{'SOURCES'})
800     {
801         $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
802     }
803     if ($contents{'OBJECTS'})
804     {
805         $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
806     }
807     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
808     {
809         $output_vars .= "\n";
810     }
812     $output_trailer .= ".SUFFIXES:\n";
813     if ($#suffixes >= 0)
814     {
815         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
816     }
817     $output_trailer .= "\n" . &file_contents ('footer');
820 # Deal with installdirs target.
821 sub handle_installdirs
823     # GNU Makefile standards recommend this.  FIXME prettyprint rule
824     # here.
825     $output_rules .= ("installdirs:"
826                       . ($recursive_install
827                          ? " installdirs-recursive\n"
828                          : "\n"));
829     if ($#installdirs >= 0)
830     {
831         $output_rules .= ("\t\$(top_srcdir)/mkinstalldirs "
832                           . join (' ', @installdirs)
833                           . "\n");
834     }
835     $output_rules .= "\n";
838 # There are several targets which need to be merged.  This is because
839 # their complete definition is compiled from many parts.  Note that we
840 # avoid double colon rules, otherwise we'd use them instead.
841 sub handle_merge_targets
843     &do_one_merge_target ('all', @all);
844     &do_one_merge_target ('info', @info);
845     &do_one_merge_target ('dvi', @dvi);
846     &do_one_merge_target ('check', @check);
848     # Handle the various install targets specially.  We do this so
849     # that (eg) "make install-exec" will run "install-exec-recursive"
850     # if required, but "make install" won't run it twice.  Step one is
851     # to see if the user specified local versions of any of the
852     # targets we handle.
853     if (defined $contents{'install-exec-local'})
854     {
855         push (@install_exec, 'install-exec-local');
856     }
857     if (defined $contents{'install-data-local'})
858     {
859         push (@install_data, 'install-data-local');
860     }
861     if (defined $contents{'uninstall-local'})
862     {
863         push (@uninstall, 'uninstall-local');
864     }
866     if (defined $contents{'install-local'})
867     {
868         print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
869     }
871     # Step two: if we are doing recursive makes, write out the
872     # appropriate rules.
873     local (@install);
874     if ($recursive_install)
875     {
876         push (@install, 'install-recursive');
877         push (@uninstall, 'uninstall-recursive');
879         if ($#install_exec >= 0)
880         {
881             $output_rules .= ('install-exec-am: '
882                               . join (' ', @install_exec)
883                               . "\n\n");
884             @install_exec = ('install-exec-recursive', 'install-exec-am');
885             push (@install, 'install-exec-am');
886         }
887         if ($#install_data >= 0)
888         {
889             $output_rules .= ('install-data-am: '
890                               . join (' ', @install_data)
891                               . "\n\n");
892             @install_data = ('install-data-recursive', 'install-data-am');
893             push (@install, 'install-data-am');
894         }
895         if ($#uninstall >= 0)
896         {
897             $output_rules .= ('uninstall-am: '
898                               . join (' ', @uninstall)
899                               . "\n\n");
900             @uninstall = ('uninstall-recursive', 'uninstall-am');
901         }
902     }
904     # Step three: print definitions users can use.
905     if ($#install_exec >= 0)
906     {
907         $output_rules .= ("install-exec: "
908                           . join (' ', @install_exec)
909                           . "\n\n");
910         push (@install, 'install-exec') if (!$recursive_install);
911     }
912     if ($#install_data >= 0)
913     {
914         $output_rules .= ("install-data: "
915                           . join (' ', @install_data)
916                           . "\n\n");
917         push (@install, 'install-data') if (!$recursive_install);
918     }
920     $output_rules .= ('install: '
921                       . join (' ', @install)
922                       . "\n\n"
923                       . 'uninstall: '
924                       . join (' ', @uninstall)
925                       . "\n\n");
928 # Helper for handle_merge_targets.
929 sub do_one_merge_target
931     local ($name, @values) = @_;
933     if (defined $contents{$name . '-local'})
934     {
935         # User defined local form of target.  So include it.
936         push (@values, $name . '-local');
937     }
939     $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
942 # Handle all 'clean' targets.
943 sub handle_clean
945     push (@clean, 'generic');
946     $output_rules .= &file_contents ('clean');
948     local ($target) = $recursive_install ? 'clean-am' : 'clean';
949     &do_one_clean_target ($target, 'mostly', '', @clean);
950     &do_one_clean_target ($target, '', 'mostly', @clean);
951     &do_one_clean_target ($target, 'dist', '', @clean);
952     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
954     local (@deps);
955     if ($recursive_install)
956     {
957         @deps = ('am', 'recursive');
958         &do_one_clean_target ('', 'mostly', '', @deps);
959         &do_one_clean_target ('', '', '', @deps);
960         &do_one_clean_target ('', 'dist', '', @deps);
961         &do_one_clean_target ('', 'maintainer-', '', @deps);
962     }
965 # Helper for handle_clean.
966 sub do_one_clean_target
968     local ($target, $name, $last_name, @deps) = @_;
970     # Special case: if target not passed, then don't generate
971     # dependency on next "lower" clean target (eg no
972     # clean<-mostlyclean derivation).  In this case the target is
973     # implicitly known to be 'clean'.
974     local ($flag) = $target;
975     if (!$flag)
976     {
977         $target = 'clean';
978     }
980     $output_rules .= $name . $target . ": ";
981     if ($flag)
982     {
983         if ($last_name || $name ne 'mostly')
984         {
985             $output_rules .= $last_name . $target . " ";
986         }
987     }
989     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
990                       . "\n");
992     # FIXME shouldn't we really print these messages before running
993     # the dependencies?
994     if ($name . $target eq 'maintainer-clean')
995     {
996         # Print a special warning.
997         $output_rules .=
998             ("\t@echo \"This command is intended for maintainers to use;\"\n"
999              . "\t@echo \"it deletes files that may require special "
1000              . "tools to rebuild.\"\n");
1001     }
1002     $output_rules .= "\n";
1005 ################################################################
1007 # Read Makefile.am and set up %contents.  Simultaneously copy lines
1008 # from Makefile.am into $output_trailer or $output_vars as
1009 # appropriate.  NOTE we put rules in the trailer section.  We want
1010 # user rules to come after our generated stuff.
1011 sub read_am_file
1013     local ($amfile) = @_;
1015     if (! open (AMFILE, $amfile))
1016     {
1017         print STDERR "automake: couldn't open $amfile: $!\n";
1018         exit 1;
1019     }
1021     local ($saw_bk) = 0;
1022     local ($was_rule) = 0;
1023     local ($last_var_name) = '';
1025     while (<AMFILE>)
1026     {
1027         chop;
1029         if ($saw_bk)
1030         {
1031             if ($was_rule)
1032             {
1033                 $output_trailer .= $_ . "\n";
1034             }
1035             else
1036             {
1037                 $output_vars .= $_ . "\n";
1038                 if (substr ($_, -1) eq "\\")
1039                 {
1040                     $contents{$last_var_name} .= substr ($_, 0,
1041                                                          length ($_) - 1);
1042                 }
1043                 else
1044                 {
1045                     $contents{$last_var_name} .= $_;
1046                 }
1047             }
1048         }
1049         elsif ($_ eq '@kr@')
1050         {
1051             # Special case: this means we want automatic
1052             # de-ANSI-fication.  FIXME think of a better way.
1053             $contents{'@kr@'} = 1;
1054         }
1055         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
1056         {
1057             # Found a rule.
1058             $was_rule = 1;
1059             # Value here doesn't matter; for targets we only note
1060             # existence.
1061             $contents{$1} = 1;
1062             $output_trailer .= $_ . "\n";
1063         }
1064         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
1065         {
1066             # Found a variable reference.
1067             $was_rule = 0;
1068             $last_var_name = $1;
1069             if (substr ($2, -1) eq "\\")
1070             {
1071                 $contents{$1} = substr ($2, 0, length ($2) - 1);
1072             }
1073             else
1074             {
1075                 $contents{$1} = $2;
1076             }
1077             $output_vars .= $_ . "\n";
1078         }
1079         elsif (m/^$/)
1080         {
1081             # Special rule: if looking at a blank line, append it to
1082             # whatever we saw last.
1083             if ($was_rule)
1084             {
1085                 $output_trailer .= "\n";
1086             }
1087             else
1088             {
1089                 $output_vars .= "\n";
1090             }
1091         }
1092         else
1093         {
1094             # This isn't an error; it is probably a continued rule.
1095             # In fact, this is what we assume.
1096             $output_trailer .= $_ . "\n";
1097         }
1099         $saw_bk = (substr ($_, -1) eq "\\");
1100     }
1102     # Include some space after user code.
1103     $output_vars .= "\n";
1104     $output_trailer .= "\n";
1107 ################################################################
1109 # Initialize global variables.
1110 sub init_globals
1112     # Helper text for dealing with man pages.
1113     $install_man_format =
1114     '   sect=@SECTION@;                         \\
1115         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1116         echo installing @MAN@ as $(mandir)/man$$sect/$$inst; \\
1117         $(INSTALL_DATA) $(srcdir)/@MAN@ $(mandir)/man$$sect/$$inst; \\
1118         if test -d $(mandir)/cat$$sect; then    \\
1119           echo formatting @MAN@ as $(mandir)/cat$$sect/$$inst;  \\
1120           $(NROFF) -man $(srcdir)/@MAN@ > $(mandir)/cat$$sect/$$inst; \\
1121         else                                    \\
1122           true;                                 \\
1123         fi
1126     $uninstall_man_format =
1127     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1128         rm -f $(mandir)/man@SECTION@/$$inst $(mandir)/cat@SECTION@/$$inst
1131     # Commonly found files we look for and automatically include in
1132     # DIST_FILES.
1133     @common_files =
1134         (
1135          "THANKS", "TODO", "README", "NEWS", "COPYING", "COPYING.LIB",
1136          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
1137          "config.guess", "config.sub"
1138          );
1140     # Commonly used files we auto-include, but only sometimes.
1141     @common_sometimes =
1142         (
1143          "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
1144          "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
1145          "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
1146          );
1148     $USAGE = "\
1149   --amdir=DIR           directory storing config files
1150   --help                print this help, then exit
1151   --include-deps        include generated dependencies in Makefile.in
1152   --output-dir=DIR      put generated Makefile.in's into DIR
1153   --version             print version number, then exit\n";
1157 ################################################################
1159 # Return contents of a file from $am_dir.
1160 sub file_contents
1162     local ($basename) = @_;
1163     local ($file) = $am_dir . '/' . $basename . '.am';
1165     if (! open (FC_FILE, $file))
1166     {
1167         print STDERR "automake: installation error: cannot open \"$file\"\n";
1168         exit 1;
1169     }
1171     # Yes, we really want to slurp it.
1172     local ($results) = join ('', <FC_FILE>);
1174     close (FC_FILE);
1176     return $results;
1179 ################################################################
1181 # Verify that the file must exist in the current directory.
1182 sub require_file
1184     local ($file) = @_;
1185     local ($fullfile) = $relative_dir . "/" . $file;
1187     if (! -f $fullfile)
1188     {
1189         print STDERR "automake: required file \"$fullfile\" not found\n";
1190         $exit_status = 1;
1191     }
1192     else
1193     {
1194         push (@dist_common, $file);
1195     }
1199 ################################################################
1201 # Return directory name of file.
1202 sub dirname
1204     local ($file) = @_;
1205     local ($sub);
1207     ($sub = $file) =~ s,/+[^/]+,,g;
1208     if ($sub eq $file)
1209     {
1210         $sub = '.';
1211     }
1213     return $sub;
1216 ################################################################
1218 # Print usage information.
1219 sub usage
1221     print "Usage: automake [OPTION] ... [Makefile]...\n";
1222     print $USAGE;
1223     print "\nFiles which are automatically distributed, if found:\n";
1224     $~ = "USAGE_FORMAT";
1225     local (@lcomm) = sort ((@common_files, @common_sometimes));
1226     local ($one, $two);
1227     while ($#lcomm >= 0)
1228     {
1229         $one = shift (@lcomm);
1230         $two = shift (@lcomm);
1231         write;
1232     }
1234     exit 0;
1237 format USAGE_FORMAT =
1238   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1239   $one,                $two