Makefile 9.28 KB
Newer Older
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# -------------------------------------------------------------
# This makefile defines the following targets
#
#   - all (default) - builds all targets and runs all tests/checks
#   - checks - runs all tests/checks
#   - peer - builds the fabric peer binary
#   - membersrvc - builds the membersrvc binary
#   - unit-test - runs the go-test based unit tests
#   - behave - runs the behave test
#   - behave-deps - ensures pre-requisites are availble for running behave manually
#   - gotools - installs go tools like golint
#   - linter - runs all code checks
#   - images[-clean] - ensures all docker images are available[/cleaned]
#   - peer-image[-clean] - ensures the peer-image is available[/cleaned] (for behave, etc)
#   - membersrvc-image[-clean] - ensures the membersrvc-image is available[/cleaned] (for behave, etc)
#   - protos - generate all protobuf artifacts based on .proto files
#   - node-sdk - builds the node.js client sdk
#   - node-sdk-unit-tests - runs the node.js client sdk unit tests
#   - clean - cleans the build area
#   - dist-clean - superset of 'clean' that also removes persistent state

39
PROJECT_NAME   = hyperledger/fabric
40
BASE_VERSION   = 0.6.1-preview
41
IS_RELEASE     = false
42
43

ifneq ($(IS_RELEASE),true)
Gregory Haskins's avatar
Gregory Haskins committed
44
EXTRA_VERSION ?= snapshot-$(shell git rev-parse --short HEAD)
45
46
47
48
49
PROJECT_VERSION=$(BASE_VERSION)-$(EXTRA_VERSION)
else
PROJECT_VERSION=$(BASE_VERSION)
endif

50
51
DOCKER_TAG=$(shell uname -m)-$(PROJECT_VERSION)

52
PKGNAME = github.com/$(PROJECT_NAME)
53
GO_LDFLAGS = -X github.com/hyperledger/fabric/metadata.Version=$(PROJECT_VERSION)
54
55
CGO_FLAGS = CGO_CFLAGS=" " CGO_LDFLAGS="-lrocksdb -lstdc++ -lm -lz -lbz2 -lsnappy"
UID = $(shell id -u)
56
CHAINTOOL_RELEASE=v0.9.1
57

