Beginnings of switch to uniform naming scheme
[platform/upstream/automake.git] / automake.in
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
4
5 eval "exec /usr/local/bin/perl -S $0 $*"
6     if $running_under_some_shell;
7
8 # automake - create Makefile.in from Makefile.am
9 # Copyright (C) 1994 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 \f
36
37 # This is TRUE if GNU make specific automatic dependency generation
38 # code should be included in generated Makefile.in.
39 $use_dependencies = 1;
40
41 # This holds our (eventual) exit status.  We don't actually exit until
42 # we have processed all input files.
43 $exit_status = 0;
44
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 = '';
50
51 # Suffixes found during a run.
52 @suffixes = ();
53
54 # This holds the contents of a Makefile.am, as parsed by read_am_file.
55 %contents = ();
56
57 # This holds the "relative directory" of the current Makefile.in.  Eg
58 # for src/Makefile.in, this is "src".
59 $relative_dir = '';
60
61 # Directory where output files go.  Actually, output files are
62 # relative to this directory.
63 $output_directory = '.';
64
65 # This holds a list of files that are included in the distribution.
66 @dist_common = ();
67
68 # List of dependencies for the obvious targets.
69 @install_data = ();
70 @install_exec = ();
71 @uninstall = ();
72 @installdirs = ();
73
74 @info = ();
75 @dvi = ();
76 @all = ();
77 @check = ();
78 @installcheck = ();
79 @clean = ();
80
81 # TRUE if current directory holds any C source files.  (Actually holds
82 # object extension, but this information is encapsulated in the
83 # function get_object_extension).
84 $dir_holds_sources = '';
85
86 # TRUE if install targets should work recursively.
87 $recursive_install = 0;
88
89 \f
90
91 &init_globals;
92
93 # Parse command line.
94 @input_files = &parse_arguments (@ARGV);
95
96 # Now do all the work on each file.
97 foreach $am_file (@input_files)
98 {
99     if (! -f ($am_file . '.am'))
100     {
101         &am_error ('no such file');
102     }
103     else
104     {
105         &generate_makefile ($am_file);
106     }
107 }
108
109 exit $exit_status;
110
111
112 ################################################################
113
114 # Parse command line.
115 sub parse_arguments
116 {
117     local (@arglist) = @_;
118     local (@make_list);
119
120     while ($#arglist >= 0)
121     {
122         if ($arglist[0] eq "--version")
123         {
124             print "Automake version $VERSION\n";
125             exit 0;
126         }
127         elsif ($arglist[0] eq "--help")
128         {
129             &usage;
130         }
131         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
132         {
133             $am_dir = $1;
134         }
135         elsif ($arglist[0] eq '--amdir')
136         {
137             if ($#arglist == 0)
138             {
139                 print STDERR
140                     "automake: no argument given for option \`$arglist[0]'\n";
141                 exit 1;
142             }
143             shift (@arglist);
144             $am_dir = $arglist[0];
145         }
146         elsif ($arglist[0] eq '--include-deps')
147         {
148             $use_dependencies = 0;
149         }
150         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
151         {
152             # Set output directory.
153             $output_directory = $1;
154         }
155         elsif ($arglist[0] eq '--output-dir')
156         {
157             if ($#arglist == 0)
158             {
159                 print STDERR
160                     "automake: no argument given for option \`$arglist[0]'\n";
161                 exit 1;
162             }
163             shift (@arglist);
164             $output_directory = $arglist[0];
165         }
166         elsif ($arglist[0] eq '--')
167         {
168             # Stop option processing.
169             shift (@arglist);
170             push (@make_list, @arglist);
171             last;
172         }
173         elsif ($arglist[0] =~ /^-/)
174         {
175             print STDERR "automake: unrecognized option -- \`$arglist[0]'\n";
176             exit 1;
177         }
178         else
179         {
180             push (@make_list, $arglist[0]);
181         }
182
183         shift (@arglist);
184     }
185
186     if ($#make_list < 0)
187     {
188         # Look around for some files.
189         push (@make_list, 'Makefile') if -f 'Makefile.am';
190
191         foreach (<*/Makefile.am>)
192         {
193             s/\.am$//;
194             push (@make_list, $_);
195         }
196
197         if ($#make_list >= 0)
198         {
199             print "automake: using ", join (' ', @make_list), "\n";
200         }
201         else
202         {
203             print STDERR "automake: no \"Makefile.am\" found or specified\n";
204             exit 1;
205         }
206     }
207
208     return (@make_list);
209 }
210
211 ################################################################
212
213 # Generate a Makefile.in given the name of the corresponding Makefile.
214 sub generate_makefile
215 {
216     local ($makefile) = @_;
217     
218     print "creating ", $makefile, ".in\n";
219
220     $relative_dir = &dirname ($makefile);
221     $output_rules = '';
222     $output_vars = '';
223     $output_trailer = '';
224     @suffixes = ();
225     %contents = ();
226     # FIXME with new 'dist' target, don't need Makefile.in.  Probably
227     # should remove it here.
228     @dist_common = ('Makefile.in', 'Makefile.am');
229     @install_data = ();
230     @install_exec = ();
231     @uninstall = ();
232     @installdirs = ();
233     $dir_holds_sources = '';
234     $recursive_install = 0;
235     @info = ();
236     @dvi = ();
237     @all = ();
238     @check = ();
239     @installcheck = ();
240     @clean = ();
241
242     # Generate header before reading .am file.  The header must come
243     # before anything else, and read_am_file copies code into the
244     # output.
245     &generate_header;
246
247     # This is always the default target.  This gives us freedom to do
248     # things in whatever order is convenient.
249     $output_rules .= "default: all\n\n";
250
251     &read_am_file ($makefile . '.am');
252
253     # Program stuff.
254     local ($programs) = &am_variable ('PROGRAMS');
255     local ($libprograms) = &am_variable ('LIBPROGRAMS');
256     local ($libraries) = &am_variable ('LIBRARIES');
257     local ($scripts) = &am_variable ('SCRIPTS');
258     local ($libscripts) = &am_variable ('LIBSCRIPTS');
259
260     &handle_programs ($programs, $libprograms, $libraries);
261     &handle_scripts ($scripts, $libscripts);
262     &handle_libraries ($libraries);
263
264     &handle_texinfo;
265     &handle_man_pages;
266     &handle_data;
267     &handle_headers;
268     &handle_subdirs;
269     &handle_configure;
270     &handle_tags;
271     &handle_dist;
272     &handle_dependencies;
273     &handle_footer;
274     &handle_merge_targets;
275     &handle_installdirs;
276     &handle_clean;
277
278     if (! -d ($output_directory . '/' . $relative_dir))
279     {
280         &mkdir ($output_directory . '/' . $relative_dir);
281     }
282     if (! open (GM_FILE, "> " . $output_directory . '/' . $makefile . ".in"))
283     {
284         &am_error ("cannot open:", $!);
285         return;
286     }
287
288     print GM_FILE $output_vars;
289     print GM_FILE $output_rules;
290     print GM_FILE $output_trailer;
291
292     close (GM_FILE);
293 }
294
295 ################################################################
296
297 # Generate header of Makefile.in.
298 sub generate_header
299 {
300     $output_vars =
301         ($output_vars
302          . "# Makefile.in generated automatically by automake "
303          . $VERSION
304          . " from Makefile.am\n");
305
306     $output_vars = $output_vars . &file_contents ('header-vars');
307 }
308
309 # Return object extension.  Just once, put some code into the output.
310 sub get_object_extension
311 {
312     if (! $dir_holds_sources)
313     {
314
315         # Boilerplate.
316         $output_vars .= &file_contents ('compile-vars');
317         $output_rules .= &file_contents ('compile');
318
319         # Check for automatic de-ANSI-fication.
320         $dir_holds_sources = '.o';
321         push (@suffixes, '.c', '.o');
322         push (@clean, 'compile');
323
324         if (defined $contents{'@kr@'})
325         {
326             $dir_holds_sources = '.${kr}o';
327             push (@suffixes, '._c', '._o');
328
329             &require_file ('ansi2knr.c');
330             &require_file ('ansi2knr.1');
331
332             $output_vars .= &file_contents ('kr-vars');
333             $output_rules .= &file_contents ('compile-kr');
334             $output_rules .= &file_contents ('clean-kr');
335
336             push (@clean, 'kr');
337         }
338     }
339     return $dir_holds_sources;
340 }
341
342 # Handle SOURCE->OBJECT transform for one program or library.
343 sub handle_source_transform
344 {
345     local ($one_file, $obj) = @_;
346
347     # Look for file_SOURCES and file_OBJECTS.
348     local (@result) = ();
349     if (defined $contents{$one_file . "_SOURCES"})
350     {
351         if (! defined $contents{$one_file . "_OBJECTS"})
352         {
353             # Turn sources into objects.
354             $_ = $contents{$one_file . "_SOURCES"};
355
356             # Ugh: Perl syntax vs Emacs.
357             local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
358
359             s/\.cc/$obj/g;
360             s/$krc1/$obj/g;
361             s/$krc2/$obj/g;
362             s/\.[cCmylfs]/$obj/g;
363
364             $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
365         }
366         else
367         {
368             &am_error ($one_file . '_OBJECTS', 'should not be defined');
369         }
370
371         @result = ('${' . $one_file . "_SOURCES}",
372                    '${' . $one_file . "_OBJECTS}");
373     }
374     else
375     {
376         $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
377                          . $one_file . "_OBJECTS = ". $one_file
378                          . $obj . "\n");
379         @result = ($one_file . '.c',
380                    $one_file . $obj);
381     }
382
383     if (defined $contents{'CONFIG_HEADER'})
384     {
385         $output_rules .= ('$(' . $one_file . "_OBJECTS): "
386                           . $contents{'CONFIG_HEADER'} . "\n");
387     }
388
389     return @result;
390 }
391
392 # Handle C programs.
393 sub handle_programs
394 {
395     local ($programs, $libprograms, $libraries) = @_;
396
397     if (!$programs && !$libprograms && !$libraries)
398     {
399         # None exist.
400         return;
401     }
402
403     local ($obj) = &get_object_extension;
404
405     local (@sources, @objects);
406     push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
407     push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
408
409     local ($one_file);
410     local ($sadd, $oadd);
411     foreach $one_file (split (' ', ($programs . ' '
412                                     . $libprograms . ' '
413                                     . $libraries)))
414     {
415         ($sadd, $oadd) = &handle_source_transform ($one_file, $obj);
416         push (@sources, $sadd);
417         push (@objects, $oadd);
418     }
419
420     $output_vars .= "\n";
421
422     # Re-init SOURCES and OBJECTS.  FIXME other code shouldn't depend
423     # on this.
424     $contents{'SOURCES'} = join (' ', @sources);
425     $contents{'OBJECTS'} = join (' ', @objects);
426
427     # Some boilerplate, and install rules.
428     if ($programs)
429     {
430         $output_rules .= &file_contents ('programs');
431         push (@install_exec, "install-programs");
432         push (@uninstall, 'uninstall-programs');
433         push (@clean, 'programs');
434         push (@installdirs, '$(bindir)');
435         push (@all, '$(PROGRAMS)');
436     }
437     if ($libprograms)
438     {
439         $output_rules .= &file_contents ('libprograms');
440         push (@install_exec, 'install-libprograms');
441         push (@uninstall, 'uninstall-libprograms');
442         push (@clean, 'libprograms');
443         push (@installdirs, '$(libexecdir)');
444         push (@all, '$(LIBPROGRAMS)');
445     }
446
447     # Handle linking.
448     if ($programs || $libprograms)
449     {
450         local ($fcont) = &file_contents ('program');
451         local ($munge);
452         foreach $one_file (split (' ', $programs . ' ' . $libprograms))
453         {
454             if (! defined $contents{$one_file . "_LDADD"})
455             {
456                 # User didn't define prog_LDADD override.  So do it.
457                 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
458             }
459
460             ($munge = $fcont) =~ s/\@PROGRAM\@/$one_file/g;
461             $output_rules .= $munge;
462         }
463     }
464 }
465
466 # Handle libraries.
467 sub handle_libraries
468 {
469     local ($libraries) = @_;
470
471     return if (!$libraries);
472
473     local (@liblist) = split (' ', $libraries);
474
475     $output_rules .= &file_contents ('libraries');
476     local ($onefile) = &file_contents ('library');
477     local ($onelib, $munge);
478     foreach $onelib (@liblist)
479     {
480         if (! defined $contents{$onelib . '_LIBADD'})
481         {
482             # Generate support for conditional object inclusion in
483             # libraries.
484             $output_vars .= $onelib . "_LIBADD =\n";
485         }
486
487         ($munge = $onefile) =~ s/\@LIBRARY\@/$onelib/g;
488         $output_rules .= $munge;
489     }
490
491     # Turn "foo" into "libfoo.a" and include macro definition.
492     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
493     $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
494                      . &file_contents ('libraries-vars'));
495
496     push (@install_exec, 'install-libraries');
497     push (@uninstall, 'uninstall-libraries');
498     push (@clean, 'libraries');
499     push (@all, '$(LIBFILES)');
500 }
501
502 # Handle scripts.
503 sub handle_scripts
504 {
505     &am_install_var ('scripts', 'SCRIPTS', 'bin', 'sbin', 'libexec',
506                      'noinst');
507 }
508
509 # Handle all Texinfo source.
510 sub handle_texinfo
511 {
512     local ($texis) = &am_variable ('TEXINFOS');
513     return if (!$texis);
514
515     local (@texis) = split (' ', $texis);
516     if ($#texis > 0)
517     {
518         &am_error ('sorry, only one file allowed in `TEXINFOS\'');
519         return;
520     }
521
522     local ($infobase);
523     ($infobase = $texis[0]) =~ s/\.texi$//;
524
525     # If 'version.texi' is referenced by input file, then include
526     # automatic versioning capability.
527     system ("grep version.texi " . $relative_dir . "/" . $texis[0]
528             . " > /dev/null 2>&1");
529     if (!$?)
530     {
531         # Got a hit.
532         push (@texis, 'version.texi');
533         push (@dist_common, 'version.texi', 'stamp-vti');
534         push (@clean, 'vti');
535
536         local ($tfile);
537         ($tfile = &file_contents ('texi-version')) =~ s/\@TEXI\@/$texis[0]/g;
538         $output_rules = $output_rules . $tfile;
539
540         &require_file ('mdate-sh');
541     }
542
543     # If user specified file_TEXINFOS, then use that as explicit
544     # dependency list.
545     if (defined $contents{$infobase . "_TEXINFOS"})
546     {
547         push (@texis, "\$" . $infobase . '_TEXINFOS');
548         push (@dist_common, "\$" . $infobase . '_TEXINFOS');
549     }
550
551     if ($#texis >= 0)
552     {
553         $output_rules = ($output_rules . $infobase . ".info: "
554                          . join (' ', @texis) . "\n\n");
555     }
556
557     # Some boilerplate.
558     $output_vars = $output_vars . &file_contents ('texinfos-vars');
559     $output_rules = $output_rules . &file_contents ('texinfos');
560
561     push (@suffixes, '.texi', '.info', '.dvi');
562     push (@uninstall, 'uninstall-info');
563     push (@clean, 'info');
564     push (@info, '$(INFO_DEPS)');
565     push (@dvi, '$(DVIS)');
566     push (@installdirs, '$(infodir)');
567     # Make sure documentation is made and installed first.
568     unshift (@install_data, 'install-info');
569     unshift (@all, 'info');
570
571     $output_vars .= ("INFOS = " . $infobase . ".info*\n"
572                      . "INFO_DEPS = " . $infobase . ".info\n"
573                      . "DVIS = " . $infobase . ".dvi\n\n");
574
575     # Do some error checking.
576     &require_file ('texinfo.tex');
577 }
578
579 # Handle any man pages.
580 sub handle_man_pages
581 {
582     return if (! defined $contents{'MANS'});
583
584     # We generate the manpage install code by hand to avoid the use of
585     # basename in the generated Makefile.
586     local (@mans) = split (' ', $contents{'MANS'});
587     local (%sections, %inames, %secmap, %fullsecmap);
588     foreach (@mans)
589     {
590         m/^(.*)\.([0-9])([a-z]*)$/;
591         $sections{$2} = 1;
592         $inames{$1} = $_;
593         $secmap{$1} = $2;
594         $fullsecmap{$1} = $2 . $3;
595     }
596
597     # Generate list of install dirs.
598     $output_rules .= "install-man:\n";
599     foreach (keys %sections)
600     {
601         push (@installdirs, '$(mandir)/man' . $_);
602         $output_rules .= ("\t" . '$(top_srcdir)/mkinstalldirs $(mandir)/man'
603                           . $_ . "\n");
604     }
605
606     # Generate install target.
607     local ($key);
608     foreach $key (keys %inames)
609     {
610         $_ = $install_man_format;
611         s/\@SECTION\@/$secmap{$key}/g;
612         s/\@MAN\@/$inames{$key}/g;
613         s/\@FULLSECT\@/$fullsecmap{$key}/g;
614         s/\@MANBASE\@/$key/g;
615         $output_rules .= $_;
616     }
617     $output_rules .= "\n";
618
619     $output_rules .= "uninstall-man:\n";
620     foreach $key (keys %inames)
621     {
622         $_ = $uninstall_man_format;
623         s/\@SECTION\@/$secmap{$key}/g;
624         s/\@MAN\@/$inames{$key}/g;
625         s/\@FULLSECT\@/$fullsecmap{$key}/g;
626         s/\@MANBASE\@/$key/g;
627         $output_rules .= $_;
628     }
629     $output_rules .= "\n";
630
631     $output_vars .= &file_contents ('mans-vars');
632
633     push (@install_data, 'install-man');
634     push (@uninstall, 'uninstall-man');
635     push (@all, '$(MANS)');
636 }
637
638 # Handle DATA variables.
639 sub handle_data
640 {
641     &am_install_var ('data', 'DATA', 'data', 'sysconf',
642                      'sharedstate', 'localstate', 'pkgdata',
643                      'noinst');
644 }
645
646 # Handle TAGS.
647 sub handle_tags
648 {
649     local ($tagging) = 0;
650
651     if (defined ($contents{'SUBDIRS'}))
652     {
653         $output_rules .= &file_contents ('tags');
654         $tagging = 1;
655     }
656     elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
657     {
658         $output_rules .= &file_contents ('tags-subd');
659         $tagging = 1;
660     }
661
662     if ($tagging)
663     {
664         $output_rules .= &file_contents ('tags-clean');
665         push (@clean, 'tags');
666     }
667     else
668     {
669         # Every Makefile must define some sort of TAGS rule.
670         # Otherwise, it would be possible for a top-level "make TAGS"
671         # to fail because some subdirectory failed.
672         $output_rules .= "tags: TAGS\nTAGS:\n\n";
673     }
674 }
675
676 # Handle 'dist' target.
677 sub handle_dist
678 {
679     # Look for common files that should be included in distribution.
680     local ($cfile);
681     foreach $cfile (@common_files)
682     {
683         if (-f ($relative_dir . "/" . $cfile))
684         {
685             push (@dist_common, $cfile);
686         }
687     }
688
689     $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
690
691     # Some boilerplate.
692     $output_vars .= &file_contents ('dist-vars');
693     if ($relative_dir ne '.')
694     {
695         # In a subdirectory.
696         $output_vars .= "subdir = " . $relative_dir . "\n\n";
697         $output_rules .= &file_contents ('dist-subd');
698     }
699     else
700     {
701         $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
702                                          ? 'dist-subd-top'
703                                          : 'dist');
704     }
705 }
706
707 # Handle auto-dependency code.
708 sub handle_dependencies
709 {
710     if ($use_dependencies)
711     {
712         # Include GNU-make-specific auto-dep code.
713         if ($dir_holds_sources)
714         {
715             $output_rules .= &file_contents ('depend');
716         }
717     }
718     else
719     {
720         # Include any auto-generated deps that are present.
721         if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
722         {
723             local ($depfile);
724             local ($gpat) = $relative_dir . "/.deps/*.P";
725
726             foreach $depfile (<${gpat}>)
727             {
728                 if (! open (DEP_FILE, $depfile))
729                 {
730                     &am_error ("couldn't open $depfile", $!);
731                     next;
732                 }
733
734                 # Slurp entire file.
735                 $output_rules .= join ('', <DEP_FILE>);
736
737                 close (DEP_FILE);
738             }
739
740             $output_rules .= "\n";
741         }       
742     }
743 }
744
745 # Handle subdirectories.
746 sub handle_subdirs
747 {
748     return if (! defined ($contents{'SUBDIRS'}));
749
750     $output_rules .= &file_contents ('subdirs');
751
752     push (@all, "all-recursive");
753     push (@check, "check-recursive");
754     push (@installcheck, "installcheck-recursive");
755     push (@info, "info-recursive");
756     push (@dvi, "dvi-recursive");
757
758     $recursive_install = 1;
759 }
760
761 # Handle remaking and configure stuff.
762 sub handle_configure
763 {
764     if ($relative_dir ne '.')
765     {
766         # In subdirectory.
767         $output_rules .= &file_contents ('remake-subd');
768     }
769     else
770     {
771         if (-f 'aclocal.m4')
772         {
773             $output_vars .= "ACLOCAL = aclocal.m4\n";
774             push (@dist_common, 'aclocal.m4');
775         }
776         $output_rules .= &file_contents ('remake');
777
778         # Look for some files we need.
779         &require_file ('install-sh');
780         &require_file ('mkinstalldirs');
781     }
782
783     if (defined ($contents{'CONFIG_HEADER'})
784         && $contents{'CONFIG_HEADER'} !~ m,/,)
785     {
786         # Header defined and in this directory.
787         if (-f 'acconfig.h')
788         {
789             $output_vars .= "ACCONFIG = acconfig.h\n";
790             push (@dist_common, 'acconfig.h');
791         }
792         if (-f 'config.h.top')
793         {
794             $output_vars .= "CONFIG_TOP = config.h.top\n";
795             push (@dist_common, 'config.h.top');
796         }
797         if (-f 'config.h.bot')
798         {
799             $output_vars .= "CONFIG_BOT = config.h.bot\n";
800             push (@dist_common, 'config.h.bot');
801         }
802
803         push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
804
805         $output_rules .= &file_contents ('remake-hdr');
806     }
807 }
808
809 # Handle C headers.
810 sub handle_headers
811 {
812     &am_install_var ('data', 'HEADERS', 'include',
813                      'oldinclude', 'pkginclude',
814                      'noinst');
815 }
816
817 # Handle footer elements.
818 sub handle_footer
819 {
820     if ($contents{'SOURCES'})
821     {
822         $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
823     }
824     if ($contents{'OBJECTS'})
825     {
826         $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
827     }
828     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
829     {
830         $output_vars .= "\n";
831     }
832
833     if (defined $contents{'SUFFIXES'})
834     {
835         push (@suffixes, '$(SUFFIXES)');
836     }
837
838     $output_trailer .= ".SUFFIXES:\n";
839     if ($#suffixes >= 0)
840     {
841         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
842     }
843     $output_trailer .= "\n" . &file_contents ('footer');
844 }
845
846 # Deal with installdirs target.
847 sub handle_installdirs
848 {
849     # GNU Makefile standards recommend this.  FIXME prettyprint rule
850     # here.
851     $output_rules .= ("installdirs:"
852                       . ($recursive_install
853                          ? " installdirs-recursive\n"
854                          : "\n"));
855     if ($#installdirs >= 0)
856     {
857         $output_rules .= ("\t\$(top_srcdir)/mkinstalldirs "
858                           . join (' ', @installdirs)
859                           . "\n");
860     }
861     $output_rules .= "\n";
862 }
863
864 # There are several targets which need to be merged.  This is because
865 # their complete definition is compiled from many parts.  Note that we
866 # avoid double colon rules, otherwise we'd use them instead.
867 sub handle_merge_targets
868 {
869     &do_one_merge_target ('all', @all);
870     &do_one_merge_target ('info', @info);
871     &do_one_merge_target ('dvi', @dvi);
872     &do_one_merge_target ('check', @check);
873     &do_one_merge_target ('installcheck', @installcheck);
874
875     # Handle the various install targets specially.  We do this so
876     # that (eg) "make install-exec" will run "install-exec-recursive"
877     # if required, but "make install" won't run it twice.  Step one is
878     # to see if the user specified local versions of any of the
879     # targets we handle.
880     if (defined $contents{'install-exec-local'})
881     {
882         push (@install_exec, 'install-exec-local');
883     }
884     if (defined $contents{'install-data-local'})
885     {
886         push (@install_data, 'install-data-local');
887     }
888     if (defined $contents{'uninstall-local'})
889     {
890         push (@uninstall, 'uninstall-local');
891     }
892
893     if (defined $contents{'install-local'})
894     {
895         &am_error ("use \`install-data' or \`install-exec', not \`install'");
896     }
897
898     # Step two: if we are doing recursive makes, write out the
899     # appropriate rules.
900     local (@install);
901     if ($recursive_install)
902     {
903         push (@install, 'install-recursive');
904         push (@uninstall, 'uninstall-recursive');
905
906         if ($#install_exec >= 0)
907         {
908             $output_rules .= ('install-exec-am: '
909                               . join (' ', @install_exec)
910                               . "\n\n");
911             @install_exec = ('install-exec-recursive', 'install-exec-am');
912             push (@install, 'install-exec-am');
913         }
914         if ($#install_data >= 0)
915         {
916             $output_rules .= ('install-data-am: '
917                               . join (' ', @install_data)
918                               . "\n\n");
919             @install_data = ('install-data-recursive', 'install-data-am');
920             push (@install, 'install-data-am');
921         }
922         if ($#uninstall >= 0)
923         {
924             $output_rules .= ('uninstall-am: '
925                               . join (' ', @uninstall)
926                               . "\n\n");
927             @uninstall = ('uninstall-recursive', 'uninstall-am');
928         }
929     }
930
931     # Step three: print definitions users can use.
932     if ($#install_exec >= 0)
933     {
934         $output_rules .= ("install-exec: "
935                           . join (' ', @install_exec)
936                           . "\n\n");
937         push (@install, 'install-exec') if (!$recursive_install);
938     }
939     if ($#install_data >= 0)
940     {
941         $output_rules .= ("install-data: "
942                           . join (' ', @install_data)
943                           . "\n\n");
944         push (@install, 'install-data') if (!$recursive_install);
945     }
946
947     $output_rules .= ('install: '
948                       . join (' ', @install)
949                       . "\n\n"
950                       . 'uninstall: '
951                       . join (' ', @uninstall)
952                       . "\n\n");
953 }
954
955 # Helper for handle_merge_targets.
956 sub do_one_merge_target
957 {
958     local ($name, @values) = @_;
959
960     if (defined $contents{$name . '-local'})
961     {
962         # User defined local form of target.  So include it.
963         push (@values, $name . '-local');
964     }
965
966     $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
967 }
968
969 # Handle all 'clean' targets.
970 sub handle_clean
971 {
972     push (@clean, 'generic');
973     $output_rules .= &file_contents ('clean');
974
975     local ($target) = $recursive_install ? 'clean-am' : 'clean';
976     &do_one_clean_target ($target, 'mostly', '', @clean);
977     &do_one_clean_target ($target, '', 'mostly', @clean);
978     &do_one_clean_target ($target, 'dist', '', @clean);
979     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
980
981     local (@deps);
982     if ($recursive_install)
983     {
984         @deps = ('am', 'recursive');
985         &do_one_clean_target ('', 'mostly', '', @deps);
986         &do_one_clean_target ('', '', '', @deps);
987         &do_one_clean_target ('', 'dist', '', @deps);
988         &do_one_clean_target ('', 'maintainer-', '', @deps);
989     }
990 }
991
992 # Helper for handle_clean.
993 sub do_one_clean_target
994 {
995     local ($target, $name, $last_name, @deps) = @_;
996
997     # Special case: if target not passed, then don't generate
998     # dependency on next "lower" clean target (eg no
999     # clean<-mostlyclean derivation).  In this case the target is
1000     # implicitly known to be 'clean'.
1001     local ($flag) = $target;
1002     if (!$flag)
1003     {
1004         $target = 'clean';
1005     }
1006
1007     $output_rules .= $name . $target . ": ";
1008     if ($flag)
1009     {
1010         if ($last_name || $name ne 'mostly')
1011         {
1012             $output_rules .= $last_name . $target . " ";
1013         }
1014     }
1015
1016     $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
1017                       . "\n");
1018
1019     # FIXME shouldn't we really print these messages before running
1020     # the dependencies?
1021     if ($name . $target eq 'maintainer-clean')
1022     {
1023         # Print a special warning.
1024         $output_rules .=
1025             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
1026              . "\t\@echo \"it deletes files that may require special "
1027              . "tools to rebuild.\"\n");
1028     }
1029     $output_rules .= "\n";
1030 }
1031
1032 ################################################################
1033
1034 # Read Makefile.am and set up %contents.  Simultaneously copy lines
1035 # from Makefile.am into $output_trailer or $output_vars as
1036 # appropriate.  NOTE we put rules in the trailer section.  We want
1037 # user rules to come after our generated stuff.
1038 sub read_am_file
1039 {
1040     local ($amfile) = @_;
1041
1042     if (! open (AMFILE, $amfile))
1043     {
1044         print STDERR "automake: couldn't open $amfile: $!\n";
1045         exit 1;
1046     }
1047
1048     local ($saw_bk) = 0;
1049     local ($was_rule) = 0;
1050     local ($last_var_name) = '';
1051
1052     while (<AMFILE>)
1053     {
1054         chop;
1055
1056         if ($saw_bk)
1057         {
1058             if ($was_rule)
1059             {
1060                 $output_trailer .= $_ . "\n";
1061             }
1062             else
1063             {
1064                 $output_vars .= $_ . "\n";
1065                 if (substr ($_, -1) eq "\\")
1066                 {
1067                     $contents{$last_var_name} .= substr ($_, 0,
1068                                                          length ($_) - 1);
1069                 }
1070                 else
1071                 {
1072                     $contents{$last_var_name} .= $_;
1073                 }
1074             }
1075         }
1076         elsif ($_ eq '@kr@')
1077         {
1078             # Special case: this means we want automatic
1079             # de-ANSI-fication.  FIXME think of a better way.
1080             $contents{'@kr@'} = 1;
1081         }
1082         elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
1083         {
1084             # Found a rule.
1085             $was_rule = 1;
1086             # Value here doesn't matter; for targets we only note
1087             # existence.
1088             $contents{$1} = 1;
1089             $output_trailer .= $_ . "\n";
1090         }
1091         elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[    ]*=[    ]*(.*)$/)
1092         {
1093             # Found a variable reference.
1094             $was_rule = 0;
1095             $last_var_name = $1;
1096             if (substr ($2, -1) eq "\\")
1097             {
1098                 $contents{$1} = substr ($2, 0, length ($2) - 1);
1099             }
1100             else
1101             {
1102                 $contents{$1} = $2;
1103             }
1104             $output_vars .= $_ . "\n";
1105         }
1106         elsif (m/^$/)
1107         {
1108             # Special rule: if looking at a blank line, append it to
1109             # whatever we saw last.
1110             if ($was_rule)
1111             {
1112                 $output_trailer .= "\n";
1113             }
1114             else
1115             {
1116                 $output_vars .= "\n";
1117             }
1118         }
1119         else
1120         {
1121             # This isn't an error; it is probably a continued rule.
1122             # In fact, this is what we assume.
1123             $output_trailer .= $_ . "\n";
1124         }
1125
1126         $saw_bk = (substr ($_, -1) eq "\\");
1127     }
1128
1129     # Include some space after user code.
1130     $output_vars .= "\n";
1131     $output_trailer .= "\n";
1132 }
1133
1134 ################################################################
1135
1136 # Initialize global variables.
1137 sub init_globals
1138 {
1139     # Associative array of standard directory names.  Entry is TRUE if
1140     # corresponding directory should be installed during
1141     # 'install-exec' phase.
1142     %exec_dir_p =
1143         ('bin' => 1,
1144          'sbin' => 1,
1145          'libexec' => 1,
1146          'data' => 0,
1147          'sysconf' => 1,
1148          'localstate' => 1,
1149          'lib' => 1,
1150          'info' => 0,
1151          'man' => 0,
1152          'include' => 0,
1153          'oldinclude' => 0,
1154          'pkgdata' => 0,
1155          'pkglib' => 1,
1156          'pkginclude' => 0
1157          );
1158
1159     # Helper text for dealing with man pages.
1160     $install_man_format =
1161     '   sect=@SECTION@;                         \\
1162         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1163         echo installing @MAN@ as $(mandir)/man$$sect/$$inst; \\
1164         $(INSTALL_DATA) $(srcdir)/@MAN@ $(mandir)/man$$sect/$$inst; \\
1165         if test -d $(mandir)/cat$$sect; then    \\
1166           echo formatting @MAN@ as $(mandir)/cat$$sect/$$inst;  \\
1167           $(NROFF) -man $(srcdir)/@MAN@ > $(mandir)/cat$$sect/$$inst; \\
1168         else                                    \\
1169           true;                                 \\
1170         fi
1171 ';
1172
1173     $uninstall_man_format =
1174     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
1175         rm -f $(mandir)/man@SECTION@/$$inst $(mandir)/cat@SECTION@/$$inst
1176 ';
1177
1178     # Commonly found files we look for and automatically include in
1179     # DIST_FILES.
1180     @common_files =
1181         (
1182          "THANKS", "TODO", "README", "NEWS", "COPYING", "COPYING.LIB",
1183          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
1184          "config.guess", "config.sub"
1185          );
1186
1187     # Commonly used files we auto-include, but only sometimes.
1188     @common_sometimes =
1189         (
1190          "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
1191          "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
1192          "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
1193          );
1194
1195     $USAGE = "\
1196   --amdir=DIR           directory storing config files
1197   --help                print this help, then exit
1198   --include-deps        include generated dependencies in Makefile.in
1199   --output-dir=DIR      put generated Makefile.in's into DIR
1200   --version             print version number, then exit\n";
1201
1202 }
1203
1204 ################################################################
1205
1206 # Return contents of a file from $am_dir.
1207 sub file_contents
1208 {
1209     local ($basename) = @_;
1210     local ($file) = $am_dir . '/' . $basename . '.am';
1211
1212     if (! open (FC_FILE, $file))
1213     {
1214         print STDERR "automake: installation error: cannot open \"$file\"\n";
1215         exit 1;
1216     }
1217
1218     # Lines starting with "##" are comments for developer use only.
1219     # Maybe this is a bad idea?
1220     local ($result) = '';
1221     while (<FC_FILE>)
1222     {
1223         $result .= $_ unless ( m/^##/);
1224     }
1225     close (FC_FILE);
1226     return $result;
1227 }
1228
1229 # Return contents of some Makefile.am variable.  Allow for AM_ style
1230 # overrides.
1231 sub am_variable
1232 {
1233     local ($varname) = @_;
1234
1235     return (defined ($contents{'AM_' . $varname})
1236             ? $contents{'AM_' . $varname}
1237             : $contents{$varname});
1238 }
1239
1240 # Handle `where_HOW' variable magic.  Does all lookups, generates
1241 # install code,and possibly generates code to define the primary
1242 # variable.  The first argument is the name of the .am file to munge,
1243 # the second argument is the primary variable (eg HEADERS), and all
1244 # subsequent arguments are possible installation locations.  FIXME
1245 # should scan all defined variables and do some error checking to
1246 # avoid typos (eg 'bnidir_PROGRAMS' should give error).  Returns TRUE
1247 # if any items were found, FALSE otherwise.
1248 sub am_install_var
1249 {
1250     local ($file, $primary, @prefixes) = @_;
1251     local (@used) = ();
1252
1253     local ($contents) = &file_contents ($file);
1254     local ($munge);
1255     foreach (@prefixes)
1256     {
1257         if (defined $contents{$_ . '_' . $primary})
1258         {
1259             push (@used, '${' . $_ . '_' . $primary . '}');
1260             if ($_ eq 'noinst')
1261             {
1262                 # Objects in noinst_FOO never get installed.
1263                 next;
1264             }
1265
1266             ($munge = $contents) =~ s/\@DIR\@/$_/g;
1267             $output_rules .= $munge;
1268
1269             push (@uninstall, 'uninstall-' . $_ . $primary);
1270             push (@installdirs, '${' . $_ . 'dir}');
1271             if ($exec_dir_p{$_})
1272             {
1273                 push (@install_exec, 'install-' . $_ . $primary);
1274             }
1275             else
1276             {
1277                 push (@install_data, 'install-' . $_ . $primary);
1278             }
1279         }
1280     }
1281
1282     if (! defined $contents{$primary} && $#used >= 0)
1283     {
1284         # Define it.
1285         $output_vars .= $primary . " = " . join (' ', @used) . "\n\n";
1286     }
1287
1288     return ($#used >= 0);
1289 }
1290
1291
1292 ################################################################
1293
1294 # Verify that the file must exist in the current directory.
1295 sub require_file
1296 {
1297     local ($file) = @_;
1298     local ($fullfile) = $relative_dir . "/" . $file;
1299
1300     if (! -f $fullfile)
1301     {
1302         &am_error ("required file \"$fullfile\" not found");
1303     }
1304     else
1305     {
1306         push (@dist_common, $file);
1307     }
1308 }
1309
1310
1311 ################################################################
1312
1313 # Return directory name of file.
1314 sub dirname
1315 {
1316     local ($file) = @_;
1317     local ($sub);
1318
1319     ($sub = $file) =~ s,/+[^/]+,,g;
1320     if ($sub eq $file)
1321     {
1322         $sub = '.';
1323     }
1324
1325     return $sub;
1326 }
1327
1328 # Make a directory.
1329 sub mkdir
1330 {
1331     local ($dirname) = @_;
1332     system ("mkdir", $dirname);
1333 }
1334
1335 ################################################################
1336
1337 # Print an error message and set exit status.
1338 sub am_error
1339 {
1340     print STDERR "automake: ${am_file}.am: ", join (' ', @_), "\n";
1341     $exit_status = 1;
1342 }
1343
1344 # Print usage information.
1345 sub usage
1346 {
1347     print "Usage: automake [OPTION] ... [Makefile]...\n";
1348     print $USAGE;
1349     print "\nFiles which are automatically distributed, if found:\n";
1350     $~ = "USAGE_FORMAT";
1351     local (@lcomm) = sort ((@common_files, @common_sometimes));
1352     local ($one, $two);
1353     while ($#lcomm >= 0)
1354     {
1355         $one = shift (@lcomm);
1356         $two = shift (@lcomm);
1357         write;
1358     }
1359
1360     exit 0;
1361 }
1362
1363 format USAGE_FORMAT =
1364   @<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<
1365   $one,                $two
1366 .