/
Makefile
177 lines (149 loc) · 6.5 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
NAME ?= berty
# FIXME: create a everythingButVendor helper that runs a wildcard but ignores node_modules/ and vendor/, and simplify variables below
GOPATH ?= $(HOME)/go
GO_ROOT_PACKAGES := . ../network
GO_ROOT_PACKAGES_VENDORS := ./vendor/% ../network/vendor/%
BIN ?= $(GOPATH)/bin/berty
BUILD_MODE ?= dev
SOURCES = $(strip $(call rwildcard, $(GO_ROOT_PACKAGES), *.go))
OUR_SOURCES = $(filter-out $(GO_ROOT_PACKAGES_VENDORS),$(SOURCES))
PROTOS = $(strip $(call rwildcard, $(GO_ROOT_PACKAGES), *.proto))
OUR_PROTOS = $(filter-out $(GO_ROOT_PACKAGES_VENDORS),$(PROTOS))
PROTOC_OPTS = -I /protobuf:./vendor/berty.tech:./vendor/github.com/gogo/protobuf:vendor:../network:.
SERVICE_PROTOS = $(call rwildcard, api, *.proto)
GENERATED_PATTERNS = %.validate.gen.go %.service.gen.go %generated.gen.go %kind.gen.go %.pb.go %.gen.js
GENERATED_FILES = $(filter $(GENERATED_PATTERNS),$(filter-out $(call rwildcard ./vendor/,*),$(call rwildcard,./,*)))
GENERATION_TARGETS = .kind.generated .client.generated .pb.generated
CGO_LDFLAGS ?= -L/usr/local/opt/openssl/lib
CGO_CPPFLAGS ?= -I/usr/local/opt/openssl/include
BUILD_ENV ?= GO111MODULE=on CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CPPFLAGS="$(CGO_CPPFLAGS)"
GIT_SHA ?= $(shell git rev-parse HEAD)
GIT_TAG ?= $(shell git describe --tags --always)
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
GIT_COMMIT_DATE ?= $(shell git show -s --format=%ct $(GIT_SHA))
GO_ROOT_PKG ?= berty.tech/core
EXT_LDFLAGS ?= -ldflags "-X $(GO_ROOT_PKG).GitSha=$(GIT_SHA) -X $(GO_ROOT_PKG).GitTag=$(GIT_TAG) -X $(GO_ROOT_PKG).GitBranch=$(GIT_BRANCH) -X $(GO_ROOT_PKG).BuildMode=$(BUILD_MODE) -X $(GO_ROOT_PKG).commitDate=$(GIT_COMMIT_DATE)"
RUN_DAEMON_OPTS ?= --log-level=debug --notification
TEST_PATHS ?= $(shell go list ./... | grep -v core/platform/desktop)
TEST_OPTS ?= -v
TEST_TIMEOUT ?= 3m
TEST_CMD ?= go test -test.timeout $(TEST_TIMEOUT) $(TEST_OPTS) $(TEST_PATHS)
PROTOC ?= protoc
GIN_OPTS ?= --immediate --port=2999 --appPort=1337 --build=./cmd/berty --excludeDir vendor
RN_PATH ?= ../client
##
## phonies
##
.PHONY: help
help:
@echo "Core commands:"
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | grep -v / | sed 's/^/ $(HELP_MSG_PREFIX)make /'
.PHONY: callvis
callvis:
cd $$GOPATH/src/berty.tech/core/cmd/berty && $(BUILD_ENV) GO111MODULE=auto go-callvis \
-nostd \
-group=pkg,type \
-focus=berty.tech/core/cmd/berty \
-ignore=berty.tech/core/vendor/go.uber.org,berty.tech/core/pkg/tracing,berty.tech/core/vendor/github.com,berty.tech/core/vendor/golang.org,berty.tech/core/vendor/google.golang.org,berty.tech/core/vendor/gopkg.in \
.
# -include=berty.tech/core/vendor/github.com/libp2p \
.PHONY: deps
deps: ./vendor
./vendor: ./go.mod
GO111MODULE=on go mod vendor
.PHONY: dev
dev: install
@if ! command -v gin &>/dev/null; then go get github.com/codegangsta/gin; fi
$(BUILD_ENV) gin $(GIN_OPTS) run -- daemon $(RUN_DAEMON_OPTS)
.PHONY: run
run: $(BIN)
$(BIN) daemon $(RUN_DAEMON_OPTS)
.PHONY: install
install: $(BIN)
$(BIN): generate $(OUR_SOURCES)
$(BUILD_ENV) go install $(EXT_LDFLAGS) -v ./cmd/...
.PHONY: testwatch
testwatch:
@if ! command -v watchman &>/dev/null; then brew install watchman; fi
while true; do clear; $(BUILD_ENV) $(TEST_CMD); sleep .3; watchman-wait . -p "**/*.go"; done
.PHONY: test
test: generate
$(BUILD_ENV) $(TEST_CMD)
# testloop runs test suite until it fails (without test caching)
.PHONY: testloop
testloop: generate
@if ! command -v watchman &>/dev/null; then go get moul.io/retry; fi
$(BUILD_ENV) retry -r $(TEST_CMD) -count 1
.PHONY: integration
integration: install
rm -f /tmp/berty.integration.db
@# initialize a new daemon on a fresh db
$(BIN) daemon --init-only --no-p2p --sql-name=berty.integration.db
.PHONY: lint
lint: generate
$(BUILD_ENV) golangci-lint run $(TEST_PATHS)
.PHONY: generate
generate: .generated
.generated: $(PROTOS)
make generate_prepare
docker run \
--volume="$(PWD)/..:/go/src/berty.tech" \
--workdir="/go/src/$(GO_ROOT_PKG)" \
--entrypoint="sh" \
--rm \
bertytech/protoc:v14 \
-xec 'make generate_local'
touch $@
.PHONY: clean
clean:
rm -f .generated $(GENERATED_FILES) $(GENERATION_TARGETS) $(BIN)
rm -rf vendor
@# FIXME: investigate
rm -rf google gogoproto github.com google.golang.org
cd $(PWD)/../client && make clean.generate
.PHONY: generate_local
generate_local: $(GENERATION_TARGETS)
@# FIXME: investigate why (sometimes?) there are "github.com" and "google.golang.org" directories generated at the root of core/
rm -rf google gogoproto github.com google.golang.org
goimports -w `find . -name "*.pb.go" -or -name "*.gen.go" | grep -v /vendor/`
gofmt -w -s `find . -name "*.pb.go" -or -name "*.gen.go" | grep -v /vendor/`
.PHONY: generate_prepare
generate_prepare: deps
.PHONY: _ci_prepare
_ci_prepare:
@# touching files to avoid regenerating files based on modification date
touch $(PROTOS) $(SOURCES)
sleep 1
touch .generated $(GENERATION_TARGETS)
##
## file-based rules
##
%.pb.go: %.proto
@echo $<
$(PROTOC) $(PROTOC_OPTS) \
--gofast_out="plugins=grpc:$(GOPATH)/src" \
--gotemplate_out=debug=false,all=true,single-package-mode=false,template_dir=./pkg/validate:. \
"$(dir $<)"/*.proto
.kind.generated: $(PROTOS)
$(PROTOC) $(PROTOC_OPTS) --gotemplate_out=debug=false,single-package-mode=true,all=true,template_dir=./entity:./entity ./entity/kind.proto
goimports -w ./entity
touch $@
.client.generated: $(PROTOS)
$(PROTOC) $(PROTOC_OPTS) --gotemplate_out=debug=false,all=true,template_dir=./api/node:./api/node ./api/node/service.proto
@set -e; for protodir in `echo $(dir $(SERVICE_PROTOS)) | tr " " "\n" | uniq`; do (set -xe; \
$(PROTOC) $(PROTOC_OPTS) --gotemplate_out=debug=false,all=false,single-package-mode=true,template_dir=./api/client:./api/client $$protodir/*.proto; \
); done
@# FIXME: the following hack is due to goimports -w not deleting duplicated imports
goimports -w ./api/client/
sed -i'' '/^$$/d' api/client/*.gen.go api/client/jsonclient/*.gen.go
goimports -w ./api/client/*.gen.go ./api/client/jsonclient/*.gen.go
sed -i'' '/^$$/d' api/client/*.gen.go api/client/jsonclient/*.gen.go
goimports -w ./api/client/*.gen.go ./api/client/jsonclient/*.gen.go
cd ../client/ && make generate
touch $@
.pb.generated: $(patsubst %.proto,%.pb.go,$(OUR_PROTOS))
rm -rf google/ # remove directory generated by protoc-gen-gotemplate for proto deps
touch $@
displayprotos:
@echo $(PROTOS)