老年人编译系列-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() //检查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...)
相关文章
|
5月前
|
Go 区块链
go 编译 自定义 图标 ico文件
go 编译 自定义 图标 ico文件
38 0
|
5月前
|
Go Windows
Go 交叉编译 跨平台编译(windows系统跨平台编译)
Go 交叉编译 跨平台编译(windows系统跨平台编译)
32 0
|
6月前
|
自然语言处理 编译器 Linux
超详细!通晓Go语言编译运行 你需要这篇攻略 | 进阶指南
超详细!通晓Go语言编译运行 你需要这篇攻略 | 进阶指南
419 0
超详细!通晓Go语言编译运行 你需要这篇攻略 | 进阶指南
|
7月前
|
算法 Java 编译器
掌握Go的运行时:从编译到执行
掌握Go的运行时:从编译到执行
132 0
|
3月前
|
缓存 IDE 编译器
假如没有go install,编译会多痛苦?
假如没有go install,编译会多痛苦?
40 0
|
3月前
|
Unix Linux Go
2.go-编译
2.go-编译
25 0
|
6月前
|
自然语言处理 编译器 Go
揭秘Go语言编译黑盒:从源代码到神奇可执行文件的完整过程
揭秘Go语言编译黑盒:从源代码到神奇可执行文件的完整过程
35 0
|
8月前
|
Go C语言
关于 Go 的编译及体积优化
关于 Go 的编译及体积优化
221 1
|
9月前
|
Linux Go Windows
Golang-编译和Go语言文件基本机构
Go语言编译和Go语言文件基本机构
73 0
|
Linux Go Windows
go windows编译linux可执行文件
go windows编译linux可执行文件
6393 0