blob: 0f330b9a0873a63df15e73ae1569f96fbfea40bc [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"
Samuel Tanf260bef2015-10-05 16:45:48 -070016 echo "Usage: $(basename "$0") CC OUTFILE"
Luis Hector Chavez40b25742013-09-22 19:44:06 -070017 exit 1
18fi
19
Samuel Tanf260bef2015-10-05 16:45:48 -070020if [ $# -eq 2 ]; then
Luis Hector Chavez40b25742013-09-22 19:44:06 -070021 CC="$1"
22 shift
Luis Hector Chavez40b25742013-09-22 19:44:06 -070023fi
24OUTFILE="$1"
25
26INCLUDES='
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 Hamaji286e2892016-05-10 17:33:08 +090036# Generate a dependency file which helps the build tool to see when it
37# should regenerate ${OUTFILE}.
Alex Deymo477f2e32016-05-12 18:35:49 -070038echo "${INCLUDES}" | ${CC} - -E -M -MF "${OUTFILE}.d.tmp"
Shinichiro Hamaji286e2892016-05-10 17:33:08 +090039# Correct the output filename.
Alex Deymo477f2e32016-05-12 18:35:49 -070040(echo "${OUTFILE}: \\" ; sed -e 's/^-\.o://' -e 's/^-://' "${OUTFILE}.d.tmp") \
41 > "${OUTFILE}.d"
42rm "${OUTFILE}.d.tmp"
Shinichiro Hamaji286e2892016-05-10 17:33:08 +090043
Scott James Remnant33df0e32015-10-12 15:14:06 -070044# 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 Frysingerb095d9e2015-10-16 14:05:14 -040050SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\
Jeff Vander Stoepc38a20b2015-12-22 07:55:19 -080051 { "\1", (unsigned long) \1 },\
Mike Frysingerb095d9e2015-10-16 14:05:14 -040052#endif // \1@'
Scott James Remnant33df0e32015-10-12 15:14:06 -070053
Luis Hector Chavez40b25742013-09-22 19:44:06 -070054# 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.
57cat <<-EOF > "${OUTFILE}"
58/* GENERATED BY MAKEFILE */
59$INCLUDES
60
61#include "libconstants.h"
62const struct constant_entry constant_table[] = {
63$(echo "$INCLUDES" | \
Samuel Tanf260bef2015-10-05 16:45:48 -070064 ${CC} -dD - -E | \
Jeff Vander Stoepc38a20b2015-12-22 07:55:19 -080065 grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]]' | \
Mike Frysinger4044ed12015-10-16 13:53:04 -040066 grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \
Mike Frysingerab0af202015-10-16 13:53:51 -040067 sort -u | \
Mike Frysingerb095d9e2015-10-16 14:05:14 -040068 sed -Ee "${SED_MULTILINE}")
Luis Hector Chavez40b25742013-09-22 19:44:06 -070069 { NULL, 0 },
70};
71EOF