1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:54:42 -07:00

syscall: use go generate to build zsyscall_windows.go

I would like to create new syscalls in src/internal/syscall,
and I prefer not to add new shell scripts for that.

Replacement for CL 136000043.

Change-Id: I840116b5914a2324f516cdb8603c78973d28aeb4
Reviewed-on: https://go-review.googlesource.com/1940
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Alex Brainman 2014-12-22 16:54:07 +11:00
parent fcfbeb3adf
commit d00024bd60
5 changed files with 43 additions and 44 deletions

View File

@ -244,9 +244,8 @@ solaris_amd64)
mktypes="GOARCH=$GOARCH go tool cgo -godefs" mktypes="GOARCH=$GOARCH go tool cgo -godefs"
;; ;;
windows_*) windows_*)
mksyscall= echo 'run "go generate syscall_windows.go" instead' 1>&2
mkerrors= exit 1
zerrors=
;; ;;
*) *)
echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2 echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
@ -256,22 +255,13 @@ esac
( (
if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
case "$GOOS" in syscall_goos="syscall_$GOOS.go"
windows) case "$GOOS" in
echo "GOOS= GOARCH= go build mksyscall_windows.go" darwin | dragonfly | freebsd | netbsd | openbsd)
echo "./mksyscall_windows syscall_windows.go security_windows.go |gofmt >zsyscall_windows.go" syscall_goos="syscall_bsd.go $syscall_goos"
echo "rm -f ./mksyscall_windows" ;;
;; esac
*) if [ -n "$mksyscall" ]; then echo "$mksyscall $syscall_goos syscall_$GOOSARCH.go |gofmt >zsyscall_$GOOSARCH.go"; fi
syscall_goos="syscall_$GOOS.go"
case "$GOOS" in
darwin | dragonfly | freebsd | netbsd | openbsd)
syscall_goos="syscall_bsd.go $syscall_goos"
;;
esac
if [ -n "$mksyscall" ]; then echo "$mksyscall $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go"; fi
;;
esac
if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
if [ -n "$mktypes" ]; then echo "$mktypes types_$GOOS.go |gofmt >ztypes_$GOOSARCH.go"; fi if [ -n "$mktypes" ]; then echo "$mktypes types_$GOOS.go |gofmt >ztypes_$GOOSARCH.go"; fi

View File

@ -1,15 +0,0 @@
:: Copyright 2013 The Go Authors. All rights reserved.
:: Use of this source code is governed by a BSD-style
:: license that can be found in the LICENSE file.
@echo off
if exist mkall.sh goto dirok
echo mkall_windows.bat must be run from src\syscall directory
goto :end
:dirok
go build mksyscall_windows.go
.\mksyscall_windows syscall_windows.go security_windows.go |gofmt >zsyscall_windows.go
del mksyscall_windows.exe
:end

View File

@ -37,6 +37,8 @@ Usage:
mksyscall_windows [flags] [path ...] mksyscall_windows [flags] [path ...]
The flags are: The flags are:
-output
Specify output file name (outputs to console if blank).
-trace -trace
Generate print statement after every syscall. Generate print statement after every syscall.
*/ */
@ -44,12 +46,15 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"go/format"
"go/parser" "go/parser"
"go/token" "go/token"
"io" "io"
"io/ioutil"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -57,7 +62,10 @@ import (
"text/template" "text/template"
) )
var PrintTraceFlag = flag.Bool("trace", false, "generate print statement after every syscall") var (
filename = flag.String("output", "", "output file name (standard output if omitted)")
printTraceFlag = flag.Bool("trace", false, "generate print statement after every syscall")
)
func trim(s string) string { func trim(s string) string {
return strings.Trim(s, " \t") return strings.Trim(s, " \t")
@ -379,7 +387,7 @@ func newFn(s string) (*Fn, error) {
f := &Fn{ f := &Fn{
Rets: &Rets{}, Rets: &Rets{},
src: s, src: s,
PrintTrace: *PrintTraceFlag, PrintTrace: *printTraceFlag,
} }
// function name and args // function name and args
prefix, body, s, found := extractSection(s, '(', ')') prefix, body, s, found := extractSection(s, '(', ')')
@ -669,8 +677,8 @@ func (src *Source) ParseFile(path string) error {
// Generate output source file from a source set src. // Generate output source file from a source set src.
func (src *Source) Generate(w io.Writer) error { func (src *Source) Generate(w io.Writer) error {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"syscalldot": syscalldot,
"packagename": packagename, "packagename": packagename,
"syscalldot": syscalldot,
} }
t := template.Must(template.New("main").Funcs(funcMap).Parse(srcTemplate)) t := template.Must(template.New("main").Funcs(funcMap).Parse(srcTemplate))
err := t.Execute(w, src) err := t.Execute(w, src)
@ -689,15 +697,31 @@ func usage() {
func main() { func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
if len(os.Args) <= 1 { if len(flag.Args()) <= 0 {
fmt.Fprintf(os.Stderr, "no files to parse provided\n") fmt.Fprintf(os.Stderr, "no files to parse provided\n")
usage() usage()
} }
src, err := ParseFiles(os.Args[1:])
src, err := ParseFiles(flag.Args())
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := src.Generate(os.Stdout); err != nil {
var buf bytes.Buffer
if err := src.Generate(&buf); err != nil {
log.Fatal(err)
}
data, err := format.Source(buf.Bytes())
if err != nil {
log.Fatal(err)
}
if *filename == "" {
_, err = os.Stdout.Write(data)
} else {
err = ioutil.WriteFile(*filename, data, 0644)
}
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
@ -705,8 +729,7 @@ func main() {
// TODO: use println instead to print in the following template // TODO: use println instead to print in the following template
const srcTemplate = ` const srcTemplate = `
{{define "main"}}// go build mksyscall_windows.go && ./mksyscall_windows{{range .Files}} {{.}}{{end}} {{define "main"}}// MACHINE GENERATED BY 'go generate' COMMAND; DO NOT EDIT
// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
package {{packagename}} package {{packagename}}

View File

@ -13,6 +13,8 @@ import (
"unsafe" "unsafe"
) )
//go:generate go run mksyscall_windows.go -output zsyscall_windows.go syscall_windows.go security_windows.go
type Handle uintptr type Handle uintptr
const InvalidHandle = ^Handle(0) const InvalidHandle = ^Handle(0)

View File

@ -1,5 +1,4 @@
// go build mksyscall_windows.go && ./mksyscall_windows syscall_windows.go security_windows.go // MACHINE GENERATED BY 'go generate' COMMAND; DO NOT EDIT
// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
package syscall package syscall