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") CC OUTFILE" 17 exit 1 18fi 19 20if [ $# -eq 2 ]; then 21 CC="$1" 22 shift 23fi 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# Generate a dependency file which helps the build tool to see when it 37# should regenerate ${OUTFILE}. 38echo "${INCLUDES}" | ${CC} - -E -M -MF "${OUTFILE}.d.tmp" 39# Correct the output filename. 40(echo "${OUTFILE}: \\" ; sed -e 's/^-\.o://' -e 's/^-://' "${OUTFILE}.d.tmp") \ 41 > "${OUTFILE}.d" 42rm "${OUTFILE}.d.tmp" 43 44# 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 50SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\ 51 { "\1", (unsigned long) \1 },\ 52#endif // \1@' 53 54# 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" | \ 64 ${CC} -dD - -E | \ 65 grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]]' | \ 66 grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \ 67 sort -u | \ 68 sed -Ee "${SED_MULTILINE}") 69 { NULL, 0 }, 70}; 71EOF 72