• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12set -e
13
14if [ $# -ne 1 ] && [ $# -ne 2 ]; then
15  echo "Usage: $(basename "$0") OUTFILE"
16  echo "Usage: $(basename "$0") INFILE OUTFILE"
17  exit 1
18fi
19
20BUILD="${CC} -dD ${SRC:-.}/gen_constants.c -E"
21GEN_DEPS=1
22
23if [ $# -eq 2 ]; then
24  BUILD="cat $1"
25  GEN_DEPS=0
26  shift
27fi
28OUTFILE="$1"
29
30if [ ${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
35
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
42SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\
43  { "\1", (unsigned long) \1 },\
44#endif  // \1@'
45
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.
49cat <<-EOF > "${OUTFILE}"
50/* GENERATED BY MAKEFILE */
51#include "gen_constants-inl.h"
52#include "libconstants.h"
53const struct constant_entry constant_table[] = {
54$(${BUILD} | \
55  grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]_]' | \
56  grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \
57  sort -u | \
58  sed -Ee "${SED_MULTILINE}")
59  { NULL, 0 },
60};
61EOF
62