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)
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@";
33 $am_dir = "@datadir@/@PACKAGE@";
35 # Commonly found files we look for and automatically include in
39 "THANKS", "TODO", "README", "NEWS", "COPYING", "COPYING.LIB",
40 "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
41 "config.guess", "config.sub"
44 # Commonly used files we auto-include, but only sometimes.
47 "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
48 "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
49 "ansi2knr.1", 'stamp-vti', "mkinstalldirs", "install-sh"
52 $USAGE = " --amdir=DIR directory storing config files
53 --help print this help, then exit
54 --version print version number, then exit
55 --include-deps include generated dependencies in Makefile\n";
59 # This is TRUE if GNU make specific automatic dependency generation
60 # code should be included in generated Makefile.in.
61 $use_dependencies = 1;
63 # This holds our (eventual) exit status. We don't actually exit until
64 # we have processed all input files.
67 # These two variables are used when generating each Makefile.in. They
68 # hold the Makefile.in until it is ready to be printed.
73 # Suffixes found during a run.
76 # This holds the contents of a Makefile.am, as parsed by read_am_file.
79 # This holds the "relative directory" of the current Makefile.in. Eg
80 # for src/Makefile.in, this is "src".
83 # This holds a list of files that are included in the distribution.
86 # List of dependencies for the obvious targets.
97 # TRUE if current directory holds any C source files.
98 $dir_holds_sources = 0;
100 # TRUE if install targets should work recursively.
101 $recursive_install = 0;
105 # Parse command line.
106 @input_files = &parse_arguments (@ARGV);
108 # Now do all the work on each file.
109 foreach $am_file (@input_files)
111 if (! -f ($am_file . '.am'))
113 print STDERR "automake: $am_file" . ".am: no such file\n";
118 &generate_makefile ($am_file);
125 ################################################################
127 # Parse command line.
130 local (@arglist) = @_;
133 while ($#arglist >= 0)
135 if ($arglist[0] eq "--version")
137 print "Automake version $VERSION\n";
140 elsif ($arglist[0] eq "--help")
144 elsif ($arglist[0] =~ /^--amdir=(.+)$/)
148 elsif ($arglist[0] eq '--amdir')
153 "automake: no argument given for option \`$arglist[0]'\n";
157 $am_dir = $arglist[0];
159 elsif ($arglist[0] eq '--include-deps')
161 $use_dependencies = 0;
163 elsif ($arglist[0] eq '--')
165 # Stop option processing.
167 push (@make_list, @arglist);
170 elsif ($arglist[0] =~ /^-/)
172 print STDERR "automake: unrecognized option -- \`$arglist[0]'\n";
177 push (@make_list, $arglist[0]);
185 # Look around for some files.
186 push (@make_list, 'Makefile') if -f 'Makefile.am';
188 foreach (<*/Makefile.am>)
191 push (@make_list, $_);
194 if ($#make_list >= 0)
196 print "automake: using ", join (' ', @make_list), "\n";
200 print STDERR "automake: no \"Makefile.am\" found or specified\n";
208 ################################################################
210 # Generate a Makefile.in given the name of the corresponding Makefile.
211 sub generate_makefile
213 local ($makefile) = @_;
215 print "creating ", $makefile, ".in\n";
217 $relative_dir = &dirname ($makefile);
220 $output_trailer = '';
223 @dist_common = ('Makefile.in', 'Makefile.am');
227 $dir_holds_sources = 0;
228 $recursive_install = 0;
235 # Generate header before reading .am file. The header must come
236 # before anything else, and read_am_file copies code into the
240 # This is always the default target. This gives us freedom to do
241 # things in whatever order is convenient.
242 $output_rules .= "default: all\n\n";
244 &read_am_file ($makefile . '.am');
247 local ($programs) = (defined ($contents{'AM_PROGRAMS'})
248 ? $contents{'AM_PROGRAMS'}
249 : $contents{'PROGRAMS'});
250 local ($libprograms) = (defined ($contents{'AM_LIBPROGRAMS'})
251 ? $contents{'AM_LIBPROGRAMS'}
252 : $contents{'LIBPROGRAMS'});
253 local ($libraries) = (defined ($contents{'AM_LIBRARIES'})
254 ? $contents{'AM_LIBRARIES'}
255 : $contents{'LIBRARIES'});
256 local ($scripts) = (defined ($contents{'AM_SCRIPTS'})
257 ? $contents{'AM_SCRIPTS'}
258 : $contents{'SCRIPTS'});
259 local ($libscripts) = (defined ($contents{'AM_LIBSCRIPTS'})
260 ? $contents{'AM_LIBSCRIPTS'}
261 : $contents{'LIBSCRIPTS'});
263 &handle_programs ($programs, $libprograms, $libraries);
264 &handle_scripts ($scripts, $libscripts);
265 &handle_libraries ($libraries);
274 &handle_dependencies;
276 &handle_merge_targets;
279 if (! open (GM_FILE, "> " . $makefile . ".in"))
281 print STDERR "automake: cannot open ", $makefile, ".in: ", $!, "\n";
286 print GM_FILE $output_vars;
287 print GM_FILE $output_rules;
288 print GM_FILE $output_trailer;
293 ################################################################
295 # Generate header of Makefile.in.
300 . "# Makefile.in generated automatically by automake "
302 . " from Makefile.am\n");
304 $output_vars = $output_vars . &file_contents ('header-vars');
305 $output_rules = $output_rules . &file_contents ('header');
308 # Handle C programs and libraries.
311 local ($programs, $libprograms, $libraries) = @_;
313 if (!$programs && !$libprograms && !$libraries)
318 $dir_holds_sources = 1;
321 $output_vars .= &file_contents ('compile-vars');
322 $output_rules .= &file_contents ('compile');
324 # Check for automatic de-ANSI-fication.
326 push (@suffixes, '.c', '.o');
327 if (defined $contents{'@kr@'})
330 push (@suffixes, '._c', '._o');
332 &require_file ('ansi2knr.c');
333 &require_file ('ansi2knr.1');
335 $output_vars .= &file_contents ('kr-vars');
336 $output_rules .= &file_contents ('compile-kr');
337 $output_rules .= &file_contents ('clean-kr');
342 local (@sources, @objects);
343 push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
344 push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
347 foreach $one_file (split (' ', ($programs . ' '
351 # Look for file_SOURCES and file_OBJECTS. FIXME file_OBJECTS
352 # should probably not be used(?)
353 if (defined $contents{$one_file . "_SOURCES"})
355 if (! defined $contents{$one_file . "_OBJECTS"})
357 # Turn sources into objects.
358 $_ = $contents{$one_file . "_SOURCES"};
360 # Ugh: Perl syntax vs Emacs.
361 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
366 s/\.[cCmylfs]/$obj/g;
368 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
371 push (@sources, '${' . $one_file . "_SOURCES}");
372 push (@objects, '${' . $one_file . "_OBJECTS}");
376 $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
377 . $one_file . "_OBJECTS = ". $one_file
379 push (@sources, $one_file . '.c');
380 push (@objects, $one_file . $obj);
383 if (defined $contents{'CONFIG_HEADER'})
385 $output_rules .= ('$(' . $one_file . "_OBJECTS): "
386 . $contents{'CONFIG_HEADER'} . "\n");
390 $output_vars .= "\n";
392 # Re-init SOURCES and OBJECTS. FIXME other code shouldn't depend
394 $contents{'SOURCES'} = join (' ', @sources);
395 $contents{'OBJECTS'} = join (' ', @objects);
397 # Some boilerplate, and install rules.
400 $output_rules .= &file_contents ('programs');
401 push (@install_exec, "install-programs");
402 push (@uninstall, 'uninstall-programs');
403 push (@clean, 'programs');
407 $output_rules .= &file_contents ('libprograms');
408 push (@install_exec, 'install-libprograms');
409 push (@uninstall, 'uninstall-libprograms');
410 push (@clean, 'libprograms');
414 if ($programs || $libprograms)
416 local ($fcont) = &file_contents ('program');
418 foreach $one_file (split (' ', $programs . ' ' . $libprograms))
420 if (! defined $contents{$one_file . "_LDADD"})
422 # User didn't define prog_LDADD override. So do it.
423 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
426 ($munge = $fcont) =~ s/@PROGRAM@/$one_file/g;
427 $output_rules .= $munge;
435 local ($libraries) = @_;
437 return if (!$libraries);
439 local (@liblist) = split (' ', $libraries);
441 $output_rules .= &file_contents ('libraries');
442 local ($onefile) = &file_contents ('library');
443 local ($onelib, $munge);
444 foreach $onelib (@liblist)
446 ($munge = $onefile) =~ s/@LIBRARY@/$onelib/g;
447 $output_rules .= $munge;
450 # Turn "foo" into "libfoo.a" and include macro definition.
451 grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
452 $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
453 . &file_contents ('libraries-vars'));
455 push (@install_exec, 'install-libraries');
456 push (@uninstall, 'uninstall-libraries');
457 push (@clean, 'libraries');
463 local ($scripts, $libscripts) = @_;
467 $output_rules .= &file_contents ('scripts');
468 push (@install_exec, 'install-scripts');
469 push (@uninstall, 'uninstall-scripts');
470 push (@clean, 'scripts');
475 $output_rules .= &file_contents ('libscripts');
476 push (@install_exec, 'install-libscripts');
477 push (@uninstall, 'uninstall-libscripts');
478 push (@clean, 'libscripts');
482 # Handle all Texinfo source.
485 return if (! defined $contents{TEXINFOS});
487 local (@texis) = split (' ', $contents{TEXINFOS});
490 print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
496 ($infobase = $texis[0]) =~ s/\.texi$//;
498 # If 'version.texi' is referenced by input file, then include
499 # automatic versioning capability.
500 system ("grep version.texi " . $relative_dir . "/" . $texis[0]
501 . " > /dev/null 2>&1");
505 push (@texis, 'version.texi');
506 push (@dist_common, 'version.texi', 'stamp-vti');
507 push (@clean, 'vti');
510 ($tfile = &file_contents ('texi-version')) =~ s/@TEXI@/$texis[0]/g;
511 $output_rules = $output_rules . $tfile;
513 &require_file ('mdate-sh');
516 # If user specified file_TEXINFOS, then use that as explicit
518 if (defined $contents{$infobase . "_TEXINFOS"})
520 push (@texis, "\$" . $infobase . '_TEXINFOS');
521 push (@dist_common, "\$" . $infobase . '_TEXINFOS');
526 $output_rules = ($output_rules . $infobase . ".info: "
527 . join (' ', @texis) . "\n\n");
531 $output_vars = $output_vars . &file_contents ('texinfos-vars');
532 $output_rules = $output_rules . &file_contents ('texinfos');
534 push (@suffixes, '.texi', '.info', '.dvi');
535 push (@uninstall, 'uninstall-info');
536 push (@clean, 'info');
537 push (@info, '$(INFO_DEPS)');
538 push (@dvi, '$(DVIS)');
539 # Make sure documentation is made and installed first.
540 unshift (@install_data, 'install-info');
541 unshift (@all, 'info');
543 $output_vars .= ("INFOS = " . $infobase . ".info*\n"
544 . "INFO_DEPS = " . $infobase . ".info\n"
545 . "DVIS = " . $infobase . ".dvi\n\n");
547 # Do some error checking.
548 &require_file ('texinfo.tex');
551 # Handle any man pages.
554 return if (! defined $contents{'MANS'});
556 $output_vars .= &file_contents ('mans-vars');
557 $output_rules .= &file_contents ('mans');
559 push (@install_data, 'install-man');
560 push (@uninstall, 'uninstall-man');
563 # Handle DATA and PACKAGEDATA.
566 if (defined $contents{'DATA'})
568 $output_rules .= &file_contents ('data');
569 push (@install_data, 'install-ddata');
570 push (@uninstall, 'uninstall-ddata');
573 if (defined $contents{'PACKAGEDATA'})
575 $output_rules .= &file_contents ('packagedata');
576 push (@install_data, 'install-pdata');
577 push (@uninstall, 'uninstall-pdata');
584 local ($tagging) = 0;
586 if (defined ($contents{'SUBDIRS'}))
588 $output_rules .= &file_contents ('tags');
591 elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
593 $output_rules .= &file_contents ('tags-subd');
599 $output_rules .= &file_contents ('tags-clean');
600 push (@clean, 'tags');
604 # Handle 'dist' target.
607 # Look for common files that should be included in distribution.
609 foreach $cfile (@common_files)
611 if (-f ($relative_dir . "/" . $cfile))
613 push (@dist_common, $cfile);
617 $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
620 if ($relative_dir ne '.')
623 $output_vars .= (&file_contents ('dist-subd-vars')
624 . "subdir = " . $relative_dir . "\n\n");
625 $output_rules .= &file_contents ('dist-subd');
629 $output_vars .= &file_contents ('dist-vars');
630 $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
636 # Handle auto-dependency code.
637 sub handle_dependencies
639 if ($use_dependencies)
641 # Include GNU-make-specific auto-dep code.
642 if ($dir_holds_sources)
644 $output_rules .= &file_contents ('depend');
649 # Include any auto-generated deps that are present.
650 if (-d ($relative_dir . "/.deps") && -f ($relative_dir . "/.deps/.P"))
653 local ($gpat) = $relative_dir . "/.deps/*.P";
655 foreach $depfile (<${gpat}>)
657 if (! open (DEP_FILE, $depfile))
659 print STDERR "automake: couldn't open $depfile: $!\n";
664 $output_rules .= join ('', <DEP_FILE>) . "\n";
672 # Handle subdirectories.
675 return if (! defined ($contents{'SUBDIRS'}));
677 $output_rules .= &file_contents ('subdirs');
679 push (@all, "all-recursive");
680 push (@check, "check-recursive");
681 push (@info, "info-recursive");
682 push (@dvi, "dvi-recursive");
684 $recursive_install = 1;
687 # Handle remaking and configure stuff.
690 if ($relative_dir ne '.')
693 $output_rules .= &file_contents ('remake-subd');
699 $output_vars .= "ACLOCAL = aclocal.m4\n";
700 push (@dist_common, 'aclocal.m4');
702 $output_rules .= &file_contents ('remake');
704 # Look for some files we need.
705 &require_file ('install-sh');
706 &require_file ('mkinstalldirs');
709 if (defined ($configure{'CONFIG_HEADER'})
710 && $contents{'CONFIG_HEADER'} !~ m,/,)
712 # Header defined and in this directory.
715 $output_vars .= "ACCONFIG = acconfig.h\n";
716 push (@dist_common, 'acconfig.h');
718 if (-f 'config.h.top')
720 $output_vars .= "CONFIG_TOP = config.h.top\n";
721 push (@dist_common, 'config.h.top');
723 if (-f 'config.h.bot')
725 $output_vars .= "CONFIG_BOT = config.h.bot\n";
726 push (@dist_common, 'config.h.bot');
729 push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
731 $output_rules .= &file_contents ('remake-hdr');
735 # Handle footer elements.
738 if ($contents{'SOURCES'})
740 $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
742 if ($contents{'OBJECTS'})
744 $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
746 if ($contents{'SOURCES'} || $contents{'OBJECTS'})
748 $output_vars .= "\n";
751 $output_trailer .= ".SUFFIXES:\n";
754 $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
756 $output_trailer .= "\n" . &file_contents ('footer');
759 # There are several targets which need to be merged. This is because
760 # their complete definition is compiled from many parts. Note that we
761 # avoid double colon rules, otherwise we'd use them instead.
762 sub handle_merge_targets
764 &do_one_merge_target ('all', @all);
765 &do_one_merge_target ('info', @info);
766 &do_one_merge_target ('dvi', @dvi);
767 &do_one_merge_target ('check', @check);
769 # Handle the various install targets specially. We do this so
770 # that (eg) "make install-exec" will run "install-exec-recursive"
771 # if required, but "make install" won't run it twice. Step one is
772 # to see if the user specified local versions of any of the
774 if (defined $contents{'install-exec-local'})
776 push (@install_exec, 'install-exec-local');
778 if (defined $contents{'install-data-local'})
780 push (@install_data, 'install-data-local');
782 if (defined $contents{'uninstall-local'})
784 push (@uninstall, 'uninstall-local');
787 if (defined $contents{'install-local'})
789 print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
792 # Step two: if we are doing recursive makes, write out the
795 if ($recursive_install)
797 push (@install, 'install-recursive');
798 push (@uninstall, 'uninstall-recursive');
800 if ($#install_exec >= 0)
802 $output_rules .= ('install-exec-am: '
803 . join (' ', @install_exec)
805 @install_exec = ('install-exec-recursive', 'install-exec-am');
806 push (@install, 'install-exec-am');
808 if ($#install_data >= 0)
810 $output_rules .= ('install-data-am: '
811 . join (' ', @install_data)
813 @install_data = ('install-data-recursive', 'install-data-am');
814 push (@install, 'install-data-am');
816 if ($#uninstall >= 0)
818 $output_rules .= ('uninstall-am: '
819 . join (' ', @uninstall)
821 @uninstall = ('uninstall-recursive', 'uninstall-am');
825 # Step three: print definitions users can use.
826 if ($#install_exec >= 0)
828 $output_rules .= ("install-exec: "
829 . join (' ', @install_exec)
831 push (@install, 'install-exec') if (!$recursive_install);
833 if ($#install_data >= 0)
835 $output_rules .= ("install-data: "
836 . join (' ', @install_data)
838 push (@install, 'install-data') if (!$recursive_install);
841 $output_rules .= ('install: '
842 . join (' ', @install)
845 . join (' ', @uninstall)
849 # Helper for handle_merge_targets.
850 sub do_one_merge_target
852 local ($name, @values) = @_;
854 if (defined $contents{$name . '-local'})
856 # User defined local form of target. So include it.
857 push (@values, $name . '-local');
860 $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
863 # Handle all 'clean' targets.
866 local ($target) = $recursive_install ? 'clean-am' : 'clean';
867 &do_one_clean_target ($target, 'mostly', '', @clean);
868 &do_one_clean_target ($target, '', 'mostly', @clean);
869 &do_one_clean_target ($target, 'dist', '', @clean);
870 &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
873 if ($recursive_install)
875 @deps = ('am', 'recursive');
876 &do_one_clean_target ('', 'mostly', '', @deps);
877 &do_one_clean_target ('', '', '', @deps);
878 &do_one_clean_target ('', 'dist', '', @deps);
879 &do_one_clean_target ('', 'maintainer-', '', @deps);
883 # Helper for handle_clean.
884 sub do_one_clean_target
886 local ($target, $name, $last_name, @deps) = @_;
888 # Special case: if target not passed, then don't generate
889 # dependency on next "lower" clean target (eg no
890 # clean<-mostlyclean derivation). In this case the target is
891 # implicitly known to be 'clean'.
892 local ($flag) = $target;
898 $output_rules .= $name . $target . ": ";
901 if ($last_name || $name ne 'mostly')
903 $output_rules .= $last_name . $target . " ";
907 $output_rules .= ($name . 'clean-' . join (' ' . $name . 'clean-', @deps)
911 ################################################################
913 # Read Makefile.am and set up %contents. Simultaneously copy lines
914 # from Makefile.am into $output_trailer or $output_vars as
915 # appropriate. NOTE we put rules in the trailer section. We want
916 # user rules to come after our generated stuff.
919 local ($amfile) = @_;
921 if (! open (AMFILE, $amfile))
923 print STDERR "automake: couldn't open $amfile: $!\n";
928 local ($was_rule) = 0;
929 local ($last_var_name) = '';
939 $output_trailer .= $_ . "\n";
943 $output_vars .= $_ . "\n";
944 if (substr ($_, -1) eq "\\")
946 $contents{$last_var_name} .= substr ($_, 0,
951 $contents{$last_var_name} .= $_;
957 # Special case: this means we want automatic
958 # de-ANSI-fication. FIXME think of a better way.
959 $contents{'@kr@'} = 1;
961 elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
965 # Value here doesn't matter; for targets we only note
968 $output_trailer .= $_ . "\n";
970 elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[ ]*=[ ]*(.*)$/)
972 # Found a variable reference.
975 if (substr ($2, -1) eq "\\")
977 $contents{$1} = substr ($2, 0, length ($2) - 1);
983 $output_vars .= $_ . "\n";
987 # Special rule: if looking at a blank line, append it to
988 # whatever we saw last.
991 $output_trailer .= "\n";
995 $output_vars .= "\n";
1000 # This isn't an error; it is probably a continued rule.
1001 # In fact, this is what we assume.
1002 $output_trailer .= $_ . "\n";
1005 $saw_bk = (substr ($_, -1) eq "\\");
1008 # Include some space after user code.
1009 $output_vars .= "\n";
1010 $output_trailer .= "\n";
1014 ################################################################
1016 # Return contents of a file from $am_dir.
1019 local ($basename) = @_;
1020 local ($file) = $am_dir . '/' . $basename . '.am';
1022 if (! open (FC_FILE, $file))
1024 print STDERR "automake: installation error: cannot open \"$file\"\n";
1028 # Yes, we really want to slurp it.
1029 local ($results) = join ('', <FC_FILE>);
1036 ################################################################
1038 # Verify that the file must exist in the current directory.
1042 local ($fullfile) = $relative_dir . "/" . $file;
1046 print STDERR "automake: required file \"$fullfile\" not found\n";
1051 push (@dist_common, $file);
1056 ################################################################
1058 # Return directory name of file.
1064 ($sub = $file) =~ s,/+[^/]+,,g;
1073 ################################################################
1075 # Print usage information.
1078 print "Usage: automake [OPTION] ... [Makefile]...\n";
1080 print "\nFiles which are automatically distributed, if found:\n";
1081 $~ = "USAGE_FORMAT";
1082 local (@lcomm) = sort ((@common_files, @common_sometimes));
1084 while ($#lcomm >= 0)
1086 $one = shift (@lcomm);
1087 $two = shift (@lcomm);
1094 format USAGE_FORMAT =
1095 @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<