xref: /aosp_15_r20/external/crosvm/third_party/minijail/gen_constants.sh (revision 4b9c6d91573e8b3a96609339b46361b5476dd0f9)
1*4b9c6d91SCole Faust#!/bin/sh
2*4b9c6d91SCole Faust
3*4b9c6d91SCole Faust# Copyright 2015 The ChromiumOS Authors
4*4b9c6d91SCole Faust# Use of this source code is governed by a BSD-style license that can be
5*4b9c6d91SCole Faust# found in the LICENSE file.
6*4b9c6d91SCole Faust
7*4b9c6d91SCole Faust# Generates a header file with a named constant table made up of "name", value
8*4b9c6d91SCole Faust# entries by including several build target header files and emitting the list
9*4b9c6d91SCole Faust# of defines.  Use of the preprocessor is needed to recursively include all
10*4b9c6d91SCole Faust# relevant headers.
11*4b9c6d91SCole Faust
12*4b9c6d91SCole Faustset -e
13*4b9c6d91SCole Faust
14*4b9c6d91SCole Faustif [ $# -ne 1 ] && [ $# -ne 2 ]; then
15*4b9c6d91SCole Faust  echo "Usage: $(basename "$0") OUTFILE"
16*4b9c6d91SCole Faust  echo "Usage: $(basename "$0") INFILE OUTFILE"
17*4b9c6d91SCole Faust  exit 1
18*4b9c6d91SCole Faustfi
19*4b9c6d91SCole Faust
20*4b9c6d91SCole FaustBUILD="${CC} -dD ${SRC:-.}/gen_constants.c -E"
21*4b9c6d91SCole FaustGEN_DEPS=1
22*4b9c6d91SCole Faust
23*4b9c6d91SCole Faustif [ $# -eq 2 ]; then
24*4b9c6d91SCole Faust  BUILD="cat $1"
25*4b9c6d91SCole Faust  GEN_DEPS=0
26*4b9c6d91SCole Faust  shift
27*4b9c6d91SCole Faustfi
28*4b9c6d91SCole FaustOUTFILE="$1"
29*4b9c6d91SCole Faust
30*4b9c6d91SCole Faustif [ ${GEN_DEPS} -eq 1 ]; then
31*4b9c6d91SCole Faust  # Generate a dependency file which helps the build tool to see when it
32*4b9c6d91SCole Faust  # should regenerate ${OUTFILE}.
33*4b9c6d91SCole Faust  ${BUILD} -M -MF "${OUTFILE}.d"
34*4b9c6d91SCole Faustfi
35*4b9c6d91SCole Faust
36*4b9c6d91SCole Faust# sed expression which extracts constants and converts them from:
37*4b9c6d91SCole Faust#   #define AT_FDWCD foo
38*4b9c6d91SCole Faust# to:
39*4b9c6d91SCole Faust# #ifdef AT_FDCWD
40*4b9c6d91SCole Faust#   { "AT_FDWCD", AT_FDCWD },
41*4b9c6d91SCole Faust# endif
42*4b9c6d91SCole FaustSED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\
43*4b9c6d91SCole Faust  { "\1", (unsigned long) \1 },\
44*4b9c6d91SCole Faust#endif  // \1@'
45*4b9c6d91SCole Faust
46*4b9c6d91SCole Faust# Passes the previous list of #includes to the C preprocessor and prints out
47*4b9c6d91SCole Faust# all #defines whose name is all-caps.  Excludes a few symbols that are known
48*4b9c6d91SCole Faust# macro functions that don't evaluate to a constant.
49*4b9c6d91SCole Faustcat <<-EOF > "${OUTFILE}"
50*4b9c6d91SCole Faust/* GENERATED BY MAKEFILE */
51*4b9c6d91SCole Faust#include "gen_constants-inl.h"
52*4b9c6d91SCole Faust#include "libconstants.h"
53*4b9c6d91SCole Faustconst struct constant_entry constant_table[] = {
54*4b9c6d91SCole Faust$(${BUILD} | \
55*4b9c6d91SCole Faust  grep -E '^#define [[:upper:]][[:upper:]0-9_]*[[:space:]]+[[:alnum:]_]' | \
56*4b9c6d91SCole Faust  grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \
57*4b9c6d91SCole Faust  sort -u | \
58*4b9c6d91SCole Faust  sed -Ee "${SED_MULTILINE}")
59*4b9c6d91SCole Faust  { NULL, 0 },
60*4b9c6d91SCole Faust};
61*4b9c6d91SCole FaustEOF
62