github.com/orangeji11/golang-sys-sw64@v0.0.0-20221228054527-b72799809e00/unix/mkpost_sw64.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; see README.md.
    12  package main
    13  
    14  import (
    15  	"bytes"
    16  	"fmt"
    17  	"go/format"
    18  	"io/ioutil"
    19  	"log"
    20  	"os"
    21  	"regexp"
    22  )
    23  
    24  func main() {
    25  	// Get the OS and architecture (using GOARCH_TARGET if it exists)
    26  	goos := "linux"
    27  	goarch := "sw64"
    28  	b, err := ioutil.ReadAll(os.Stdin)
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  
    33  	// Intentionally export __val fields in Fsid and Sigset_t
    34  	valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__val(\s+\S+\s+)}`)
    35  	b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$3}"))
    36  
    37  	// Intentionally export __fds_bits field in FdSet
    38  	fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`)
    39  	b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}"))
    40  
    41  	// If we have empty Ptrace structs, we should delete them. Only s390x emits
    42  	// nonempty Ptrace structs.
    43  	ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`)
    44  	b = ptraceRexexp.ReplaceAll(b, nil)
    45  
    46  	// Replace the control_regs union with a blank identifier for now.
    47  	controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`)
    48  	b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64"))
    49  
    50  	// Remove fields that are added by glibc
    51  	// Note that this is unstable as the identifers are private.
    52  	removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`)
    53  	b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
    54  
    55  	// Convert [65]int8 to [65]byte in Utsname members to simplify
    56  	// conversion to string; see golang.org/issue/20753
    57  	convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`)
    58  	b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
    59  
    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  
    64  	// Remove spare fields (e.g. in Statx_t)
    65  	spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
    66  	b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
    67  
    68  	// Remove cgo padding fields
    69  	removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
    70  	b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
    71  
    72  	// Remove padding, hidden, or unused fields
    73  	removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`)
    74  	b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
    75  
    76  	// Remove the first line of warning from cgo
    77  	b = b[bytes.IndexByte(b, '\n')+1:]
    78  	// Modify the command in the header to include:
    79  	//  mkpost, our own warning, and a build tag.
    80  	replacement := fmt.Sprintf(`$1 | go run mkpost.go
    81  // Code generated by the command above; see README.md. DO NOT EDIT.
    82  
    83  // +build %s,%s`, goarch, goos)
    84  	cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
    85  	b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
    86  
    87  	// gofmt
    88  	b, err = format.Source(b)
    89  	if err != nil {
    90  		log.Fatal(err)
    91  	}
    92  
    93  	os.Stdout.Write(b)
    94  }