Gregory Haskins's avatar
Gregory Haskins committed
58
EXECUTABLES = go docker git curl
59
60
61
62
63
64
65
K := $(foreach exec,$(EXECUTABLES),\
	$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH: Check dependencies")))

# SUBDIRS are components that have their own Makefiles that we can invoke
SUBDIRS = gotools sdk/node
SUBDIRS:=$(strip $(SUBDIRS))

66
67
68
69
# Make our baseimage depend on any changes to images/base or scripts/provision
BASEIMAGE_RELEASE = $(shell cat ./images/base/release)
BASEIMAGE_DEPS    = $(shell git ls-files images/base scripts/provision)

70
JAVASHIM_DEPS =  $(shell git ls-files core/chaincode/shim/java)
71
PROJECT_FILES = $(shell git ls-files)
72
IMAGES = base src ccenv peer membersrvc javaenv
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

all: peer membersrvc checks

checks: linter unit-test behave

.PHONY: $(SUBDIRS)
$(SUBDIRS):
	cd $@ && $(MAKE)

.PHONY: peer
peer: build/bin/peer
peer-image: build/image/peer/.dummy

.PHONY: membersrvc
membersrvc: build/bin/membersrvc
membersrvc-image: build/image/membersrvc/.dummy

unit-test: peer-image gotools
91
	@./scripts/goUnitTests.sh $(DOCKER_TAG) "$(GO_LDFLAGS)"
92

93
94
95
96
97
98
99
node-sdk: sdk/node

node-sdk-unit-tests: peer membersrvc
	cd sdk/node && $(MAKE) unit-tests

unit-tests: unit-test node-sdk-unit-tests

100
101
102
.PHONY: images
images: $(patsubst %,build/image/%/.dummy, $(IMAGES))

103
behave-deps: images peer build/bin/block-listener
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
behave: behave-deps
	@echo "Running behave tests"
	@cd bddtests; behave $(BEHAVE_OPTS)

linter: gotools
	@echo "LINT: Running code checks.."
	@echo "Running go vet"
	go vet ./consensus/...
	go vet ./core/...
	go vet ./events/...
	go vet ./examples/...
	go vet ./membersrvc/...
	go vet ./peer/...
	go vet ./protos/...
	@echo "Running goimports"
	@./scripts/goimports.sh

# We (re)build protoc-gen-go from within docker context so that
# we may later inject the binary into a different docker environment
# This is necessary since we cannot guarantee that binaries built
# on the host natively will be compatible with the docker env.
125
%/bin/protoc-gen-go: build/image/base/.dummy Makefile
126
127
128
129
130
131
	@echo "Building $@"
	@mkdir -p $(@D)
	@docker run -i \
		--user=$(UID) \
		-v $(abspath vendor/github.com/golang/protobuf):/opt/gopath/src/github.com/golang/protobuf \
		-v $(abspath $(@D)):/opt/gopath/bin \
132
		hyperledger/fabric-baseimage go install github.com/golang/protobuf/protoc-gen-go
133

Gregory Haskins's avatar
Gregory Haskins committed
134
build/bin/chaintool: Makefile
135
	@echo "Installing chaintool"
Gregory Haskins's avatar
Gregory Haskins committed
136
137
138
139
140
141
142
	@mkdir -p $(@D)
	curl -L https://github.com/hyperledger/fabric-chaintool/releases/download/$(CHAINTOOL_RELEASE)/chaintool > $@
	chmod +x $@

%/bin/chaintool: build/bin/chaintool
	@mkdir -p $(@D)
	@cp $^ $@
143

144
145
146
147
148
# JIRA FAB-243 - Mark build/docker/bin artifacts explicitly as secondary
#                since they are never referred to directly. This prevents
#                the makefile from deleting them inadvertently.
.SECONDARY: build/docker/bin/peer build/docker/bin/membersrvc

149
150
151
152
153
154
155
156
157
158
# We (re)build a package within a docker context but persist the $GOPATH/pkg
# directory so that subsequent builds are faster
build/docker/bin/%: build/image/src/.dummy $(PROJECT_FILES)
	$(eval TARGET = ${patsubst build/docker/bin/%,%,${@}})
	@echo "Building $@"
	@mkdir -p build/docker/bin build/docker/pkg
	@docker run -i \
		--user=$(UID) \
		-v $(abspath build/docker/bin):/opt/gopath/bin \
		-v $(abspath build/docker/pkg):/opt/gopath/pkg \
159
		hyperledger/fabric-src:$(DOCKER_TAG) go install -ldflags "$(GO_LDFLAGS)" github.com/hyperledger/fabric/$(TARGET)
160
	@touch $@
161
162
163
164

build/bin:
	mkdir -p $@

165
166
167
# Both peer and peer-image depend on ccenv-image and javaenv-image (all docker env images it supports)
build/bin/peer: build/image/ccenv/.dummy build/image/javaenv/.dummy
build/image/peer/.dummy: build/image/ccenv/.dummy build/image/javaenv/.dummy
168
169
170
171
172
173

build/bin/block-listener:
	@mkdir -p $(@D)
	$(CGO_FLAGS) GOBIN=$(abspath $(@D)) go install $(PKGNAME)/examples/events/block-listener
	@echo "Binary available as $@"
	@touch $@
174

175
build/bin/%: build/image/base/.dummy $(PROJECT_FILES)
176
177
	@mkdir -p $(@D)
	@echo "$@"
178
	$(CGO_FLAGS) GOBIN=$(abspath $(@D)) go install -ldflags "$(GO_LDFLAGS)" $(PKGNAME)/$(@F)
179
180
181
	@echo "Binary available as $@"
	@touch $@

182
183
184
185
186
187
188
# Special override for base-image.
build/image/base/.dummy: $(BASEIMAGE_DEPS)
	@echo "Building docker base-image"
	@mkdir -p $(@D)
	@./scripts/provision/docker.sh $(BASEIMAGE_RELEASE)
	@touch $@

189
# Special override for src-image
190
build/image/src/.dummy: build/image/base/.dummy $(PROJECT_FILES)
191
192
	@echo "Building docker src-image"
	@mkdir -p $(@D)
193
194
195
	@cat images/src/Dockerfile.in \
		| sed -e 's/_TAG_/$(DOCKER_TAG)/g' \
		> $(@D)/Dockerfile
196
	@git ls-files | tar -jcT - > $(@D)/gopath.tar.bz2
197
198
	docker build -t $(PROJECT_NAME)-src $(@D)
	docker tag $(PROJECT_NAME)-src $(PROJECT_NAME)-src:$(DOCKER_TAG)
199
200
201
202
203
	@touch $@

# Special override for ccenv-image (chaincode-environment)
build/image/ccenv/.dummy: build/image/src/.dummy build/image/ccenv/bin/protoc-gen-go build/image/ccenv/bin/chaintool Makefile
	@echo "Building docker ccenv-image"
204
205
206
207
208
	@cat images/ccenv/Dockerfile.in \
		| sed -e 's/_TAG_/$(DOCKER_TAG)/g' \
		> $(@D)/Dockerfile
	docker build -t $(PROJECT_NAME)-ccenv $(@D)
	docker tag $(PROJECT_NAME)-ccenv $(PROJECT_NAME)-ccenv:$(DOCKER_TAG)
209
210
	@touch $@

211
# Special override for java-image
212
213
214
215
# Following items are packed and sent to docker context while building image
# 1. Java shim layer source code
# 2. Proto files used to generate java classes
# 3. Gradle settings file
216
217
218
build/image/javaenv/.dummy: Makefile $(JAVASHIM_DEPS)
	@echo "Building docker javaenv-image"
	@mkdir -p $(@D)
219
	@cat images/javaenv/Dockerfile.in > $(@D)/Dockerfile
220
	@git ls-files core/chaincode/shim/java | tar -jcT - > $(@D)/javashimsrc.tar.bz2
221
	@git ls-files protos core/chaincode/shim/table.proto settings.gradle  | tar -jcT - > $(@D)/protos.tar.bz2
222
223
	docker build -t $(PROJECT_NAME)-javaenv $(@D)
	docker tag $(PROJECT_NAME)-javaenv $(PROJECT_NAME)-javaenv:$(DOCKER_TAG)
224
225
	@touch $@

226
227
228
229
230
# Default rule for image creation
build/image/%/.dummy: build/image/src/.dummy build/docker/bin/%
	$(eval TARGET = ${patsubst build/image/%/.dummy,%,${@}})
	@echo "Building docker $(TARGET)-image"
	@mkdir -p $(@D)/bin
231
232
233
	@cat images/app/Dockerfile.in \
		| sed -e 's/_TAG_/$(DOCKER_TAG)/g' \
		> $(@D)/Dockerfile
234
	cp build/docker/bin/$(TARGET) $(@D)/bin
235
236
	docker build -t $(PROJECT_NAME)-$(TARGET) $(@D)
	docker tag $(PROJECT_NAME)-$(TARGET) $(PROJECT_NAME)-$(TARGET):$(DOCKER_TAG)
237
238
239
240
241
242
	@touch $@

.PHONY: protos
protos: gotools
	./devenv/compile_protos.sh

243
244
245
246
base-image-clean:
	-docker rmi -f $(PROJECT_NAME)-baseimage
	-@rm -rf build/image/base ||:

247
248
src-image-clean: ccenv-image-clean peer-image-clean membersrvc-image-clean

249
250
%-image-clean:
	$(eval TARGET = ${patsubst %-image-clean,%,${@}})
251
	-docker images -q $(PROJECT_NAME)-$(TARGET) | xargs -r docker rmi -f
252
253
	-@rm -rf build/image/$(TARGET) ||:

254
images-clean: $(patsubst %,%-image-clean, $(IMAGES))
255
256
257
258
259
260
261
262
263
264
265
266

.PHONY: $(SUBDIRS:=-clean)
$(SUBDIRS:=-clean):
	cd $(patsubst %-clean,%,$@) && $(MAKE) clean

.PHONY: clean
clean: images-clean $(filter-out gotools-clean, $(SUBDIRS:=-clean))
	-@rm -rf build ||:

.PHONY: dist-clean
dist-clean: clean gotools-clean
	-@rm -rf /var/hyperledger/* ||: