blob: 8754a032534af7bf435a783694e61a4082f2cfac [file] [log] [blame]
Mike Frysinger761ed9b2014-02-16 01:59:20 -05001#!/bin/sh
2
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +00003usage()
4{
Mike Frysinger761ed9b2014-02-16 01:59:20 -05005 cat <<EOF
6Usage: $0 <input> <output>
7
8Generate xlat header files from <input> (a file or dir of files) and write
9the generated headers to <output>.
10EOF
11 exit 1
12}
13
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +000014cond_xlat()
15{
16 local line val m def xlat
17 line="$1"; shift
18
19 val="$(printf %s "${line}" | sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
20 m="${val%%|*}"
21 def="$(printf %s "${line}" |
22 sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
23
24 if [ "${m}" = "${m#1<<}" ]; then
25 xlat="XLAT(${val}),"
26 else
27 m="${m#1<<}"
28 xlat=" { ${val}, \"${m}\" },"
29 fi
30
31 if [ -z "${def}" ]; then
32 cat <<-EOF
33 #if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})
34 ${xlat}
35 #endif
36 EOF
37 else
38 cat <<-EOF
39 #if !(defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m}))
40 # define ${m} ${def}
41 #endif
42 ${xlat}
43 EOF
44 fi
45}
46
47gen_header()
48{
Mike Frysinger761ed9b2014-02-16 01:59:20 -050049 local input="$1" output="$2" name="$3"
Mike Frysinger761ed9b2014-02-16 01:59:20 -050050 echo "generating ${output}"
51 (
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000052 local defs="${0%/*}/../defs.h"
53 local prefix
54 if grep -x "extern const struct xlat ${name}\\[\\];" "${defs}" > /dev/null; then
55 prefix=
56 else
57 prefix='static '
58 fi
59
60 cat <<-EOF
61 /* Generated by $0 from $1; do not edit. */
62
63 ${prefix}const struct xlat ${name}[] = {
64 EOF
65 local unconditional= unterminated= line
66 while read line; do
67 LC_COLLATE=C
Mike Frysinger761ed9b2014-02-16 01:59:20 -050068 case ${line} in
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000069 '#unconditional')
70 unconditional=1
Mike Frysinger761ed9b2014-02-16 01:59:20 -050071 ;;
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000072 '#unterminated')
73 unterminated=1
74 ;;
75 [A-Z_]*) # symbolic constants
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +000076 if [ -n "${unconditional}" ]; then
77 echo " XLAT(${line}),"
78 else
79 cond_xlat "${line}"
80 fi
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000081 ;;
82 '1<<'[A-Z_]*) # symbolic constants with shift
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +000083 m="${line%% *}"
84 if [ -n "${unconditional}" ]; then
85 echo " { ${line}, \"${line#1<<}\" },"
86 else
87 cond_xlat "${line}"
88 fi
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000089 ;;
90 [0-9]*) # numeric constants
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +000091 echo " XLAT(${line}),"
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000092 ;;
93 *) # verbatim lines
94 echo "${line}"
Mike Frysinger761ed9b2014-02-16 01:59:20 -050095 ;;
96 esac
97 done < "${input}"
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +000098 if [ -n "${unterminated}" ]; then
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +000099 echo " /* this array should remain not NULL-terminated */"
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +0000100 else
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +0000101 echo " XLAT_END"
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +0000102 fi
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500103 echo "};"
104 ) >"${output}"
105}
106
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +0000107gen_make()
108{
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500109 local output="$1"
110 local name
111 shift
112 echo "generating ${output}"
113 (
114 printf "XLAT_INPUT_FILES = "
115 printf 'xlat/%s.in ' "$@"
116 echo
117 printf "XLAT_HEADER_FILES = "
118 printf 'xlat/%s.h ' "$@"
119 echo
120 for name; do
121 printf '$(top_srcdir)/xlat/%s.h: $(top_srcdir)/xlat/%s.in $(top_srcdir)/xlat/gen.sh\n' \
122 "${name}" "${name}"
123 echo ' $(AM_V_GEN)$(top_srcdir)/xlat/gen.sh $< $@'
124 done
125 ) >"${output}"
126}
127
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +0000128gen_git()
129{
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500130 local output="$1"
131 shift
132 echo "generating ${output}"
133 (
134 printf '/%s\n' .gitignore Makemodule.am
135 printf '/%s.h\n' "$@"
136 ) >"${output}"
137}
138
Dmitry V. Levincff9f8a2015-02-18 03:47:17 +0000139main()
140{
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500141 case $# in
142 0) set -- "${0%/*}" "${0%/*}" ;;
143 2) ;;
144 *) usage ;;
145 esac
146
147 local input="$1"
148 local output="$2"
149 local name
Mike Frysinger48ceaee2015-02-26 18:06:16 -0500150 local jobs=0
151 local ncpus="$(getconf _NPROCESSORS_ONLN)"
152 [ "${ncpus}" -ge 1 ] ||
153 ncpus=1
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500154
155 if [ -d "${input}" ]; then
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +0000156 local f names=
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500157 for f in "${input}"/*.in; do
Dmitry V. Levin5153b5c2014-04-25 23:04:13 +0000158 [ -f "${f}" ] || continue
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500159 name=${f##*/}
160 name=${name%.in}
161 gen_header "${f}" "${output}/${name}.h" "${name}" &
162 names="${names} ${name}"
Mike Frysinger48ceaee2015-02-26 18:06:16 -0500163 : $(( jobs += 1 ))
164 if [ ${jobs} -ge ${ncpus} ]; then
165 jobs=0
166 wait
167 fi
Mike Frysinger761ed9b2014-02-16 01:59:20 -0500168 done
169 gen_git "${output}/.gitignore" ${names}
170 gen_make "${output}/Makemodule.am" ${names}
171 wait
172 else
173 name=${input##*/}
174 name=${name%.in}
175 gen_header "${input}" "${output}" "${name}"
176 fi
177}
178
179main "$@"