5 # automake - create Makefile.in from Makefile.am
6 # Copyright (C) 1994 Free Software Foundation, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
23 # Perl reimplementation by Tom Tromey <tromey@drip.colorado.edu>.
26 # Parameters set by configure. Not to be changed.
27 $PACKAGE = "@PACKAGE@";
28 $VERSION = "@VERSION@";
30 $am_dir = "@datadir@/@PACKAGE@";
32 # Commonly found files we look for and automatically include in
36 "THANKS", "TODO", "README", "NEWS", "COPYING", "COPYING.LIB",
37 "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
38 "config.guess", "config.sub", "mkinstalldirs", "install-sh"
41 # Commonly used files we auto-include, but only sometimes.
44 "version.texi", "aclocal.m4", "acconfig.h", "config.h.top",
45 "config.h.bot", "stamp-h.in", "mdate-sh", "ansi2knr.c",
49 $USAGE = " --amdir=DIR directory storing config files
50 --help print this help, then exit
51 --version print version number, then exit
52 --include-deps include generated dependencies in Makefile\n";
56 # This is TRUE if GNU make specific automatic dependency generation
57 # code should be included in generated Makefile.in.
58 $use_dependencies = 1;
60 # This holds our (eventual) exit status. We don't actually exit until
61 # we have processed all input files.
64 # These two variables are used when generating each Makefile.in. They
65 # hold the Makefile.in until it is ready to be printed.
70 # Suffixes found during a run.
73 # This holds the contents of a Makefile.am, as parsed by read_am_file.
76 # This holds the "relative directory" of the current Makefile.in. Eg
77 # for src/Makefile.in, this is "src".
80 # This holds a list of files that are included in the distribution.
83 # List of dependencies for the obvious targets.
93 # TRUE if current directory holds any C source files.
94 $dir_holds_sources = 0;
96 # TRUE if install targets should work recursively.
97 $recursive_install = 0;
101 # Parse command line.
102 @input_files = &parse_arguments (@ARGV);
104 # Now do all the work on each file.
105 foreach $am_file (@input_files)
107 if (! -f ($am_file . '.am'))
109 print STDERR "automake: $am_file" . ".am: no such file\n";
114 &generate_makefile ($am_file);
121 ################################################################
123 # Parse command line.
126 local (@arglist) = @_;
129 while ($#arglist >= 0)
131 if ($arglist[0] eq "--version")
133 print "Automake version $VERSION\n";
136 elsif ($arglist[0] eq "--help")
140 elsif ($arglist[0] =~ /^--amdir=(.+)$/)
144 elsif ($arglist[0] eq '--amdir')
149 "automake: no argument given for option \`$arglist[0]'\n";
153 $am_dir = $arglist[0];
155 elsif ($arglist[0] eq '--include-deps')
157 $use_dependencies = 0;
159 elsif ($arglist[0] eq '--')
161 # Stop option processing.
163 push (@make_list, @arglist);
166 elsif ($arglist[0] =~ /^-/)
168 print STDERR "automake: unrecognized option -- \`$arglist[0]'\n";
173 push (@make_list, $arglist[0]);
181 # Look around for some files.
182 push (@make_list, 'Makefile') if -f 'Makefile.am';
184 foreach (<*/Makefile.am>)
187 push (@make_list, $_);
190 if ($#make_list >= 0)
192 print "automake: using ", join (' ', @make_list), "\n";
196 print STDERR "automake: no \"Makefile.am\" found or specified\n";
204 ################################################################
206 # Generate a Makefile.in given the name of the corresponding Makefile.
207 sub generate_makefile
209 local ($makefile) = @_;
211 print "creating ", $makefile, ".in\n";
213 $relative_dir = &dirname ($makefile);
216 $output_trailer = '';
219 @dist_common = ('Makefile.in', 'Makefile.am');
223 $dir_holds_sources = 0;
224 $recursive_install = 0;
230 # Generate header before reading .am file. The header must come
231 # before anything else, and read_am_file copies code into the
235 # This is always the default target. This gives us freedom to do
236 # things in whatever order is convenient.
237 $output_rules .= "default: all\n\n";
239 &read_am_file ($makefile . '.am');
242 local ($programs) = (defined ($contents{'AM_PROGRAMS'})
243 ? $contents{'AM_PROGRAMS'}
244 : $contents{'PROGRAMS'});
245 local ($libprograms) = (defined ($contents{'AM_LIBPROGRAMS'})
246 ? $contents{'AM_LIBPROGRAMS'}
247 : $contents{'LIBPROGRAMS'});
248 local ($libraries) = (defined ($contents{'AM_LIBRARIES'})
249 ? $contents{'AM_LIBRARIES'}
250 : $contents{'LIBRARIES'});
251 local ($scripts) = (defined ($contents{'AM_SCRIPTS'})
252 ? $contents{'AM_SCRIPTS'}
253 : $contents{'SCRIPTS'});
254 local ($libscripts) = (defined ($contents{'AM_LIBSCRIPTS'})
255 ? $contents{'AM_LIBSCRIPTS'}
256 : $contents{'LIBSCRIPTS'});
258 &handle_programs ($programs, $libprograms, $libraries);
259 &handle_scripts ($scripts, $libscripts);
260 &handle_libraries ($libraries);
269 &handle_dependencies;
271 &handle_merge_targets;
273 if (! open (GM_FILE, "> " . $makefile . ".in"))
275 print STDERR "automake: cannot open ", $makefile, ".in: ", $!, "\n";
280 print GM_FILE $output_vars;
281 print GM_FILE $output_rules;
282 print GM_FILE $output_trailer;
287 ################################################################
289 # Generate header of Makefile.in.
294 . "# Makefile.in generated automatically by automake "
296 . " from Makefile.am\n");
298 $output_vars = $output_vars . &file_contents ('header-vars');
299 $output_rules = $output_rules . &file_contents ('header');
302 # Handle C programs and libraries.
305 local ($programs, $libprograms, $libraries) = @_;
307 if (!$programs && !$libprograms && !$libraries)
312 $dir_holds_sources = 1;
315 $output_vars .= &file_contents ('compile-vars');
316 $output_rules .= &file_contents ('compile');
318 # Check for automatic de-ANSI-fication.
320 push (@suffixes, '.c', '.o');
321 if (defined $contents{'@kr@'})
324 push (@suffixes, '._c', '._o');
326 &require_file ('ansi2knr.c');
327 &require_file ('ansi2knr.1');
329 $output_vars .= &file_contents ('kr-vars');
330 $output_rules .= &file_contents ('compile-kr');
333 local (@sources, @objects);
334 push (@sources, '${SOURCES}') if (defined $contents{'SOURCES'});
335 push (@objects, '${OBJECTS}') if (defined $contents{'OBJECTS'});
338 foreach $one_file (split (' ', ($programs . ' '
342 # Look for file_SOURCES and file_OBJECTS. FIXME file_OBJECTS
343 # should probably not be used(?)
344 if (defined $contents{$one_file . "_SOURCES"})
346 if (! defined $contents{$one_file . "_OBJECTS"})
348 # Turn sources into objects.
349 $_ = $contents{$one_file . "_SOURCES"};
351 # Ugh: Perl syntax vs Emacs.
352 local ($krc1, $krc2) = ('\.\$\{kr\}c', '\.\$\(kr\)c');
357 s/\.[cCmylfs]/$obj/g;
359 $output_vars .= $one_file . "_OBJECTS = " . $_ . "\n";
362 push (@sources, '${' . $one_file . "_SOURCES}");
363 push (@objects, '${' . $one_file . "_OBJECTS}");
367 $output_vars .= ($one_file . "_SOURCES = " . $one_file . ".c\n"
368 . $one_file . "_OBJECTS = ". $one_file
370 push (@sources, $one_file . '.c');
371 push (@objects, $one_file . $obj);
374 if (defined $contents{'CONFIG_HEADER'})
376 $output_rules .= ('$(' . $one_file . "_OBJECTS): "
377 . $contents{'CONFIG_HEADER'} . "\n");
381 $output_vars .= "\n";
383 # Re-init SOURCES and OBJECTS. FIXME other code shouldn't depend
385 $contents{'SOURCES'} = join (' ', @sources);
386 $contents{'OBJECTS'} = join (' ', @objects);
388 # Some boilerplate, and install rules.
391 $output_rules .= &file_contents ('programs');
392 push (@install_exec, "install-programs");
393 push (@uninstall, 'uninstall-programs');
397 $output_rules .= &file_contents ('libprograms');
398 push (@install_exec, 'install-libprograms');
399 push (@uninstall, 'uninstall-libprograms');
403 if ($programs || $libprograms)
405 local ($fcont) = &file_contents ('program');
407 foreach $one_file (split (' ', $programs . ' ' . $libprograms))
409 if (! defined $contents{$one_file . "_LDADD"})
411 # User didn't define prog_LDADD override. So do it.
412 $output_vars .= $one_file . '_LDADD = ${LDADD}' . "\n";
415 ($munge = $fcont) =~ s/@PROGRAM@/$one_file/g;
416 $output_rules .= $munge;
424 local ($libraries) = @_;
426 return if (!$libraries);
428 local (@liblist) = split (' ', $libraries);
430 $output_rules .= &file_contents ('libraries');
431 local ($onefile) = &file_contents ('library');
432 local ($onelib, $munge);
433 foreach $onelib (@liblist)
435 ($munge = $onefile) =~ s/@LIBRARY@/$onelib/g;
436 $output_rules .= $munge;
439 # Turn "foo" into "libfoo.a" and include macro definition.
440 grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
441 $output_vars .= ("LIBFILES = " . join (' ', @liblist) . "\n\n"
442 . &file_contents ('libraries-vars'));
444 push (@install_exec, 'install-libraries');
445 push (@uninstall, 'uninstall-libraries');
451 local ($scripts, $libscripts) = @_;
455 $output_rules .= &file_contents ('scripts');
456 push (@install_exec, 'install-scripts');
457 push (@uninstall, 'uninstall-scripts');
462 $output_rules .= &file_contents ('libscripts');
463 push (@install_exec, 'install-libscripts');
464 push (@uninstall, 'uninstall-libscripts');
468 # Handle all Texinfo source.
471 return if (! defined $contents{TEXINFOS});
473 local (@texis) = split (' ', $contents{TEXINFOS});
476 print STDERR "automake: sorry, only one file allowed in \`TEXINFOS'\n";
482 ($infobase = $texis[0]) =~ s/\.texi$//;
484 # If 'version.texi' is referenced by input file, then include
485 # automatic versioning capability.
486 system ("grep version.texi " . $relative_dir . "/" . $texis[0]
487 . " > /dev/null 2>&1");
491 push (@texis, 'version.texi');
492 push (@dist_common, 'version.texi');
494 ($tfile = &file_contents ('texi-version')) =~ s/@TEXI@/$texis[0]/g;
495 $output_rules = $output_rules . $tfile;
497 &require_file ('mdate-sh');
500 # If user specified file_TEXINFOS, then use that as explicit
502 if (defined $contents{$infobase . "_TEXINFOS"})
504 push (@texis, "\$" . $infobase . '_TEXINFOS');
505 push (@dist_common, "\$" . $infobase . '_TEXINFOS');
510 $output_rules = ($output_rules . $infobase . ".info: "
511 . join (' ', @texis) . "\n\n");
515 $output_vars = $output_vars . &file_contents ('texinfos-vars');
516 $output_rules = $output_rules . &file_contents ('texinfos');
518 push (@suffixes, '.texi', '.info', '.dvi');
519 push (@install_data, 'install-info');
520 push (@uninstall, 'uninstall-info');
522 push (@info, '$(INFO_DEPS)');
523 push (@dvi, '$(DVIS)');
525 $output_vars .= ("INFOS = " . $infobase . "info*\n"
526 . "INFO_DEPS = " . $infobase . ".info\n"
527 . "DVIS = " . $infobase . ".dvi\n\n");
529 # Do some error checking.
530 &require_file ('texinfo.tex');
533 # Handle any man pages.
536 return if (! defined $contents{'MANS'});
538 $output_vars .= &file_contents ('mans-vars');
539 $output_rules .= &file_contents ('mans');
541 push (@install_data, 'install-man');
542 push (@uninstall, 'uninstall-man');
545 # Handle DATA and PACKAGEDATA.
548 if (defined $contents{'DATA'})
550 $output_rules .= &file_contents ('data');
551 push (@install_data, 'install-ddata');
552 push (@uninstall, 'uninstall-ddata');
555 if (defined $contents{'PACKAGEDATA'})
557 $output_rules .= &file_contents ('packagedata');
558 push (@install_data, 'install-pdata');
559 push (@uninstall, 'uninstall-pdata');
566 if (defined ($contents{'SUBDIRS'}))
568 $output_rules .= &file_contents ('tags');
570 elsif ($dir_holds_sources || defined ($contents{'ETAGS_ARGS'}))
572 $output_rules .= &file_contents ('tags-subd');
576 # Handle 'dist' target.
579 # Look for common files that should be included in distribution.
581 foreach $cfile (@common_files)
583 if (-f ($relative_dir . "/" . $cfile))
585 push (@dist_common, $cfile);
589 $output_vars .= "DIST_COMMON = " . join (' ', @dist_common) . "\n\n";
592 if ($relative_dir ne '.')
595 $output_vars .= (&file_contents ('dist-subd-vars')
596 . "subdir = " . $relative_dir . "\n\n");
597 $output_rules .= &file_contents ('dist-subd');
601 $output_vars .= &file_contents ('dist-vars');
602 $output_rules .= &file_contents (defined ($contents{'SUBDIRS'})
608 # Handle auto-dependency code.
609 sub handle_dependencies
611 if ($use_dependencies)
613 # Include GNU-make-specific auto-dep code.
614 if ($dir_holds_sources)
616 $output_rules .= &file_contents ('depend');
621 # Include any auto-generated deps that are present.
622 if (-d ($relative_dir . "/.deps") && -f ($relative_dir . ".deps/.P"))
625 local ($gpat) = $relative_dir . "/.deps/*.P";
627 foreach $depfile (<${gpat}>)
629 if (! open (DEP_FILE, $depfile))
631 print STDERR "automake: couldn't open $depfile: $!\n";
635 $output_rules .= <DEP_FILE>;
643 # Handle subdirectories.
646 return if (! defined ($contents{'SUBDIRS'}));
648 $output_rules .= &file_contents ('subdirs');
650 push (@all, "all-recursive");
651 push (@check, "check-recursive");
652 push (@info, "info-recursive");
653 push (@dvi, "dvi-recursive");
655 $recursive_install = 1;
658 # Handle remaking and configure stuff.
661 if ($relative_dir ne '.')
664 $output_rules .= &file_contents ('remake-subd');
670 $output_vars .= "ACLOCAL = aclocal.m4\n";
671 push (@dist_common, 'aclocal.m4');
673 $output_rules .= &file_contents ('remake');
676 if (defined ($configure{'CONFIG_HEADER'})
677 && $contents{'CONFIG_HEADER'} !~ m,/,)
679 # Header defined and in this directory.
682 $output_vars .= "ACCONFIG = acconfig.h\n";
683 push (@dist_common, 'acconfig.h');
685 if (-f 'config.h.top')
687 $output_vars .= "CONFIG_TOP = config.h.top\n";
688 push (@dist_common, 'config.h.top');
690 if (-f 'config.h.bot')
692 $output_vars .= "CONFIG_BOT = config.h.bot\n";
693 push (@dist_common, 'config.h.bot');
696 push (@dist_common, 'stamp-h.in', $contents{'CONFIG_HEADER'} . '.in');
698 $output_rules .= &file_contents ('remake-hdr');
702 # Handle footer elements.
705 if ($contents{'SOURCES'})
707 $output_vars .= "SOURCES = " . $contents{'SOURCES'} . "\n";
709 if ($contents{'OBJECTS'})
711 $output_vars .= "OBJECTS = " . $contents{'OBJECTS'} . "\n";
713 if ($contents{'SOURCES'} || $contents{'OBJECTS'})
715 $output_vars .= "\n";
718 $output_trailer .= ".SUFFIXES:\n";
721 $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
723 $output_trailer .= "\n" . &file_contents ('footer');
726 # There are several targets which need to be merged. This is because
727 # their complete definition is compiled from many parts. Note that we
728 # avoid double colon rules, otherwise we'd use them instead.
729 sub handle_merge_targets
731 &do_one_merge_target ('all', @all);
732 &do_one_merge_target ('info', @info);
733 &do_one_merge_target ('dvi', @dvi);
734 &do_one_merge_target ('check', @check);
736 # Handle the various install targets specially. We do this so
737 # that (eg) "make install-exec" will run "install-exec-recursive"
738 # if required, but "make install" won't run it twice. Step one is
739 # to see if the user specified local versions of any of the
741 if (defined $contents{'install-exec-local'})
743 push (@install_exec, 'install-exec-local');
745 if (defined $contents{'install-data-local'})
747 push (@install_data, 'install-data-local');
749 if (defined $contents{'uninstall-local'})
751 push (@uninstall, 'uninstall-local');
754 if (defined $contents{'install-local'})
756 print STDERR "automake: use \`install-data' or \`install-exec', not \`install'\n";
759 # Step two: if we are doing recursive makes, write out the
762 if ($recursive_install)
764 push (@install, 'install-recursive');
765 push (@uninstall, 'uninstall-recursive');
767 if ($#install_exec >= 0)
769 $output_rules .= ('install-exec-am: '
770 . join (' ', @install_exec)
772 @install_exec = ('install-exec-recursive', 'install-exec-am');
773 push (@install, 'install-exec-am');
775 if ($#install_data >= 0)
777 $output_rules .= ('install-data-am: '
778 . join (' ', @install_data)
780 @install_data = ('install-data-recursive', 'install-data-am');
781 push (@install, 'install-data-am');
783 if ($#uninstall >= 0)
785 $output_rules .= ('uninstall-am: '
786 . join (' ', @uninstall)
788 @uninstall = ('uninstall-recursive', 'uninstall-am');
792 # Step three: print definitions users can use.
793 if ($#install_exec >= 0)
795 $output_rules .= ("install-exec: "
796 . join (' ', @install_exec)
798 push (@install, 'install-exec') if (!$recursive_install);
800 if ($#install_data >= 0)
802 $output_rules .= ("install-data: "
803 . join (' ', @install_data)
805 push (@install, 'install-data') if (!$recursive_install);
808 $output_rules .= ('install: '
809 . join (' ', @install)
812 . join (' ', @uninstall)
816 # Helper for handle_merge_targets.
817 sub do_one_merge_target
819 local ($name, @values) = @_;
821 if (defined $contents{$name . '-local'})
823 # User defined local form of target. So include it.
824 push (@values, $name . '-local');
827 $output_rules .= $name . ": " . join (' ', @values) . "\n\n";
830 ################################################################
832 # Read Makefile.am and set up %contents. Simultaneously copy lines
833 # from Makefile.am into $output_rules or $output_vars as appropriate.
834 # NOTE we put rules in the trailer section. We want user rules
835 # to come after our generated stuff.
838 local ($amfile) = @_;
840 if (! open (AMFILE, $amfile))
842 print STDERR "automake: couldn't open $amfile: $!\n";
847 local ($was_rule) = 0;
848 local ($last_var_name) = '';
858 $output_trailer .= $_ . "\n";
862 $output_vars .= $_ . "\n";
863 if (substr ($_, -1) eq "\\")
865 $contents{$last_var_name} .= substr ($_, 0,
870 $contents{$last_var_name} .= $_;
876 # Special case: this means we want automatic
877 # de-ANSI-fication. FIXME think of a better way.
878 $contents{'@kr@'} = 1;
880 elsif (m/^ *([a-zA-Z_.][a-zA-Z0-9_.]*) *:/)
884 # Value here doesn't matter; for targets we only note
887 $output_trailer .= $_ . "\n";
889 elsif (m/^ *([A-Za-z][A-Za-z0-9_]*)[ ]*=[ ]*(.*)$/)
891 # Found a variable reference.
894 if (substr ($2, -1) eq "\\")
896 $contents{$1} = substr ($2, 0, length ($2) - 1);
902 $output_vars .= $_ . "\n";
906 # Special rule: if looking at a blank line, append it to
907 # whatever we saw last.
910 $output_trailer .= "\n";
914 $output_vars .= "\n";
919 # This isn't an error; it is probably a continued rule.
920 # In fact, this is what we assume.
921 $output_trailer .= $_ . "\n";
924 $saw_bk = (substr ($_, -1) eq "\\");
927 # Include some space after user code.
928 $output_vars .= "\n";
929 $output_trailer .= "\n";
933 ################################################################
935 # Return contents of a file from $am_dir.
938 local ($basename) = @_;
939 local ($file) = $am_dir . '/' . $basename . '.am';
941 if (! open (FC_FILE, $file))
943 print STDERR "automake: installation error: cannot open \"$file\"\n";
947 # Yes, we really want to slurp it.
948 local ($results) = join ('', <FC_FILE>);
955 ################################################################
957 # Verify that the file must exist in the current directory.
961 local ($fullfile) = $relative_dir . "/" . $file;
965 print STDERR "automake: required file \"$fullfile\" not found\n";
970 push (@dist_common, $file);
975 ################################################################
977 # Return directory name of file.
983 ($sub = $file) =~ s,/+[^/]+,,g;
992 ################################################################
994 # Print usage information.
997 print "Usage: automake [OPTION] ... [Makefile]...\n";
999 print "\nFiles which are automatically distributed, if found:\n";
1000 $~ = "USAGE_FORMAT";
1001 local (@lcomm) = sort ((@common_files, @common_sometimes));
1003 while ($#lcomm >= 0)
1005 $one = shift (@lcomm);
1006 $two = shift (@lcomm);
1013 format USAGE_FORMAT =
1014 @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<