mkpost.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // +build ignore
  5. // mkpost processes the output of cgo -godefs to
  6. // modify the generated types. It is used to clean up
  7. // the sys API in an architecture specific manner.
  8. //
  9. // mkpost is run after cgo -godefs; see README.md.
  10. package main
  11. import (
  12. "bytes"
  13. "fmt"
  14. "go/format"
  15. "io/ioutil"
  16. "log"
  17. "os"
  18. "regexp"
  19. )
  20. func main() {
  21. // Get the OS and architecture (using GOARCH_TARGET if it exists)
  22. goos := os.Getenv("GOOS")
  23. goarch := os.Getenv("GOARCH_TARGET")
  24. if goarch == "" {
  25. goarch = os.Getenv("GOARCH")
  26. }
  27. // Check that we are using the new build system if we should be.
  28. if goos == "linux" && goarch != "sparc64" {
  29. if os.Getenv("GOLANG_SYS_BUILD") != "docker" {
  30. os.Stderr.WriteString("In the new build system, mkpost should not be called directly.\n")
  31. os.Stderr.WriteString("See README.md\n")
  32. os.Exit(1)
  33. }
  34. }
  35. b, err := ioutil.ReadAll(os.Stdin)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. // If we have empty Ptrace structs, we should delete them. Only s390x emits
  40. // nonempty Ptrace structs.
  41. ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`)
  42. b = ptraceRexexp.ReplaceAll(b, nil)
  43. // Replace the control_regs union with a blank identifier for now.
  44. controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`)
  45. b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64"))
  46. // Remove fields that are added by glibc
  47. // Note that this is unstable as the identifers are private.
  48. removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`)
  49. b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
  50. // Convert [65]int8 to [65]byte in Utsname members to simplify
  51. // conversion to string; see golang.org/issue/20753
  52. convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`)
  53. b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
  54. // Remove spare fields (e.g. in Statx_t)
  55. spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
  56. b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
  57. // Remove cgo padding fields
  58. removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
  59. b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
  60. // We refuse to export private fields on s390x
  61. if goarch == "s390x" && goos == "linux" {
  62. // Remove padding, hidden, or unused fields
  63. removeFieldsRegex = regexp.MustCompile(`\bX_\S+`)
  64. b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
  65. }
  66. // Remove the first line of warning from cgo
  67. b = b[bytes.IndexByte(b, '\n')+1:]
  68. // Modify the command in the header to include:
  69. // mkpost, our own warning, and a build tag.
  70. replacement := fmt.Sprintf(`$1 | go run mkpost.go
  71. // Code generated by the command above; see README.md. DO NOT EDIT.
  72. // +build %s,%s`, goarch, goos)
  73. cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
  74. b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
  75. // gofmt
  76. b, err = format.Source(b)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. os.Stdout.Write(b)
  81. }