Home | History | Annotate | Download | only in xlat
      1 #!/bin/sh
      2 
      3 usage() {
      4 	cat <<EOF
      5 Usage: $0 <input> <output>
      6 
      7 Generate xlat header files from <input> (a file or dir of files) and write
      8 the generated headers to <output>.
      9 EOF
     10 	exit 1
     11 }
     12 
     13 gen_header() {
     14 	local input="$1" output="$2" name="$3"
     15 	echo "generating ${output}"
     16 	(
     17 	local defs="${0%/*}/../defs.h"
     18 	local prefix
     19 	if grep -x "extern const struct xlat ${name}\\[\\];" "${defs}" > /dev/null; then
     20 		prefix=
     21 	else
     22 		prefix='static '
     23 	fi
     24 
     25 	cat <<-EOF
     26 		/* Generated by $0 from $1; do not edit. */
     27 
     28 		${prefix}const struct xlat ${name}[] = {
     29 	EOF
     30 	local unconditional= unterminated= line
     31 	while read line; do
     32 		LC_COLLATE=C
     33 		case ${line} in
     34 		'#unconditional')
     35 			unconditional=1
     36 			;;
     37 		'#unterminated')
     38 			unterminated=1
     39 			;;
     40 		[A-Z_]*)	# symbolic constants
     41 			local m="${line%%|*}"
     42 			[ -n "${unconditional}" ] ||
     43 				echo "#if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})"
     44 			echo "	XLAT(${line}),"
     45 			[ -n "${unconditional}" ] ||
     46 				echo "#endif"
     47 			;;
     48 		'1<<'[A-Z_]*)	# symbolic constants with shift
     49 			local m="${line#1<<}"
     50 			[ -n "${unconditional}" ] ||
     51 				echo "#if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})"
     52 			echo "	{ ${line}, \"${m}\" },"
     53 			[ -n "${unconditional}" ] ||
     54 				echo "#endif"
     55 			;;
     56 		[0-9]*)	# numeric constants
     57 			echo "	XLAT(${line}),"
     58 			;;
     59 		*)	# verbatim lines
     60 			echo "${line}"
     61 			;;
     62 		esac
     63 	done < "${input}"
     64 	if [ -n "${unterminated}" ]; then
     65 		echo "  /* this array should remain not NULL-terminated */"
     66 	else
     67 		echo "	XLAT_END"
     68 	fi
     69 	echo "};"
     70 	) >"${output}"
     71 }
     72 
     73 gen_make() {
     74 	local output="$1"
     75 	local name
     76 	shift
     77 	echo "generating ${output}"
     78 	(
     79 		printf "XLAT_INPUT_FILES = "
     80 		printf 'xlat/%s.in ' "$@"
     81 		echo
     82 		printf "XLAT_HEADER_FILES = "
     83 		printf 'xlat/%s.h ' "$@"
     84 		echo
     85 		for name; do
     86 			printf '$(top_srcdir)/xlat/%s.h: $(top_srcdir)/xlat/%s.in $(top_srcdir)/xlat/gen.sh\n' \
     87 				"${name}" "${name}"
     88 			echo '	$(AM_V_GEN)$(top_srcdir)/xlat/gen.sh $< $@'
     89 		done
     90 	) >"${output}"
     91 }
     92 
     93 gen_git() {
     94 	local output="$1"
     95 	shift
     96 	echo "generating ${output}"
     97 	(
     98 		printf '/%s\n' .gitignore Makemodule.am
     99 		printf '/%s.h\n' "$@"
    100 	) >"${output}"
    101 }
    102 
    103 main() {
    104 	case $# in
    105 	0) set -- "${0%/*}" "${0%/*}" ;;
    106 	2) ;;
    107 	*) usage ;;
    108 	esac
    109 
    110 	local input="$1"
    111 	local output="$2"
    112 	local name
    113 
    114 	if [ -d "${input}" ]; then
    115 		local f names=
    116 		for f in "${input}"/*.in; do
    117 			[ -f "${f}" ] || continue
    118 			name=${f##*/}
    119 			name=${name%.in}
    120 			gen_header "${f}" "${output}/${name}.h" "${name}" &
    121 			names="${names} ${name}"
    122 		done
    123 		gen_git "${output}/.gitignore" ${names}
    124 		gen_make "${output}/Makemodule.am" ${names}
    125 		wait
    126 	else
    127 		name=${input##*/}
    128 		name=${name%.in}
    129 		gen_header "${input}" "${output}" "${name}"
    130 	fi
    131 }
    132 
    133 main "$@"
    134