Skip to content

Commit

Permalink
Add build tags to all generated files
Browse files Browse the repository at this point in the history
Allow to build this package with older Go versions which don't know
about certain GOOS/GOARCH combinations.

Fixes #26
  • Loading branch information
tklauser committed Aug 30, 2021
1 parent 7a5cfaa commit 8886e70
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 8 deletions.
28 changes: 20 additions & 8 deletions mksysconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
)

func gensysconf(in, out string) error {
func gensysconf(in, out, goos, goarch string) error {
if _, err := os.Stat(in); err != nil {
if os.IsNotExist(err) {
return nil
Expand All @@ -32,31 +33,42 @@ func gensysconf(in, out string) error {
fmt.Fprintln(os.Stderr, string(b))
return err
}

goBuild, build := goos, goos
if goarch != "" {
goBuild = fmt.Sprintf("%s && %s", goos, goarch)
build = fmt.Sprintf("%s,%s", goos, goarch)
}

r := fmt.Sprintf(`$1
//go:build %s
// +build %s`, goBuild, build)
cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
b = cgoCommandRegex.ReplaceAll(b, []byte(r))

b, err = format.Source(b)
if err != nil {
return err
}
if err := ioutil.WriteFile(out, b, 0644); err != nil {
return err
}
return nil
return ioutil.WriteFile(out, b, 0644)
}

func main() {
goos := runtime.GOOS
goos, goarch := runtime.GOOS, runtime.GOARCH
if goos == "illumos" {
goos = "solaris"
}
defs := fmt.Sprintf("sysconf_defs_%s.go", goos)
if err := gensysconf(defs, "z"+defs); err != nil {
if err := gensysconf(defs, "z"+defs, goos, ""); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

vals := fmt.Sprintf("sysconf_values_%s.go", runtime.GOOS)
// sysconf variable values are GOARCH-specific, thus write per GOARCH
zvals := fmt.Sprintf("zsysconf_values_%s_%s.go", runtime.GOOS, runtime.GOARCH)
if err := gensysconf(vals, zvals); err != nil {
if err := gensysconf(vals, zvals, goos, goarch); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down
3 changes: 3 additions & 0 deletions zsysconf_defs_darwin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_defs_dragonfly.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_defs_freebsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_defs_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_defs_netbsd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs sysconf_defs_netbsd.go

//go:build netbsd
// +build netbsd

package sysconf

const (
Expand Down
3 changes: 3 additions & 0 deletions zsysconf_defs_openbsd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs sysconf_defs_openbsd.go

//go:build openbsd
// +build openbsd

package sysconf

const (
Expand Down
3 changes: 3 additions & 0 deletions zsysconf_defs_solaris.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_freebsd_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_freebsd_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_freebsd_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_freebsd_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_mips.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_mips64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_mips64le.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_mipsle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_ppc64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_ppc64le.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_riscv64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zsysconf_values_linux_s390x.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8886e70

Please sign in to comment.