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. 13package main 14 15import ( 16 "bytes" 17 "fmt" 18 "go/format" 19 "io/ioutil" 20 "log" 21 "os" 22 "regexp" 23) 24 25func 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 // Intentionally export __val fields in Fsid and Sigset_t 54 valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`) 55 b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}")) 56 57 // Intentionally export __fds_bits field in FdSet 58 fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`) 59 b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}")) 60 61 // Intentionally export __icmp6_filt field in icmpv6_filter 62 icmpV6Regex := regexp.MustCompile(`type (ICMPv6Filter) struct {(\s+)X__icmp6_filt(\s+\S+\s+)}`) 63 b = icmpV6Regex.ReplaceAll(b, []byte("type $1 struct {${2}Filt$3}")) 64 65 // If we have empty Ptrace structs, we should delete them. Only s390x emits 66 // nonempty Ptrace structs. 67 ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`) 68 b = ptraceRexexp.ReplaceAll(b, nil) 69 70 // Replace the control_regs union with a blank identifier for now. 71 controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`) 72 b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64")) 73 74 // Remove fields that are added by glibc 75 // Note that this is unstable as the identifers are private. 76 removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`) 77 b = removeFieldsRegex.ReplaceAll(b, []byte("_")) 78 79 // Convert [65]int8 to [65]byte in Utsname members to simplify 80 // conversion to string; see golang.org/issue/20753 81 convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`) 82 b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) 83 84 // Convert [n]int8 to [n]byte in Statvfs_t members to simplify 85 // conversion to string. 86 convertStatvfsRegex := regexp.MustCompile(`((Fstype|Mnton|Mntfrom)name)(\s+)\[(\d+)\]int8`) 87 b = convertStatvfsRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) 88 89 // Convert []int8 to []byte in device mapper ioctl interface 90 convertDmIoctlNames := regexp.MustCompile(`(Name|Uuid|Target_type|Data)(\s+)\[(\d+)\]u?int8`) 91 dmIoctlTypes := regexp.MustCompile(`type Dm(\S+) struct {[^}]*}`) 92 dmStructs := dmIoctlTypes.FindAll(b, -1) 93 for _, s := range dmStructs { 94 newNames := convertDmIoctlNames.ReplaceAll(s, []byte("$1$2[$3]byte")) 95 b = bytes.Replace(b, s, newNames, 1) 96 } 97 98 // Convert []int8 to []byte in ctl_info ioctl interface 99 convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`) 100 ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`) 101 ctlInfoStructs := ctlInfoType.FindAll(b, -1) 102 for _, s := range ctlInfoStructs { 103 newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte")) 104 b = bytes.Replace(b, s, newNames, 1) 105 } 106 107 // Convert [1024]int8 to [1024]byte in Ptmget members 108 convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`) 109 b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte")) 110 111 // Remove spare fields (e.g. in Statx_t) 112 spareFieldsRegex := regexp.MustCompile(`X__spare\S*`) 113 b = spareFieldsRegex.ReplaceAll(b, []byte("_")) 114 115 // Remove cgo padding fields 116 removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`) 117 b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_")) 118 119 // Remove padding, hidden, or unused fields 120 removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`) 121 b = removeFieldsRegex.ReplaceAll(b, []byte("_")) 122 123 // Remove the first line of warning from cgo 124 b = b[bytes.IndexByte(b, '\n')+1:] 125 // Modify the command in the header to include: 126 // mkpost, our own warning, and a build tag. 127 replacement := fmt.Sprintf(`$1 | go run mkpost.go 128// Code generated by the command above; see README.md. DO NOT EDIT. 129 130// +build %s,%s`, goarch, goos) 131 cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`) 132 b = cgoCommandRegex.ReplaceAll(b, []byte(replacement)) 133 134 // Rename Stat_t time fields 135 if goos == "freebsd" && goarch == "386" { 136 // Hide Stat_t.[AMCB]tim_ext fields 137 renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`) 138 b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_")) 139 } 140 renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`) 141 b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}")) 142 143 // gofmt 144 b, err = format.Source(b) 145 if err != nil { 146 log.Fatal(err) 147 } 148 149 os.Stdout.Write(b) 150} 151