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 EthtoolDrvinfo
99	convertEthtoolDrvinfoNames := regexp.MustCompile(`(Driver|Version|Fw_version|Bus_info|Erom_version|Reserved2)(\s+)\[(\d+)\]u?int8`)
100	ethtoolDrvinfoTypes := regexp.MustCompile(`type EthtoolDrvinfo struct {[^}]*}`)
101	ethtoolDrvinfoStructs := ethtoolDrvinfoTypes.FindAll(b, -1)
102	for _, s := range ethtoolDrvinfoStructs {
103		newNames := convertEthtoolDrvinfoNames.ReplaceAll(s, []byte("$1$2[$3]byte"))
104		b = bytes.Replace(b, s, newNames, 1)
105	}
106
107	// Convert []int8 to []byte in ctl_info ioctl interface
108	convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`)
109	ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`)
110	ctlInfoStructs := ctlInfoType.FindAll(b, -1)
111	for _, s := range ctlInfoStructs {
112		newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte"))
113		b = bytes.Replace(b, s, newNames, 1)
114	}
115
116	// Convert [1024]int8 to [1024]byte in Ptmget members
117	convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`)
118	b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte"))
119
120	// Remove spare fields (e.g. in Statx_t)
121	spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
122	b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
123
124	// Remove cgo padding fields
125	removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
126	b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
127
128	// Remove padding, hidden, or unused fields
129	removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`)
130	b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
131
132	// Remove the first line of warning from cgo
133	b = b[bytes.IndexByte(b, '\n')+1:]
134	// Modify the command in the header to include:
135	//  mkpost, our own warning, and a build tag.
136	replacement := fmt.Sprintf(`$1 | go run mkpost.go
137// Code generated by the command above; see README.md. DO NOT EDIT.
138
139//go:build %s && %s
140// +build %s,%s`, goarch, goos, goarch, goos)
141	cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
142	b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
143
144	// Rename Stat_t time fields
145	if goos == "freebsd" && goarch == "386" {
146		// Hide Stat_t.[AMCB]tim_ext fields
147		renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`)
148		b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_"))
149	}
150	renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`)
151	b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}"))
152
153	// gofmt
154	b, err = format.Source(b)
155	if err != nil {
156		log.Fatal(err)
157	}
158
159	os.Stdout.Write(b)
160}
161