2014-02-16 01:59:20 -05:00
#!/bin/sh
2015-02-18 03:47:17 +00:00
usage( )
{
2014-02-16 01:59:20 -05:00
cat <<EOF
Usage: $0 <input> <output>
Generate xlat header files from <input> ( a file or dir of files) and write
the generated headers to <output>.
EOF
exit 1
}
2015-02-18 03:47:17 +00:00
cond_xlat( )
{
local line val m def xlat
line = " $1 " ; shift
val = " $( printf %s " ${ line } " | sed -n 's/^\([^[:space:]]\+\).*$/\1/p' ) "
m = " ${ val %%|* } "
def = " $( printf %s " ${ line } " |
sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p' ) "
if [ " ${ m } " = " ${ m #1<< } " ] ; then
xlat = " XLAT( ${ val } ), "
else
m = " ${ m #1<< } "
xlat = " { ${ val } , \" ${ m } \" }, "
fi
if [ -z " ${ def } " ] ; then
cat <<-EOF
#if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})
${ xlat }
#endif
EOF
else
cat <<-EOF
#if !(defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m}))
# define ${m} ${def}
#endif
${ xlat }
EOF
fi
}
gen_header( )
{
2014-02-16 01:59:20 -05:00
local input = " $1 " output = " $2 " name = " $3 "
echo " generating ${ output } "
(
2014-04-25 23:04:13 +00:00
local defs = " ${ 0 %/* } /../defs.h "
local prefix
if grep -x " extern const struct xlat ${ name } \\[\\]; " " ${ defs } " > /dev/null; then
prefix =
else
prefix = 'static '
fi
cat <<-EOF
/* Generated by $0 from $1 ; do not edit. */
${ prefix } const struct xlat ${ name } [ ] = {
EOF
local unconditional = unterminated = line
while read line; do
LC_COLLATE = C
2014-02-16 01:59:20 -05:00
case ${ line } in
2014-04-25 23:04:13 +00:00
'#unconditional' )
unconditional = 1
; ;
'#unterminated' )
unterminated = 1
2014-02-16 01:59:20 -05:00
; ;
2014-04-25 23:04:13 +00:00
[ A-Z_] *) # symbolic constants
2015-02-18 03:47:17 +00:00
if [ -n " ${ unconditional } " ] ; then
echo " XLAT( ${ line } ), "
else
cond_xlat " ${ line } "
fi
2014-04-25 23:04:13 +00:00
; ;
'1<<' [ A-Z_] *) # symbolic constants with shift
2015-02-18 03:47:17 +00:00
m = " ${ line %% * } "
if [ -n " ${ unconditional } " ] ; then
echo " { ${ line } , \" ${ line #1<< } \" }, "
else
cond_xlat " ${ line } "
fi
2014-04-25 23:04:13 +00:00
; ;
[ 0-9] *) # numeric constants
2015-02-18 03:47:17 +00:00
echo " XLAT( ${ line } ), "
2014-04-25 23:04:13 +00:00
; ;
*) # verbatim lines
echo " ${ line } "
2014-02-16 01:59:20 -05:00
; ;
esac
done < " ${ input } "
2014-04-25 23:04:13 +00:00
if [ -n " ${ unterminated } " ] ; then
2015-02-18 03:47:17 +00:00
echo " /* this array should remain not NULL-terminated */"
2014-04-25 23:04:13 +00:00
else
2015-02-18 03:47:17 +00:00
echo " XLAT_END"
2014-04-25 23:04:13 +00:00
fi
2014-02-16 01:59:20 -05:00
echo "};"
) >" ${ output } "
}
2015-02-18 03:47:17 +00:00
gen_make( )
{
2014-02-16 01:59:20 -05:00
local output = " $1 "
local name
shift
echo " generating ${ output } "
(
printf "XLAT_INPUT_FILES = "
printf 'xlat/%s.in ' " $@ "
echo
printf "XLAT_HEADER_FILES = "
printf 'xlat/%s.h ' " $@ "
echo
for name; do
printf '$(top_srcdir)/xlat/%s.h: $(top_srcdir)/xlat/%s.in $(top_srcdir)/xlat/gen.sh\n' \
" ${ name } " " ${ name } "
echo ' $(AM_V_GEN)$(top_srcdir)/xlat/gen.sh $< $@'
done
) >" ${ output } "
}
2015-02-18 03:47:17 +00:00
gen_git( )
{
2014-02-16 01:59:20 -05:00
local output = " $1 "
shift
echo " generating ${ output } "
(
printf '/%s\n' .gitignore Makemodule.am
printf '/%s.h\n' " $@ "
) >" ${ output } "
}
2015-02-18 03:47:17 +00:00
main( )
{
2014-02-16 01:59:20 -05:00
case $# in
0) set -- " ${ 0 %/* } " " ${ 0 %/* } " ; ;
2) ; ;
*) usage ; ;
esac
local input = " $1 "
local output = " $2 "
local name
2015-02-26 18:06:16 -05:00
local jobs = 0
local ncpus = " $( getconf _NPROCESSORS_ONLN) "
[ " ${ ncpus } " -ge 1 ] ||
ncpus = 1
2014-02-16 01:59:20 -05:00
if [ -d " ${ input } " ] ; then
2014-04-25 23:04:13 +00:00
local f names =
2014-02-16 01:59:20 -05:00
for f in " ${ input } " /*.in; do
2014-04-25 23:04:13 +00:00
[ -f " ${ f } " ] || continue
2014-02-16 01:59:20 -05:00
name = ${ f ##*/ }
name = ${ name %.in }
gen_header " ${ f } " " ${ output } / ${ name } .h " " ${ name } " &
names = " ${ names } ${ name } "
2015-02-26 18:06:16 -05:00
: $(( jobs + = 1 ))
if [ ${ jobs } -ge ${ ncpus } ] ; then
jobs = 0
wait
fi
2014-02-16 01:59:20 -05:00
done
gen_git " ${ output } /.gitignore " ${ names }
gen_make " ${ output } /Makemodule.am " ${ names }
wait
else
name = ${ input ##*/ }
name = ${ name %.in }
gen_header " ${ input } " " ${ output } " " ${ name } "
fi
}
main " $@ "