• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2
3# Copyright 2012 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 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
15if [ $# -ne 1 ] && [ $# -ne 2 ]; then
16  echo "Usage: $(basename "$0") OUTFILE"
17  echo "Usage: $(basename "$0") INFILE OUTFILE"
18  exit 1
19fi
20
21build() {
22  ${CC:-cc} -dD "${SRC:-.}"/gen_syscalls.c -E "$@"
23}
24GEN_DEPS=1
25
26if [ $# -eq 2 ]; then
27  build() {
28    cat "${CAT_FILE}"
29  }
30  CAT_FILE="$1"
31  GEN_DEPS=0
32  shift
33fi
34OUTFILE="$1"
35
36if [ "${GEN_DEPS}" -eq 1 ]; then
37  # Generate a dependency file which helps the build tool to see when it
38  # should regenerate ${OUTFILE}.
39  build -M -MF "${OUTFILE}.d"
40fi
41
42# sed expression which extracts system calls that are
43# defined via asm/unistd.h.  It converts them from:
44#  #define __NR_read foo
45# to:
46# #ifdef __NR_read
47#  { "read", __NR_read },
48# #endif
49SED_MULTILINE='s/#define __(ARM_)?(NR_)([[:lower:]0-9_]*) (.*)$/#ifdef __\1\2\3\
50{ "\1\3", __\1\2\3 },\
51#endif/g p;'
52
53cat <<-EOF > "${OUTFILE}"
54/* GENERATED BY MAKEFILE */
55#include <stddef.h>
56#include "gen_syscalls-inl.h"
57#include "libsyscalls.h"
58const struct syscall_entry syscall_table[] = {
59$(build | sed -Ene "${SED_MULTILINE}")
60  { NULL, -1 },
61};
62
63const size_t syscall_table_size =
64    sizeof(syscall_table) / sizeof(syscall_table[0]);
65EOF
66