...

Source file src/github.com/tklauser/go-sysconf/mksysconf.go

Documentation: github.com/tklauser/go-sysconf

     1  // Copyright 2018 Tobias Klauser. 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  //go:build ignore
     6  // +build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"go/format"
    13  	"io/ioutil"
    14  	"os"
    15  	"os/exec"
    16  	"regexp"
    17  	"runtime"
    18  )
    19  
    20  func gensysconf(in, out, goos, goarch string) error {
    21  	if _, err := os.Stat(in); err != nil {
    22  		if os.IsNotExist(err) {
    23  			return nil
    24  		} else {
    25  			return err
    26  		}
    27  	}
    28  
    29  	cmd := exec.Command("go", "tool", "cgo", "-godefs", in)
    30  	defer os.RemoveAll("_obj")
    31  	b, err := cmd.CombinedOutput()
    32  	if err != nil {
    33  		fmt.Fprintln(os.Stderr, string(b))
    34  		return err
    35  	}
    36  
    37  	goBuild, build := goos, goos
    38  	if goarch != "" {
    39  		goBuild = fmt.Sprintf("%s && %s", goos, goarch)
    40  		build = fmt.Sprintf("%s,%s", goos, goarch)
    41  	}
    42  
    43  	r := fmt.Sprintf(`$1
    44  
    45  //go:build %s
    46  // +build %s`, goBuild, build)
    47  	cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
    48  	b = cgoCommandRegex.ReplaceAll(b, []byte(r))
    49  
    50  	b, err = format.Source(b)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	return ioutil.WriteFile(out, b, 0644)
    55  }
    56  
    57  func main() {
    58  	goos, goarch := runtime.GOOS, runtime.GOARCH
    59  	if goos == "illumos" {
    60  		goos = "solaris"
    61  	}
    62  	defs := fmt.Sprintf("sysconf_defs_%s.go", goos)
    63  	if err := gensysconf(defs, "z"+defs, goos, ""); err != nil {
    64  		fmt.Fprintln(os.Stderr, err)
    65  		os.Exit(1)
    66  	}
    67  
    68  	vals := fmt.Sprintf("sysconf_values_%s.go", runtime.GOOS)
    69  	// sysconf variable values are GOARCH-specific, thus write per GOARCH
    70  	zvals := fmt.Sprintf("zsysconf_values_%s_%s.go", runtime.GOOS, runtime.GOARCH)
    71  	if err := gensysconf(vals, zvals, goos, goarch); err != nil {
    72  		fmt.Fprintln(os.Stderr, err)
    73  		os.Exit(1)
    74  	}
    75  }
    76  

View as plain text