summaryrefslogtreecommitdiff
path: root/util/autoconf/autoheader.sh
blob: 354d4d09d90f2392ccd710cd88f0be6161212079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/sh
# autoheader -- create `config.h.in' from `configure.in'
# Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Written by Roland McGrath.

# If given no args, create `config.h.in' from template file `configure.in'.
# With one arg, create a header file on standard output from
# the given template file.

usage="\
Usage: autoheader [-h] [--help] [-m dir] [--macrodir=dir] 
       [-l dir] [--localdir=dir] [--version] [template-file]" 

# NLS nuisances.
# Only set `LANG' and `LC_ALL' to "C" if already set.
# These must not be set unconditionally because not all systems understand
# e.g. LANG=C (notably SCO).
if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
if test "${LANG+set}"   = set; then LANG=C;   export LANG;   fi

test -z "${AC_MACRODIR}" && AC_MACRODIR=@datadir@
test -z "${M4}" && M4=@M4@
case "${M4}" in
/*) # Handle the case that m4 has moved since we were configured.
    # It may have been found originally in a build directory.
    test -f "${M4}" || M4=m4 ;;
esac

localdir=.
show_version=no

while test $# -gt 0 ; do
   case "${1}" in 
      -h | --help | --h* )
         echo "${usage}"; exit 0 ;;
      --localdir=* | --l*=* )
         localdir="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
         shift ;;
      -l | --localdir | --l*)
         shift
         test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
         localdir="${1}"
         shift ;;
      --macrodir=* | --m*=* )
         AC_MACRODIR="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
         shift ;;
      -m | --macrodir | --m* ) 
         shift
         test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
         AC_MACRODIR="${1}"
         shift ;;
      --version | --v* )
         show_version=yes; shift ;;
      -- )     # Stop option processing
        shift; break ;;
      - )	# Use stdin as input.
        break ;;
      -* )
        echo "${usage}" 1>&2; exit 1 ;;
      * )
        break ;;
   esac
done

if test $show_version = yes; then
  version=`sed -n 's/define.AC_ACVERSION.[ 	]*\([0-9.]*\).*/\1/p' \
    $AC_MACRODIR/acgeneral.m4`
  echo "Autoconf version $version"
  exit 0
fi

TEMPLATES="${AC_MACRODIR}/acconfig.h"
test -r $localdir/acconfig.h && TEMPLATES="${TEMPLATES} $localdir/acconfig.h"

case $# in
  0) infile=configure.in ;;
  1) infile=$1 ;;
  *) echo "$usage" >&2; exit 1 ;;
esac

config_h=config.h
syms=
types=
funcs=
headers=
libs=

if test "$localdir" != .; then
  use_localdir="-I$localdir -DAC_LOCALDIR=$localdir"
else
  use_localdir=
fi

# Use the frozen version of Autoconf if available.
r= f=
# Some non-GNU m4's don't reject the --help option, so give them /dev/null.
case `$M4 --help < /dev/null 2>&1` in
*reload-state*) test -r $AC_MACRODIR/autoheader.m4f && { r=--reload f=f; } ;;
*traditional*) ;;
*) echo Autoconf requires GNU m4 1.1 or later >&2; exit 1 ;;
esac

# Extract assignments of SYMS, TYPES, FUNCS, HEADERS, and LIBS from the
# modified autoconf processing of the input file.  The sed hair is
# necessary to win for multi-line macro invocations.
eval "`$M4 -I$AC_MACRODIR $use_localdir $r autoheader.m4$f $infile |
       sed -n -e '
		: again
		/^@@@.*@@@$/s/^@@@\(.*\)@@@$/\1/p
		/^@@@/{
			s/^@@@//p
			n
			s/^/@@@/
			b again
		}'`"

# Make SYMS newline-separated rather than blank-separated, and remove dups.
# Start each symbol with a blank (to match the blank after "#undef")
# to reduce the possibility of mistakenly matching another symbol that
# is a substring of it.
syms="`for sym in $syms; do echo $sym; done | sort | uniq | sed 's@^@ @'`"

