老年人编译系列-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...)
相关文章
|
编译器 测试技术 Go
Go语言的自给自足:编译自身的神奇之旅
Go语言的自给自足:编译自身的神奇之旅
|
Go 区块链
go 编译 自定义 图标 ico文件
go 编译 自定义 图标 ico文件
199 0
|
Go Windows
Go 交叉编译 跨平台编译(windows系统跨平台编译)
Go 交叉编译 跨平台编译(windows系统跨平台编译)
220 0
|
NoSQL IDE Go
Go 语言源码级调试器 Delve
Go 语言源码级调试器 Delve
280 0
|
11月前
|
监控 Java Go
编译时插桩,Go应用监控的最佳选择
本文讲解了阿里云编译器团队和可观测团队为了实现Go应用监控选择编译时插桩的原因,同时还介绍了其他的监控方案以及它们的优缺点。
952 113
|
编译器 Go
go语言编译选项
【10月更文挑战第17天】
262 5
go语言编译时常量表达式
【10月更文挑战第20天】
131 3
|
存储 Cloud Native Go
Go语言 WaitGroup 源码知多少
Go语言 WaitGroup 源码知多少
197 0
|
编译器 Go 开发者
Go 在编译时评估隐式类型的优点详解
【8月更文挑战第31天】
92 0

热门文章

最新文章