Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # Generates a header file with a named constant table made up of "name", value |
| 8 | # entries by including several build target header files and emitting the list |
| 9 | # of defines. Use of the preprocessor is needed to recursively include all |
| 10 | # relevant headers. |
| 11 | |
| 12 | set -e |
| 13 | |
| 14 | if [ $# -ne 1 ] && [ $# -ne 3]; then |
| 15 | echo "Usage: $(basename "$0") OUTFILE" |
| 16 | echo "Usage: $(basename "$0") CC CFLAGS OUTFILE" |
| 17 | exit 1 |
| 18 | fi |
| 19 | |
| 20 | if [ $# -eq 3 ]; then |
| 21 | CC="$1" |
| 22 | shift |
| 23 | CFLAGS="$1" |
| 24 | shift |
| 25 | fi |
| 26 | OUTFILE="$1" |
| 27 | |
| 28 | INCLUDES=' |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <linux/prctl.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <stddef.h> |
| 34 | #include <signal.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h>' |
| 37 | |
| 38 | # Passes the previous list of #includes to the C preprocessor and prints out |
| 39 | # all #defines whose name is all-caps. Excludes a few symbols that are known |
| 40 | # macro functions that don't evaluate to a constant. |
| 41 | cat <<-EOF > "${OUTFILE}" |
| 42 | /* GENERATED BY MAKEFILE */ |
| 43 | $INCLUDES |
| 44 | |
| 45 | #include "libconstants.h" |
| 46 | const struct constant_entry constant_table[] = { |
| 47 | $(echo "$INCLUDES" | \ |
| 48 | ${CC} ${CFLAGS} -dD - -E | \ |
| 49 | grep '^#define [A-Z][A-Z0-9_]* ' | \ |
| 50 | grep -v '\(SIGRTMAX\|SIGRTMIN\|SIG_\|NULL\)' | \ |
| 51 | sort | \ |
| 52 | uniq | \ |
| 53 | sed -e 's/#define \([A-Z0-9_]\+\).*$/#ifdef \1\n { "\1", \1 },\n#endif \/\/ \1/') |
| 54 | { NULL, 0 }, |
| 55 | }; |
| 56 | EOF |