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

简介: dist是用来编译go语言的工具,而且dist本身是由go语言写的所以可以用低版本的go编译dist, 再用dist编译高版本的go/src/cmd/dist是go dist工具的代码目录main.go中的main为入口函数主要的流程为检测操作系统检测系统架构bginit() //启...

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()
  • bootstrapBuildTools()
  • install("runtime")
  • install("cmd/go")
  • goInstall(goBootstrap, append([]string{"-i"}, toolchain...)...)
  • goInstall(goBootstrap, append([]string{"-a", "-i"}, toolchain...)...)
  • goInstall(goBootstrap, targets...)
相关文章
|
6月前
|
NoSQL IDE Go
Go 语言源码级调试器 Delve
Go 语言源码级调试器 Delve
39 0
|
21天前
|
存储 测试技术 Go
掌握Go语言:深入探究Go语言中的命令源码文件与参数处理技巧(3)
掌握Go语言:深入探究Go语言中的命令源码文件与参数处理技巧(3)
|
5月前
|
存储 Cloud Native Go
Go语言 WaitGroup 源码知多少
Go语言 WaitGroup 源码知多少
|
3月前
|
Kubernetes Go 数据库
分享48个Go源码,总有一款适合您
分享48个Go源码,总有一款适合您
53 0
|
4月前
|
Go Python
go cmd 使用 与 结构 说明,go cmd 调用 python
go cmd 使用 与 结构 说明,go cmd 调用 python
28 0
|
8月前
|
存储 缓存 安全
Go源码解析之chan.go
Go源码解析之chan.go
165 0
|
8月前
|
存储 缓存 监控
Go源码解析之proc.go
Go源码解析之proc.go
192 0
|
8月前
|
存储 搜索推荐 编译器
Go源码解析之select.go
Go源码解析之select.go
71 0
|
8月前
|
存储 机器学习/深度学习 Unix
Go源码解析之format.go(2)
Go源码解析之format.go(2)
69 0
|
8月前
|
存储 Unix Go
Go源码解析之format.go(1)
Go源码解析之format.go(1)
77 0

热门文章

最新文章