1#!/usr/bin/env bash
2# Copyright 2009 The Go Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file.
5
6# The unix package provides access to the raw system call
7# interface of the underlying operating system.  Porting Go to
8# a new architecture/operating system combination requires
9# some manual effort, though there are tools that automate
10# much of the process.  The auto-generated files have names
11# beginning with z.
12#
13# This script runs or (given -n) prints suggested commands to generate z files
14# for the current system.  Running those commands is not automatic.
15# This script is documentation more than anything else.
16#
17# * asm_${GOOS}_${GOARCH}.s
18#
19# This hand-written assembly file implements system call dispatch.
20# There are three entry points:
21#
22# 	func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr);
23# 	func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr);
24# 	func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr);
25#
26# The first and second are the standard ones; they differ only in
27# how many arguments can be passed to the kernel.
28# The third is for low-level use by the ForkExec wrapper;
29# unlike the first two, it does not call into the scheduler to
30# let it know that a system call is running.
31#
32# * syscall_${GOOS}.go
33#
34# This hand-written Go file implements system calls that need
35# special handling and lists "//sys" comments giving prototypes
36# for ones that can be auto-generated.  Mksyscall reads those
37# comments to generate the stubs.
38#
39# * syscall_${GOOS}_${GOARCH}.go
40#
41# Same as syscall_${GOOS}.go except that it contains code specific
42# to ${GOOS} on one particular architecture.
43#
44# * types_${GOOS}.c
45#
46# This hand-written C file includes standard C headers and then
47# creates typedef or enum names beginning with a dollar sign
48# (use of $ in variable names is a gcc extension).  The hardest
49# part about preparing this file is figuring out which headers to
50# include and which symbols need to be #defined to get the
51# actual data structures that pass through to the kernel system calls.
52# Some C libraries present alternate versions for binary compatibility
53# and translate them on the way in and out of system calls, but
54# there is almost always a #define that can get the real ones.
55# See types_darwin.c and types_linux.c for examples.
56#
57# * zerror_${GOOS}_${GOARCH}.go
58#
59# This machine-generated file defines the system's error numbers,
60# error strings, and signal numbers.  The generator is "mkerrors.sh".
61# Usually no arguments are needed, but mkerrors.sh will pass its
62# arguments on to godefs.
63#
64# * zsyscall_${GOOS}_${GOARCH}.go
65#
66# Generated by mksyscall.pl; see syscall_${GOOS}.go above.
67#
68# * zsysnum_${GOOS}_${GOARCH}.go
69#
70# Generated by mksysnum_${GOOS}.
71#
72# * ztypes_${GOOS}_${GOARCH}.go
73#
74# Generated by godefs; see types_${GOOS}.c above.
75
76GOOSARCH="${GOOS}_${GOARCH}"
77
78# defaults
79mksyscall="./mksyscall.pl"
80mkerrors="./mkerrors.sh"
81zerrors="zerrors_$GOOSARCH.go"
82mksysctl=""
83zsysctl="zsysctl_$GOOSARCH.go"
84mksysnum=
85mktypes=
86run="sh"
87
88case "$1" in
89-syscalls)
90	for i in zsyscall*go
91	do
92		sed 1q $i | sed 's;^// ;;' | sh > _$i && gofmt < _$i > $i
93		rm _$i
94	done
95	exit 0
96	;;
97-n)
98	run="cat"
99	shift
100esac
101
102case "$#" in
1030)
104	;;
105*)
106	echo 'usage: mkall.sh [-n]' 1>&2
107	exit 2
108esac
109
110GOOSARCH_in=syscall_$GOOSARCH.go
111case "$GOOSARCH" in
112_* | *_ | _)
113	echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
114	exit 1
115	;;
116darwin_386)
117	mkerrors="$mkerrors -m32"
118	mksyscall="./mksyscall.pl -l32"
119	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk macosx)/usr/include/sys/syscall.h"
120	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
121	;;
122darwin_amd64)
123	mkerrors="$mkerrors -m64"
124	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk macosx)/usr/include/sys/syscall.h"
125	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
126	;;
127darwin_arm)
128	mkerrors="$mkerrors"
129	mksysnum="./mksysnum_darwin.pl /usr/include/sys/syscall.h"
130	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
131	;;
132darwin_arm64)
133	mkerrors="$mkerrors -m64"
134	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk iphoneos)/usr/include/sys/syscall.h"
135	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
136	;;
137dragonfly_386)
138	mkerrors="$mkerrors -m32"
139	mksyscall="./mksyscall.pl -l32 -dragonfly"
140	mksysnum="curl -s 'http://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master' | ./mksysnum_dragonfly.pl"
141	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
142	;;
143dragonfly_amd64)
144	mkerrors="$mkerrors -m64"
145	mksyscall="./mksyscall.pl -dragonfly"
146	mksysnum="curl -s 'http://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master' | ./mksysnum_dragonfly.pl"
147	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
148	;;
149freebsd_386)
150	mkerrors="$mkerrors -m32"
151	mksyscall="./mksyscall.pl -l32"
152	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
153	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
154	;;
155freebsd_amd64)
156	mkerrors="$mkerrors -m64"
157	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
158	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
159	;;
160freebsd_arm)
161	mkerrors="$mkerrors"
162	mksyscall="./mksyscall.pl -l32 -arm"
163	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
164	# Let the type of C char be signed for making the bare syscall
165	# API consistent across over platforms.
166	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
167	;;
168linux_386)
169	mkerrors="$mkerrors -m32"
170	mksyscall="./mksyscall.pl -l32"
171	mksysnum="./mksysnum_linux.pl /usr/include/asm/unistd_32.h"
172	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
173	;;
174linux_amd64)
175	unistd_h=$(ls -1 /usr/include/asm/unistd_64.h /usr/include/x86_64-linux-gnu/asm/unistd_64.h 2>/dev/null | head -1)
176	if [ "$unistd_h" = "" ]; then
177		echo >&2 cannot find unistd_64.h
178		exit 1
179	fi
180	mkerrors="$mkerrors -m64"
181	mksysnum="./mksysnum_linux.pl $unistd_h"
182	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
183	;;
184linux_arm)
185	mkerrors="$mkerrors"
186	mksyscall="./mksyscall.pl -l32 -arm"
187	mksysnum="curl -s 'http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/arch/arm/include/uapi/asm/unistd.h' | ./mksysnum_linux.pl -"
188	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
189	;;
190linux_arm64)
191	unistd_h=$(ls -1 /usr/include/asm/unistd.h /usr/include/asm-generic/unistd.h 2>/dev/null | head -1)
192	if [ "$unistd_h" = "" ]; then
193		echo >&2 cannot find unistd_64.h
194		exit 1
195	fi
196	mksysnum="./mksysnum_linux.pl $unistd_h"
197	# Let the type of C char be signed for making the bare syscall
198	# API consistent across over platforms.
199	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
200	;;
201linux_ppc64)
202	GOOSARCH_in=syscall_linux_ppc64x.go
203	unistd_h=/usr/include/asm/unistd.h
204	mkerrors="$mkerrors -m64"
205	mksysnum="./mksysnum_linux.pl $unistd_h"
206	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
207	;;
208linux_ppc64le)
209	GOOSARCH_in=syscall_linux_ppc64x.go
210	unistd_h=/usr/include/powerpc64le-linux-gnu/asm/unistd.h
211	mkerrors="$mkerrors -m64"
212	mksysnum="./mksysnum_linux.pl $unistd_h"
213	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
214	;;
215linux_s390x)
216	GOOSARCH_in=syscall_linux_s390x.go
217	unistd_h=/usr/include/asm/unistd.h
218	mkerrors="$mkerrors -m64"
219	mksysnum="./mksysnum_linux.pl $unistd_h"
220	# Let the type of C char be signed to make the bare sys
221	# API more consistent between platforms.
222	# This is a deliberate departure from the way the syscall
223	# package generates its version of the types file.
224	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
225	;;
226netbsd_386)
227	mkerrors="$mkerrors -m32"
228	mksyscall="./mksyscall.pl -l32 -netbsd"
229	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
230	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
231	;;
232netbsd_amd64)
233	mkerrors="$mkerrors -m64"
234	mksyscall="./mksyscall.pl -netbsd"
235	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
236	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
237	;;
238openbsd_386)
239	mkerrors="$mkerrors -m32"
240	mksyscall="./mksyscall.pl -l32 -openbsd"
241	mksysctl="./mksysctl_openbsd.pl"
242	zsysctl="zsysctl_openbsd.go"
243	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
244	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
245	;;
246openbsd_amd64)
247	mkerrors="$mkerrors -m64"
248	mksyscall="./mksyscall.pl -openbsd"
249	mksysctl="./mksysctl_openbsd.pl"
250	zsysctl="zsysctl_openbsd.go"
251	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
252	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
253	;;
254solaris_amd64)
255	mksyscall="./mksyscall_solaris.pl"
256	mkerrors="$mkerrors -m64"
257	mksysnum=
258	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
259	;;
260*)
261	echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
262	exit 1
263	;;
264esac
265
266(
267	if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
268	case "$GOOS" in
269	*)
270		syscall_goos="syscall_$GOOS.go"
271		case "$GOOS" in
272		darwin | dragonfly | freebsd | netbsd | openbsd)
273			syscall_goos="syscall_bsd.go $syscall_goos"
274			;;
275		esac
276		if [ -n "$mksyscall" ]; then echo "$mksyscall $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go"; fi
277		;;
278	esac
279	if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
280	if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
281	if [ -n "$mktypes" ]; then
282		echo "echo // +build $GOARCH,$GOOS > ztypes_$GOOSARCH.go";
283		echo "$mktypes types_$GOOS.go | go run mkpost.go >>ztypes_$GOOSARCH.go";
284	fi
285) | $run
286