3 # Copyright (c) 2014-2015 Mike Frysinger <vapier@gentoo.org>
4 # Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
5 # Copyright (c) 2014-2018 The strace developers.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # 3. The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 Usage: $0 <input> <output>
35 Generate xlat header files from <input> (a file or dir of files) and write
36 the generated headers to <output>.
47 val="$(printf %s "$line" |
48 LC_ALL=C sed -r -n 's/^([[:alpha:]_][[:alnum:]_]*).*$/\1/p')"
51 def="$(printf %s "${line}" |
52 sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
54 if [ -n "$def" ]; then
56 #if defined($val) || (defined(HAVE_DECL_$val) && HAVE_DECL_$val)
57 DIAG_PUSH_IGNORE_TAUTOLOGICAL_COMPARE
58 static_assert(($val) == ($def), "$val != $def");
59 DIAG_POP_IGNORE_TAUTOLOGICAL_COMPARE
72 [ 1 = "$value_indexed" ] && printf " [%s] =" "${val}"
73 if [ -z "${val_type-}" ]; then
76 echo " XLAT_TYPE(${val_type}, ${val}),"
86 [ 1 = "$value_indexed" ] && printf " [%s] =" "${val}"
87 if [ -z "${val_type-}" ]; then
88 echo " XLAT_PAIR(${val}, \"${str}\"),"
90 echo " XLAT_TYPE_PAIR(${val_type}, ${val}, \"${str}\"),"
96 local line val m def xlat
99 val="$(printf %s "${line}" | sed -r -n 's/^([^[:space:]]+).*$/\1/p')"
101 def="$(printf %s "${line}" |
102 sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
104 if [ "${m}" = "${m#1<<}" ]; then
105 xlat="$(print_xlat "${val}")"
107 xlat="$(print_xlat_pair "1ULL<<${val#1<<}" "${val}")"
111 if [ -z "${def}" ]; then
113 #if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})
124 local input="$1" output="$2" name="$3"
125 echo "generating ${output}"
127 local defs="${0%/*}/../defs.h"
128 local mpers="${0%/*}/../mpers_xlat.h"
129 local decl="extern const struct xlat ${name}[];"
130 local in_defs= in_mpers=
134 if grep -F -x "$decl" "$defs" > /dev/null; then
136 elif grep -F -x "$decl" "$mpers" > /dev/null; then
141 /* Generated by $0 from $1; do not edit. */
143 #include "gcc_compat.h"
144 #include "static_assert.h"
148 local unconditional= line
149 # 1st pass: output directives.
152 line=$(printf "%s" "$line" | \
153 sed "s|[[:space:]]*/\*.*\*/[[:space:]]*||")
166 # to be processed during 2nd pass
175 [ -n "$unconditional" ] ||
183 #ifndef XLAT_MACROS_ONLY
187 if [ -n "$in_defs" ]; then
192 elif [ -n "$in_mpers" ]; then
200 # if !(defined HAVE_M32_MPERS || defined HAVE_MX32_MPERS)
208 # error static const struct xlat ${name} in mpers mode
216 echo "const struct xlat ${name}[] = {"
218 unconditional= val_type=
219 # 2nd pass: output everything.
222 line=$(printf "%s" "$line" | \
223 sed "s|[[:space:]]*/\*.*\*/[[:space:]]*||")
235 val_type="${line#\#val_type }"
237 [A-Z_]*) # symbolic constants
238 if [ -n "${unconditional}" ]; then
244 '1<<'[A-Z_]*) # symbolic constants with shift
245 if [ -n "${unconditional}" ]; then
246 print_xlat_pair "1ULL<<${line#1<<}" "${line}"
251 [0-9]*) # numeric constants
264 # endif /* !IN_MPERS */
266 #endif /* !XLAT_MACROS_ONLY */
276 echo "generating ${output}"
278 printf "XLAT_INPUT_FILES = "
279 printf 'xlat/%s.in ' "$@"
281 printf "XLAT_HEADER_FILES = "
282 printf 'xlat/%s.h ' "$@"
285 printf '$(top_srcdir)/xlat/%s.h: $(top_srcdir)/xlat/%s.in $(top_srcdir)/xlat/gen.sh\n' \
287 echo ' $(AM_V_GEN)$(top_srcdir)/xlat/gen.sh $< $@'
296 echo "generating ${output}"
298 printf '/%s\n' .gitignore Makemodule.am
299 printf '/%s.h\n' "$@"
306 0) set -- "${0%/*}" "${0%/*}" ;;
315 local ncpus="$(getconf _NPROCESSORS_ONLN)"
317 [ "${ncpus}" -ge 1 ] ||
320 if [ -d "${input}" ]; then
322 for f in "${input}"/*.in; do
323 [ -f "${f}" ] || continue
326 gen_header "${f}" "${output}/${name}.h" "${name}" &
328 names="${names} ${name}"
330 if [ "${jobs}" -gt "$(( ncpus * 2 ))" ]; then
333 wait -n 2>/dev/null || wait "$wait_pid"
339 gen_git "${output}/.gitignore" ${names} &
340 gen_make "${output}/Makemodule.am" ${names} &
345 gen_header "${input}" "${output}" "${name}"