The Things Network LoRaWAN Stack V3 学习笔记 1.2 源码编译

前言

源码编译是重头戏,这节笔记记录如何使用 make 命令编译相关部件。由于部分包在墙外,带来了一点麻烦,还分享一个 replace 方式来翻墙的办法。

小能手这段时间在学习 The Things Network LoRaWAN Stack V3,从使用和代码等角度对该 Stack 进行了分析,详细可点此查看

1 依赖包替换

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/go@v0.36.0: 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/go@v0.34.0: 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/sys@v0.0.0-20190209173611-3b5209105503: 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/sys@v0.0.0-20180909124046-d0be0721c37e: 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/sys@v0.0.0-20181011152604-fa43e7bc11ba: 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/genproto@v0.0.0-20190201180003-4b09977fb922: 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/genproto@v0.0.0-20181016170114-94acd270e44e: 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/sync@v0.0.0-20180314180146-1d60e4601c6f: 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/sys@v0.0.0-20181030150119-7e31e0c00fa0: 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/net@v0.0.0-20181023162649-9b4f9f5ad519: 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/net@v0.0.0-20190206173232-65e2d4e15006: 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/tools@v0.0.0-20190211224914-44bee7e801e4: 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/text@v0.3.0: 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/sync@v0.0.0-20181108010431-42b317875d0f: 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/sys@v0.0.0-20180905080454-ebe1bf3edb33: 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/appengine@v1.2.0: 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/oauth2@v0.0.0-20190211225200-5f6b76b7c9dd: 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/oauth2@v0.0.0-20181017192945-9dcd33a902f4: 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/image@v0.0.0-20190209060608-ef4a1470e0dc: 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/net@v0.0.0-20180906233101-161cd47e91fd: 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/net@v0.0.0-20181017193950-04a2e542c03f: 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/crypto@v0.0.0-20180904163835-0709b304e793: 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/crypto@v0.0.0-20190211182817-74369b46fc67: 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/crypto@v0.0.0-20181015023909-0c41d7ab0a0e: 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/grpc@v1.15.0: 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/grpc@v1.18.0: 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/time@v0.0.0-20180412165947-fbb02b2291d2: 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/api@v0.0.0-20181017004218-3f6e8463aa1d: 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
)

替换之后,所有包就很顺畅地下载下来了。

2 编译准备

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
+ yarn@1.12.3
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 fsevents@1.2.4: The platform "linux" is incompatible with this module.
info "fsevents@1.2.4" 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 fsevents@1.2.4: The platform "linux" is incompatible with this module.
info "fsevents@1.2.4" 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'

3 基础编译

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

4 cli 编译

这个命令行工具是和 Stack 的各组件进行交互的必要工具。

go build ./cmd/ttn-lw-cli/

5 stack 编译

go build ./cmd/ttn-lw-stack/

小能手使用一个弱爆了虚机进行编译,竟然出错,因此下一篇还额外分享了一个交叉编译的方式,即在 MAC 上为 Linux 编译程序。就当是体验 GO 的一大特色。

6 编译全部

If you must, you can build all arifacts with the following command:

make clean build-all

END


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值