From dcccaf793887ea81d7c0d1637393dbcacaa73cdc Mon Sep 17 00:00:00 2001 From: Sergey Yarmonov Date: Sat, 4 May 2019 18:42:06 +0700 Subject: [PATCH] Fix compilation for old versions of go --- .travis.yml | 3 +- compilation_test.go | 80 ++++++++++++++++++++++++++++++++++++++++++++ syscall_dup.go | 1 + syscall_dup_pre17.go | 13 +++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 compilation_test.go create mode 100644 syscall_dup_pre17.go diff --git a/.travis.yml b/.travis.yml index aed4e4c..03d4305 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,13 +2,14 @@ language: go go: - 1.3 + - 1.5 - tip before_install: - go get -t -v ./... script: - - go test -coverprofile=coverage.txt -covermode=atomic + - go test -v -coverprofile=coverage.txt -covermode=atomic after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/compilation_test.go b/compilation_test.go new file mode 100644 index 0000000..5a2da1e --- /dev/null +++ b/compilation_test.go @@ -0,0 +1,80 @@ +package daemon + +import ( + "os" + "os/exec" + "runtime" + "strconv" + "strings" + "testing" +) + +func TestCompilation(t *testing.T) { + if testing.Short() { + t.Skip("short mode") + } + if !requireMinor(5) { + t.Skip(runtime.Version(), "cross-compilation requires compiler bootstrapping") + } + + pairs := []string{ + "darwin/386", + "darwin/amd64", + "dragonfly/amd64", + "freebsd/386", + "freebsd/amd64", + "freebsd/arm", + "linux/386", + "linux/amd64", + "linux/arm", + "linux/arm64", + "netbsd/386", + "netbsd/amd64", + "netbsd/arm", + "openbsd/386", + "openbsd/amd64", + "openbsd/arm", + "solaris/amd64", + "windows/386", + "windows/amd64", + + // TODO(yar): support plan9 + //"plan9/386", + //"plan9/amd64", + //"plan9/arm", + } + + env := os.Environ() + for i := range pairs { + p := pairs[i] + pair := strings.Split(p, "/") + goos, goarch := pair[0], pair[1] + if goos == "solaris" && !requireMinor(7) { + t.Log("skip, solaris requires at least go1.7") + continue + } + cmd := exec.Command("go", "build", "./") + env := append([]string(nil), env...) + cmd.Env = append(env, "GOOS="+goos, "GOARCH="+goarch) + out, err := cmd.CombinedOutput() + if len(out) > 0 { + t.Log(p, "\n", string(out)) + } + if err != nil { + t.Error(p, err) + } + } +} + +func requireMinor(minor int) bool { + str := runtime.Version() + if !strings.HasPrefix(str, "go1.") { + return true + } + str = strings.TrimPrefix(str, "go1.") + ver, err := strconv.Atoi(str) + if err != nil { + return false + } + return ver >= minor +} diff --git a/syscall_dup.go b/syscall_dup.go index 24beecc..fbf1b4f 100644 --- a/syscall_dup.go +++ b/syscall_dup.go @@ -1,5 +1,6 @@ // +build !linux !arm64 // +build !windows +// +build go1.7 package daemon diff --git a/syscall_dup_pre17.go b/syscall_dup_pre17.go new file mode 100644 index 0000000..b95621b --- /dev/null +++ b/syscall_dup_pre17.go @@ -0,0 +1,13 @@ +// +build !linux !arm64 +// +build !windows +// +build !go1.7 + +package daemon + +import ( + "syscall" +) + +func syscallDup(oldfd int, newfd int) (err error) { + return syscall.Dup2(oldfd, newfd) +}