github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/_vendor/src/golang.org/x/sys/unix/mkpost.go (about)

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