欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Go 源码编译笔记

程序员文章站 2022-07-14 20:57:21
...

本文介绍如何编译 Go 源码

1. 环境准备

要编译 Go 源码,首先要准备编译环境
鉴于 Go 目前也是通过 Go 语言编写的(自举),因此准备一个 Go 环境即可
以 Ubuntu 18.04 为例:

# cd /tmp 
# wget https://studygolang.com/dl/golang/go1.13.3.linux-amd64.tar.gz
# tar zxvf go1.13.3.linux-amd64.tar.gz -C /usr/local
# echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
# source /etc/profile

然后,设置一个 GOPATH 作为工作空间,例如:

# mkdir /home/work
# echo "export GOPATH=/home/work" >> /etc/profile
# source /etc/profile

注意,启用 CGO 时,仍需要安装 C 编译器

# apt install gcc -y

或者,如果不需要 CGO 可以通过设置 CGO_ENABLED=0 关闭该功能

2. 源码克隆及编译

接下来使用 git 克隆源码

# cd $GOPATH
# mkdir -p src/go.googlesource.com
# cd src/go.googlesource.com
# git clone https://github.com/golang/go.git
# cd go/src
# ./all.bash

编译成功的输出类似这样:

Building Go cmd/dist using /opt/go.
Building Go toolchain1 using /opt/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for linux/amd64.

