测试修改并编译golang源码
最近为了做Hyperledger Fabric国密改造,涉及到了golang源码的改动。特将操作过程整理如下,以供参考: golang的源码安装其实比较简单,只需运行源码包中的脚本src/all.bash,等到出现类似以下字样就安装好了: Installed Go for linux/amd64 in xxx(目录地址) Installed commands in xxx(目录地址) 但是在源码安装1.5版本以上的go时会报以下的错误 : ##### Building Go bootstrap tool. cmd/dist ERROR: Cannot find /home/fabric/go1.4/bin/go. Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4. 这是由于go 1.5版以后的编译安装需要1.4版本go,所以如果想要通过源码方式安装高版本go,必须先安装好1.4版本的go。 让我们开始操作吧!为了方便修改调试,可以fork官方的仓库(https://github.com/golang/go.git),然后拉取自己仓库中的代码,例如我的用户名是MangoDowner 第一步、安装golang 1.4主要操作如下:
[email?protected]:~$ su root
[email?protected]:~# export GOPATH=/opt/gopath [email?protected]:~# cd $GOPATH/src/github.com/MangoDowner/ [email?protected]:/opt/gopath/src/github.com/MangoDowner# git clone https://github.com/MangoDowner/go.git Cloning into ‘go‘... remote: Counting objects: 322777,done. remote: Compressing objects: 100% (73/73),done. remote: Total 322777 (delta 32),reused 54 (delta 28),pack-reused 322675 Receiving objects: 100% (322777/322777),147.71 MiB | 3.49 MiB/s,done. Resolving deltas: 100% (255582/255582),done.
[email?protected]:/opt/gopath/src/github.com/MangoDowner# cd go [email?protected]:/opt/gopath/src/github.com/MangoDowner/go# git checkout release-branch.go1.4 Branch release-branch.go1.4 set up to track remote branch release-branch.go1.4 from origin. Switched to a new branch ‘release-branch.go1.4‘
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go# cd src [email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# ./make.bash # Building C bootstrap tool. cmd/dist # Building compilers and Go bootstrap tool for host,linux/amd64. lib9 libbio liblink cmd/cc cmd/gc cmd/6l .... # Checking API compatibility. Skipping cmd/api checks real 0m0.538s user 0m0.310s sys 0m0.191s ALL TESTS PASSED --- Installed Go for linux/amd64 in /root/software/go Installed commands in /root/software/go/bin *** You need to add /root/software/go/bin to your PATH. 如果遇到报错 cannot load DWARF output from $WORK/os/user/_obj//_cgo_.o: decoding dwarf section info at offset 0x4: unsupported version 0 需要关闭cgo支持,重新编译 [email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# export CGO_ENABLED=0 [email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# ./make.bash
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# cp -rp ../../go /usr/local/go1.4 这样你就能得到1.4版本的go了。 第二步、安装golang 1.9主要操作如下:
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# export CGO_ENABLED=1
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# export GOROOT_BOOTSTRAP=/usr/local/go1.4
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# cd ../ [email?protected]:/opt/gopath/src/github.com/MangoDowner/go# git checkout release-branch.go1.9 Branch release-branch.go1.9 set up to track remote branch release-branch.go1.9 from origin. Switched to a new branch ‘release-branch.go1.9‘
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go# cd src [email?protected]:/opt/gopath/src/github.com/MangoDowner/go# ./make.bash ...
[email?protected]:/opt/gopath/src/github.com/MangoDowner/go/src# cp -rp ../../go /usr/local/go 结语:上述操作结束之后,当然还是要设置GOROOT之类的环境变量才可以,这样的文章很多,不再赘述。 而且,相信都能改golang源码的你,这些已经不在话下了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |