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 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"
18 "log"
19 "os"
20 "regexp"
21 "bytes"
22 )
23 24 func main() {
25 b, err := io.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 switch {
34 case goarch == "s390x" && goos == "linux":
35 // Export the types of PtraceRegs fields.
36 re := regexp.MustCompile("ptrace(Psw|Fpregs|Per)")
37 s = re.ReplaceAllString(s, "Ptrace$1")
38 39 // Replace padding fields inserted by cgo with blank identifiers.
40 re = regexp.MustCompile("Pad_cgo[A-Za-z0-9_]*")
41 s = re.ReplaceAllString(s, "_")
42 43 // We want to keep the X_ fields that are already consistently exported
44 // for the other linux GOARCH settings.
45 // Hide them and restore later.
46 s = bytes.Replace(s, "X__val", "MKPOSTFSIDVAL", 1)
47 s = bytes.Replace(s, "X__ifi_pad", "MKPOSTIFIPAD", 1)
48 s = bytes.Replace(s, "X_f", "MKPOSTSYSINFOTF", 1)
49 50 // Replace other unwanted fields with blank identifiers.
51 re = regexp.MustCompile("X_[A-Za-z0-9_]*")
52 s = re.ReplaceAllString(s, "_")
53 54 // Restore preserved fields.
55 s = bytes.Replace(s, "MKPOSTFSIDVAL", "X__val", 1)
56 s = bytes.Replace(s, "MKPOSTIFIPAD", "X__ifi_pad", 1)
57 s = bytes.Replace(s, "MKPOSTSYSINFOTF", "X_f", 1)
58 59 // Force the type of RawSockaddr.Data to [14]int8 to match
60 // the existing gccgo API.
61 re = regexp.MustCompile("(Data\\s+\\[14\\])uint8")
62 s = re.ReplaceAllString(s, "${1}int8")
63 64 case goos == "freebsd":
65 // Keep pre-FreeBSD 10 / non-POSIX 2008 names for timespec fields
66 re := regexp.MustCompile("(A|M|C|Birth)tim\\s+Timespec")
67 s = re.ReplaceAllString(s, "${1}timespec Timespec")
68 }
69 70 // gofmt
71 b, err = format.Source([]byte(s))
72 if err != nil {
73 log.Fatal(err)
74 }
75 76 // Append this command to the header to show where the new file
77 // came from.
78 re := regexp.MustCompile("(cgo -godefs [a-zA-Z0-9_]+\\.go.*)")
79 s = re.ReplaceAllString(string(b), "$1 | go run mkpost.go")
80 81 fmt.Print(s)
82 }
83