##### Testing packages.
ok  	archive/tar	0.727s
ok  	archive/zip	2.549s
ok  	bufio	0.141s
ok  	bytes	0.341s
ok  	compress/bzip2	0.287s
ok  	compress/flate	1.252s
ok  	compress/gzip	0.098s
ok  	compress/lzw	0.054s
ok  	compress/zlib	0.145s
ok  	container/heap	0.047s
ok  	container/list	0.013s
ok  	container/ring	0.041s
ok  	context	1.033s
ok  	crypto	0.015s
ok  	crypto/aes	0.045s
ok  	crypto/cipher	0.044s
ok  	crypto/des	0.043s
ok  	crypto/dsa	0.049s
ok  	crypto/ecdsa	0.414s
ok  	crypto/ed25519	0.114s
ok  	crypto/elliptic	0.038s
ok  	crypto/hmac	0.021s
ok  	crypto/internal/subtle	0.007s
ok  	crypto/md5	0.010s
ok  	crypto/rand	0.038s
ok  	crypto/rc4	0.108s
ok  	crypto/rsa	0.172s
ok  	crypto/sha1	0.050s
ok  	crypto/sha256	0.055s
ok  	crypto/sha512	0.027s
ok  	crypto/subtle	0.019s
ok  	crypto/tls	2.253s
ok  	crypto/x509	1.194s
ok  	database/sql	0.653s
ok  	database/sql/driver	0.025s
ok  	debug/dwarf	0.058s
ok  	debug/elf	0.090s
ok  	debug/gosym	0.030s
ok  	debug/macho	0.014s
ok  	debug/pe	0.046s
ok  	debug/plan9obj	0.029s
ok  	encoding/ascii85	0.041s
ok  	encoding/asn1	0.011s
ok  	encoding/base32	0.042s
ok  	encoding/base64	0.033s
ok  	encoding/binary	0.016s
ok  	encoding/csv	0.065s
ok  	encoding/gob	0.109s
ok  	encoding/hex	0.033s
ok  	encoding/json	0.172s
ok  	encoding/pem	0.028s
ok  	encoding/xml	0.063s
ok  	errors	0.015s
ok  	expvar	0.021s
ok  	flag	0.033s
ok  	fmt	0.170s
ok  	go/ast	0.054s
ok  	go/build	0.427s
ok  	go/constant	0.027s
ok  	go/doc	0.238s
ok  	go/format	0.043s
ok  	go/importer	0.398s
ok  	go/internal/gccgoimporter	0.044s
ok  	go/internal/gcimporter	0.810s
ok  	go/internal/srcimporter	2.168s
ok  	go/parser	0.044s
ok  	go/printer	0.684s
ok  	go/scanner	0.098s
ok  	go/token	0.067s
ok  	go/types	3.857s
ok  	hash	0.065s
ok  	hash/adler32	0.076s
ok  	hash/crc32	0.032s
ok  	hash/crc64	0.021s
ok  	hash/fnv	0.013s
ok  	html	0.055s
ok  	html/template	0.197s
ok  	image	0.513s
ok  	image/color	0.084s
ok  	image/draw	0.428s
ok  	image/gif	1.931s
ok  	image/jpeg	1.128s
ok  	image/png	0.143s
ok  	index/suffixarray	0.541s
ok  	internal/cpu	0.206s
ok  	internal/fmtsort	0.050s
ok  	internal/oserror	0.018s
ok  	internal/poll	0.323s
ok  	internal/reflectlite	0.039s
ok  	internal/singleflight	0.027s
ok  	internal/trace	0.103s
ok  	internal/xcoff	0.062s
ok  	io	0.066s
ok  	io/ioutil	0.076s
ok  	log	0.018s
ok  	log/syslog	1.317s
ok  	math	0.044s
ok  	math/big	0.925s
ok  	math/bits	0.007s
ok  	math/cmplx	0.009s
ok  	math/rand	0.238s
ok  	mime	0.055s
ok  	mime/multipart	0.361s
ok  	mime/quotedprintable	0.043s
ok  	net	7.583s
ok  	net/http	5.935s
ok  	net/http/cgi	1.185s
ok  	net/http/cookiejar	0.042s
ok  	net/http/fcgi	0.051s
ok  	net/http/httptest	0.116s
ok  	net/http/httptrace	0.012s
ok  	net/http/httputil	0.140s
ok  	net/http/internal	0.094s
ok  	net/http/pprof	2.032s
ok  	net/internal/socktest	0.007s
ok  	net/mail	0.026s
ok  	net/rpc	0.070s
ok  	net/rpc/jsonrpc	0.076s
ok  	net/smtp	0.084s
ok  	net/textproto	0.009s
ok  	net/url	0.069s
ok  	os	0.945s
ok  	os/exec	0.768s
ok  	os/signal	5.410s
ok  	os/user	0.044s
ok  	path	0.008s
ok  	path/filepath	0.204s
ok  	plugin	0.051s
ok  	reflect	0.435s
ok  	regexp	0.213s
ok  	regexp/syntax	1.253s
ok  	runtime	104.836s
ok  	runtime/debug	0.087s
ok  	runtime/internal/atomic	0.031s
ok  	runtime/internal/math	0.024s
ok  	runtime/internal/sys	0.015s
ok  	runtime/pprof	11.936s
ok  	runtime/pprof/internal/profile	0.057s
ok  	runtime/trace	1.521s
ok  	sort	0.157s
ok  	strconv	0.864s
ok  	strings	0.309s
ok  	sync	0.708s
ok  	sync/atomic	0.067s
ok  	syscall	5.986s
ok  	testing	0.202s
ok  	testing/quick	0.091s
ok  	text/scanner	0.020s
ok  	text/tabwriter	0.029s
ok  	text/template	0.356s
ok  	text/template/parse	0.027s
ok  	time	3.423s
ok  	unicode	0.034s
ok  	unicode/utf16	0.037s
ok  	unicode/utf8	0.056s
ok  	cmd/addr2line	8.449s
ok  	cmd/api	0.166s
ok  	cmd/asm/internal/asm	2.906s
ok  	cmd/asm/internal/lex	0.034s
ok  	cmd/compile	0.029s
ok  	cmd/compile/internal/gc	29.002s
ok  	cmd/compile/internal/ssa	1.090s
ok  	cmd/compile/internal/syntax	0.066s
ok  	cmd/compile/internal/test	0.016s [no tests to run]
ok  	cmd/compile/internal/types	0.029s
ok  	cmd/cover	8.509s
ok  	cmd/doc	0.182s
ok  	cmd/fix	0.062s
ok  	cmd/go	135.530s
ok  	cmd/go/internal/auth	0.024s
ok  	cmd/go/internal/cache	1.126s
ok  	cmd/go/internal/dirhash	0.039s
ok  	cmd/go/internal/generate	0.027s
ok  	cmd/go/internal/get	0.041s
ok  	cmd/go/internal/imports	0.066s
ok  	cmd/go/internal/load	0.066s
ok  	cmd/go/internal/lockedfile	0.026s
ok  	cmd/go/internal/lockedfile/internal/filelock	0.073s
ok  	cmd/go/internal/modconv	0.049s
ok  	cmd/go/internal/modfetch	0.118s
ok  	cmd/go/internal/modfetch/codehost	0.033s
ok  	cmd/go/internal/modfile	0.012s
ok  	cmd/go/internal/modload	0.031s
ok  	cmd/go/internal/module	0.022s
ok  	cmd/go/internal/mvs	0.009s
ok  	cmd/go/internal/note	0.063s
ok  	cmd/go/internal/par	0.048s
ok  	cmd/go/internal/renameio	0.191s
ok  	cmd/go/internal/search	0.014s
ok  	cmd/go/internal/semver	0.169s
ok  	cmd/go/internal/sumweb	0.050s
ok  	cmd/go/internal/tlog	2.338s
ok  	cmd/go/internal/txtar	0.014s
ok  	cmd/go/internal/web	0.057s
ok  	cmd/go/internal/work	0.056s
ok  	cmd/gofmt	0.165s
ok  	cmd/internal/buildid	0.645s
ok  	cmd/internal/dwarf	0.017s
ok  	cmd/internal/edit	0.012s
ok  	cmd/internal/goobj	1.685s
ok  	cmd/internal/obj	0.026s
ok  	cmd/internal/obj/arm64	0.088s
ok  	cmd/internal/obj/x86	0.803s
ok  	cmd/internal/objabi	0.004s
ok  	cmd/internal/src	0.018s
ok  	cmd/internal/test2json	0.378s
ok  	cmd/link	19.763s
ok  	cmd/link/internal/ld	8.869s
ok  	cmd/link/internal/sym	0.037s
ok  	cmd/nm	11.694s
ok  	cmd/objdump	5.120s
ok  	cmd/pack	3.999s
ok  	cmd/trace	0.458s
ok  	cmd/vet	7.637s

