分享
  1. 首页
  2. 文章

修改编译golang源码

夏之绘 · · 2035 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Go 语言诞生5周年!

友情提示:本文使用Markdown编写,黑色背景文字可能需要横向拖动才能看清全文

最近为了做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

主要操作如下:

  • 为了方便操作,我们切换到root用户
fabric@fabric-VirtualBox:~$ su root
  • 为了方便统一管理,将golang源码放入GOPATH中
root@fabric:~# export GOPATH=/opt/gopath
root@fabric:~# cd $GOPATH/src/github.com/MangoDowner/
root@fabric:/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.

友情提醒下,虽然可能有点啰嗦,但是MangoDowner/go这个仓库其实是不存在的,我自己改的源码放在了MangoDowner/go-gm下,这里只是为了方便举例子...

  • 切换为go 1.4分支
root@fabric:/opt/gopath/src/github.com/MangoDowner# cd go
root@fabric:/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'
  • 进入src目录,并运行all.bash 安装脚本,稍等片刻即可安装成功:
root@fabric:/opt/gopath/src/github.com/MangoDowner/go# cd src
root@fabric:/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支持,重新编译

root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# export CGO_ENABLED=0 
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# ./make.bash
  • 最后,我们将编译好的go 1.4复制到/usr/local下方便以后使用
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# cp -rp ../../go /usr/local/go1.4 

这样你就能得到1.4版本的go了。

第二步、安装golang 1.9

主要操作如下:

  • 我们需要编译好的golang环境支持c语言的文件,所以需要开启cgo
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# export CGO_ENABLED=1
  • 我们需要指定由go 1.4进行编译,所以得设置以下环境变量
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# export GOROOT_BOOTSTRAP=/usr/local/go1.4

这里就用到了前面复制得到的go 1.4目录

  • 回到go源码根目录,并切换分支至1.9
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# cd ../
root@fabric:/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'
  • 下面的过程就和编译go1.4很类似,不再赘述
root@fabric:/opt/gopath/src/github.com/MangoDowner/go# cd src
root@fabric:/opt/gopath/src/github.com/MangoDowner/go# ./make.bash
...
  • 最后将编译好的go 1.9复制到/usr/local下,作为默认的golang标准库目录
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# cp -rp ../../go /usr/local/go

结语:

上述操作结束之后,当然还是要设置GOROOT之类的环境变量才可以,这样的文章很多,不再赘述。

而且,相信都能改golang源码的你,这些已经不在话下了。


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:夏之绘

查看原文:修改编译golang源码

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
2035 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