编译GO1.8.3源代码

插曲

Host系统是ubuntu16.04 32bits
已经从GO的官方下载了GO-Linux386,并解压在/usr/local下面

$ ls /usr/local
bin  etc  games  go  include  lib  man  sbin  share  src
$ ls /usr/local/go
api      blog             doc          LICENSE  pkg         src
AUTHORS  CONTRIBUTING.md  favicon.ico  misc     README.md   test
bin      CONTRIBUTORS     lib          PATENTS  robots.txt  VERSION

并且设置了下面几个环境变量, 具体设置是在~/.profile里添加了如下几行:

export GOROOT=/usr/local/go
PATH=$PATH:$GOROOT/bin:/home/jack/go/bin 

说明下,/home/jack/go/bin这个目录下面是golang的一些工具集,比如gocode等等,主配置vim-go时修改用的,这里可以忽略这个环境变量。

下载GO1.8.3编译

这里我用wget命令从googleleapis这个网站上下载的。
jack@jxes-VirtualBox:~/samba_share/golang$ wget https://storage.googleapis.com/golang/go1.8.3.src.tar.gz
–2017-08-04 15:40:40– https://storage.googleapis.com/golang/go1.8.3.src.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)… 216.58.200.48, 2404:6800:4008:803::2010
Connecting to storage.googleapis.com (storage.googleapis.com)|216.58.200.48|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 15345996 (15M) [application/x-gzip]
Saving to: ‘go1.8.3.src.tar.gz’

go1.8.3.src.tar.gz 100%[===================>] 14.63M 3.36MB/s in 5.5s

2017-08-04 15:40:47 (2.64 MB/s) - ‘go1.8.3.src.tar.gz’ saved [15345996/15345996]

jack@jxes-VirtualBox:~/samba_share/golang$ ls
go1.8.3.src.tar.gz

或者直接从google open source上git clone:

$ git clone https://go.googlesource.com/go
$ cd go
$ git checkout go1.8.3

在编译前需要先安装几个工具gcc, libc6-dev:

sudo apt install gcc libc6-dev

有些系统下可能还需要安装 下面几个,如果默认没有安装的情况下:

sudo apt-get install bison ed gawk make

将上面下载的压缩包解压后,进入src目录编译,直接运行里面的script就可以编译:

jack@jxes-VirtualBox:~/samba_share/golang/go/src$ ./all.bash 
##### Building Go bootstrap tool.
cmd/dist
ERROR: Cannot find /home/jack/go1.4/bin/go.
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.

从编译错可以看到,GO的工具是依赖Go1.4来编译的,接下来就需要下载Go1.4。
从官方可以知道,Go1.4是最后一个发行版本,它的工具链tool chain是用C写的,所以可以直接用C编译器就可以编译,这也就是Go1.8.3为什么不能编译的原因,因为Go1.4以后,tool chain不再是用C写了,而是用Go写的。

下载Go1.4

下载方法同上

jack@jxes-VirtualBox:~/samba_share/golang/go1.4$ wget  https://storage.googleapis.com/golang/go1.4.3.src.tar.gz
--2017-08-04 16:09:00--  https://storage.googleapis.com/golang/go1.4.3.src.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 172.217.27.144, 2404:6800:4008:800::2010
Connecting to storage.googleapis.com (storage.googleapis.com)|172.217.27.144|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10875170 (10M) [application/octet-stream]
Saving to: ‘go1.4.3.src.tar.gz’

go1.4.3.src.tar.gz  100%[===================>]  10.37M  1.92MB/s    in 5.9s    

2017-08-04 16:09:08 (1.75 MB/s) - ‘go1.4.3.src.tar.gz’ saved [10875170/10875170]

编译方法同上,直接运行all.bash脚本

jack@jxes-VirtualBox:~/samba_share/golang/go1.4/go/src$ ./all.bash 
# Building C bootstrap tool.
cmd/dist

# Building compilers and Go bootstrap tool for host, linux/386.
lib9
libbio
liblink
cmd/cc
.......
# Building packages and commands for linux/386.
runtime
errors
sync/atomic
........
# cmd/go
/home/jack/samba_share/golang/go1.4/go/pkg/linux_386/runtime/cgo.a(_all.o): unknown relocation type 43; compiled without -fpic?
/home/jack/samba_share/golang/go1.4/go/pkg/linux_386/runtime/cgo.a(_all.o): unknown relocation type 43; compiled without -fpic?
runtime/cgo(.text): unexpected relocation type 299
runtime/cgo(.text): unexpected relocation type 299
runtime/pprof
cmd/gofmt
..........
cmd/pprof
# cmd/pprof
/home/jack/samba_share/golang/go1.4/go/pkg/linux_386/runtime/cgo.a(_all.o): unknown relocation type 43; compiled without -fpic?
/home/jack/samba_share/golang/go1.4/go/pkg/linux_386/runtime/cgo.a(_all.o): unknown relocation type 43; compiled without -fpic?
runtime/cgo(.text): unexpected relocation type 299
runtime/cgo(.text): unexpected relocation type 299
cmd/yacc
......
os/user
runtime/debug
runtime/race
testing
testing/iotest
testing/quick
text/scanner

