您现在的位置是:首页 > 文章详情

老年人编译系列-go cmd/dist源码

日期:2019-05-16点击:442

dist是用来编译go语言的工具,而且dist本身是由go语言写的
所以可以用低版本的go编译dist, 再用dist编译高版本的go
/src/cmd/dist是go dist工具的代码目录
main.go中的main为入口函数
主要的流程为

  • 检测操作系统
  • 检测系统架构
  • bginit() //启动一组后端运行的goroutine, 用于在后台运行go
  • xinit() //初始化全局变量/状态
  • xmain() //执行全局commands变量中存储的操作
  • xexit() //资源检查/进程退出
    查看usage()函数可知
func usage() { xprintf(`usage: go tool dist [command] Commands are: banner print installation banner bootstrap rebuild everything clean deletes all built files env [-p] print environment (-p: include $PATH) install [dir] install individual directory list [-json] list all supported platforms test [-h] run Go test(s) version print Go version All commands take -v flags to emit extra information. `) xexit(2) }

查看commands的定义

// commands records the available commands. var commands = map[string]func(){ "banner": cmdbanner, "bootstrap": cmdbootstrap, "clean": cmdclean, "env": cmdenv, "install": cmdinstall, "list": cmdlist, "test": cmdtest, "version": cmdversion, }

着重看cmdbootstrap流程
cmdbootstrap流程中

  • 检查rebuildall, debug, nobanner三个参数
  • setup() //初始化变量
  • checkCC() //检查C++编译器
  • bootstrapBuildTools() //编译bootstrap工具链,主要的目录都列在了bootstrapDirs变量中
  • install("runtime")
  • install("cmd/go")
  • goInstall(goBootstrap, append([]string{"-i"}, toolchain...)...)
  • goInstall(goBootstrap, append([]string{"-a", "-i"}, toolchain...)...)
  • goInstall(goBootstrap, targets...)
原文链接:https://yq.aliyun.com/articles/702698
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章