[ci] Update macos jobs
[xapian.git] / xapian-core / preautoreconf
blob4721d7015df034eb80c7d839858d7ccf7af0a71d
1 #!/usr/bin/perl
2 # preautoconf - generate lists of sources for doxygen to extract docs from
4 # Copyright (C) 2005,2007,2013,2018 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;
21 use warnings;
23 my @makefile_am;
24 # version.h is generated by configure, and isn't listed in BUILT_SOURCES.
25 my %built_sources = qw(include/xapian/version.h 1);
27 if ($0 =~ m!(.*)/!) {
28 $1 eq '.' or chdir $1 or die $!;
31 my %s;
32 for (parse_makefile_am("")) {
33 $s{$_}++;
35 my @apidoc_src = ();
36 my @sourcedoc_src = ();
37 for (sort keys %s) {
38 s!^\./!!;
39 next if m!^tests/! || $_ eq 'include/xapian/errordispatch.h';
40 my $src = $built_sources{$_} ? '$(top_builddir)' : '$T';
41 $src .= "/$_";
42 if (m!^include/!) {
43 push @apidoc_src, $src;
44 } else {
45 push @sourcedoc_src, $src;
49 open MAKEFRAG, ">docs/docsource.tmp" or die $!;
50 print MAKEFRAG "APIDOC_SRC=", join("\\\n\t", "", @apidoc_src), "\n";
51 print MAKEFRAG "SOURCEDOC_SRC=", join("\\\n\t", "", @sourcedoc_src), "\n";
52 close MAKEFRAG or do { unlink "docs/docsource.tmp"; die $! };
53 rename "docs/docsource.tmp", "docs/docsource.mk" or do { unlink "docs/docsource.tmp"; die $! };
55 open MAKEFRAG, ">docsource.tmp" or die $!;
56 print MAKEFRAG "docsource.mk: preautoreconf\n";
57 print MAKEFRAG "\tcd \$(top_srcdir) && '$^X' ./preautoreconf\n";
58 # Add dummy rules so that "make" works even if a Makefile.am is removed.
59 for (@makefile_am) {
60 print MAKEFRAG "\n$_:\n";
62 close MAKEFRAG or do { unlink "docsource.tmp"; die $! };
63 rename "docsource.tmp", "docsource.mk" or do { unlink "docsource.tmp"; die $! };
65 exit 0;
67 sub parse_makefile_am {
68 my $dir = shift;
69 my %v;
70 my @l;
71 my $makefile_am = $dir . "Makefile.am";
72 my @pending = ();
73 open M, "<$makefile_am" or die "$makefile_am: $!\n";
74 push @makefile_am, $makefile_am;
75 while (<M>) {
76 pending:
77 chomp;
78 while (s/\\$/ /) {
79 if (@pending) {
80 $_ .= shift @pending;
81 } else {
82 $_ .= <M>;
84 chomp;
86 if (/^\s*(\w+)\s*[+:]?=\s*(.*?)\s*(?:#.*)?$/) {
87 my $var = $1;
88 if (exists $v{$var}) {
89 $v{$var} .= " $2";
90 } else {
91 $v{$var} = $2;
92 if ($var =~ /_(?:SOURCE|HEADER)S$/) {
93 push @l, $var;
96 } elsif (/^\s*include\s+(\S+)/ && $1 ne 'docsource.mk') {
97 # automake looks for a nested included file starting from the
98 # directory which the original file was in, so the behaviour
99 # here is correct.
100 my $inc = $dir . $1;
101 open INC, "<$inc" or die "$inc: $!\n";
102 push @makefile_am, $inc;
103 unshift @pending, <INC>;
104 close INC;
106 if (@pending) {
107 $_ = shift @pending;
108 goto pending;
111 close M;
113 if (exists $v{BUILT_SOURCES}) {
114 for (map {/^$/ ? () : $dir.$_} split /\s+/,
115 expand('BUILT_SOURCES', \%v)) {
116 $built_sources{$_}++;
120 my @src;
121 for (@l) {
122 push @src, map {/^$/ ? () : $dir.$_} split /\s+/, expand($_, \%v);
125 my $subdirs = $v{DIST_SUBDIRS} || $v{SUBDIRS} || '';
126 for (split /\s+/, $subdirs) {
127 next if $_ eq ".";
128 push @src, parse_makefile_am("$dir$_/");
130 return @src;
133 sub expand {
134 my ($var, $from, $to, $vref);
135 if (scalar @_ == 2) {
136 ($var, $vref) = @_;
137 } else {
138 ($var, $from, $to, $vref) = @_;
141 my $val = $$vref{$var};
143 # Deal with $(FOO:.bar=.c) pattern substitutions.
144 if (defined $from) {
145 my $x = $val;
146 $val =~ s/\Q$from\E(?:\s|$)/$to/g;
149 if (!defined $val) {
150 print STDERR "$0: Unknown variable: \$($var)\n";
151 return "";
153 $val =~ s/\$\((\w+)(?::([^=)]+)=([^=)]+))?\)/expand($1, $2, $3, $vref)/eg;
154 return $val;