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.
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.
11 // mkpost is run after cgo -godefs; see README.md.
25 // Get the OS and architecture (using GOARCH_TARGET if it exists)
26 goos := os.Getenv("GOOS")
27 goarch := os.Getenv("GOARCH_TARGET")
29 goarch = os.Getenv("GOARCH")
31 // Check that we are using the new build system if we should be.
32 if goos == "linux" && goarch != "sparc64" {
33 if os.Getenv("GOLANG_SYS_BUILD") != "docker" {
34 os.Stderr.WriteString("In the new build system, mkpost should not be called directly.\n")
35 os.Stderr.WriteString("See README.md\n")
40 b, err := ioutil.ReadAll(os.Stdin)
45 // If we have empty Ptrace structs, we should delete them. Only s390x emits
46 // nonempty Ptrace structs.
47 ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`)
48 b = ptraceRexexp.ReplaceAll(b, nil)
50 // Replace the control_regs union with a blank identifier for now.
51 controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`)
52 b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64"))
54 // Remove fields that are added by glibc
55 // Note that this is unstable as the identifers are private.
56 removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`)
57 b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
59 // We refuse to export private fields on s390x
60 if goarch == "s390x" && goos == "linux" {
61 // Remove cgo padding fields
62 removeFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
63 b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
65 // Remove padding, hidden, or unused fields
66 removeFieldsRegex = regexp.MustCompile(`X_\S+`)
67 b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
70 // Remove the first line of warning from cgo
71 b = b[bytes.IndexByte(b, '\n')+1:]
72 // Modify the command in the header to include:
73 // mkpost, our own warning, and a build tag.
74 replacement := fmt.Sprintf(`$1 | go run mkpost.go
75 // Code generated by the command above; see README.md. DO NOT EDIT.
77 // +build %s,%s`, goarch, goos)
78 cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
79 b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
82 b, err = format.Source(b)