blob: 0ad29ba9a203b418c0da7924f8fe4d8ad8d249ed [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
36# Passes the previous list of #includes to the C preprocessor and prints out
37# all #defines whose name is all-caps. Excludes a few symbols that are known
38# macro functions that don't evaluate to a constant.
39cat <<-EOF > "${OUTFILE}"
40/* GENERATED BY MAKEFILE */
41$INCLUDES
42
43#include "libconstants.h"
44const struct constant_entry constant_table[] = {
45$(echo "$INCLUDES" | \
Samuel Tanf260bef2015-10-05 16:45:48 -070046 ${CC} -dD - -E | \
Luis Hector Chavez40b25742013-09-22 19:44:06 -070047 grep '^#define [A-Z][A-Z0-9_]* ' | \
48 grep -v '\(SIGRTMAX\|SIGRTMIN\|SIG_\|NULL\)' | \
49 sort | \
50 uniq | \
51 sed -e 's/#define \([A-Z0-9_]\+\).*$/#ifdef \1\n { "\1", \1 },\n#endif \/\/ \1/')
52 { NULL, 0 },
53};
54EOF