Use go module as dependency tool instead of glide
[src/xds/xds-server.git] / Makefile
1  ###########################################################################
2 # Copyright 2017-2019 IoT.bzh
3 #
4 # author: Sebastien Douheret <sebastien@iot.bzh>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 ###########################################################################
18
19 # Application Name
20 TARGET=xds-server
21
22
23 # Syncthing version to install
24 SYNCTHING_VERSION = 0.14.38
25 SYNCTHING_INOTIFY_VERSION = 0.8.7
26
27
28 # Retrieve git tag/commit to set version & sub-version strings
29 GIT_DESC := $(shell git describe --always --tags --match "[0-9]*")
30 VERSION := $(firstword $(subst -, ,$(GIT_DESC)))
31 ifeq (-,$(findstring -,$(GIT_DESC)))
32         SUB_VERSION := $(subst $(VERSION)-,,$(GIT_DESC))
33 endif
34 ifeq ($(VERSION), )
35         VERSION := unknown-dev
36 endif
37 ifeq ($(SUB_VERSION), )
38         SUB_VERSION := $(shell date +'%Y-%m-%d_%H%M%S')
39 endif
40
41 # for backward compatibility
42 ifneq ($(origin INSTALL_DIR), undefined)
43         DESTDIR := $(INSTALL_DIR)
44 endif
45 ifneq ($(origin INSTALL_WEBAPP_DIR), undefined)
46         DESTDIR_WWW := $(INSTALL_WEBAPP_DIR)
47 endif
48
49 # Configurable variables for installation (default /opt/AGL/...)
50 ifeq ($(origin DESTDIR), undefined)
51         DESTDIR := /opt/AGL/xds/server
52 endif
53 ifeq ($(origin DESTDIR_WWW), undefined)
54         DESTDIR_WWW := $(DESTDIR)/www
55 endif
56
57 HOST_GOOS=$(shell go env GOOS)
58 HOST_GOARCH=$(shell go env GOARCH)
59 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
60 REPOPATH=gerrit.automotivelinux.org/gerrit/src/xds/$(TARGET)
61
62 EXT=
63 ifeq ($(HOST_GOOS), windows)
64         EXT=.exe
65 endif
66
67 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
68 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
69 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../../../..)
70 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
71 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
72 PACKAGE_DIR := $(ROOT_SRCDIR)/package
73 PACKAGE_LIST=./lib/... ./test/... .
74
75 export GO111MODULE=on
76 export GOPATH := $(ROOT_GOPRJ):$(shell go env GOPATH)
77 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
78
79 # Check Go version
80 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
81 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
82 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
83 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 12 \) ] && echo true)
84 CHECKERRMSG := "ERROR: Go version 1.12 or higher is requested (current detected version: $(GOVERSION))."
85
86
87 VERBOSE_1 := -v
88 VERBOSE_2 := -v -x
89
90 # Release or Debug mode
91 ifeq ($(filter 1,$(RELEASE) $(REL)),)
92         GO_LDFLAGS=
93         # disable compiler optimizations and inlining
94         GO_GCFLAGS=-N -l
95         BUILD_MODE="Debug mode"
96         WEBAPP_BUILD_RULE=build
97 else
98         # optimized code without debug info
99         GO_LDFLAGS=-s -w
100         GO_GCFLAGS=
101         BUILD_MODE="Release mode"
102         WEBAPP_BUILD_RULE=build:prod
103 endif
104
105 # Build Package name (model: <target>_<arch>-<version>.<nb_commit_from_last_tag>.zip)
106 ifeq (-g,$(findstring -g,$(GIT_DESC)))
107         NB_COMMIT=$(firstword $(subst -, ,$(SUB_VERSION)))
108 else
109         NB_COMMIT=0
110 endif
111 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).$(NB_COMMIT).zip
112
113 .PHONY: all
114 all: tools/syncthing build
115
116 .PHONY: build
117 build: checkgover gomod xds webapp
118
119 xds: scripts tools/syncthing/copytobin
120         @echo "### Build XDS server (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
121         @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/$(TARGET)$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
122
123 .PHONY: test
124 test: checkgorace
125         go clean -testcache
126 ifndef name
127         go test --race ./test -v
128 else
129         go test --race ./test -v -run $(name)
130 endif
131
132 vet:
133         go vet $(PACKAGE_LIST)
134
135 fmt:
136         go fmt $(PACKAGE_LIST)
137
138 run: build/xds tools/syncthing/copytobin
139         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log info $(XDS_SERVER_RUN_ARGS)
140
141 debug: build/xds tools/syncthing/copytobin
142         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log debug $(XDS_SERVER_DEBUG_ARGS)
143
144 .PHONY: clean
145 clean:
146         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
147
148 .PHONY: distclean
149 distclean: clean
150         (cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./vendor ./*.zip ./webapp/dist ./webapp/node_modules ./scripts/sdks/agl/sdks_*.json)
151          go clean -modcache
152
153 .PHONY: clean-lock
154 clean-lock: distclean
155         (cd $(ROOT_SRCDIR) && rm -f ./go.sum ./webapp/package-lock.json)
156
157 webapp: webapp/install
158         mkdir -p $(ROOT_SRCDIR)/webapp/dist $(ROOT_SRCDIR)/webapp/dist/fonts
159         (cd $(ROOT_SRCDIR)/webapp && cp -a ./assets ./src/index.html ./node_modules/font-awesome/css/font-awesome.min.css ./dist/)
160         (cd $(ROOT_SRCDIR)/webapp && cp -a ./node_modules/font-awesome/fonts/* ./dist/fonts/)
161
162 webapp/install:
163         (cd webapp && npm install)
164         @[ -d ${DESTDIR}/usr ] && { echo "Removing unwanted ${DESTDIR}/usr directory"; rm -rf ${DESTDIR}/usr; } || true
165
166 .PHONY: scripts
167 scripts:
168         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/xds-utils scripts/sdks $(LOCAL_BINDIR)
169
170 .PHONY: conffile
171 conffile:
172         cat $(ROOT_SRCDIR)/conf.d/etc/xds/server/server-config.json \
173                 | sed -e s,"www","$(DESTDIR_WWW)",g \
174                  > $(DESTDIR)/server-config.json.in
175
176 .PHONY: install
177 install:
178         @test -e $(LOCAL_BINDIR)/xds-server$(EXT) -a -d webapp/dist || { echo "Please execute first: make all"; exit 1; }
179         @test -d $(LOCAL_BINDIR)/xds-utils || { echo "Please execute first: make all"; exit 1; }
180         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all"; exit 1; }
181         mkdir -p $(DESTDIR) \
182                 && cp -a $(LOCAL_BINDIR)/* $(DESTDIR)
183         mkdir -p $(DESTDIR_WWW) \
184                 && cp -a webapp/dist/* $(DESTDIR_WWW)
185
186 .PHONY: _package
187 _package: clean
188         make -f $(ROOT_SRCDIR)/Makefile all install  DESTDIR=$(PACKAGE_DIR)/xds-server
189         make -f $(ROOT_SRCDIR)/Makefile conffile  DESTDIR=$(PACKAGE_DIR)/xds-server DESTDIR_WWW=www
190         cp -r $(ROOT_SRCDIR)/conf.d $(PACKAGE_DIR)/xds-server
191         rm -f $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE)
192         (cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-server)
193
194 # On support Linux for now
195 .PHONY: package
196 package:
197         @echo "# Build linux amd64..."
198         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile _package
199         make -f $(ROOT_SRCDIR)/Makefile clean
200
201 .PHONY: package-all
202 package-all: package
203
204 .PHONY: gomod
205 gomod:
206         go get
207
208 gomod/debug:
209         @echo "replace gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git => $(ROOT_SRCDIR)/../xds-common" >> $(ROOT_SRCDIR)/go.mod
210         @echo "Add replace in go.mod file - done."
211
212 vendor: gomod
213         go mod vendor
214
215 vendor/debug: vendor
216         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
217                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git  )
218
219 .PHONY: tools/syncthing
220 tools/syncthing:
221         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
222         mkdir -p $(LOCAL_TOOLSDIR); \
223         DESTDIR=$(LOCAL_TOOLSDIR) \
224         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
225         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
226         ./scripts/xds-utils/get-syncthing.sh; }
227
228 .PHONY: tools/syncthing/copytobin
229 tools/syncthing/copytobin:
230         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
231         @mkdir -p $(LOCAL_BINDIR)
232         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
233
234 .PHONY:
235 checkgover:
236         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
237
238 .PHONY:
239 checkgorace: checkgover
240         @ls $(shell go env GOROOT)/src/runtime/race/*.syso 1> /dev/null 2>&1 || { echo "ERROR: go-race package mandatory to run test. Please install it, for example: zypper install go-race"; exit 1; }
241
242 .PHONY: help
243 help:
244         @echo "Main supported rules:"
245         @echo "  all                (default)"
246         @echo "  build"
247         @echo "  test        (use: name=TestExec to run a specific test)"
248         @echo "  package"
249         @echo "  install"
250         @echo "  clean"
251         @echo "  distclean"
252         @echo ""
253         @echo "Influential make variables:"
254         @echo "  V                 - Build verbosity {0,1,2}."
255         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."