mkpost.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 by mkall.sh.
  10. package main
  11. import (
  12. "fmt"
  13. "go/format"
  14. "io/ioutil"
  15. "log"
  16. "os"
  17. "regexp"
  18. )
  19. func main() {
  20. b, err := ioutil.ReadAll(os.Stdin)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. s := string(b)
  25. goarch := os.Getenv("GOARCH")
  26. goos := os.Getenv("GOOS")
  27. if goarch == "s390x" && goos == "linux" {
  28. // Export the types of PtraceRegs fields.
  29. re := regexp.MustCompile("ptrace(Psw|Fpregs|Per)")
  30. s = re.ReplaceAllString(s, "Ptrace$1")
  31. // Replace padding fields inserted by cgo with blank identifiers.
  32. re = regexp.MustCompile("Pad_cgo[A-Za-z0-9_]*")
  33. s = re.ReplaceAllString(s, "_")
  34. // Replace other unwanted fields with blank identifiers.
  35. re = regexp.MustCompile("X_[A-Za-z0-9_]*")
  36. s = re.ReplaceAllString(s, "_")
  37. // Replace the control_regs union with a blank identifier for now.
  38. re = regexp.MustCompile("(Control_regs)\\s+\\[0\\]uint64")
  39. s = re.ReplaceAllString(s, "_ [0]uint64")
  40. }
  41. // gofmt
  42. b, err = format.Source([]byte(s))
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. // Append this command to the header to show where the new file
  47. // came from.
  48. re := regexp.MustCompile("(cgo -godefs [a-zA-Z0-9_]+\\.go.*)")
  49. b = re.ReplaceAll(b, []byte("$1 | go run mkpost.go"))
  50. fmt.Printf("%s", b)
  51. }