github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/syscall/mkpost.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 // mkpost processes the output of cgo -godefs to 8 // modify the generated types. It is used to clean up 9 // the syscall API in an architecture specific manner. 10 // 11 // mkpost is run after cgo -godefs by mkall.sh. 12 package main 13 14 import ( 15 "fmt" 16 "go/format" 17 "io/ioutil" 18 "log" 19 "os" 20 "regexp" 21 "strings" 22 ) 23 24 func main() { 25 b, err := ioutil.ReadAll(os.Stdin) 26 if err != nil { 27 log.Fatal(err) 28 } 29 s := string(b) 30 31 goarch := os.Getenv("GOARCH") 32 goos := os.Getenv("GOOS") 33 if goarch == "s390x" && goos == "linux" { 34 // Export the types of PtraceRegs fields. 35 re := regexp.MustCompile("ptrace(Psw|Fpregs|Per)") 36 s = re.ReplaceAllString(s, "Ptrace$1") 37 38 // Replace padding fields inserted by cgo with blank identifiers. 39 re = regexp.MustCompile("Pad_cgo[A-Za-z0-9_]*") 40 s = re.ReplaceAllString(s, "_") 41 42 // We want to keep X__val in Fsid. Hide it and restore it later. 43 s = strings.Replace(s, "X__val", "MKPOSTFSIDVAL", 1) 44 45 // Replace other unwanted fields with blank identifiers. 46 re = regexp.MustCompile("X_[A-Za-z0-9_]*") 47 s = re.ReplaceAllString(s, "_") 48 49 // Restore X__val in Fsid. 50 s = strings.Replace(s, "MKPOSTFSIDVAL", "X__val", 1) 51 52 // Force the type of RawSockaddr.Data to [14]int8 to match 53 // the existing gccgo API. 54 re = regexp.MustCompile("(Data\\s+\\[14\\])uint8") 55 s = re.ReplaceAllString(s, "${1}int8") 56 } 57 58 // gofmt 59 b, err = format.Source([]byte(s)) 60 if err != nil { 61 log.Fatal(err) 62 } 63 64 // Append this command to the header to show where the new file 65 // came from. 66 re := regexp.MustCompile("(cgo -godefs [a-zA-Z0-9_]+\\.go.*)") 67 s = re.ReplaceAllString(string(b), "$1 | go run mkpost.go") 68 69 fmt.Print(s) 70 }