Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 3 | usage() |
| 4 | { |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 5 | cat <<EOF |
| 6 | Usage: $0 <input> <output> |
| 7 | |
| 8 | Generate xlat header files from <input> (a file or dir of files) and write |
| 9 | the generated headers to <output>. |
| 10 | EOF |
| 11 | exit 1 |
| 12 | } |
| 13 | |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 14 | cond_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 | |
| 47 | gen_header() |
| 48 | { |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 49 | local input="$1" output="$2" name="$3" |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 50 | echo "generating ${output}" |
| 51 | ( |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 52 | 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 Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 68 | case ${line} in |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 69 | '#unconditional') |
| 70 | unconditional=1 |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 71 | ;; |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 72 | '#unterminated') |
| 73 | unterminated=1 |
| 74 | ;; |
| 75 | [A-Z_]*) # symbolic constants |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 76 | if [ -n "${unconditional}" ]; then |
| 77 | echo " XLAT(${line})," |
| 78 | else |
| 79 | cond_xlat "${line}" |
| 80 | fi |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 81 | ;; |
| 82 | '1<<'[A-Z_]*) # symbolic constants with shift |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 83 | m="${line%% *}" |
| 84 | if [ -n "${unconditional}" ]; then |
| 85 | echo " { ${line}, \"${line#1<<}\" }," |
| 86 | else |
| 87 | cond_xlat "${line}" |
| 88 | fi |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 89 | ;; |
| 90 | [0-9]*) # numeric constants |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 91 | echo " XLAT(${line})," |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 92 | ;; |
| 93 | *) # verbatim lines |
| 94 | echo "${line}" |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 95 | ;; |
| 96 | esac |
| 97 | done < "${input}" |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 98 | if [ -n "${unterminated}" ]; then |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 99 | echo " /* this array should remain not NULL-terminated */" |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 100 | else |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 101 | echo " XLAT_END" |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 102 | fi |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 103 | echo "};" |
| 104 | ) >"${output}" |
| 105 | } |
| 106 | |
Dmitry V. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 107 | gen_make() |
| 108 | { |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 109 | 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. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 128 | gen_git() |
| 129 | { |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 130 | 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. Levin | cff9f8a | 2015-02-18 03:47:17 +0000 | [diff] [blame] | 139 | main() |
| 140 | { |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 141 | 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 Frysinger | 48ceaee | 2015-02-26 18:06:16 -0500 | [diff] [blame] | 150 | local jobs=0 |
| 151 | local ncpus="$(getconf _NPROCESSORS_ONLN)" |
| 152 | [ "${ncpus}" -ge 1 ] || |
| 153 | ncpus=1 |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 154 | |
| 155 | if [ -d "${input}" ]; then |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 156 | local f names= |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 157 | for f in "${input}"/*.in; do |
Dmitry V. Levin | 5153b5c | 2014-04-25 23:04:13 +0000 | [diff] [blame] | 158 | [ -f "${f}" ] || continue |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 159 | name=${f##*/} |
| 160 | name=${name%.in} |
| 161 | gen_header "${f}" "${output}/${name}.h" "${name}" & |
| 162 | names="${names} ${name}" |
Mike Frysinger | 48ceaee | 2015-02-26 18:06:16 -0500 | [diff] [blame] | 163 | : $(( jobs += 1 )) |
| 164 | if [ ${jobs} -ge ${ncpus} ]; then |
| 165 | jobs=0 |
| 166 | wait |
| 167 | fi |
Mike Frysinger | 761ed9b | 2014-02-16 01:59:20 -0500 | [diff] [blame] | 168 | 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 | |
| 179 | main "$@" |