mkall.sh 9.0 KB

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