-
Notifications
You must be signed in to change notification settings - Fork 51
/
Makefile
375 lines (324 loc) · 10.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
BASE_DIR := ${shell pwd | sed -e 's/ /\\ /g'}
# Control build verbosity
# V=1,2: Enable echo of commands
# V=2: Enable bug/verbose options in tools and scripts
ifeq ($(V),1)
export Q :=
else
ifeq ($(V),2)
export Q :=
else
export Q := @
endif
endif
VERSION := v$(shell cat VERSION)
CONTAINER_VERSION="latest"
DOCKER_IMAGE="lfedge/edge-home-orchestration-go"
-include $(BASE_DIR)/.config
ifeq ($(CONFIG_MNEDC_SERVER),y)
RUN_OPTIONS += -e MNEDC=server
endif
ifeq ($(CONFIG_MNEDC_CLIENT),y)
RUN_OPTIONS += -e MNEDC=client
endif
ifeq ($(CONFIG_SECURE_MODE),y)
RUN_OPTIONS += -e SECURE=true
endif
ifeq ($(CONFIG_CLOUD_SYNC),y)
RUN_OPTIONS += -e CLOUD_SYNC=true
endif
ifeq ($(CONFIG_WEB_UI),y)
RUN_OPTIONS += -e WEBUI=true
endif
ifeq ($(CONFIG_LOGLEVEL),y)
RUN_OPTIONS += -e LOGLEVEL=$(subst ",,$(CONFIG_LOGLEVEL_VALUE))
endif
# Go parameters
GOCMD := GO111MODULE=on go
GOBUILD := $(GOCMD) build
GOCLEAN := $(GOCMD) clean
GOLINT := golint
GOVET := $(GOCMD) vet
GOCOVER := gocov
GOMOBILE := gomobile
DOCKER := docker
GO_COMMIT_ID := $(shell git rev-parse --short HEAD)
GO_LDFLAGS := -ldflags '-extldflags "-static" -X main.version=$(VERSION) -X main.commitID=$(GO_COMMIT_ID)'
GO_MOBILE_LDFLAGS := -ldflags '-X main.version=$(VERSION) -X main.commitID=$(GO_COMMIT_ID)'
# Target parameters
PKG_NAME := edge-orchestration
# Go Application target
CMD_DIR := $(BASE_DIR)/cmd
CMD_SRC := $(CMD_DIR)/edge-orchestration/main.go
BIN_DIR := $(BASE_DIR)/bin
BIN_FILE := $(PKG_NAME)
WEB_DIR := $(BASE_DIR)/web
# Go Library for C-archive
LIBRARY_NAME := liborchestration
HEADER_FILE := orchestration.h
LIBRARY_FILE := liborchestration.a
CUR_HEADER_FILE := $(LIBRARY_NAME).h
CUR_LIBRARY_FILE := $(LIBRARY_NAME).a
OBJ_SRC_DIR := $(CMD_DIR)/edge-orchestration/capi
INTERFACE_OUT_DIR := $(BIN_DIR)/capi/output
ifeq ($(CONFIG_ARM),y)
INTERFACE_OUT_INC_DIR := $(INTERFACE_OUT_DIR)/inc/linux_arm
INTERFACE_OUT_BIN_DIR := $(INTERFACE_OUT_DIR)/bin/linux_arm
INTERFACE_OUT_LIB_DIR := $(INTERFACE_OUT_DIR)/lib/linux_arm
CONTAINER_ARCH="arm32v7"
GOARCH=arm
CC="arm-linux-gnueabi-gcc"
GOARM=7
ANDROID_TARGET="android/arm"
else ifeq ($(CONFIG_ARM64),y)
INTERFACE_OUT_INC_DIR := $(INTERFACE_OUT_DIR)/inc/linux_aarch64
INTERFACE_OUT_BIN_DIR := $(INTERFACE_OUT_DIR)/bin/linux_aarch64
INTERFACE_OUT_LIB_DIR := $(INTERFACE_OUT_DIR)/lib/linux_aarch64
CONTAINER_ARCH="arm64v8"
GOARCH=arm64
CC="aarch64-linux-gnu-gcc"
ANDROID_TARGET="android/arm64"
else ifeq ($(CONFIG_X86),y)
INTERFACE_OUT_INC_DIR := $(INTERFACE_OUT_DIR)/inc/linux_x86
INTERFACE_OUT_BIN_DIR := $(INTERFACE_OUT_DIR)/bin/linux_x86
INTERFACE_OUT_LIB_DIR := $(INTERFACE_OUT_DIR)/lib/linux_x86
CONTAINER_ARCH="i386"
GOARCH=386
CC="gcc"
ANDROID_TARGET="android/386"
else ifeq ($(CONFIG_X86_64),y)
INTERFACE_OUT_INC_DIR := $(INTERFACE_OUT_DIR)/inc/linux_x86-64
INTERFACE_OUT_BIN_DIR := $(INTERFACE_OUT_DIR)/bin/linux_x86-64
INTERFACE_OUT_LIB_DIR := $(INTERFACE_OUT_DIR)/lib/linux_x86-64
CONTAINER_ARCH="amd64"
GOARCH=amd64
CC="gcc"
ANDROID_TARGET="android/amd64"
endif
# Go 3rdParty packages
BUILD_VENDOR_DIR := $(BASE_DIR)/vendor
# Go Library for android
ANDROID_LIBRARY_NAME := liborchestration
ANDROID_LIBRARY_FILE := liborchestration.aar
ANDROID_JAR_FILE := liborchestration-sources.jar
ANDROID_SRC_DIR := $(CMD_DIR)/edge-orchestration/javaapi
ANDROID_LIBRARY_OUT_DIR := $(BIN_DIR)/javaapi/output
.DEFAULT_GOAL := all
TEST_PKG_DIRS = $(filter-out $@,$(MAKECMDGOALS))
ifeq ($(word 2, $(TEST_PKG_DIRS)),)
TEST_PKG_DIRS="internal/..."
endif
define print_header
@ echo ""
@ echo "--------------------------------------"
@ echo " "$1
@ echo "--------------------------------------"
endef
define go-vendor
$(call print_header, "Go Mod Vendor")
$(GOCMD) mod vendor
endef
## edge-orchestration binary build
define build_binary
$(call print_header, "Create Executable binary")
GOARM=$(GOARM) GOARCH=$(GOARCH) $(GOBUILD) -a $(GO_LDFLAGS) -o $(BIN_DIR)/$(BIN_FILE) $(CMD_SRC) || exit 1
$(Q) ls -al $(BIN_DIR)
endef
## edge-orchestration static archive build
define build-object-c
$(call print_header, "Create Static object of Orchestration for $(GOARCH)")
$(Q) mkdir -p $(INTERFACE_OUT_INC_DIR) $(INTERFACE_OUT_LIB_DIR)
CGO_ENABLED=1 GOARM=$(GOARM) GOARCH=$(GOARCH) CC=$(CC) $(GOBUILD) $(GO_LDFLAGS) -o $(INTERFACE_OUT_LIB_DIR)/$(CUR_LIBRARY_FILE) -buildmode=c-archive $(OBJ_SRC_DIR) || exit 1
$(Q) mv $(INTERFACE_OUT_LIB_DIR)/$(CUR_HEADER_FILE) $(INTERFACE_OUT_INC_DIR)/$(HEADER_FILE)
$(Q) ls -al $(INTERFACE_OUT_LIB_DIR)
endef
## edge-orchestration archive
define build-result
tree $(INTERFACE_OUT_DIR)
tree $(ANDROID_LIBRARY_OUT_DIR)
endef
## edge-orchestration android library build
build-object-java:
$(Q) mkdir -p $(ANDROID_LIBRARY_OUT_DIR)
$(Q) rm -rf $(BUILD_VENDOR_DIR)
$(Q) go get golang.org/x/[email protected]
$(GOMOBILE) bind $(GO_MOBILE_LDFLAGS) -o $(ANDROID_LIBRARY_OUT_DIR)/$(ANDROID_LIBRARY_FILE) -target=$(ANDROID_TARGET) -androidapi=23 $(ANDROID_SRC_DIR) || exit 1
$(Q) go mod tidy
$(Q) ls -al $(ANDROID_LIBRARY_OUT_DIR)
## webui build
build-webui:
$(call print_header, "Build web contents")
cd web && npm install && npm run build
$(Q) ls -al $(WEB_DIR)
## edge-orchestration container build
build_docker_container:
$(call print_header, "Create Docker container $(CONTAINER_ARCH)")
-docker rm -f $(PKG_NAME)
-docker rmi -f $(DOCKER_IMAGE):$(CONTAINER_VERSION)
$(Q) mkdir -p $(BASE_DIR)/bin/qemu
ifeq ($(CONFIG_ARM),y)
ifneq ($(shell uname -m),armv7l)
$(Q) cp /usr/bin/qemu-arm-static $(BASE_DIR)/bin/qemu
endif
endif
ifeq ($(CONFIG_ARM64),y)
ifneq ($(shell uname -m),aarch64)
$(Q) cp /usr/bin/qemu-aarch64-static $(BASE_DIR)/bin/qemu
endif
endif
$(DOCKER) build --tag $(DOCKER_IMAGE):$(CONTAINER_VERSION) --file $(BASE_DIR)/Dockerfile --build-arg PLATFORM=$(CONTAINER_ARCH) .
-docker save -o $(BASE_DIR)/bin/edge-orchestration.tar $(DOCKER_IMAGE)
## go test and coverage
test-go:
$(GOCOVER) test -v ./$(TEST_PKG_DIRS) > coverage.out
$(GOCOVER) report coverage.out
$(GOCOVER)-html coverage.out > coverage.html
$(Q) -rm -rf $(BASE_DIR)/internal/controller/discoverymgr/testDB
$(Q) firefox coverage.html &
## build clean
clean: go.sum
$(call print_header, "Build clean")
$(Q) -rm -rf $(BUILD_VENDOR_DIR)
$(Q) $(GOCMD) mod tidy
$(Q) $(GOCLEAN)
$(Q) -rm -rf $(INTERFACE_OUT_DIR)
$(Q) -rm -rf $(ANDROID_LIBRARY_OUT_DIR)
$(Q) -rm -rf $(BASE_DIR)/bin/$(PKG_NAME)*
$(Q) -rm -rf $(BASE_DIR)/coverage.out
$(Q) -rm -rf $(BASE_DIR)/coverage.html
$(Q) make -C examples/native clean
distclean: clean
$(Q) rm -f .config
$(Q) rm -f .config.old
$(Q) rm -f Dockerfile
## check go style and static analysis
lint:
$(call print_header, "Analysis source code golint & go vet")
$(GOLINT) ./internal/...
$(GOVET) -v ./internal/...
staticcheck:
$(Q) -staticcheck ./...
fuzz:
$(call print_header, "Run Fuzzer (go test -fuzz)")
$(Q) ./tools/fuzz-all.sh $1
## format go files
fmt:
$(Q) make clean
$(call print_header, "Formatting source code using gofmt")
$(Q) gofmt -s -w ./internal
$(Q) gofmt -s -w ./cmd
## show help
help:
@echo 'Usage: make <TARGETS>'
@echo ''
@echo 'Available targets are:'
@echo ''
@echo ' all Build project for current platform.'
@echo ' clean Remove binaries, artifacts.'
@echo ' create_context Prepare configuration.'
@echo ' fmt Run: gofmt -s -w ./ .'
@echo ' fuzz Run: go test -fuzz .'
@echo ' help Show this help screen.'
@echo ' lint Run golint and go vet.'
@echo ' menuconfig Change configuration by kconfig-frontends.'
@echo ' run Run docker container.'
@echo ' staticcheck Run staticcheck.'
@echo ' stop Stop and remove docker container.'
@echo ' test Run unit tests.'
@echo ''
## define build target not a file
.PHONY: all binary build clean fmt fuzz help lint run staticcheck stop test
define stop_docker_container
$(call print_header, "Stop Docker container")
$(Q) if [ ! -z ${shell docker ps -a --format "{{.Names}}" --filter name=^/$(PKG_NAME)} ]; then \
docker stop $(PKG_NAME) > /dev/null ; \
fi
$(Q) docker ps -a
endef
define rm_docker_container
$(call print_header, "Remove Docker container")
$(Q) if [ ! -z ${shell docker ps -a --format "{{.Names}}" --filter name=^/$(PKG_NAME)} ]; then \
docker rm -f $(PKG_NAME) > /dev/null ; \
fi
$(Q) docker ps -a
endef
##
all: check_context
make clean
$(call go-vendor)
ifeq ($(CONFIG_CONTAINER),y)
$(Q) cp configs/defdockerfiles/$(CONFIG_DOCKERFILE) Dockerfile
$(call build_binary)
make build_docker_container
else ifeq ($(CONFIG_NATIVE),y)
$(call build-object-c)
$(call build-result)
else ifeq ($(CONFIG_ANDROID),y)
$(call print_header, "Target Binary is for Android")
$(call print_header, "Create Android archive from Java interface")
make build-object-java
$(call build-result)
endif
binary: check_context
make clean
$(call go-vendor)
ifeq ($(CONFIG_CONTAINER),y)
$(call build_binary)
else ifeq ($(CONFIG_NATIVE),y)
$(call build-object-c)
$(call build-result)
else ifeq ($(CONFIG_ANDROID),y)
$(call print_header, "Target Binary is for Android")
$(call print_header, "Create Android archive from Java interface")
make build-object-java
$(call build-result)
endif
.config:
ifeq ($(CONFIGFILE),)
$(Q) echo "" ; echo "CONFIGFILE not been specified:"
$(Q) echo " make create_context CONFIGFILE=<configfile>" ; echo "" ; exit 1
endif
$(Q) cp configs/defconfigs/$(CONFIGFILE) .config
create_context: .config
check_context:
$(Q) if [ ! -e ${BASE_DIR}/.config ]; then \
echo "" ; echo "Edge-Orchestration has not been configured:" ; \
echo " make create_context CONFIGFILE=<configfile>" ; echo "" ; \
exit 1 ; \
fi
go.sum:
$(Q) $(GOCMD) mod tidy
run:
$(call print_header, "Run Docker container ")
$(Q) docker run -it -d \
--privileged \
--network="host" \
--name $(PKG_NAME) \
$(RUN_OPTIONS) \
-v /var/edge-orchestration/:/var/edge-orchestration/:rw \
-v /var/run/docker.sock:/var/run/docker.sock:rw \
-v /proc/:/process/:ro \
$(DOCKER_IMAGE):$(CONTAINER_VERSION)
$(Q) docker container ls
stop:
$(call stop_docker_container)
$(call rm_docker_container)
test: go.sum
$(call print_header, "Build test to calculate Coverage")
$(Q) -sudo systemctl stop ${SERVICE_FILE}
$(Q) -sudo systemctl status ${SERVICE_FILE}
$(Q) mkdir -p /tmp/foo
$(call stop_docker_container)
$(call print_header, "build test for $(TEST_PKG_DIRS)")
make test-go TEST_PKG_DIRS=$(TEST_PKG_DIRS)
do_menuconfig:
$(Q) kconfig-mconf Kconfig
menuconfig: do_menuconfig
do_savedefconfig:
$(Q) cp -f .config configs/defconfigs/$(CONFIG_CONFIGFILE)
savedefconfig: do_savedefconfig
# Building binaries through multi-stage build
buildx_binary:
$(call print_header, "Create Executable binary through buildx")
$(GOBUILD) -a $(GO_LDFLAGS) -o $(BIN_DIR)/$(BIN_FILE) $(CMD_SRC) || exit 1
$(Q) ls -al $(BIN_DIR)