Add missing credit for Gaurav
[xapian.git] / xapian-core / preautoreconf
blobe7c4b408835a8cfcf54d378b424c35a9fd8fe4fd
1 #!/usr/bin/perl -w
2 # preautoconf - generate lists of sources for doxygen to extract docs from
4 # Copyright (C) 2005,2007,2013 Olly Betts
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 use strict;
22 my @makefile_am;
23 # version.h is generated by configure, and isn't listed in BUILT_SOURCES.
24 my %built_sources = qw(include/xapian/version.h 1);
26 if ($0 =~ m!(.*)/!) {
27 $1 eq '.' or chdir $1 or die $!;
30 my %s;
31 for (parse_makefile_am("")) {
32 $s{$_}++;
34 my @apidoc_src = ();
35 my @sourcedoc_src = ();
36 for (sort keys %s) {
37 s!^\./!!;
38 next if m!^tests/! || $_ eq 'include/xapian/errordispatch.h';
39 my $src = $built_sources{$_} ? '$(top_builddir)' : '$T';
40 $src .= "/$_";
41 if (m!^include/!) {
42 push @apidoc_src, $src;
43 } else {
44 push @sourcedoc_src, $src;
48 open MAKEFRAG, ">docs/docsource.tmp" or die $!;
49 print MAKEFRAG "APIDOC_SRC=", join("\\\n\t", "", @apidoc_src), "\n";
50 print MAKEFRAG "SOURCEDOC_SRC=", join("\\\n\t", "", @sourcedoc_src), "\n";
51 close MAKEFRAG or do { unlink "docs/docsource.tmp"; die $! };
52 rename "docs/docsource.tmp", "docs/docsource.mk" or do { unlink "docs/docsource.tmp"; die $! };
54 open MAKEFRAG, ">docsource.tmp" or die $!;
55 print MAKEFRAG "docsource.mk: preautoreconf\n";
56 print MAKEFRAG "\tcd \$(top_srcdir) && ./preautoreconf\n";
57 # Add dummy rules so that "make" works even if a Makefile.am is removed.
58 for (@makefile_am) {
59 print MAKEFRAG "\n$_:\n";
61 close MAKEFRAG or do { unlink "docsource.tmp"; die $! };
62 rename "docsource.tmp", "docsource.mk" or do { unlink "docsource.tmp"; die $! };
64 exit 0;
66 sub parse_makefile_am {
67 my $dir = shift;
68 my %v;
69 my @l;
70 my $makefile_am = $dir . "Makefile.am";
71 my @pending = ();
72 open M, "<$makefile_am" or die "$makefile_am: $!\n";
73 push @makefile_am, $makefile_am;
74 while (<M>) {
75 pending:
76 chomp;
77 while (s/\\$/ /) {
78 if (@pending) {
79 $_ .= shift @pending;
80 } else {
81 $_ .= <M>;
83 chomp;
85 if (/^\s*(\w+)\s*[+:]?=\s*(.*?)\s*(?:#.*)?$/) {
86 my $var = $1;
87 if (exists $v{$var}) {
88 $v{$var} .= " $2";
89 } else {
90 $v{$var} = $2;
91 if ($var =~ /_(?:SOURCE|HEADER)S$/) {
92 push @l, $var;
95 } elsif (/^\s*include\s+(\S+)/ && $1 ne 'docsource.mk') {
96 # automake looks for a nested included file starting from the
97 # directory which the original file was in, so the behaviour
98 # here is correct.
99 my $inc = $dir . $1;
100 open INC, "<$inc" or die "$inc: $!\n";
101 push @makefile_am, $inc;
102 unshift @pending, <INC>;
103 close INC;
105 if (@pending) {
106 $_ = shift @pending;
107 goto pending;
110 close M;
112 if (exists $v{BUILT_SOURCES}) {
113 for (map {/^$/ ? () : $dir.$_} split /\s+/,
114 expand('BUILT_SOURCES', \%v)) {
115 $built_sources{$_}++;
119 my @src;
120 for (@l) {
121 push @src, map {/^$/ ? () : $dir.$_} split /\s+/, expand($_, \%v);
124 my $subdirs = $v{DIST_SUBDIRS} || $v{SUBDIRS} || '';
125 for (split /\s+/, $subdirs) {
126 next if $_ eq ".";
127 push @src, parse_makefile_am("$dir$_/");
129 return @src;
132 sub expand {
133 my ($var, $from, $to, $vref);
134 if (scalar @_ == 2) {
135 ($var, $vref) = @_;
136 } else {
137 ($var, $from, $to, $vref) = @_;
140 my $val = $$vref{$var};
142 # Deal with $(FOO:.bar=.c) pattern substitutions.
143 if (defined $from) {
144 my $x = $val;
145 $val =~ s/\Q$from\E(?:\s|$)/$to/g;
148 if (!defined $val) {
149 print STDERR "$0: Unknown variable: \$($var)\n";
150 return "";
152 $val =~ s/\$\((\w+)(?::([^=)]+)=([^=)]+))?\)/expand($1, $2, $3, $vref)/eg;
153 return $val;