本文介绍Golang交叉编译工具Gox的安装
Gox
gox是Golang常用的交叉编译工具,比如在制作指定操作系统版本的程序docker镜像。
Gox安装
1
2
3
4
|
export GOPROXY=https://goproxy.cn
go get github.com/mitchellh/gox
|
然后在默认安装的位置$GOPATH/bin
下找到gox,如:
1
| /home/dragon/GO_projects/bin/gox
|
如要直接运行命令使用,则将gox拷贝到$GOROOT/bin
下
Gox说明
可以使用 -h 调出帮助
如下:
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
| $ gox -h
Usage: gox [options] [packages]
Gox cross-compiles Go applications in parallel.
If no specific operating systems or architectures are specified, Gox
will build for all pairs supported by your version of Go.
Options:
-arch="" Space-separated list of architectures to build for
-build-toolchain Build cross-compilation toolchain
-cgo Sets CGO_ENABLED=1, requires proper C toolchain (advanced)
-gcflags="" Additional '-gcflags' value to pass to go build
-ldflags="" Additional '-ldflags' value to pass to go build
-asmflags="" Additional '-asmflags' value to pass to go build
-tags="" Additional '-tags' value to pass to go build
-mod="" Additional '-mod' value to pass to go build
-os="" Space-separated list of operating systems to build for
-osarch="" Space-separated list of os/arch pairs to build for
-osarch-list List supported os/arch pairs for your Go version
-output="foo" Output path template. See below for more info
-parallel=-1 Amount of parallelism, defaults to number of CPUs
-race Build with the go race detector enabled, requires CGO
-gocmd="go" Build command, defaults to Go
-rebuild Force rebuilding of package that were up to date
-verbose Verbose mode
Output path template:
The output path for the compiled binaries is specified with the
"-output" flag. The value is a string that is a Go text template.
The default value is "{{.Dir}}_{{.OS}}_{{.Arch}}". The variables and
their values should be self-explanatory.
Platforms (OS/Arch):
The operating systems and architectures to cross-compile for may be
specified with the "-arch" and "-os" flags. These are space separated lists
of valid GOOS/GOARCH values to build for, respectively. You may prefix an
OS or Arch with "!" to negate and not build for that platform. If the list
is made up of only negations, then the negations will come from the default
list.
Additionally, the "-osarch" flag may be used to specify complete os/arch
pairs that should be built or ignored. The syntax for this is what you would
expect: "darwin/amd64" would be a valid osarch value. Multiple can be space
separated. An os/arch pair can begin with "!" to not build for that platform.
The "-osarch" flag has the highest precedent when determing whether to
build for a platform. If it is included in the "-osarch" list, it will be
built even if the specific os and arch is negated in "-os" and "-arch",
respectively.
Platform Overrides:
The "-gcflags", "-ldflags" and "-asmflags" options can be overridden per-platform
by using environment variables. Gox will look for environment variables
in the following format and use those to override values if they exist:
GOX_[OS]_[ARCH]_GCFLAGS
GOX_[OS]_[ARCH]_LDFLAGS
GOX_[OS]_[ARCH]_ASMFLAGS
|
第一次使用要进行构建交叉编译的库,这一步很慢,我的机器大概需要5分钟左右.
构建好就可以使用了,如果你想构建全平台(主流)的文件,那么直接 gox
,目录下会生成对应平台的文件.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| $ gox
Number of parallel builds: 4
--> darwin/386: github.com/mitchellh/gox
--> darwin/amd64: github.com/mitchellh/gox
--> linux/386: github.com/mitchellh/gox
--> linux/amd64: github.com/mitchellh/gox
--> linux/arm: github.com/mitchellh/gox
--> freebsd/386: github.com/mitchellh/gox
--> freebsd/amd64: github.com/mitchellh/gox
--> openbsd/386: github.com/mitchellh/gox
--> openbsd/amd64: github.com/mitchellh/gox
--> windows/386: github.com/mitchellh/gox
--> windows/amd64: github.com/mitchellh/gox
--> freebsd/arm: github.com/mitchellh/gox
--> netbsd/386: github.com/mitchellh/gox
--> netbsd/amd64: github.com/mitchellh/gox
--> netbsd/arm: github.com/mitchellh/gox
--> plan9/386: github.com/mitchellh/gox
|
当然,如果要指定平台可以使用如下命令:
1
2
3
4
5
| $ gox -os="linux"
or
$ gox -osarch="linux/amd64"
or
$ gox -osarch="linux/amd64 windows/amd64"
|
如果要指定输出目录和文件名可以使用:
1
| $ gox -osarch="darwin/amd64" -output="output_path"
|
备注: gox交叉编译不支持 CGO,具体命令如下
1
| CGO_ENABLED=0 gox -osarch=${OSARCH} -ldflags ${LD_FLAGS} -output=${OUTPUT_PATH}
|
实例
github上kube-batch项目的镜像编译
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # Make the image of kube-batch
1.add gox to $GOROOT/bin
2.copy all items from vendor/* to $GOPATH
3.import the image alpine3.9
4.take a Terminal comand in $GOPATH/github.com/kubernetes-sigs/kube-batch : make images
# gox in Makefile
rel_bins:
CGO_ENABLED=0 gox -osarch=${REL_OSARCH} -ldflags ${LD_FLAGS} \
-output=${BIN_DIR}/{{.OS}}/{{.Arch}}/kube-batch ./cmd/kube-batch
|
附录
Usage
If you know how to use go build
, then you know how to use Gox. For
example, to build the current package, specify no parameters and just
call gox
. Gox will parallelize based on the number of CPUs you have
by default and build for every platform by default:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| $ gox
Number of parallel builds: 4
--> darwin/386: github.com/mitchellh/gox
--> darwin/amd64: github.com/mitchellh/gox
--> linux/386: github.com/mitchellh/gox
--> linux/amd64: github.com/mitchellh/gox
--> linux/arm: github.com/mitchellh/gox
--> freebsd/386: github.com/mitchellh/gox
--> freebsd/amd64: github.com/mitchellh/gox
--> openbsd/386: github.com/mitchellh/gox
--> openbsd/amd64: github.com/mitchellh/gox
--> windows/386: github.com/mitchellh/gox
--> windows/amd64: github.com/mitchellh/gox
--> freebsd/arm: github.com/mitchellh/gox
--> netbsd/386: github.com/mitchellh/gox
--> netbsd/amd64: github.com/mitchellh/gox
--> netbsd/arm: github.com/mitchellh/gox
--> plan9/386: github.com/mitchellh/gox
|
Or, if you want to build a package and sub-packages:
Or, if you want to build multiple distinct packages:
1
2
| $ gox github.com/mitchellh/gox github.com/hashicorp/serf
...
|
Or if you want to just build for linux:
1
2
| $ gox -os="linux"
...
|
Or maybe you just want to build for 64-bit linux:
1
2
| $ gox -osarch="linux/amd64"
...
|
And more! Just run gox -h
for help and additional information.
A big thanks to these other options for existing. They each paved the
way in many aspects to make Go cross-compilation approachable.
Dave Cheney’s golang-crosscompile -
Gox compiles for multiple platforms and can therefore easily run on
any platform Go supports, whereas Dave’s scripts require a shell. Gox
will also parallelize builds. Dave’s scripts build sequentially. Gox has
much easier to use OS/Arch filtering built in.
goxc -
A very richly featured tool that can even do things such as build system
packages, upload binaries, generate download webpages, etc. Gox is a
super slim alternative that only cross-compiles binaries. Gox builds packages in parallel, whereas
goxc doesn’t. Gox doesn’t enforce a specific output structure for built
binaries.