##### os/user with tag osusergo
ok  	os/user	0.007s

##### GOMAXPROCS=2 runtime -cpu=1,2,4 -quick
ok  	runtime	26.612s

##### cmd/go terminal test
PASS
ok  	cmd/go/testdata/testterminal18153	0.003s

##### Testing without libgcc.
ok  	crypto/x509	1.079s
ok  	net	0.057s
ok  	os/user	0.044s

##### internal linking of -buildmode=pie
ok  	reflect	3.032s
ok  	os/user	1.147s

##### sync -cpu=10
ok  	sync	1.074s

##### Testing race detector
ok  	runtime/race	36.131s
ok  	flag	1.117s
ok  	net	1.193s
ok  	os	1.403s
ok  	os/exec	3.170s
ok  	encoding/gob	1.073s
ok  	flag	1.136s
ok  	os/exec	3.345s

##### ../misc/cgo/stdio
PASS

##### ../misc/cgo/life
PASS

##### ../misc/cgo/test
PASS
ok  	misc/cgo/test	14.151s
PASS
ok  	misc/cgo/test	14.085s
PASS
ok  	misc/cgo/test	14.387s
PASS
ok  	misc/cgo/testtls	0.031s
PASS
ok  	misc/cgo/testtls	0.014s
PASS
ok  	misc/cgo/testtls	0.043s
PASS
ok  	misc/cgo/nocgo	0.008s
PASS
ok  	misc/cgo/nocgo	0.024s
PASS
ok  	misc/cgo/nocgo	0.017s
PASS
ok  	misc/cgo/test	12.304s
PASS
ok  	misc/cgo/test	13.081s
PASS
ok  	misc/cgo/test	11.226s
PASS
ok  	misc/cgo/testtls	0.020s
PASS
ok  	misc/cgo/nocgo	0.034s

##### ../misc/cgo/testgodefs

##### ../misc/cgo/testso
ok  	misc/cgo/testso	2.325s

##### ../misc/cgo/testsovar
ok  	misc/cgo/testsovar	2.190s

##### ../misc/cgo/testcarchive
SKIP - short mode and $GO_BUILDER_NAME not set

##### ../misc/cgo/testcshared
SKIP - short mode and $GO_BUILDER_NAME not set

##### ../misc/cgo/testshared
ok  	misc/cgo/testshared	56.327s

##### ../misc/cgo/testplugin
ok  	misc/cgo/testplugin	0.003s

##### ../misc/cgo/testasan

##### ../misc/cgo/testsanitizers
PASS

##### ../misc/cgo/errors
PASS

##### ../misc/cgo/testsigfwd

##### ../test/bench/go1

##### ../test

##### API check
+pkg debug/elf, type Symbol struct, Library string
+pkg debug/elf, type Symbol struct, Version string

ALL TESTS PASSED
---
Installed Go for linux/amd64 in /home/work/src/go.googlesource.com/go
Installed commands in /home/work/src/go.googlesource.com/go/bin
*** You need to add /home/work/src/go.googlesource.com/go/bin to your PATH.

编译出的二进制文件位于 $GOPATH/src/go.googlesource.com/go/bin 路径下

相关标签: go 源码编译