-
Notifications
You must be signed in to change notification settings - Fork 27
/
mksysconf.go
79 lines (67 loc) · 1.6 KB
/
mksysconf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import (
"fmt"
"go/format"
"os"
"os/exec"
"regexp"
"runtime"
)
func gensysconf(in, out, goos, goarch string) error {
if _, err := os.Stat(in); err != nil {
if os.IsNotExist(err) {
return nil
} else {
return err
}
}
cmd := exec.Command("go", "tool", "cgo", "-godefs", in)
defer os.RemoveAll("_obj")
b, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintln(os.Stderr, string(b))
return err
}
goBuild := goos
if goarch != "" {
goBuild = fmt.Sprintf("%s && %s", goos, goarch)
}
r := fmt.Sprintf(`$1
//go:build %s`, goBuild)
cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
b = cgoCommandRegex.ReplaceAll(b, []byte(r))
b, err = format.Source(b)
if err != nil {
return err
}
return os.WriteFile(out, b, 0644)
}
func main() {
goos := os.Getenv("GOOS_TARGET")
if goos == "" {
goos = runtime.GOOS
if goos == "illumos" {
goos = "solaris"
}
}
goarch := os.Getenv("GOARCH_TARGET")
if goarch == "" {
goarch = runtime.GOARCH
}
defs := fmt.Sprintf("sysconf_defs_%s.go", goos)
if err := gensysconf(defs, "z"+defs, goos, ""); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
vals := fmt.Sprintf("sysconf_values_%s.go", goos)
// sysconf variable values are GOARCH-specific, thus write per GOARCH
zvals := fmt.Sprintf("zsysconf_values_%s_%s.go", goos, goarch)
if err := gensysconf(vals, zvals, goos, goarch); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}