github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/syscall/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 syscall 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  		// Force the type of RawSockaddr.Data to [14]int8 to match
    46  		// the existing gccgo API.
    47  		re = regexp.MustCompile("(Data\\s+\\[14\\])uint8")
    48  		s = re.ReplaceAllString(s, "${1}int8")
    49  	}
    50  
    51  	// gofmt
    52  	b, err = format.Source([]byte(s))
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	// Append this command to the header to show where the new file
    58  	// came from.
    59  	re := regexp.MustCompile("(cgo -godefs [a-zA-Z0-9_]+\\.go.*)")
    60  	s = re.ReplaceAllString(string(b), "$1 | go run mkpost.go")
    61  
    62  	fmt.Print(s)
    63  }