From ed68885143b335bebc789fa86fb6dc381bfea9ea Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Sat, 25 Jun 2016 20:27:24 +0300 Subject: [PATCH] add Go plugin --- plugins/go/README.md | 34 ++++++++++ plugins/go/go-1-fixes.patch | 108 ++++++++++++++++++++++++++++++++ plugins/go/go-native.mk | 31 +++++++++ plugins/go/go.mk | 59 +++++++++++++++++ plugins/go/src/mxe-test/main.go | 7 +++ 5 files changed, 239 insertions(+) create mode 100644 plugins/go/README.md create mode 100644 plugins/go/go-1-fixes.patch create mode 100644 plugins/go/go-native.mk create mode 100644 plugins/go/go.mk create mode 100644 plugins/go/src/mxe-test/main.go diff --git a/plugins/go/README.md b/plugins/go/README.md new file mode 100644 index 00000000..60c3bfd3 --- /dev/null +++ b/plugins/go/README.md @@ -0,0 +1,34 @@ +# Go plugin for MXE + +See also article [cross-compile go code, including cgo][1] +by Dimitri John Ledkov. + +[1]: http://blog.surgut.co.uk/2014/06/cross-compile-go-code-including-cgo.html + +Package `go-native` installs native Go 1.4. This version of Go +doesn't depend on Go installation. + +Package `go` uses native Go 1.4 as a bootstrap and installs Go 1.6 +as a cross-compiler to windows/386 or windows/amd64. Versions of +Go starting with 1.5 need Go installation to build. + +To build Go packages for windows/386 or windows/amd64, you have to set +the [GOPATH](https://golang.org/doc/code.html#GOPATH) environment variable +and call `usr/bin/$(TARGET)-go` wrapper script. + +## Example + +Building [gohs](https://github.com/flier/gohs), GoLang Binding of +[HyperScan](https://01.org/hyperscan). + +``` +$ MXE_PLUGIN_DIRS=plugins/go make hyperscan go +$ mkdir gopath +$ GOPATH=`pwd`/gopath ./usr/bin/i686-w64-mingw32.static-go get \ + github.com/flier/gohs/examples/simplegrep +$ ./gopath/bin/windows_386/simplegrep.exe root /etc/passwd +Scanning 42 bytes with Hyperscan +root:x:0:0:root:/root:/bin/bash +root:x:0:0:root:/root:/bin/bash +root:x:0:0:root:/root:/bin/bash +``` diff --git a/plugins/go/go-1-fixes.patch b/plugins/go/go-1-fixes.patch new file mode 100644 index 00000000..27281c76 --- /dev/null +++ b/plugins/go/go-1-fixes.patch @@ -0,0 +1,108 @@ +This file is part of MXE. +See index.html for further information. + +Contains ad hoc patches for cross building. + +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Boris Nagaev +Date: Sat, 25 Jun 2016 13:51:06 +0200 +Subject: [PATCH] cgo: add environmental variable override for pkg-config + +Allow overriding default name of `pkg-config` utility via +environmental variable PKG_CONFIG (same as used by autoconf +pkg.m4 macros). This facilitates easy cross-compilation of cgo +code. + +Original patch against Go <= 1.4 was written by +xnox_canonical in 2014. +Source: https://codereview.appspot.com/104960043/ + +Rebased against Go 1.6.2 by Boris Nagaev . + +diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go +index 1111111..2222222 100644 +--- a/src/cmd/dist/build.go ++++ b/src/cmd/dist/build.go +@@ -41,6 +41,7 @@ var ( + defaultldflags string + defaultcxxtarget string + defaultcctarget string ++ defaultpkgconfigtarget string + rebuildall bool + defaultclang bool + +@@ -203,6 +204,12 @@ func xinit() { + } + defaultcxxtarget = b + ++ b = os.Getenv("PKG_CONFIG") ++ if b == "" { ++ b = "pkg-config" ++ } ++ defaultpkgconfigtarget = b ++ + // For tools being invoked but also for os.ExpandEnv. + os.Setenv("GO386", go386) + os.Setenv("GOARCH", goarch) +diff --git a/src/cmd/dist/buildgo.go b/src/cmd/dist/buildgo.go +index 1111111..2222222 100644 +--- a/src/cmd/dist/buildgo.go ++++ b/src/cmd/dist/buildgo.go +@@ -15,6 +15,7 @@ import "fmt" + // package main + // const defaultCC = + // const defaultCXX = ++// const defaultPkgConfig = + // + // It is invoked to write cmd/go/zdefaultcc.go + // but we also write cmd/cgo/zdefaultcc.go +@@ -27,8 +28,9 @@ func mkzdefaultcc(dir, file string) { + "package main\n"+ + "\n"+ + "const defaultCC = `%s`\n"+ +- "const defaultCXX = `%s`\n", +- defaultcctarget, defaultcxxtarget) ++ "const defaultCXX = `%s`\n"+ ++ "const defaultPkgConfig = `%s`\n", ++ defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget) + + writefile(out, file, writeSkipSame) + +diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go +index 1111111..2222222 100644 +--- a/src/cmd/go/build.go ++++ b/src/cmd/go/build.go +@@ -1575,13 +1575,19 @@ func (b *builder) build(a *action) (err error) { + return nil + } + ++// pkgconfigCmd returns a pkg-config binary name ++// defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist. ++func (b *builder) pkgconfigCmd() string { ++ return envList("PKG_CONFIG", defaultPkgConfig)[0] ++} ++ + // Calls pkg-config if needed and returns the cflags/ldflags needed to build the package. + func (b *builder) getPkgConfigFlags(p *Package) (cflags, ldflags []string, err error) { + if pkgs := p.CgoPkgConfig; len(pkgs) > 0 { + var out []byte +- out, err = b.runOut(p.Dir, p.ImportPath, nil, "pkg-config", "--cflags", pkgs) ++ out, err = b.runOut(p.Dir, p.ImportPath, nil, b.pkgconfigCmd(), "--cflags", pkgs) + if err != nil { +- b.showOutput(p.Dir, "pkg-config --cflags "+strings.Join(pkgs, " "), string(out)) ++ b.showOutput(p.Dir, b.pkgconfigCmd()+" --cflags "+strings.Join(pkgs, " "), string(out)) + b.print(err.Error() + "\n") + err = errPrintedOutput + return +@@ -1589,9 +1595,9 @@ func (b *builder) getPkgConfigFlags(p *Package) (cflags, ldflags []string, err e + if len(out) > 0 { + cflags = strings.Fields(string(out)) + } +- out, err = b.runOut(p.Dir, p.ImportPath, nil, "pkg-config", "--libs", pkgs) ++ out, err = b.runOut(p.Dir, p.ImportPath, nil, b.pkgconfigCmd(), "--libs", pkgs) + if err != nil { +- b.showOutput(p.Dir, "pkg-config --libs "+strings.Join(pkgs, " "), string(out)) ++ b.showOutput(p.Dir, b.pkgconfigCmd()+" --libs "+strings.Join(pkgs, " "), string(out)) + b.print(err.Error() + "\n") + err = errPrintedOutput + return diff --git a/plugins/go/go-native.mk b/plugins/go/go-native.mk new file mode 100644 index 00000000..fb724efe --- /dev/null +++ b/plugins/go/go-native.mk @@ -0,0 +1,31 @@ +# This file is part of MXE. +# See index.html for further information. + +PKG := go-native +$(PKG)_WEBSITE := https://golang.org/ +$(PKG)_OWNER := https://github.com/starius +$(PKG)_IGNORE := +$(PKG)_VERSION := 1.4.3 +$(PKG)_CHECKSUM := 9947fc705b0b841b5938c48b22dc33e9647ec0752bae66e50278df4f23f64959 +$(PKG)_SUBDIR := go +$(PKG)_FILE := go$($(PKG)_VERSION).src.tar.gz +$(PKG)_URL := https://storage.googleapis.com/golang/$($(PKG)_FILE) +$(PKG)_DEPS_$(BUILD) := +$(PKG)_TARGETS := $(BUILD) + +define $(PKG)_UPDATE + $(WGET) -q -O- 'https://golang.org/dl/' | \ + $(SED) -n 's,.*go\(1.4.[0-9][^>]*\)\.src\.tar.*,\1,p' | \ + $(SORT) -h | tail -1 +endef + +define $(PKG)_BUILD + cd '$(1)/src' && \ + GOROOT_FINAL='$(PREFIX)/$(TARGET)/go' \ + ./make.bash + + mkdir -p '$(PREFIX)/$(TARGET)/go' + for d in include src bin pkg; do \ + cp -a '$(1)'/$$d '$(PREFIX)/$(TARGET)/go/' ; \ + done +endef diff --git a/plugins/go/go.mk b/plugins/go/go.mk new file mode 100644 index 00000000..36764061 --- /dev/null +++ b/plugins/go/go.mk @@ -0,0 +1,59 @@ +# This file is part of MXE. +# See index.html for further information. + +PKG := go +$(PKG)_WEBSITE := https://golang.org/ +$(PKG)_OWNER := https://github.com/starius +$(PKG)_IGNORE := +$(PKG)_VERSION := 1.6.2 +$(PKG)_CHECKSUM := 787b0b750d037016a30c6ed05a8a70a91b2e9db4bd9b1a2453aa502a63f1bccc +$(PKG)_SUBDIR := go +$(PKG)_FILE := go$($(PKG)_VERSION).src.tar.gz +$(PKG)_URL := https://storage.googleapis.com/golang/$($(PKG)_FILE) +$(PKG)_DEPS := + +define $(PKG)_UPDATE + $(WGET) -q -O- 'https://golang.org/dl/' | \ + $(SED) -n 's,.*go\(1.6.[0-9][^>]*\)\.src\.tar.*,\1,p' | \ + $(SORT) -h | tail -1 +endef + +define $(PKG)_BUILD + cd '$(1)/src' && \ + GOROOT_BOOTSTRAP='$(PREFIX)/$(BUILD)/go' \ + GOROOT_FINAL='$(PREFIX)/$(TARGET)/go' \ + GOOS=windows \ + GOARCH='$(if $(findstring x86_64,$(TARGET)),amd64,386)' \ + ./make.bash + + mkdir -p '$(PREFIX)/$(TARGET)/go' + for d in include src bin pkg; do \ + cp -a '$(1)'/$$d '$(PREFIX)/$(TARGET)/go/' ; \ + done + + #create prefixed go wrapper script + mkdir -p '$(PREFIX)/bin/$(TARGET)' + (echo '#!/usr/bin/env bash'; \ + echo 'echo "== Using MXE Go wrapper: $(PREFIX)/bin/$(TARGET)-go"'; \ + echo 'set -xue'; \ + echo 'CGO_ENABLED=1 \'; \ + echo 'GOOS=windows \'; \ + echo 'GOARCH=$(if $(findstring x86_64,$(TARGET)),amd64,386) \'; \ + echo 'CC=$(PREFIX)/bin/$(TARGET)-gcc \'; \ + echo 'CXX=$(PREFIX)/bin/$(TARGET)-g++ \'; \ + echo 'PKG_CONFIG=$(PREFIX)/bin/$(TARGET)-pkg-config \'; \ + echo 'exec $(PREFIX)/$(TARGET)/go/bin/go \'; \ + echo '"$$@"'; \ + ) \ + > '$(PREFIX)/bin/$(TARGET)-go' + chmod 0755 '$(PREFIX)/bin/$(TARGET)-go' + + GOPATH=$(PWD)/plugins/go \ + '$(TARGET)-go' build \ + -o '$(PREFIX)/$(TARGET)/go/bin/test-go.exe' \ + mxe-test +endef + +# -buildmode=shared not supported on windows +# See https://golang.org/s/execmodes +$(PKG)_BUILD_SHARED = diff --git a/plugins/go/src/mxe-test/main.go b/plugins/go/src/mxe-test/main.go new file mode 100644 index 00000000..c6d46fb4 --- /dev/null +++ b/plugins/go/src/mxe-test/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("hello, world\n") +}