mkpost.go 5.4 KB

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