if test $# -eq 0; then
  tmpout=autoh$$
  trap "rm -f $tmpout; exit 1" 1 2 15
  exec > $tmpout
fi

# Support "outfile[:infile]", defaulting infile="outfile.in".
case "$config_h" in
*:*) config_h_in=`echo "$config_h"|sed 's%.*:%%'`
     config_h=`echo "$config_h"|sed 's%:.*%%'` ;;
*) config_h_in="${config_h}.in" ;;
esac

# Don't write "do not edit" -- it will get copied into the
# config.h, which it's ok to edit.
echo "/* ${config_h_in}.  Generated automatically from $infile by autoheader.  */"

test -r ${config_h}.top && cat ${config_h}.top
test -r $localdir/acconfig.h &&
  grep @TOP@ $localdir/acconfig.h >/dev/null &&
  sed '/@TOP@/,$d' $localdir/acconfig.h

# This puts each template paragraph on its own line, separated by @s.
if test -n "$syms"; then
   # Make sure the boundary of template files is also the boundary
   # of the paragraph.  Extra newlines don't hurt since they will
   # be removed.
   for t in $TEMPLATES; do cat $t; echo; echo; done |
   # The sed script is suboptimal because it has to take care of
   # some broken seds (e.g. AIX) that remove '\n' from the
   # pattern/hold space if the line is empty. (junio@twinsun.com).
   sed -n -e '
	/^[ 	]*$/{
		x
		s/\n/@/g
		p
		s/.*/@/
		x
	}
	H' | sed -e 's/@@*/@/g' |
   # Select each paragraph that refers to a symbol we picked out above.
   fgrep "$syms" |
   tr @ \\012
fi

echo "$types" | tr , \\012 | sort | uniq | while read ctype; do
  test -z "$ctype" && continue
  # Solaris 2.3 tr rejects noncontiguous characters in character classes.
  sym="`echo "${ctype}" | tr '[a-z] *' '[A-Z]_P'`"
  echo "
/* The number of bytes in a ${ctype}.  */
#undef SIZEOF_${sym}"
done

# /bin/sh on the Alpha gives `for' a random value if $funcs is empty.
if test -n "$funcs"; then
  for func in `for x in $funcs; do echo $x; done | sort | uniq`; do
    sym="`echo ${func} | sed 's/[^a-zA-Z0-9_]/_/g' | tr '[a-z]' '[A-Z]'`"
    echo "
/* Define if you have the ${func} function.  */
#undef HAVE_${sym}"
  done
fi

if test -n "$headers"; then
  for header in `for x in $headers; do echo $x; done | sort | uniq`; do
    sym="`echo ${header} | sed 's/[^a-zA-Z0-9_]/_/g' | tr '[a-z]' '[A-Z]'`"
    echo "
/* Define if you have the <${header}> header file.  */
#undef HAVE_${sym}"
  done
fi

if test -n "$libs"; then
  for lib in `for x in $libs; do echo $x; done | sort | uniq`; do
   sym="`echo ${lib} | sed 's/[^a-zA-Z0-9_]/_/g' | tr '[a-z]' '[A-Z]'`"
    echo "
/* Define if you have the ${lib} library (-l${lib}).  */
#undef HAVE_LIB${sym}"
  done
fi

test -r $localdir/acconfig.h &&
  grep @BOTTOM@ $localdir/acconfig.h >/dev/null &&
  sed '1,/@BOTTOM@/d' $localdir/acconfig.h
test -f ${config_h}.bot && cat ${config_h}.bot

status=0

if test -n "$syms"; then
  for sym in $syms; do
    if fgrep $sym $TEMPLATES >/dev/null; then
      : # All is well.
    else
      echo "$0: Symbol \`${sym}' is not covered by $TEMPLATES" >&2
      status=1
    fi
  done
fi

if test $# -eq 0; then
  if test $status -eq 0; then
    if cmp -s $tmpout ${config_h_in}; then
      rm -f $tmpout
    else
      mv -f $tmpout ${config_h_in}
    fi
  else
    rm -f $tmpout
  fi
fi

exit $status