blob: 7e1707c78214e0c986c61b702b4ff363ed59b36e [file] [log] [blame]
Lei Zhangeee31552012-10-17 21:27:10 -07001#!/bin/sh
2
3# Copyright (c) 2012 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 system call table made up of "name",
8# syscall_nr entries by including the build target <asm/unistd.h> and
9# emitting the list of defines. Use of the compiler is needed to
10# dereference the actual provider of syscall definitions.
11# E.g., asm/unistd_32.h or asm/unistd_64.h, etc.
12
13set -e
14
Samuel Tanc0e60a22015-10-09 12:30:21 -070015if [ $# -ne 1 ] && [ $# -ne 2 ]; then
16 echo "Usage: $(basename "$0") OUTFILE"
Dan Willemsen2acbec52017-09-14 17:28:36 -070017 echo "Usage: $(basename "$0") INFILE OUTFILE"
Lei Zhangeee31552012-10-17 21:27:10 -070018 exit 1
19fi
20
Mike Frysingerdd5a8842018-08-15 15:57:44 -040021BUILD="${CC} -dD ${SRC:-.}/gen_syscalls.c -E"
Dan Willemsen2acbec52017-09-14 17:28:36 -070022GEN_DEPS=1
23
Samuel Tanc0e60a22015-10-09 12:30:21 -070024if [ $# -eq 2 ]; then
Dan Willemsen2acbec52017-09-14 17:28:36 -070025 BUILD="cat $1"
26 GEN_DEPS=0
Samuel Tanc0e60a22015-10-09 12:30:21 -070027 shift
28fi
Lei Zhangeee31552012-10-17 21:27:10 -070029OUTFILE="$1"
30
Dan Willemsen2acbec52017-09-14 17:28:36 -070031if [ ${GEN_DEPS} -eq 1 ]; then
32 # Generate a dependency file which helps the build tool to see when it
33 # should regenerate ${OUTFILE}.
34 ${BUILD} -M -MF "${OUTFILE}.d"
35fi
Shinichiro Hamaji286e2892016-05-10 17:33:08 +090036
Lei Zhangeee31552012-10-17 21:27:10 -070037# sed expression which extracts system calls that are
38# defined via asm/unistd.h. It converts them from:
39# #define __NR_read foo
40# to:
41# #ifdef __NR_read
42# { "read", __NR_read },
43# #endif
Mike Frysingerb962e7e2015-10-16 13:50:31 -040044SED_MULTILINE='s/#define __(ARM_)?(NR_)([[:lower:]0-9_]*) (.*)$/#ifdef __\1\2\3\
Scott James Remnantae2aed82015-09-24 14:35:31 -070045{ "\1\3", __\1\2\3 },\
46#endif/g p;'
Lei Zhangeee31552012-10-17 21:27:10 -070047
48cat <<-EOF > "${OUTFILE}"
49/* GENERATED BY MAKEFILE */
50#include <stddef.h>
Alistair Delva3b58ccb2020-08-24 13:54:16 -070051#include "gen_syscalls-inl.h"
Lei Zhangeee31552012-10-17 21:27:10 -070052#include "libsyscalls.h"
53const struct syscall_entry syscall_table[] = {
Dan Willemsen2acbec52017-09-14 17:28:36 -070054$(${BUILD} | sed -Ene "${SED_MULTILINE}")
Lei Zhangeee31552012-10-17 21:27:10 -070055 { NULL, -1 },
56};
Nicole Anderson-Au461d8fb2020-11-09 22:53:40 +000057
58const size_t syscall_table_size =
59 sizeof(syscall_table) / sizeof(syscall_table[0]);
Lei Zhangeee31552012-10-17 21:27:10 -070060EOF