源码编译是重头戏,这节笔记记录如何使用 make 命令编译相关部件。由于部分包在墙外,带来了一点麻烦,还分享一个 replace 方式来的办法。
小能手这段时间在学习 The Things Network LoRaWAN Stack V3,从使用和代码等角度对该 Stack 进行了分析,详细可点此查看。
TTN Stack V3 采用了 go moudles 来进行包依赖管理。我们可以看到工程目录下,存在着一个 go.mod,记录了整个工程的依赖包。
我们使用 go mod download 命令,来下载依赖包。
ubuntu@VM-0-6-ubuntu:~/lorawan-stack$ go mod download
go: finding github.com/kamilsk/retry/v4 v4.0.0
go: cloud.google.com/[email protected]: unrecognized import path "cloud.google.com/go" (https fetch: Get https://cloud.google.com/go?go-get=1: dial tcp 172.217.163.238:443: i/o timeout)
go: cloud.google.com/[email protected]: unrecognized import path "cloud.google.com/go" (https fetch: Get https://cloud.google.com/go?go-get=1: dial tcp 172.217.163.238:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: google.golang.org/[email protected]: unrecognized import path "google.golang.org/genproto" (https fetch: Get https://google.golang.org/genproto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: google.golang.org/[email protected]: unrecognized import path "google.golang.org/genproto" (https fetch: Get https://google.golang.org/genproto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sync" (https fetch: Get https://golang.org/x/sync?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/tools" (https fetch: Get https://golang.org/x/tools?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: finding gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/text" (https fetch: Get https://golang.org/x/text?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sync" (https fetch: Get https://golang.org/x/sync?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: google.golang.org/[email protected]: unrecognized import path "google.golang.org/appengine" (https fetch: Get https://google.golang.org/appengine?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/oauth2" (https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/oauth2" (https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: finding github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/image" (https fetch: Get https://golang.org/x/image?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: google.golang.org/[email protected]: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: google.golang.org/[email protected]: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/time" (https fetch: Get https://golang.org/x/time?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: google.golang.org/[email protected]: unrecognized import path "google.golang.org/api" (https fetch: Get https://google.golang.org/api?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: error loading module requirements
果然被墙。
我的大神同事们分享了一个好办法,使用go modules的replace功能了。由于被墙的那些包,通常在 github 有相同的包,因此我们只需把它们替换成 github 包。
replace (
cloud.google.com/go => github.com/GoogleCloudPlatform/google-cloud-go latest
golang.org/x/sys => github.com/golang/sys latest
golang.org/x/sync => github.com/golang/sync latest
golang.org/x/net => github.com/golang/net latest
golang.org/x/tools => github.com/golang/tools latest
golang.org/x/text => github.com/golang/text latest
golang.org/x/oauth2 => github.com/golang/oauth2 latest
golang.org/x/image => github.com/golang/image latest
golang.org/x/crypto => github.com/golang/crypto latest
golang.org/x/time => github.com/golang/time latest
golang.org/x/lint => github.com/golang/lint latest
golang.org/x/build => github.com/golang/build latest
golang.org/x/perf => github.com/golang/perf latest
golang.org/x/xerrors => github.com/golang/xerrors latest
golang.org/x/exp => github.com/golang/exp latest
golang.org/x/mobile => github.com/golang/mobile latest
google.golang.org/genproto => github.com/juelite/google.golang.org-genproto latest
google.golang.org/appengine => github.com/golang/appengine latest
google.golang.org/grpc => github.com/grpc/grpc-go latest
google.golang.org/api => github.com/googleapis/google-api-go-client latest
)
替换之后,所有包就很顺畅地下载下来了。
ubuntu@VM-0-6-ubuntu:~/lorawan-stack$ make init
make[1]: Entering directory '/home/ubuntu/lorawan-stack'
GO111MODULE=on go run github.com/magefile/mage -compile .mage/run
lorawan-stack Installing git hook: pre-commit
lorawan-stack Installing git hook: pre-push
lorawan-stack Installing git hook: commit-msg
lorawan-stack Initializing go
make[2]: Entering directory '/home/ubuntu/lorawan-stack'
lorawan-stack Installing go dependencies
make[2]: Leaving directory '/home/ubuntu/lorawan-stack'
lorawan-stack Initializing js
make[2]: Entering directory '/home/ubuntu/lorawan-stack'
lorawan-stack Installing js dev dependencies
+ [email protected]
added 1 package and audited 1 package in 3.241s
found 0 vulnerabilities
make[2]: Leaving directory '/home/ubuntu/lorawan-stack'
make[2]: Entering directory '/home/ubuntu/lorawan-stack'
lorawan-stack Installing js dev dependencies
lorawan-stack Fetching JS SDK dependencies
yarn install v1.12.3
[1/4] Resolving packages...
[2/4] Fetching packages...
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
warning Your current version of Yarn is out of date. The latest version is "1.13.0", while you're on "1.12.3".
info To upgrade, run the following command:
$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
Done in 24.55s.
make[2]: Leaving directory '/home/ubuntu/lorawan-stack'
make[2]: Entering directory '/home/ubuntu/lorawan-stack'
lorawan-stack Building JS SDK files
yarn run v1.12.3
$ babel src -d dist --no-emoji --no-progress --quiet
Done in 2.68s.
make[2]: Leaving directory '/home/ubuntu/lorawan-stack'
make[2]: Entering directory '/home/ubuntu/lorawan-stack'
lorawan-stack Installing js dev dependencies
lorawan-stack Installing js dependencies
yarn install v1.12.3
[1/4] Resolving packages...
[2/4] Fetching packages...
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 77.69s.
make[2]: Leaving directory '/home/ubuntu/lorawan-stack'
lorawan-stack Generating certificates
2019/03/11 16:47:50 wrote cert.pem
2019/03/11 16:47:50 wrote key.pem
make[1]: Leaving directory '/home/ubuntu/lorawan-stack'
make dev-deps
make deps
make js.build
必须要先编译,js涉及页面处理,后面整个 Stack 编译也是需要的。
如果你和我一样出现如下错误,我劝你换个好点的机子试试,我的 CVM 还搞不定。
lorawan-stack Building client [webpack -c .cache/config/webpack.config.js --colors --progress]
92% chunk asset optimization UglifyJSPlugin o
.make/js/build.make:68: recipe for target 'js.webpack-main' failed
make[1]: *** [js.webpack-main] Killed
make[1]: Leaving directory '/home/ubuntu/lorawan-stack'
.make/js/build.make:52: recipe for target 'js.build-main' failed
make: *** [js.build-main] Error 2
我尝试过如下办法,比如 --optimization=false,以及关闭多线程,还是没能解决问题,暂时先作罢。
https://github.com/angular/angular-cli/issues/5775
https://segmentfault.com/q/1010000015291213?sort=created
这个命令行工具是和 Stack 的各组件进行交互的必要工具。
go build ./cmd/ttn-lw-cli/
go build ./cmd/ttn-lw-stack/
小能手使用一个弱爆了虚机进行编译,竟然出错,因此下一篇还额外分享了一个交叉编译的方式,即在 MAC 上为 Linux 编译程序。就当是体验 GO 的一大特色。
If you must, you can build all arifacts with the following command:
make clean build-all