1
2
3
4
5
6
7
8
9
10
11
12 package main
13
14 import (
15 "bytes"
16 "fmt"
17 "go/format"
18 "io"
19 "log"
20 "os"
21 "regexp"
22 )
23
24 func main() {
25
26 goos := os.Getenv("GOOS_TARGET")
27 if goos == "" {
28 goos = os.Getenv("GOOS")
29 }
30 goarch := os.Getenv("GOARCH_TARGET")
31 if goarch == "" {
32 goarch = os.Getenv("GOARCH")
33 }
34
35 if goos == "linux" {
36 if os.Getenv("GOLANG_SYS_BUILD") != "docker" {
37 os.Stderr.WriteString("In the Docker-based build system, mkpost should not be called directly.\n")
38 os.Stderr.WriteString("See README.md\n")
39 os.Exit(1)
40 }
41 }
42
43 b, err := io.ReadAll(os.Stdin)
44 if err != nil {
45 log.Fatal(err)
46 }
47
48 if goos == "aix" {
49
50
51 sttimespec := regexp.MustCompile(`_Ctype_struct_st_timespec`)
52 b = sttimespec.ReplaceAll(b, []byte("Timespec"))
53 }
54
55 if goos == "darwin" {
56
57
58
59 kinfoProcPointerRegex := regexp.MustCompile(`\*_Ctype_struct_(pgrp|proc|session|sigacts|ucred|user|vnode)`)
60 b = kinfoProcPointerRegex.ReplaceAll(b, []byte("uintptr"))
61
62
63
64
65
66 externProcStarttimeRegex := regexp.MustCompile(`P_un\s*\[\d+\]byte`)
67 b = externProcStarttimeRegex.ReplaceAll(b, []byte("P_starttime Timeval"))
68
69
70
71 convertEprocRegex := regexp.MustCompile(`(P_comm|Wmesg|Login)(\s+)\[(\d+)\]int8`)
72 b = convertEprocRegex.ReplaceAll(b, []byte("$1$2[$3]byte"))
73 }
74
75 if goos == "freebsd" {
76
77
78 ptraceLwpInfoStruct := regexp.MustCompile(`(?s:type PtraceLwpInfoStruct struct \{.*?\})`)
79 b = ptraceLwpInfoStruct.ReplaceAllFunc(b, func(in []byte) []byte {
80 return bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo"))
81 })
82
83 siginfoStruct := regexp.MustCompile(`(?s:type __Siginfo struct \{.*?\})`)
84 b = siginfoStruct.ReplaceAllFunc(b, func(in []byte) []byte {
85 out := append([]byte{}, in...)
86 out = append(out, '\n', '\n')
87 out = append(out,
88 bytes.ReplaceAll(
89 bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo")),
90 []byte("*byte"), []byte("uintptr"))...)
91 return out
92 })
93
94
95
96 ptraceIoDescStruct := regexp.MustCompile(`(?s:type PtraceIoDesc struct \{.*?\})`)
97 addrField := regexp.MustCompile(`(\bOffs\s+)\*byte`)
98 b = ptraceIoDescStruct.ReplaceAllFunc(b, func(in []byte) []byte {
99 return addrField.ReplaceAll(in, []byte(`${1}uintptr`))
100 })
101 }
102
103 if goos == "solaris" {
104
105 convertIovecBase := regexp.MustCompile(`Base\s+\*int8`)
106 iovecType := regexp.MustCompile(`type Iovec struct {[^}]*}`)
107 iovecStructs := iovecType.FindAll(b, -1)
108 for _, s := range iovecStructs {
109 newNames := convertIovecBase.ReplaceAll(s, []byte("Base *byte"))
110 b = bytes.Replace(b, s, newNames, 1)
111 }
112 }
113
114 if goos == "linux" && goarch != "riscv64" {
115
116 hwprobeConstRexexp := regexp.MustCompile(`const\s+\(\s+RISCV_HWPROBE_[^\)]+\)`)
117 b = hwprobeConstRexexp.ReplaceAll(b, nil)
118 }
119
120
121 valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`)
122 b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}"))
123
124
125 fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`)
126 b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}"))
127
128
129 icmpV6Regex := regexp.MustCompile(`type (ICMPv6Filter) struct {(\s+)X__icmp6_filt(\s+\S+\s+)}`)
130 b = icmpV6Regex.ReplaceAll(b, []byte("type $1 struct {${2}Filt$3}"))
131
132
133 convertSockaddrStorageData := regexp.MustCompile(`(X__ss_padding)\s+\[(\d+)\]u?int8`)
134 sockaddrStorageType := regexp.MustCompile(`type SockaddrStorage struct {[^}]*}`)
135 sockaddrStorageStructs := sockaddrStorageType.FindAll(b, -1)
136 for _, s := range sockaddrStorageStructs {
137 newNames := convertSockaddrStorageData.ReplaceAll(s, []byte("Data [$2]byte"))
138 b = bytes.Replace(b, s, newNames, 1)
139 }
140
141
142
143 ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`)
144 b = ptraceRexexp.ReplaceAll(b, nil)
145
146
147
148 hwprobeRexexp := regexp.MustCompile(`type RISCVHWProbePairs struct {\s*}`)
149 b = hwprobeRexexp.ReplaceAll(b, nil)
150
151
152 controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`)
153 b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64"))
154
155
156
157 removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`)
158 b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
159
160
161
162 convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`)
163 b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
164
165
166
167 convertStatvfsRegex := regexp.MustCompile(`(([Ff]stype|[Mm]nton|[Mm]ntfrom)name|mntfromspec)(\s+)\[(\d+)\]int8`)
168 b = convertStatvfsRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
169
170
171 convertDmIoctlNames := regexp.MustCompile(`(Name|Uuid|Target_type|Data)(\s+)\[(\d+)\]u?int8`)
172 dmIoctlTypes := regexp.MustCompile(`type Dm(\S+) struct {[^}]*}`)
173 dmStructs := dmIoctlTypes.FindAll(b, -1)
174 for _, s := range dmStructs {
175 newNames := convertDmIoctlNames.ReplaceAll(s, []byte("$1$2[$3]byte"))
176 b = bytes.Replace(b, s, newNames, 1)
177 }
178
179
180 convertEthtoolDrvinfoNames := regexp.MustCompile(`(Driver|Version|Fw_version|Bus_info|Erom_version|Reserved2)(\s+)\[(\d+)\]u?int8`)
181 ethtoolDrvinfoTypes := regexp.MustCompile(`type EthtoolDrvinfo struct {[^}]*}`)
182 ethtoolDrvinfoStructs := ethtoolDrvinfoTypes.FindAll(b, -1)
183 for _, s := range ethtoolDrvinfoStructs {
184 newNames := convertEthtoolDrvinfoNames.ReplaceAll(s, []byte("$1$2[$3]byte"))
185 b = bytes.Replace(b, s, newNames, 1)
186 }
187
188
189 convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`)
190 ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`)
191 ctlInfoStructs := ctlInfoType.FindAll(b, -1)
192 for _, s := range ctlInfoStructs {
193 newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte"))
194 b = bytes.Replace(b, s, newNames, 1)
195 }
196
197
198 convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`)
199 b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte"))
200
201
202 spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
203 b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
204
205
206 removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
207 b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
208
209
210 removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`)
211 b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
212
213
214 b = b[bytes.IndexByte(b, '\n')+1:]
215
216
217 replacement := fmt.Sprintf(`$1 | go run mkpost.go
218 // Code generated by the command above; see README.md. DO NOT EDIT.
219
220 //go:build %s && %s`, goarch, goos)
221 cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
222 b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
223
224
225 if goos == "freebsd" && goarch == "386" {
226
227 renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`)
228 b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_"))
229 }
230 renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`)
231 b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}"))
232
233
234 b, err = format.Source(b)
235 if err != nil {
236 log.Fatal(err)
237 }
238
239 os.Stdout.Write(b)
240 }
241
View as plain text