Text file src/syscall/mkall.sh

     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 syscall 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  
    76  GOOSARCH="${GOOS}_${GOARCH}"
    77  
    78  # defaults
    79  mksyscall="./mksyscall.pl"
    80  mkerrors="./mkerrors.sh"
    81  zerrors="zerrors_$GOOSARCH.go"
    82  mksysctl=""
    83  zsysctl="zsysctl_$GOOSARCH.go"
    84  mksysnum=
    85  mktypes=
    86  mkasm=
    87  run="sh"
    88  
    89  case "$1" in
    90  -syscalls)
    91  	for i in zsyscall*go
    92  	do
    93  		# Run the command line that appears in the first line
    94  		# of the generated file to regenerate it.
    95  		sed 1q $i | sed 's;^// ;;' | sh > _$i && gofmt < _$i > $i
    96  		rm _$i
    97  	done
    98  	exit 0
    99  	;;
   100  -n)
   101  	run="cat"
   102  	shift
   103  esac
   104  
   105  case "$#" in
   106  0)
   107  	;;
   108  *)
   109  	echo 'usage: mkall.sh [-n]' 1>&2
   110  	exit 2
   111  esac
   112  
   113  GOOSARCH_in=syscall_$GOOSARCH.go
   114  case "$GOOSARCH" in
   115  _* | *_ | _)
   116  	echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
   117  	exit 1
   118  	;;
   119  aix_ppc64)
   120  	mkerrors="$mkerrors -maix64"
   121  	mksyscall="./mksyscall_libc.pl -aix"
   122  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   123  	;;
   124  darwin_amd64)
   125  	mkerrors="$mkerrors -m64"
   126  	mksyscall="./mksyscall.pl -darwin"
   127  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   128  	mkasm="go run mkasm.go"
   129  	;;
   130  darwin_arm64)
   131  	mkerrors="$mkerrors -m64"
   132  	mksyscall="./mksyscall.pl -darwin"
   133  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   134  	mkasm="go run mkasm.go"
   135  	;;
   136  dragonfly_amd64)
   137  	mkerrors="$mkerrors -m64"
   138  	mksyscall="./mksyscall.pl -dragonfly"
   139  	mksysnum="curl -s 'http://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master' | ./mksysnum_dragonfly.pl"
   140  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   141  	;;
   142  freebsd_386)
   143  	mkerrors="$mkerrors -m32"
   144  	mksyscall="./mksyscall.pl -l32"
   145  	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
   146  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   147  	;;
   148  freebsd_amd64)
   149  	mkerrors="$mkerrors -m64"
   150  	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
   151  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   152  	;;
   153  freebsd_arm)
   154  	mkerrors="$mkerrors"
   155  	mksyscall="./mksyscall.pl -l32 -arm"
   156  	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
   157  	# Let the type of C char be signed to make the bare syscall
   158  	# API consistent between platforms.
   159  	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
   160  	;;
   161  freebsd_arm64)
   162  	mkerrors="$mkerrors"
   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 to make the bare syscall
   165  	# API consistent between platforms.
   166  	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
   167  	;;
   168  linux_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  	;;
   174  linux_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  	;;
   184  linux_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  	;;
   190  linux_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 to make the bare syscall
   198  	# API consistent between platforms.
   199  	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
   200  	;;
   201  linux_loong64)
   202          GOOSARCH_in=syscall_linux_loong64.go
   203          unistd_h=$(ls -1 /usr/include/asm/unistd.h /usr/include/asm-generic/unistd.h 2>/dev/null | head -1)
   204          if [ "$unistd_h" = "" ]; then
   205                  echo >&2 cannot find unistd.h
   206                  exit 1
   207          fi
   208          mksysnum="./mksysnum_linux.pl $unistd_h"
   209          mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   210          ;;
   211  linux_mips)
   212  	GOOSARCH_in=syscall_linux_mipsx.go
   213  	unistd_h=/usr/include/asm/unistd.h
   214  	mksyscall="./mksyscall.pl -b32 -arm"
   215  	mkerrors="$mkerrors"
   216  	mksysnum="./mksysnum_linux.pl $unistd_h"
   217  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   218  	;;
   219  linux_mipsle)
   220  	GOOSARCH_in=syscall_linux_mipsx.go
   221  	unistd_h=/usr/include/asm/unistd.h
   222  	mksyscall="./mksyscall.pl -l32 -arm"
   223  	mkerrors="$mkerrors"
   224  	mksysnum="./mksysnum_linux.pl $unistd_h"
   225  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   226  	;;
   227  linux_mips64)
   228  	GOOSARCH_in=syscall_linux_mips64x.go
   229  	unistd_h=/usr/include/asm/unistd.h
   230  	mkerrors="$mkerrors -m64"
   231  	mksysnum="./mksysnum_linux.pl $unistd_h"
   232  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   233  	;;
   234  linux_mips64le)
   235  	GOOSARCH_in=syscall_linux_mips64x.go
   236  	unistd_h=/usr/include/asm/unistd.h
   237  	mkerrors="$mkerrors -m64"
   238  	mksysnum="./mksysnum_linux.pl $unistd_h"
   239  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   240  	;;
   241  linux_ppc64)
   242  	GOOSARCH_in=syscall_linux_ppc64x.go
   243  	unistd_h=/usr/include/asm/unistd.h
   244  	mkerrors="$mkerrors -m64"
   245  	mksysnum="./mksysnum_linux.pl $unistd_h"
   246  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   247  	;;
   248  linux_ppc64le)
   249  	GOOSARCH_in=syscall_linux_ppc64x.go
   250  	unistd_h=/usr/include/powerpc64le-linux-gnu/asm/unistd.h
   251  	mkerrors="$mkerrors -m64"
   252  	mksysnum="./mksysnum_linux.pl $unistd_h"
   253  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   254  	;;
   255  linux_riscv64)
   256  	unistd_h=$(ls -1 /usr/include/asm/unistd.h /usr/include/asm-generic/unistd.h 2>/dev/null | head -1)
   257  	if [ "$unistd_h" = "" ]; then
   258  		echo >&2 cannot find unistd_64.h
   259  		exit 1
   260  	fi
   261  	mksysnum="./mksysnum_linux.pl $unistd_h"
   262  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   263  	;;
   264  linux_s390x)
   265  	GOOSARCH_in=syscall_linux_s390x.go
   266  	unistd_h=/usr/include/asm/unistd.h
   267  	mkerrors="$mkerrors -m64"
   268  	mksysnum="./mksysnum_linux.pl $unistd_h"
   269  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   270  	;;
   271  netbsd_386)
   272  	mkerrors="$mkerrors -m32"
   273  	mksyscall="./mksyscall.pl -l32 -netbsd"
   274  	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
   275  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   276  	;;
   277  netbsd_amd64)
   278  	mkerrors="$mkerrors -m64"
   279  	mksyscall="./mksyscall.pl -netbsd"
   280  	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
   281  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   282  	;;
   283  netbsd_arm)
   284  	mkerrors="$mkerrors -m32"
   285  	mksyscall="./mksyscall.pl -l32 -netbsd -arm"
   286  	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
   287  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   288  	;;
   289  netbsd_arm64)
   290  	mkerrors="$mkerrors -m64"
   291  	mksyscall="./mksyscall.pl -netbsd"
   292  	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
   293  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   294  	;;
   295  openbsd_386)
   296  	GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
   297  	mkerrors="$mkerrors -m32"
   298  	mksyscall="./mksyscall.pl -l32 -openbsd -libc"
   299  	mksysctl="./mksysctl_openbsd.pl"
   300  	zsysctl="zsysctl_openbsd.go"
   301  	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
   302  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   303  	mkasm="go run mkasm.go"
   304  	;;
   305  openbsd_amd64)
   306  	GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
   307  	mkerrors="$mkerrors -m64"
   308  	mksyscall="./mksyscall.pl -openbsd -libc"
   309  	mksysctl="./mksysctl_openbsd.pl"
   310  	zsysctl="zsysctl_openbsd.go"
   311  	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
   312  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   313  	mkasm="go run mkasm.go"
   314  	;;
   315  openbsd_arm)
   316  	GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
   317  	mkerrors="$mkerrors"
   318  	mksyscall="./mksyscall.pl -l32 -openbsd -arm -libc"
   319  	mksysctl="./mksysctl_openbsd.pl"
   320  	zsysctl="zsysctl_openbsd.go"
   321  	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
   322  	# Let the type of C char be signed to make the bare syscall
   323  	# API consistent between platforms.
   324  	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
   325  	mkasm="go run mkasm.go"
   326  	;;
   327  openbsd_arm64)
   328  	GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
   329  	mkerrors="$mkerrors -m64"
   330  	mksyscall="./mksyscall.pl -openbsd -libc"
   331  	mksysctl="./mksysctl_openbsd.pl"
   332  	zsysctl="zsysctl_openbsd.go"
   333  	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
   334  	# Let the type of C char be signed to make the bare syscall
   335  	# API consistent between platforms.
   336  	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
   337  	mkasm="go run mkasm.go"
   338  	;;
   339  openbsd_mips64)
   340  	GOOSARCH_in="syscall_openbsd1.go syscall_openbsd_$GOARCH.go"
   341  	mkerrors="$mkerrors -m64"
   342  	mksyscall="./mksyscall.pl -openbsd"
   343  	mksysctl="./mksysctl_openbsd.pl"
   344  	zsysctl="zsysctl_openbsd.go"
   345  	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
   346  	# Let the type of C char be signed to make the bare syscall
   347  	# API consistent between platforms.
   348  	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
   349  	;;
   350  plan9_386)
   351  	mkerrors=
   352  	mksyscall="./mksyscall.pl -l32 -plan9"
   353  	mksysnum="./mksysnum_plan9.sh /n/sources/plan9/sys/src/libc/9syscall/sys.h"
   354  	mktypes="XXX"
   355  	;;
   356  solaris_amd64)
   357  	mksyscall="./mksyscall_libc.pl -solaris"
   358  	mkerrors="$mkerrors -m64"
   359  	mksysnum=
   360  	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
   361  	;;
   362  windows_*)
   363  	echo 'run "go generate" instead' 1>&2
   364  	exit 1
   365  	;;
   366  *)
   367  	echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
   368  	exit 1
   369  	;;
   370  esac
   371  
   372  (
   373  	if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
   374  	syscall_goos="syscall_$GOOS.go"
   375   	case "$GOOS" in
   376  	darwin | dragonfly | freebsd | netbsd | openbsd)
   377  		syscall_goos="syscall_bsd.go $syscall_goos"
   378   		;;
   379   	esac
   380  	if [ -n "$mksyscall" ]; then echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go"; fi
   381  	if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
   382  	if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
   383  	if [ -n "$mktypes" ]; then
   384  		# ztypes_$GOOSARCH.go could be erased before "go run mkpost.go" is called.
   385  		# Therefore, "go run" tries to recompile syscall package but ztypes is empty and it fails.
   386  		echo "$mktypes types_$GOOS.go |go run mkpost.go >ztypes_$GOOSARCH.go.NEW && mv ztypes_$GOOSARCH.go.NEW ztypes_$GOOSARCH.go";
   387  	fi
   388  	if [ -n "$mkasm" ]; then echo "$mkasm $GOOS $GOARCH"; fi
   389  ) | $run
   390  

View as plain text