github.com/comwrg/go/src@v0.0.0-20220319063731-c238d0440370/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 //go:build ignore 6 // +build ignore 7 8 // mkpost processes the output of cgo -godefs to 9 // modify the generated types. It is used to clean up 10 // the syscall API in an architecture specific manner. 11 // 12 // mkpost is run after cgo -godefs by mkall.sh. 13 package main 14 15 import ( 16 "fmt" 17 "go/format" 18 "io" 19 "log" 20 "os" 21 "regexp" 22 "strings" 23 ) 24 25 func main() { 26 b, err := io.ReadAll(os.Stdin) 27 if err != nil { 28 log.Fatal(err) 29 } 30 s := string(b) 31 32 goarch := os.Getenv("GOARCH") 33 goos := os.Getenv("GOOS") 34 switch { 35 case goarch == "s390x" && goos == "linux": 36 // Export the types of PtraceRegs fields. 37 re := regexp.MustCompile("ptrace(Psw|Fpregs|Per)") 38 s = re.ReplaceAllString(s, "Ptrace$1") 39 40 // Replace padding fields inserted by cgo with blank identifiers. 41 re = regexp.MustCompile("Pad_cgo[A-Za-z0-9_]*") 42 s = re.ReplaceAllString(s, "_") 43 44 // We want to keep X__val in Fsid. Hide it and restore it later. 45 s = strings.Replace(s, "X__val", "MKPOSTFSIDVAL", 1) 46 47 // Replace other unwanted fields with blank identifiers. 48 re = regexp.MustCompile("X_[A-Za-z0-9_]*") 49 s = re.ReplaceAllString(s, "_") 50 51 // Restore X__val in Fsid. 52 s = strings.Replace(s, "MKPOSTFSIDVAL", "X__val", 1) 53 54 // Force the type of RawSockaddr.Data to [14]int8 to match 55 // the existing gccgo API. 56 re = regexp.MustCompile("(Data\\s+\\[14\\])uint8") 57 s = re.ReplaceAllString(s, "${1}int8") 58 59 case goos == "freebsd": 60 // Keep pre-FreeBSD 10 / non-POSIX 2008 names for timespec fields 61 re := regexp.MustCompile("(A|M|C|Birth)tim\\s+Timespec") 62 s = re.ReplaceAllString(s, "${1}timespec Timespec") 63 } 64 65 // gofmt 66 b, err = format.Source([]byte(s)) 67 if err != nil { 68 log.Fatal(err) 69 } 70 71 // Append this command to the header to show where the new file 72 // came from. 73 re := regexp.MustCompile("(cgo -godefs [a-zA-Z0-9_]+\\.go.*)") 74 s = re.ReplaceAllString(string(b), "$1 | go run mkpost.go") 75 76 fmt.Print(s) 77 }