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