blob: 8dcbca241be8d54653aeb5c684ce364ad2b4eccd [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
14if [ $# -ne 1 ] && [ $# -ne 3]; then
15 echo "Usage: $(basename "$0") OUTFILE"
16 echo "Usage: $(basename "$0") CC CFLAGS OUTFILE"
17 exit 1
18fi
19
20if [ $# -eq 3 ]; then
21 CC="$1"
22 shift
23 CFLAGS="$1"
24 shift
25fi
26OUTFILE="$1"
27
28INCLUDES='
29#include <errno.h>
30#include <fcntl.h>
31#include <linux/prctl.h>
32#include <linux/sched.h>
33#include <stddef.h>
34#include <signal.h>
35#include <sys/stat.h>
36#include <sys/types.h>'
37
38# Passes the previous list of #includes to the C preprocessor and prints out
39# all #defines whose name is all-caps. Excludes a few symbols that are known
40# macro functions that don't evaluate to a constant.
41cat <<-EOF > "${OUTFILE}"
42/* GENERATED BY MAKEFILE */
43$INCLUDES
44
45#include "libconstants.h"
46const struct constant_entry constant_table[] = {
47$(echo "$INCLUDES" | \
48 ${CC} ${CFLAGS} -dD - -E | \
49 grep '^#define [A-Z][A-Z0-9_]* ' | \
50 grep -v '\(SIGRTMAX\|SIGRTMIN\|SIG_\|NULL\)' | \
51 sort | \
52 uniq | \
53 sed -e 's/#define \([A-Z0-9_]\+\).*$/#ifdef \1\n { "\1", \1 },\n#endif \/\/ \1/')
54 { NULL, 0 },
55};
56EOF