Add checking about go-race requested to run tests
[src/xds/xds-server.git] / Makefile
1  ###########################################################################
2 # Copyright 2017-2018 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
74 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
75 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
76
77 # Check Go version
78 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
79 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
80 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
81 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 9 \) ] && echo true)
82 CHECKERRMSG := "ERROR: Go version 1.9.0 or higher is requested (current detected version: $(GOVERSION))."
83
84
85 VERBOSE_1 := -v
86 VERBOSE_2 := -v -x
87
88 # Release or Debug mode
89 ifeq ($(filter 1,$(RELEASE) $(REL)),)
90         GO_LDFLAGS=
91         # disable compiler optimizations and inlining
92         GO_GCFLAGS=-N -l
93         BUILD_MODE="Debug mode"
94         WEBAPP_BUILD_RULE=build
95 else
96         # optimized code without debug info
97         GO_LDFLAGS=-s -w
98         GO_GCFLAGS=
99         BUILD_MODE="Release mode"
100         WEBAPP_BUILD_RULE=build:prod
101 endif
102
103 # Build Package name (model: <target>_<arch>-<version>.<nb_commit_from_last_tag>.zip)
104 ifeq (-g,$(findstring -g,$(GIT_DESC)))
105         NB_COMMIT=$(firstword $(subst -, ,$(SUB_VERSION)))
106 else
107         NB_COMMIT=0
108 endif
109 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).$(NB_COMMIT).zip
110
111 .PHONY: all
112 all: tools/syncthing build
113
114 .PHONY: build
115 build: checkgover vendor xds webapp
116
117 xds: scripts tools/syncthing/copytobin
118         @echo "### Build XDS server (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
119         @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)" .
120
121 .PHONY: test
122 test: checkgorace tools/glide
123 ifndef name
124         GOCACHE=off go test --race ./test -v
125 else
126         GOCACHE=off go test --race ./test -v -run $(name)
127 endif
128
129 vet: tools/glide
130         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
131
132 fmt: tools/glide
133         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
134
135 run: build/xds tools/syncthing/copytobin
136         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log info $(XDS_SERVER_RUN_ARGS)
137
138 debug: build/xds tools/syncthing/copytobin
139         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log debug $(XDS_SERVER_DEBUG_ARGS)
140
141 .PHONY: clean
142 clean:
143         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
144
145 .PHONY: distclean
146 distclean: clean
147         (cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./glide.lock ./vendor ./*.zip ./webapp/dist ./webapp/node_modules ./scripts/sdks/agl/sdks_*.json)
148
149 webapp: webapp/install
150         mkdir -p $(ROOT_SRCDIR)/webapp/dist $(ROOT_SRCDIR)/webapp/dist/fonts
151         (cd $(ROOT_SRCDIR)/webapp && cp -a ./assets ./src/index.html ./node_modules/font-awesome/css/font-awesome.min.css ./dist/)
152         (cd $(ROOT_SRCDIR)/webapp && cp -a ./node_modules/font-awesome/fonts/* ./dist/fonts/)
153
154 webapp/install:
155         (cd webapp && npm install)
156         @[ -d ${DESTDIR}/usr ] && { echo "Removing unwanted ${DESTDIR}/usr directory"; rm -rf ${DESTDIR}/usr; } || true
157
158 .PHONY: scripts
159 scripts:
160         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/xds-utils scripts/sdks $(LOCAL_BINDIR)
161
162 .PHONY: conffile
163 conffile:
164         cat $(ROOT_SRCDIR)/conf.d/etc/xds/server/server-config.json \
165                 | sed -e s,"www","$(DESTDIR_WWW)",g \
166                  > $(DESTDIR)/server-config.json.in
167
168 .PHONY: install
169 install:
170         @test -e $(LOCAL_BINDIR)/xds-server$(EXT) -a -d webapp/dist || { echo "Please execute first: make all"; exit 1; }
171         @test -d $(LOCAL_BINDIR)/xds-utils || { echo "Please execute first: make all"; exit 1; }
172         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all"; exit 1; }
173         mkdir -p $(DESTDIR) \
174                 && cp -a $(LOCAL_BINDIR)/* $(DESTDIR)
175         mkdir -p $(DESTDIR_WWW) \
176                 && cp -a webapp/dist/* $(DESTDIR_WWW)
177
178 .PHONY: _package
179 _package: clean
180         make -f $(ROOT_SRCDIR)/Makefile all install  DESTDIR=$(PACKAGE_DIR)/xds-server
181         make -f $(ROOT_SRCDIR)/Makefile conffile  DESTDIR=$(PACKAGE_DIR)/xds-server DESTDIR_WWW=www
182         cp -r $(ROOT_SRCDIR)/conf.d $(PACKAGE_DIR)/xds-server
183         rm -f $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE)
184         (cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-server)
185
186 # On support Linux for now
187 .PHONY: package
188 package:
189         @echo "# Build linux amd64..."
190         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile _package
191         make -f $(ROOT_SRCDIR)/Makefile clean
192
193 .PHONY: package-all
194 package-all: package
195
196 vendor: tools/glide glide.yaml
197         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
198
199 vendor/debug: vendor
200         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
201                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git  )
202
203 .PHONY: tools/glide
204 tools/glide:
205         @test -f $(LOCAL_TOOLSDIR)/glide || { \
206                 echo "Downloading glide"; \
207                 mkdir -p $(LOCAL_TOOLSDIR); \
208                 curl --silent --connect-timeout 60 --retry 3 -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
209         }
210
211 .PHONY: tools/syncthing
212 tools/syncthing:
213         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
214         mkdir -p $(LOCAL_TOOLSDIR); \
215         DESTDIR=$(LOCAL_TOOLSDIR) \
216         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
217         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
218         ./scripts/xds-utils/get-syncthing.sh; }
219
220 .PHONY:
221 tools/syncthing/copytobin:
222         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
223         @mkdir -p $(LOCAL_BINDIR)
224         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
225
226 .PHONY:
227 checkgover:
228         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
229
230 .PHONY:
231 checkgorace: checkgover
232         @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; }
233
234 .PHONY: help
235 help:
236         @echo "Main supported rules:"
237         @echo "  all         (default)"
238         @echo "  build"
239         @echo "  test        (use: name=TestExec to run a specific test)"
240         @echo "  package"
241         @echo "  install"
242         @echo "  clean"
243         @echo "  distclean"
244         @echo ""
245         @echo "Influential make variables:"
246         @echo "  V                 - Build verbosity {0,1,2}."
247         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."