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" |
Dan Willemsen | 2acbec5 | 2017-09-14 17:28:36 -0700 | [diff] [blame] | 16 | echo "Usage: $(basename "$0") INFILE OUTFILE" |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 17 | exit 1 |
| 18 | fi |
| 19 | |
Mike Frysinger | dd5a884 | 2018-08-15 15:57:44 -0400 | [diff] [blame^] | 20 | BUILD="${CC} -dD ${SRC:-.}/gen_constants.c -E" |
Dan Willemsen | 2acbec5 | 2017-09-14 17:28:36 -0700 | [diff] [blame] | 21 | GEN_DEPS=1 |
| 22 | |
Samuel Tan | f260bef | 2015-10-05 16:45:48 -0700 | [diff] [blame] | 23 | if [ $# -eq 2 ]; then |
Dan Willemsen | 2acbec5 | 2017-09-14 17:28:36 -0700 | [diff] [blame] | 24 | BUILD="cat $1" |
| 25 | GEN_DEPS=0 |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 26 | shift |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 27 | fi |
| 28 | OUTFILE="$1" |
| 29 | |
Dan Willemsen | 2acbec5 | 2017-09-14 17:28:36 -0700 | [diff] [blame] | 30 | if [ ${GEN_DEPS} -eq 1 ]; then |
| 31 | # Generate a dependency file which helps the build tool to see when it |
| 32 | # should regenerate ${OUTFILE}. |
| 33 | ${BUILD} -M -MF "${OUTFILE}.d" |
| 34 | fi |
Shinichiro Hamaji | 286e289 | 2016-05-10 17:33:08 +0900 | [diff] [blame] | 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\ |
Jeff Vander Stoep | c38a20b | 2015-12-22 07:55:19 -0800 | [diff] [blame] | 43 | { "\1", (unsigned long) \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 */ |
Dan Willemsen | 2acbec5 | 2017-09-14 17:28:36 -0700 | [diff] [blame] | 51 | #include "gen_constants-inl.h" |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 52 | #include "libconstants.h" |
| 53 | const struct constant_entry constant_table[] = { |
Dan Willemsen | 2acbec5 | 2017-09-14 17:28:36 -0700 | [diff] [blame] | 54 | $(${BUILD} | \ |
Luis Hector Chavez | 7ebd941 | 2017-08-28 19:34:24 -0700 | [diff] [blame] | 55 | grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]_]' | \ |
Mike Frysinger | 4044ed1 | 2015-10-16 13:53:04 -0400 | [diff] [blame] | 56 | grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \ |
Mike Frysinger | ab0af20 | 2015-10-16 13:53:51 -0400 | [diff] [blame] | 57 | sort -u | \ |
Mike Frysinger | b095d9e | 2015-10-16 14:05:14 -0400 | [diff] [blame] | 58 | sed -Ee "${SED_MULTILINE}") |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 59 | { NULL, 0 }, |
| 60 | }; |
| 61 | EOF |