• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2
3# Copyright 2015 The ChromiumOS Authors
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() {
21  ${CC:-cc} -dD "${SRC:-.}"/gen_constants.c -E "$@"
22}
23GEN_DEPS=1
24
25if [ $# -eq 2 ]; then
26  build() {
27    cat "${CAT_FILE}"
28  }
29  CAT_FILE="$1"
30  GEN_DEPS=0
31  shift
32fi
33OUTFILE="$1"
34
35if [ "${GEN_DEPS}" -eq 1 ]; then
36  # Generate a dependency file which helps the build tool to see when it
37  # should regenerate ${OUTFILE}.
38  build -M -MF "${OUTFILE}.d"
39fi
40
41# sed expression which extracts constants and converts them from:
42#   #define AT_FDWCD foo
43# to:
44# #ifdef AT_FDCWD
45#   { "AT_FDWCD", AT_FDCWD },
46# endif
47SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\
48  { "\1", (unsigned long) \1 },\
49#endif  // \1@'
50
51# Passes the previous list of #includes to the C preprocessor and prints out
52# all #defines whose name is all-caps.  Excludes a few symbols that are known
53# macro functions that don't evaluate to a constant.
54cat <<-EOF > "${OUTFILE}"
55/* GENERATED BY MAKEFILE */
56#include "gen_constants-inl.h"
57#include "libconstants.h"
58const struct constant_entry constant_table[] = {
59$(build | \
60  grep -E '^#define [[:upper:]][[:upper:]0-9_]*[[:space:]]+[[:alnum:]_]' | \
61  grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \
62  sort -u | \
63  sed -Ee "${SED_MULTILINE}")
64  { NULL, 0 },
65};
66EOF
67