从上面的编译过程可以看到,整个过程编译中是有错误的,后来到官方查找了一下,发现官方对此有介绍,而且步骤写得很详细,发现是我下载的Go1.4.3版本有问题。
先贴下官方的网址:
https://golang.org/doc/install/source
下面从官方摘抄:

To build a bootstrap tool chain from source, use either the git branch release-branch.go1.4 or go1.4-bootstrap-20170531.tar.gz, which contains the Go 1.4 source code plus accumulated fixes to keep the tools running on newer operating systems. (Go 1.4 was the last distribution in which the tool chain was written in C.) After unpacking the Go 1.4 source, cd to the src subdirectory and run make.bash (or, on Windows, make.bat).

从官方介绍可以看出,今年才发行了一包Go1.4专门针对bootstrap的,下面就是下载go1.4-bootstrap-20170531.tar.gz,然后按上面同样的方式编译通过。编译过程省略,下面是编译结束后的log,注意:我在编译前把之前设置的两个环境变量都取消了设置,unset GOPATHunset GOROOT

.......
ALL TESTS PASSED    //出现这句表示编译成功

---
Installed Go for linux/386 in /home/jack/samba_share/golang/go
Installed commands in /home/jack/samba_share/golang/go/bin
*** You need to add /home/jack/samba_share/golang/go/bin to your PATH.

查看编译后的bin文档:

jack@jxes-VirtualBox:~/samba_share/golang/go$ ls bin
go  gofmt

再次编译Go1.8.3

编译前先要设备环境变量GOROOT_BOOTSTRAP,这个环境变量指向前面刚编译好的Go1.4-bootstrap

ack@jxes-VirtualBox:~/samba_share/golang/go/src$ export GOROOT_BOOTSTRAP=/home/jack/samba_share/golang/go1.4

编译方法与前面类似,直接运行script:

jack@jxes-VirtualBox:~/samba_share/golang/go/src$ ./all.bash 
##### Building Go bootstrap tool.
cmd/dist

##### Building Go toolchain using /home/jack/samba_share/golang/go1.4.
bootstrap/cmd/internal/dwarf
bootstrap/cmd/internal/sys
........
cmd/vet/internal/whitelist
cmd/vet


##### cmd/go terminal test
PASS
ok      _/home/jack/samba_share/golang/go/src/cmd/go/testdata/testterminal18153 0.020s

##### Testing packages.
ok      archive/tar 0.103s
ok      archive/zip 2.850s
........
ok      _/home/jack/samba_share/golang/go/test/bench/go1    10.056s
##### ../test

##### API check
Go version is "go1.8.3", ignoring -next /home/jack/samba_share/golang/go/api/next.txt

ALL TESTS PASSED

---
Installed Go for linux/386 in /home/jack/samba_share/golang/go
Installed commands in /home/jack/samba_share/golang/go/bin
*** You need to add /home/jack/samba_share/golang/go/bin to your PATH.

最后说明下,上面GO1.4 bootstrap版,与GO1.8.3版编译时都是用all.bash,这个除编译还会做测试验证,如果只想编译不需测试,可以运行make.bash

使用现有的Go环境编译

这部分也是官方介绍

To use a binary release as a bootstrap tool chain, see the downloads page or use any other packaged Go distribution.

在此文开头插曲中提到,我事先有在/usr/local/下面安装了go1.8.3-linux386,所以可以直接基于此版本来编译Go1.8.3的源代码。需要注意的是,需要把GOROOT_BOOTSTRAP指定为/usr/local/go,或直接export GOROOT_BOOTSTRAP=$GOROOT (GOROOT在我的环境中已经设置为/usr/local/go)
然后,进入源代码的src目录下,直接运行all.bash或make.bash即编译成功。

交叉编译

前面我是默认在当前host环境下编译,即linux386,如果要编译其他平台,可以参考官方介绍,目前这部分还没有实际去验证

To cross-compile a bootstrap tool chain from source, which is necessary on systems Go 1.4 did not target (for example, linux/ppc64le), install Go on a different system and run bootstrap.bash.

When run as (for example)

$ GOOS=linux GOARCH=ppc64 ./bootstrap.bash
bootstrap.bash cross-compiles a toolchain for that GOOS/GOARCH combination, leaving the resulting tree in ../../go-${GOOS}-${GOARCH}-bootstrap. That tree can be copied to a machine of the given target type and used as GOROOT_BOOTSTRAP to bootstrap a local build.

你可能感兴趣的:(GoLang,Go语言)