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 | |
Shinichiro Hamaji | 286e289 | 2016-05-10 17:33:08 +0900 | [diff] [blame] | 36 | # Generate a dependency file which helps the build tool to see when it |
| 37 | # should regenerate ${OUTFILE}. |
Alex Deymo | 477f2e3 | 2016-05-12 18:35:49 -0700 | [diff] [blame] | 38 | echo "${INCLUDES}" | ${CC} - -E -M -MF "${OUTFILE}.d.tmp" |
Shinichiro Hamaji | 286e289 | 2016-05-10 17:33:08 +0900 | [diff] [blame] | 39 | # Correct the output filename. |
Alex Deymo | 477f2e3 | 2016-05-12 18:35:49 -0700 | [diff] [blame] | 40 | (echo "${OUTFILE}: \\" ; sed -e 's/^-\.o://' -e 's/^-://' "${OUTFILE}.d.tmp") \ |
| 41 | > "${OUTFILE}.d" |
| 42 | rm "${OUTFILE}.d.tmp" |
Shinichiro Hamaji | 286e289 | 2016-05-10 17:33:08 +0900 | [diff] [blame] | 43 | |
Scott James Remnant | 33df0e3 | 2015-10-12 15:14:06 -0700 | [diff] [blame] | 44 | # sed expression which extracts constants and converts them from: |
| 45 | # #define AT_FDWCD foo |
| 46 | # to: |
| 47 | # #ifdef AT_FDCWD |
| 48 | # { "AT_FDWCD", AT_FDCWD }, |
| 49 | # endif |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 50 | SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\ |
Jeff Vander Stoep | c38a20b | 2015-12-22 07:55:19 -0800 | [diff] [blame] | 51 | { "\1", (unsigned long) \1 },\ |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 52 | #endif // \1@' |
Scott James Remnant | 33df0e3 | 2015-10-12 15:14:06 -0700 | [diff] [blame] | 53 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 54 | # Passes the previous list of #includes to the C preprocessor and prints out |
| 55 | # all #defines whose name is all-caps. Excludes a few symbols that are known |
| 56 | # macro functions that don't evaluate to a constant. |
| 57 | cat <<-EOF > "${OUTFILE}" |
| 58 | /* GENERATED BY MAKEFILE */ |
| 59 | $INCLUDES |
| 60 | |
| 61 | #include "libconstants.h" |
| 62 | const struct constant_entry constant_table[] = { |
| 63 | $(echo "$INCLUDES" | \ |
Samuel Tan | f260bef | 2015-10-05 16:45:48 -0700 | [diff] [blame] | 64 | ${CC} -dD - -E | \ |
Jeff Vander Stoep | c38a20b | 2015-12-22 07:55:19 -0800 | [diff] [blame] | 65 | grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]]' | \ |
Mike Frysinger | 4044ed1 | 2015-10-16 13:53:04 -0400 | [diff] [blame] | 66 | grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \ |
Mike Frysinger | ab0af20 | 2015-10-16 13:53:51 -0400 | [diff] [blame] | 67 | sort -u | \ |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 68 | sed -Ee "${SED_MULTILINE}") |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 69 | { NULL, 0 }, |
| 70 | }; |
| 71 | EOF |