Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 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 | |
Dan Willemsen | 31b9b84 | 2021-08-31 12:51:40 -0700 | [diff] [blame] | 5 | //go:build ignore |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 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" |
Colin Cross | 846c316 | 2021-05-14 11:11:40 -0700 | [diff] [blame] | 17 | "io" |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 18 | "log" |
| 19 | "os" |
| 20 | "regexp" |
Dan Willemsen | 1d8b74d | 2017-01-19 15:06:31 -0800 | [diff] [blame] | 21 | "strings" |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func main() { |
Colin Cross | 846c316 | 2021-05-14 11:11:40 -0700 | [diff] [blame] | 25 | b, err := io.ReadAll(os.Stdin) |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 26 | if err != nil { |
| 27 | log.Fatal(err) |
| 28 | } |
| 29 | s := string(b) |
| 30 | |
| 31 | goarch := os.Getenv("GOARCH") |
| 32 | goos := os.Getenv("GOOS") |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame] | 33 | switch { |
| 34 | case goarch == "s390x" && goos == "linux": |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 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 | |
Dan Willemsen | 1d8b74d | 2017-01-19 15:06:31 -0800 | [diff] [blame] | 43 | // We want to keep X__val in Fsid. Hide it and restore it later. |
| 44 | s = strings.Replace(s, "X__val", "MKPOSTFSIDVAL", 1) |
| 45 | |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 46 | // Replace other unwanted fields with blank identifiers. |
| 47 | re = regexp.MustCompile("X_[A-Za-z0-9_]*") |
| 48 | s = re.ReplaceAllString(s, "_") |
| 49 | |
Dan Willemsen | 1d8b74d | 2017-01-19 15:06:31 -0800 | [diff] [blame] | 50 | // Restore X__val in Fsid. |
| 51 | s = strings.Replace(s, "MKPOSTFSIDVAL", "X__val", 1) |
| 52 | |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 53 | // Force the type of RawSockaddr.Data to [14]int8 to match |
| 54 | // the existing gccgo API. |
| 55 | re = regexp.MustCompile("(Data\\s+\\[14\\])uint8") |
| 56 | s = re.ReplaceAllString(s, "${1}int8") |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame] | 57 | |
| 58 | case goos == "freebsd": |
| 59 | // Keep pre-FreeBSD 10 / non-POSIX 2008 names for timespec fields |
| 60 | re := regexp.MustCompile("(A|M|C|Birth)tim\\s+Timespec") |
| 61 | s = re.ReplaceAllString(s, "${1}timespec Timespec") |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // gofmt |
| 65 | b, err = format.Source([]byte(s)) |
| 66 | if err != nil { |
| 67 | log.Fatal(err) |
| 68 | } |
| 69 | |
| 70 | // Append this command to the header to show where the new file |
| 71 | // came from. |
| 72 | re := regexp.MustCompile("(cgo -godefs [a-zA-Z0-9_]+\\.go.*)") |
| 73 | s = re.ReplaceAllString(string(b), "$1 | go run mkpost.go") |
| 74 | |
| 75 | fmt.Print(s) |
| 76 | } |