另客网go项目公用的代码库

mkpost.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Intentionally export __val fields in Fsid and Sigset_t
  40. valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__val(\s+\S+\s+)}`)
  41. b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$3}"))
  42. // Intentionally export __fds_bits field in FdSet
  43. fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`)
  44. b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}"))
  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)
  49. // Replace the control_regs union with a blank identifier for now.
  50. controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`)
  51. b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64"))
  52. // Remove fields that are added by glibc
  53. // Note that this is unstable as the identifers are private.
  54. removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`)
  55. b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
  56. // Convert [65]int8 to [65]byte in Utsname members to simplify
  57. // conversion to string; see golang.org/issue/20753
  58. convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`)
  59. b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
  60. // Convert [1024]int8 to [1024]byte in Ptmget members
  61. convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`)
  62. b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte"))
  63. // Remove spare fields (e.g. in Statx_t)
  64. spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
  65. b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
  66. // Remove cgo padding fields
  67. removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
  68. b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
  69. // Remove padding, hidden, or unused fields
  70. removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`)
  71. b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
  72. // Remove the first line of warning from cgo
  73. b = b[bytes.IndexByte(b, '\n')+1:]
  74. // Modify the command in the header to include:
  75. // mkpost, our own warning, and a build tag.
  76. replacement := fmt.Sprintf(`$1 | go run mkpost.go
  77. // Code generated by the command above; see README.md. DO NOT EDIT.
  78. // +build %s,%s`, goarch, goos)
  79. cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
  80. b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
  81. // gofmt
  82. b, err = format.Source(b)
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. os.Stdout.Write(b)
  87. }