blob: ed0956e0d8e13a045b26456a1919d99c2b77b3e1 [file] [log] [blame]
Luis Hector Chavez40b25742013-09-22 19:44:06 -07001#!/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
12set -e
13
Samuel Tanf260bef2015-10-05 16:45:48 -070014if [ $# -ne 1 ] && [ $# -ne 2 ]; then
Luis Hector Chavez40b25742013-09-22 19:44:06 -070015 echo "Usage: $(basename "$0") OUTFILE"
Dan Willemsen2acbec52017-09-14 17:28:36 -070016 echo "Usage: $(basename "$0") INFILE OUTFILE"
Luis Hector Chavez40b25742013-09-22 19:44:06 -070017 exit 1
18fi
19
Dan Willemsen2acbec52017-09-14 17:28:36 -070020BUILD="${CC} -dD gen_constants.c -E"
21GEN_DEPS=1
22
Samuel Tanf260bef2015-10-05 16:45:48 -070023if [ $# -eq 2 ]; then
Dan Willemsen2acbec52017-09-14 17:28:36 -070024 BUILD="cat $1"
25 GEN_DEPS=0
Luis Hector Chavez40b25742013-09-22 19:44:06 -070026 shift
Luis Hector Chavez40b25742013-09-22 19:44:06 -070027fi
28OUTFILE="$1"
29
Dan Willemsen2acbec52017-09-14 17:28:36 -070030if [ ${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"
34fi
Shinichiro Hamaji286e2892016-05-10 17:33:08 +090035
Scott James Remnant33df0e32015-10-12 15:14:06 -070036# 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 Frysingerb095d9e2015-10-16 14:05:14 -040042SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\
Jeff Vander Stoepc38a20b2015-12-22 07:55:19 -080043 { "\1", (unsigned long) \1 },\
Mike Frysingerb095d9e2015-10-16 14:05:14 -040044#endif // \1@'
Scott James Remnant33df0e32015-10-12 15:14:06 -070045
Luis Hector Chavez40b25742013-09-22 19:44:06 -070046# 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.
49cat <<-EOF > "${OUTFILE}"
50/* GENERATED BY MAKEFILE */
Dan Willemsen2acbec52017-09-14 17:28:36 -070051#include "gen_constants-inl.h"
Luis Hector Chavez40b25742013-09-22 19:44:06 -070052#include "libconstants.h"
53const struct constant_entry constant_table[] = {
Dan Willemsen2acbec52017-09-14 17:28:36 -070054$(${BUILD} | \
Luis Hector Chavez7ebd9412017-08-28 19:34:24 -070055 grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]_]' | \
Mike Frysinger4044ed12015-10-16 13:53:04 -040056 grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \
Mike Frysingerab0af202015-10-16 13:53:51 -040057 sort -u | \
Mike Frysingerb095d9e2015-10-16 14:05:14 -040058 sed -Ee "${SED_MULTILINE}")
Luis Hector Chavez40b25742013-09-22 19:44:06 -070059 { NULL, 0 },
60};
61EOF