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 | |
Samuel Tan | f260bef | 2015-10-05 16:45:48 -0700 | [diff] [blame] | 14 | if [ $# -ne 1 ] && [ $# -ne 2 ]; then |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 15 | echo "Usage: $(basename "$0") OUTFILE" |
Samuel Tan | f260bef | 2015-10-05 16:45:48 -0700 | [diff] [blame] | 16 | echo "Usage: $(basename "$0") CC OUTFILE" |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 17 | exit 1 |
| 18 | fi |
| 19 | |
Samuel Tan | f260bef | 2015-10-05 16:45:48 -0700 | [diff] [blame] | 20 | if [ $# -eq 2 ]; then |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 21 | CC="$1" |
| 22 | shift |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 23 | fi |
| 24 | OUTFILE="$1" |
| 25 | |
| 26 | INCLUDES=' |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <linux/prctl.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <stddef.h> |
| 32 | #include <signal.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h>' |
| 35 | |
Scott James Remnant | 33df0e3 | 2015-10-12 15:14:06 -0700 | [diff] [blame] | 36 | # sed expression which extracts constants and converts them from: |
| 37 | # #define AT_FDWCD foo |
| 38 | # to: |
| 39 | # #ifdef AT_FDCWD |
| 40 | # { "AT_FDWCD", AT_FDCWD }, |
| 41 | # endif |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 42 | SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\ |
Scott James Remnant | 33df0e3 | 2015-10-12 15:14:06 -0700 | [diff] [blame] | 43 | { "\1", \1 },\ |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 44 | #endif // \1@' |
Scott James Remnant | 33df0e3 | 2015-10-12 15:14:06 -0700 | [diff] [blame] | 45 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 46 | # Passes the previous list of #includes to the C preprocessor and prints out |
| 47 | # all #defines whose name is all-caps. Excludes a few symbols that are known |
| 48 | # macro functions that don't evaluate to a constant. |
| 49 | cat <<-EOF > "${OUTFILE}" |
| 50 | /* GENERATED BY MAKEFILE */ |
| 51 | $INCLUDES |
| 52 | |
| 53 | #include "libconstants.h" |
| 54 | const struct constant_entry constant_table[] = { |
| 55 | $(echo "$INCLUDES" | \ |
Samuel Tan | f260bef | 2015-10-05 16:45:48 -0700 | [diff] [blame] | 56 | ${CC} -dD - -E | \ |
Mike Frysinger | b962e7e | 2015-10-16 13:50:31 -0400 | [diff] [blame] | 57 | grep '^#define [[:upper:]][[:upper:]0-9_]* ' | \ |
Mike Frysinger | 4044ed1 | 2015-10-16 13:53:04 -0400 | [diff] [blame] | 58 | grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \ |
Mike Frysinger | ab0af20 | 2015-10-16 13:53:51 -0400 | [diff] [blame] | 59 | sort -u | \ |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 60 | sed -Ee "${SED_MULTILINE}") |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 61 | { NULL, 0 }, |
| 62 | }; |
| 63 | EOF |