【golang】从源代码编译golang编译器

本文详细记录了在Ubuntu 18.04系统中,如何从源代码编译Go 1.17的过程,包括下载go1.4作为bootstrap编译器,配置环境变量,编译最新版Go,并验证编译器版本信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统信息

tsu5@hhvm-clx-atom:~/tony$ cat /etc/*release

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION=“Ubuntu 18.04.5 LTS”
NAME=“Ubuntu”
VERSION=“18.04.5 LTS (Bionic Beaver)”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=“Ubuntu 18.04.5 LTS”
VERSION_ID=“18.04”
HOME_URL=“https://www.ubuntu.com/”
SUPPORT_URL=“https://help.ubuntu.com/”
BUG_REPORT_URL=“https://bugs.launchpad.net/ubuntu/”
PRIVACY_POLICY_URL=“https://www.ubuntu.com/legal/terms-and-policies/privacy-policy”
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

tsu5@hhvm-clx-atom:~/tony$ gcc --version

gcc (Ubuntu 10.1.0-2ubuntu1~18.04) 10.1.0
Copyright © 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

tsu5@hhvm-clx-atom:~/tony$ g++ --version

g++ (Ubuntu 10.1.0-2ubuntu1~18.04) 10.1.0
Copyright © 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

下载并编译go1.4用作bootstrap编译器

$ cd ~/tony
$ wget https://dl.google.com/go/go1.4-bootstrap-20171003.tar.gz
$ tar xvf go1.4-bootstrap-20171003.tar.gz
# 解压后的目录名为go,将其改为go1.4
$ mv go go1.4
$ export CGO_ENABLED=0
$ cd go1.4/src
$ ./make.bash

# 编译完成后,go/gofmt会被安装到go1.4/bin目录
tsu5@hhvm-clx-atom:~/tony/go1.4/bin$ ls -l
total 12840
-rwxr-xr-x 1 tsu5 tsu5 9550864 Jul  2 10:02 go
-rwxr-xr-x 1 tsu5 tsu5 3596112 Jul  2 10:02 gofmt

# 我们将使用这个go1.4版本的编译器编译出最新的go编译器
tsu5@hhvm-clx-atom:~/tony/go1.4/bin$ ./go version
go version go1.4-bootstrap-20170531 linux/amd64

下载golang源代码

$ cd ~/tony
# 注意:clone是指定代码库目录名为goroot
# 新编译出来的golang编译器将会被安装在goroot/bin目录
$ git clone https://go.googlesource.com/go goroot
$ cd goroot/src
# 不要忘记设置这个环境变量
$ export GOROOT_BOOTSTRAP=/home/tsu5/tony/go1.4

编译最新的golang

tsu5@hhvm-clx-atom:~/tony/goroot/src$ ./all.bash 
Building Go cmd/dist using /home/tsu5/tony/go1.4/. (go1.4-bootstrap-20170531 linux/amd64)
Building Go toolchain1 using /home/tsu5/tony/go1.4/.
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.128s
ok      archive/zip     0.187s
ok      bufio   0.089s
ok      bytes   0.225s
ok      compress/bzip2  0.090s
ok      compress/flate  0.698s
ok      compress/gzip   0.062s
ok      compress/lzw    0.028s
ok      compress/zlib   0.064s
ok      container/heap  0.037s
ok      container/list  0.034s
ok      container/ring  0.020s
ok      context 0.034s
ok      crypto  0.047s
ok      crypto/aes      0.040s
ok      crypto/cipher   0.022s
ok      crypto/des      0.025s
ok      crypto/dsa      0.030s
ok      crypto/ecdsa    0.030s
ok      crypto/ed25519  0.118s
ok      crypto/ed25519/internal/edwards25519    6.956s
ok      crypto/ed25519/internal/edwards25519/field      3.132s
ok      crypto/elliptic 0.158s
ok      crypto/elliptic/internal/fiat   0.005s
ok      crypto/hmac     0.022s
ok      crypto/internal/subtle  0.020s
ok      crypto/md5      0.038s
ok      crypto/rand     0.031s
ok      crypto/rc4      0.057s
ok      crypto/rsa      0.144s
ok      crypto/sha1     0.068s
ok      crypto/sha256   0.059s
ok      crypto/sha512   0.036s
ok      crypto/subtle   0.027s
ok      crypto/tls      0.648s
ok      crypto/x509     0.639s
ok      database/sql    0.701s
ok      database/sql/driver     0.079s
ok      debug/dwarf     0.041s
ok      debug/elf       0.031s
ok      debug/gosym     0.078s
ok      debug/macho     0.040s
ok      debug/pe        0.042s
ok      debug/plan9obj  0.021s
ok      embed/internal/embedtest        0.023s
ok      encoding/ascii85        0.015s
ok      encoding/asn1   0.045s
ok      encoding/base32 0.098s
ok      encoding/base64 0.016s
ok      encoding/binary 0.109s
ok      encoding/csv    0.024s
ok      encoding/gob    0.058s
ok      encoding/hex    0.112s
ok      encoding/json   0.227s
ok      encoding/pem    0.119s
ok      encoding/xml    0.124s
ok      errors  0.082s
ok      expvar  0.057s
ok      flag    0.141s
ok      fmt     0.150s
ok      go/ast  0.040s
ok      go/build        1.369s
ok      go/build/constraint     0.030s
ok      go/constant     0.052s
ok      go/doc  0.118s
ok      go/format       0.046s
ok      go/importer     0.239s
ok      go/internal/gccgoimporter       0.090s
ok      go/internal/gcimporter  0.715s
ok      go/internal/srcimporter 0.879s
ok      go/parser       0.038s
ok      go/printer      0.397s
ok      go/scanner      0.083s
ok      go/token        0.078s
ok      go/types        2.710s
ok      hash    0.054s
ok      hash/adler32    0.111s
ok      hash/crc32      0.030s
ok      hash/crc64      0.102s
ok      hash/fnv        0.067s
ok      hash/maphash    0.229s
ok      html    0.083s
ok      html/template   0.170s
ok      image   0.124s
ok      image/color     0.078s
ok      image/draw      0.111s
ok      image/gif       0.394s
ok      image/jpeg      0.278s
ok      image/png       0.153s
ok      index/suffixarray       0.393s
ok      internal/abi    0.247s
ok      internal/cpu    0.077s
ok      internal/execabs        0.065s
ok      internal/fmtsort        0.078s
ok      internal/itoa   0.059s
ok      internal/poll   0.201s
ok      internal/profile        0.092s
ok      internal/reflectlite    0.206s
ok      internal/singleflight   0.088s
ok      internal/trace  0.140s
ok      internal/unsafeheader   0.070s
ok      internal/xcoff  0.048s
ok      io      0.104s
ok      io/fs   0.067s
ok      io/ioutil       0.083s
ok      log     0.033s
ok      log/syslog      1.357s
ok      math    0.085s
ok      math/big        1.636s
ok      math/bits       0.067s
ok      math/cmplx      0.042s
ok      math/rand       0.204s
ok      mime    0.065s
ok      mime/multipart  0.256s
ok      mime/quotedprintable    0.143s
ok      net     4.120s
ok      net/http        10.202s
ok      net/http/cgi    1.036s
ok      net/http/cookiejar      0.051s
ok      net/http/fcgi   0.189s
ok      net/http/httptest       0.128s
ok      net/http/httptrace      0.046s
ok      net/http/httputil       0.226s
ok      net/http/internal       0.098s
ok      net/http/internal/ascii 0.123s
ok      net/http/pprof  5.207s
ok      net/internal/socktest   0.074s
ok      net/mail        0.068s
ok      net/rpc 0.102s
ok      net/rpc/jsonrpc 0.085s
ok      net/smtp        0.100s
ok      net/textproto   0.077s
ok      net/url 0.037s
ok      os      1.585s
ok      os/exec 0.931s
ok      os/signal       2.769s
ok      os/user 0.040s
ok      path    0.054s
ok      path/filepath   0.043s
ok      plugin  0.100s
ok      reflect 0.554s
ok      regexp  0.206s
ok      regexp/syntax   0.567s
ok      runtime 26.466s
ok      runtime/debug   0.178s
ok      runtime/internal/atomic 0.126s
ok      runtime/internal/math   0.044s
ok      runtime/internal/sys    0.090s
ok      runtime/metrics 0.046s
ok      runtime/pprof   4.205s
ok      runtime/trace   1.312s
ok      sort    0.128s
ok      strconv 0.619s
ok      strings 0.160s
ok      sync    1.020s
ok      sync/atomic     2.838s
ok      syscall 1.302s
ok      testing 1.459s
ok      testing/fstest  0.051s
ok      testing/iotest  0.097s
ok      testing/quick   0.124s
ok      text/scanner    0.174s
ok      text/tabwriter  0.096s
ok      text/template   0.114s
ok      text/template/parse     0.214s
ok      time    2.170s
ok      unicode 0.181s
ok      unicode/utf16   0.045s
ok      unicode/utf8    0.061s
ok      cmd/addr2line   3.501s
ok      cmd/api 4.332s
ok      cmd/asm/internal/asm    1.363s
ok      cmd/asm/internal/lex    0.123s
ok      cmd/compile/internal/dwarfgen   0.656s
ok      cmd/compile/internal/importer   1.143s
ok      cmd/compile/internal/ir 0.072s
ok      cmd/compile/internal/logopt     1.647s
ok      cmd/compile/internal/noder      0.122s
ok      cmd/compile/internal/ssa        0.510s
ok      cmd/compile/internal/ssagen     0.109s
ok      cmd/compile/internal/syntax     0.129s
ok      cmd/compile/internal/test       8.003s
ok      cmd/compile/internal/typecheck  0.743s
ok      cmd/compile/internal/types      0.040s
ok      cmd/compile/internal/types2     1.416s
ok      cmd/cover       3.099s
ok      cmd/doc 0.299s
ok      cmd/fix 1.481s
ok      cmd/go  20.777s
ok      cmd/go/internal/auth    0.036s
ok      cmd/go/internal/cache   0.133s
ok      cmd/go/internal/fsys    0.078s
ok      cmd/go/internal/generate        0.081s
ok      cmd/go/internal/get     0.049s
ok      cmd/go/internal/imports 0.022s
ok      cmd/go/internal/load    0.066s
ok      cmd/go/internal/lockedfile      0.165s
ok      cmd/go/internal/lockedfile/internal/filelock    0.100s
ok      cmd/go/internal/modconv 0.017s
ok      cmd/go/internal/modfetch        0.062s
ok      cmd/go/internal/modfetch/codehost       0.070s
ok      cmd/go/internal/modfetch/zip_sum_test   0.075s
ok      cmd/go/internal/modload 0.023s
ok      cmd/go/internal/mvs     0.097s
ok      cmd/go/internal/par     0.113s
ok      cmd/go/internal/search  0.065s
ok      cmd/go/internal/str     0.073s
ok      cmd/go/internal/test    0.069s
ok      cmd/go/internal/txtar   0.030s
ok      cmd/go/internal/vcs     0.035s
ok      cmd/go/internal/web     0.051s
ok      cmd/go/internal/work    0.050s
ok      cmd/gofmt       0.128s
ok      cmd/internal/archive    0.309s
ok      cmd/internal/buildid    0.349s
ok      cmd/internal/dwarf      0.014s
ok      cmd/internal/edit       0.025s
ok      cmd/internal/goobj      0.060s
ok      cmd/internal/moddeps    1.886s
ok      cmd/internal/obj        0.114s
ok      cmd/internal/obj/ppc64  0.226s
ok      cmd/internal/obj/riscv  0.149s
ok      cmd/internal/obj/s390x  0.052s
ok      cmd/internal/obj/x86    0.739s
ok      cmd/internal/objabi     0.058s
ok      cmd/internal/pkgpath    0.123s
ok      cmd/internal/src        0.103s
ok      cmd/internal/sys        0.060s
ok      cmd/internal/test2json  0.200s
ok      cmd/link        4.633s
ok      cmd/link/internal/benchmark     0.176s
ok      cmd/link/internal/ld    1.843s
ok      cmd/link/internal/loader        0.067s
ok      cmd/nm  1.722s
ok      cmd/objdump     1.731s
ok      cmd/pack        3.815s
ok      cmd/pprof       4.336s
ok      cmd/trace       0.303s
ok      cmd/vet 6.705s

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

##### go/... and cmd/gofmt tests with tag typeparams
ok      go/ast  0.004s
ok      go/build        1.130s
ok      go/build/constraint     0.028s
ok      go/constant     0.007s
ok      go/doc  0.033s
ok      go/format       0.006s
ok      go/importer     0.069s
ok      go/internal/gccgoimporter       0.009s
ok      go/internal/gcimporter  0.217s
ok      go/internal/srcimporter 0.583s
?       go/internal/typeparams  [no test files]
ok      go/parser       0.030s
ok      go/printer      0.335s
ok      go/scanner      0.028s
ok      go/token        0.014s
ok      go/types        2.100s
ok      cmd/gofmt       0.026s

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

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

##### Testing without libgcc.
ok      net     0.007s
ok      os/user 0.005s

##### internal linking of -buildmode=pie
ok      reflect 0.504s

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

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

##### ../test

##### API check

ALL TESTS PASSED
---
Installed Go for linux/amd64 in /home/tsu5/tony/goroot
Installed commands in /home/tsu5/tony/goroot/bin
*** You need to add /home/tsu5/tony/goroot/bin to your PATH.

检查golang编译版本信息

$ export PATH=$PATH:/home/tsu5/tony/goroot/bin

tsu5@hhvm-clx-atom:~/tony/goroot/bin$ ./go version
go version devel go1.17-03761ede02 Thu Jul 1 19:09:57 2021 +0000 linux/amd64
tsu5@hhvm-clx-atom:~/tony/goroot/bin$ ./go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tsu5/.cache/go-build"
GOENV="/home/tsu5/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/tsu5/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tsu5/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/tsu5/tony/goroot"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/tsu5/tony/goroot/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.17-03761ede02 Thu Jul 1 19:09:57 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3226031228=/tmp/go-build -gno-record-gcc-switches"

编译一个简单的go程序

// hello.go
package main

import "fmt"

func main() {
	fmt.Printf("hello, world\n")
}
$ go run hello.go 
Hello, are you ready to Go?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值