1#!/bin/sh
2
3output="syscalls.h"
4
5# For soon to be able to provide an argument
6# for output file so the header generation can
7# be added as a dependency in Android.bp
8if [ $# -gt 0 ]; then
9	output=$1
10	echo "ltp syscalls gen output: "$output
11	rm -f "${output}"
12fi
13
14rm -f "${output}".[1-9]*
15output_pid="${output}.$$"
16
17max_jobs=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
18: ${max_jobs:=1}
19
20srcdir=${0%/*}
21
22err() {
23	echo "$*" 1>&2
24	exit 1
25}
26
27cat << EOF > "${output_pid}"
28/************************************************
29 * GENERATED FILE: DO NOT EDIT/PATCH THIS FILE  *
30 *  change your arch specific .in file instead  *
31 ************************************************/
32
33/*
34 * Here we stick all the ugly *fallback* logic for linux
35 * system call numbers (those __NR_ thingies).
36 *
37 * Licensed under the GPLv2 or later, see the COPYING file.
38 */
39
40#ifndef __LAPI_SYSCALLS_H__
41#define __LAPI_SYSCALLS_H__
42
43#include <errno.h>
44#include <sys/syscall.h>
45#include <asm/unistd.h>
46#include "cleanup.c"
47
48#define ltp_syscall(NR, ...) ({ \\
49	int __ret; \\
50	if (NR == __LTP__NR_INVALID_SYSCALL) { \\
51		errno = ENOSYS; \\
52		__ret = -1; \\
53	} else { \\
54		__ret = syscall(NR, ##__VA_ARGS__); \\
55	} \\
56	if (__ret == -1 && errno == ENOSYS) { \\
57		tst_brkm(TCONF, CLEANUP, \\
58			"syscall(%d) " #NR " not supported on your arch", \\
59			NR); \\
60	} \\
61	__ret; \\
62})
63
64#define tst_syscall(NR, ...) ({ \\
65	int tst_ret; \\
66	if (NR == __LTP__NR_INVALID_SYSCALL) { \\
67		errno = ENOSYS; \\
68		tst_ret = -1; \\
69	} else { \\
70		tst_ret = syscall(NR, ##__VA_ARGS__); \\
71	} \\
72	if (tst_ret == -1 && errno == ENOSYS) { \\
73		tst_brk(TCONF, "syscall(%d) " #NR " not supported", NR); \\
74	} \\
75	tst_ret; \\
76})
77
78EOF
79
80jobs=0
81for arch in $(cat "${srcdir}/order") ; do
82	(
83	echo "Generating data for arch $arch ... "
84
85	(
86	echo
87	case ${arch} in
88		sparc64) echo "#if defined(__sparc__) && defined(__arch64__)" ;;
89		sparc) echo "#if defined(__sparc__) && !defined(__arch64__)" ;;
90		s390) echo "#if defined(__s390__) && !defined(__s390x__)" ;;
91		mips_n32) echo "#if defined(__mips__) && defined(_ABIN32)" ;;
92		mips_n64) echo "#if defined(__mips__) && defined(_ABI64)" ;;
93		mips_o32) echo "#if defined(__mips__) && defined(_ABIO32)" ;;
94		*) echo "#ifdef __${arch}__" ;;
95	esac
96	while read line ; do
97		set -- ${line}
98		nr="__NR_$1"
99		shift
100		if [ $# -eq 0 ] ; then
101			err "invalid line found: $line"
102		fi
103		echo "# ifndef ${nr}"
104		echo "#  define ${nr} $*"
105		echo "# endif"
106	done < "${srcdir}/${arch}.in"
107	echo "#endif"
108	echo
109	) >> "${output_pid}.${arch}"
110
111	) &
112
113	jobs=$(( jobs + 1 ))
114	if [ ${jobs} -ge ${max_jobs} ] ; then
115		wait || exit 1
116		jobs=0
117	fi
118done
119
120echo "Generating stub list ... "
121(
122echo
123echo "/* Common stubs */"
124echo "#define __LTP__NR_INVALID_SYSCALL -1" >> "${output_pid}"
125for nr in $(awk '{print $1}' "${srcdir}/"*.in | sort -u) ; do
126	nr="__NR_${nr}"
127	echo "# ifndef ${nr}"
128	echo "#  define ${nr} __LTP__NR_INVALID_SYSCALL"
129	echo "# endif"
130done
131echo "#endif"
132) >> "${output_pid}._footer"
133
134wait || exit 1
135
136printf "Combining them all ... "
137for arch in $(cat "${srcdir}/order") _footer ; do
138	cat "${output_pid}.${arch}"
139done >> "${output_pid}"
140if [ $# -gt 0 ]; then
141	mv "${output_pid}" "${output}"
142else
143	mv "${output_pid}" "../${output}"
144fi
145rm -f "${output_pid}"*
146echo "OK!"
147