3 # Copyright (c) 2014-2015 Mike Frysinger <vapier@gentoo.org>
4 # Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # 3. The name of the author may not be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 Usage: $0 <input> <output>
34 Generate xlat header files from <input> (a file or dir of files) and write
35 the generated headers to <output>.
46 val="$(printf %s "$line" |
47 sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
50 def="$(printf %s "${line}" |
51 sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
53 if [ -n "$def" ]; then
55 #if !(defined($val) || (defined(HAVE_DECL_$val) && HAVE_DECL_$val))
67 if [ -z "${val_type-}" ]; then
70 echo " XLAT_TYPE(${val_type}, ${val}),"
80 if [ -z "${val_type-}" ]; then
81 echo " XLAT_PAIR(${val}, \"${str}\"),"
83 echo " XLAT_TYPE_PAIR(${val_type}, ${val}, \"${str}\"),"
89 local line val m def xlat
92 val="$(printf %s "${line}" | sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
94 def="$(printf %s "${line}" |
95 sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
97 if [ "${m}" = "${m#1<<}" ]; then
98 xlat="$(print_xlat "${val}")"
100 xlat="$(print_xlat_pair "1ULL<<${val#1<<}" "${val}")"
104 if [ -z "${def}" ]; then
106 #if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})
117 local input="$1" output="$2" name="$3"
118 echo "generating ${output}"
120 local defs="${0%/*}/../defs.h"
121 local mpers="${0%/*}/../mpers_xlat.h"
122 local decl="extern const struct xlat ${name}[];"
123 local in_defs= in_mpers=
125 if grep -F -x "$decl" "$defs" > /dev/null; then
127 elif grep -F -x "$decl" "$mpers" > /dev/null; then
131 echo "/* Generated by $0 from $1; do not edit. */"
133 local unconditional= unterminated= line
134 # 1st pass: output directives.
148 # to be processed during 2nd pass
154 [ -n "$unconditional" ] ||
161 if [ -n "$in_defs" ]; then
166 elif [ -n "$in_mpers" ]; then
174 # if !(defined HAVE_M32_MPERS || defined HAVE_MX32_MPERS)
182 # error static const struct xlat ${name} in mpers mode
189 echo "const struct xlat ${name}[] = {"
191 unconditional= val_type=
192 # 2nd pass: output everything.
203 # processed during 1st pass
206 val_type="${line#\#val_type }"
208 [A-Z_]*) # symbolic constants
209 if [ -n "${unconditional}" ]; then
215 '1<<'[A-Z_]*) # symbolic constants with shift
216 if [ -n "${unconditional}" ]; then
217 print_xlat_pair "1ULL<<${line#1<<}" "${line}"
222 [0-9]*) # numeric constants
230 if [ -n "${unterminated}" ]; then
231 echo " /* this array should remain not NULL-terminated */"
239 #endif /* !IN_MPERS */
249 echo "generating ${output}"
251 printf "XLAT_INPUT_FILES = "
252 printf 'xlat/%s.in ' "$@"
254 printf "XLAT_HEADER_FILES = "
255 printf 'xlat/%s.h ' "$@"
258 printf '$(top_srcdir)/xlat/%s.h: $(top_srcdir)/xlat/%s.in $(top_srcdir)/xlat/gen.sh\n' \
260 echo ' $(AM_V_GEN)$(top_srcdir)/xlat/gen.sh $< $@'
269 echo "generating ${output}"
271 printf '/%s\n' .gitignore Makemodule.am
272 printf '/%s.h\n' "$@"
279 0) set -- "${0%/*}" "${0%/*}" ;;
288 local ncpus="$(getconf _NPROCESSORS_ONLN)"
289 [ "${ncpus}" -ge 1 ] ||
292 if [ -d "${input}" ]; then
294 for f in "${input}"/*.in; do
295 [ -f "${f}" ] || continue
298 gen_header "${f}" "${output}/${name}.h" "${name}" &
299 names="${names} ${name}"
301 if [ ${jobs} -ge ${ncpus} ]; then
306 gen_git "${output}/.gitignore" ${names}
307 gen_make "${output}/Makemodule.am" ${names}
312 gen_header "${input}" "${output}" "${name}"