github.com/Kalvelign/golang-windows-sys-lib@v0.0.0-20221121121202-63da651435e1/unix/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 sys API in an architecture specific manner. 11 // 12 // mkpost is run after cgo -godefs; see README.md. 13 package main 14 15 import ( 16 "bytes" 17 "fmt" 18 "go/format" 19 "io/ioutil" 20 "log" 21 "os" 22 "regexp" 23 ) 24 25 func main() { 26 // Get the OS and architecture (using GOARCH_TARGET if it exists) 27 goos := os.Getenv("GOOS") 28 goarch := os.Getenv("GOARCH_TARGET") 29 if goarch == "" { 30 goarch = os.Getenv("GOARCH") 31 } 32 // Check that we are using the Docker-based build system if we should be. 33 if goos == "linux" { 34 if os.Getenv("GOLANG_SYS_BUILD") != "docker" { 35 os.Stderr.WriteString("In the Docker-based build system, mkpost should not be called directly.\n") 36 os.Stderr.WriteString("See README.md\n") 37 os.Exit(1) 38 } 39 } 40 41 b, err := ioutil.ReadAll(os.Stdin) 42 if err != nil { 43 log.Fatal(err) 44 } 45 46 if goos == "aix" { 47 // Replace type of Atim, Mtim and Ctim by Timespec in Stat_t 48 // to avoid having both StTimespec and Timespec. 49 sttimespec := regexp.MustCompile(`_Ctype_struct_st_timespec`) 50 b = sttimespec.ReplaceAll(b, []byte("Timespec")) 51 } 52 53 if goos == "darwin" { 54 // KinfoProc contains various pointers to objects stored 55 // in kernel space. Replace these by uintptr to prevent 56 // accidental dereferencing. 57 kinfoProcPointerRegex := regexp.MustCompile(`\*_Ctype_struct_(pgrp|proc|session|sigacts|ucred|user|vnode)`) 58 b = kinfoProcPointerRegex.ReplaceAll(b, []byte("uintptr")) 59 60 // ExternProc contains a p_un member that in kernel 61 // space stores a pair of pointers and in user space 62 // stores the process creation time. We only care about 63 // the process creation time. 64 externProcStarttimeRegex := regexp.MustCompile(`P_un\s*\[\d+\]byte`) 65 b = externProcStarttimeRegex.ReplaceAll(b, []byte("P_starttime Timeval")) 66 67 // Convert [n]int8 to [n]byte in Eproc and ExternProc members to 68 // simplify conversion to string. 69 convertEprocRegex := regexp.MustCompile(`(P_comm|Wmesg|Login)(\s+)\[(\d+)\]int8`) 70 b = convertEprocRegex.ReplaceAll(b, []byte("$1$2[$3]byte")) 71 } 72 73 if goos == "freebsd" { 74 // Inside PtraceLwpInfoStruct replace __Siginfo with __PtraceSiginfo, 75 // Create __PtraceSiginfo as a copy of __Siginfo where every *byte instance is replaced by uintptr 76 ptraceLwpInfoStruct := regexp.MustCompile(`(?s:type PtraceLwpInfoStruct struct \{.*?\})`) 77 b = ptraceLwpInfoStruct.ReplaceAllFunc(b, func(in []byte) []byte { 78 return bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo")) 79 }) 80 81 siginfoStruct := regexp.MustCompile(`(?s:type __Siginfo struct \{.*?\})`) 82 b = siginfoStruct.ReplaceAllFunc(b, func(in []byte) []byte { 83 out := append([]byte{}, in...) 84 out = append(out, '\n', '\n') 85 out = append(out, 86 bytes.ReplaceAll( 87 bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo")), 88 []byte("*byte"), []byte("uintptr"))...) 89 return out 90 }) 91 92 // Inside PtraceIoDesc replace all *byte with uintptr 93 ptraceIoDescStruct := regexp.MustCompile(`(?s:type PtraceIoDesc struct \{.*?\})`) 94 b = ptraceIoDescStruct.ReplaceAllFunc(b, func(in []byte) []byte { 95 return bytes.ReplaceAll(in, []byte("*byte"), []byte("uintptr")) 96 }) 97 } 98 99 // Intentionally export __val fields in Fsid and Sigset_t 100 valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`) 101 b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}")) 102 103 // Intentionally export __fds_bits field in FdSet 104 fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`) 105 b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}")) 106 107 // Intentionally export __icmp6_filt field in icmpv6_filter 108 icmpV6Regex := regexp.MustCompile(`type (ICMPv6Filter) struct {(\s+)X__icmp6_filt(\s+\S+\s+)}`) 109 b = icmpV6Regex.ReplaceAll(b, []byte("type $1 struct {${2}Filt$3}")) 110 111 // If we have empty Ptrace structs, we should delete them. Only s390x emits 112 // nonempty Ptrace structs. 113 ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`) 114 b = ptraceRexexp.ReplaceAll(b, nil) 115 116 // Replace the control_regs union with a blank identifier for now. 117 controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`) 118 b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64")) 119 120 // Remove fields that are added by glibc 121 // Note that this is unstable as the identifers are private. 122 removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`) 123 b = removeFieldsRegex.ReplaceAll(b, []byte("_")) 124 125 // Convert [65]int8 to [65]byte in Utsname members to simplify 126 // conversion to string; see golang.org/issue/20753 127 convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`) 128 b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) 129 130 // Convert [n]int8 to [n]byte in Statvfs_t and Statfs_t members to simplify 131 // conversion to string. 132 convertStatvfsRegex := regexp.MustCompile(`(([Ff]stype|[Mm]nton|[Mm]ntfrom)name|mntfromspec)(\s+)\[(\d+)\]int8`) 133 b = convertStatvfsRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) 134 135 // Convert []int8 to []byte in device mapper ioctl interface 136 convertDmIoctlNames := regexp.MustCompile(`(Name|Uuid|Target_type|Data)(\s+)\[(\d+)\]u?int8`) 137 dmIoctlTypes := regexp.MustCompile(`type Dm(\S+) struct {[^}]*}`) 138 dmStructs := dmIoctlTypes.FindAll(b, -1) 139 for _, s := range dmStructs { 140 newNames := convertDmIoctlNames.ReplaceAll(s, []byte("$1$2[$3]byte")) 141 b = bytes.Replace(b, s, newNames, 1) 142 } 143 144 // Convert []int8 to []byte in EthtoolDrvinfo 145 convertEthtoolDrvinfoNames := regexp.MustCompile(`(Driver|Version|Fw_version|Bus_info|Erom_version|Reserved2)(\s+)\[(\d+)\]u?int8`) 146 ethtoolDrvinfoTypes := regexp.MustCompile(`type EthtoolDrvinfo struct {[^}]*}`) 147 ethtoolDrvinfoStructs := ethtoolDrvinfoTypes.FindAll(b, -1) 148 for _, s := range ethtoolDrvinfoStructs { 149 newNames := convertEthtoolDrvinfoNames.ReplaceAll(s, []byte("$1$2[$3]byte")) 150 b = bytes.Replace(b, s, newNames, 1) 151 } 152 153 // Convert []int8 to []byte in ctl_info ioctl interface 154 convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`) 155 ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`) 156 ctlInfoStructs := ctlInfoType.FindAll(b, -1) 157 for _, s := range ctlInfoStructs { 158 newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte")) 159 b = bytes.Replace(b, s, newNames, 1) 160 } 161 162 // Convert [1024]int8 to [1024]byte in Ptmget members 163 convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`) 164 b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte")) 165 166 // Remove spare fields (e.g. in Statx_t) 167 spareFieldsRegex := regexp.MustCompile(`X__spare\S*`) 168 b = spareFieldsRegex.ReplaceAll(b, []byte("_")) 169 170 // Remove cgo padding fields 171 removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`) 172 b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_")) 173 174 // Remove padding, hidden, or unused fields 175 removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`) 176 b = removeFieldsRegex.ReplaceAll(b, []byte("_")) 177 178 // Remove the first line of warning from cgo 179 b = b[bytes.IndexByte(b, '\n')+1:] 180 // Modify the command in the header to include: 181 // mkpost, our own warning, and a build tag. 182 replacement := fmt.Sprintf(`$1 | go run mkpost.go 183 // Code generated by the command above; see README.md. DO NOT EDIT. 184 185 //go:build %s && %s 186 // +build %s,%s`, goarch, goos, goarch, goos) 187 cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`) 188 b = cgoCommandRegex.ReplaceAll(b, []byte(replacement)) 189 190 // Rename Stat_t time fields 191 if goos == "freebsd" && goarch == "386" { 192 // Hide Stat_t.[AMCB]tim_ext fields 193 renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`) 194 b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_")) 195 } 196 renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`) 197 b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}")) 198 199 // gofmt 200 b, err = format.Source(b) 201 if err != nil { 202 log.Fatal(err) 203 } 204 205 os.Stdout.Write(b) 206 }