Update .gitreview file
[src/xds/xds-agent.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-agent
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 # Configurable variables for installation (default /opt/AGL/...)
42 ifeq ($(origin DESTDIR), undefined)
43         DESTDIR := /opt/AGL/xds/agent
44 endif
45 ifeq ($(origin DESTDIR_WWW), undefined)
46         DESTDIR_WWW := $(DESTDIR)/www
47 endif
48
49 HOST_GOOS=$(shell go env GOOS)
50 HOST_GOARCH=$(shell go env GOARCH)
51 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
52 REPOPATH=gerrit.automotivelinux.org/gerrit/src/xds/$(TARGET)
53
54 EXT=
55 ifeq ($(HOST_GOOS), windows)
56         EXT=.exe
57 endif
58
59 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
60 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
61 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../../../..)
62 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
63 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
64 PACKAGE_DIR := $(ROOT_SRCDIR)/package
65 PACKAGE_LIST=./lib/... .
66
67 export GO111MODULE=on
68 export GOPATH := $(ROOT_GOPRJ):$(shell go env GOPATH)
69 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
70
71 # Check Go version
72 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
73 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
74 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
75 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 12 \) ] && echo true)
76 CHECKERRMSG := "ERROR: Go version 1.12 or higher is requested (current detected version: $(GOVERSION))."
77
78
79 VERBOSE_1 := -v
80 VERBOSE_2 := -v -x
81
82 # Release or Debug mode
83 ifeq ($(filter 1,$(RELEASE) $(REL)),)
84         GO_LDFLAGS=
85         # disable compiler optimizations and inlining
86         GO_GCFLAGS=-N -l
87         BUILD_MODE="Debug mode"
88         WEBAPP_BUILD_RULE=build
89 else
90         # optimized code without debug info
91         GO_LDFLAGS=-s -w
92         GO_GCFLAGS=
93         BUILD_MODE="Release mode"
94         WEBAPP_BUILD_RULE=build:prod
95 endif
96
97 # Build Package name (model: <target>_<arch>-<version>.<nb_commit_from_last_tag>.zip)
98 ifeq (-g,$(findstring -g,$(GIT_DESC)))
99         NB_COMMIT=$(firstword $(subst -, ,$(SUB_VERSION)))
100 else
101         NB_COMMIT=0
102 endif
103 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).$(NB_COMMIT).zip
104
105 .PHONY: all
106 all: tools/syncthing build
107
108 .PHONY: build
109 build: checkgover gomod xds webapp
110
111 xds: scripts tools/syncthing/copytobin
112         @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
113         @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)" .
114
115 .PHONY: test
116 test: checkgorace
117         go clean -testcache
118         go test --race $(PACKAGE_LIST)
119
120 vet:
121         go vet $(PACKAGE_LIST)
122
123 fmt:
124         go fmt $(PACKAGE_LIST)
125
126 run: build/xds tools/syncthing/copytobin
127         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log info $(XDS_AGENT_RUN_ARGS)
128
129 debug: build/xds tools/syncthing/copytobin
130         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log debug $(XDS_AGENT_DEBUG_ARGS)
131
132 .PHONY: clean
133 clean:
134         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
135
136 .PHONY: distclean
137 distclean: clean
138         (cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./vendor ./*.zip ./webapp/dist ./webapp/node_modules)
139          go clean -modcache
140
141 .PHONY: clean-lock
142 clean-lock: distclean
143         (cd $(ROOT_SRCDIR) && rm -f ./go.sum ./webapp/package-lock.json)
144
145 webapp: webapp/install
146 ifneq ($(SKIP_WEBAPP_BUILD), true)
147         cd webapp && npm run $(WEBAPP_BUILD_RULE)
148 else
149         @echo "Skipping webapp build (SKIP_WEBAPP_BUILD var is set)"
150 endif
151
152 webapp/debug:
153         cd webapp && npm run watch
154
155 webapp/install:
156 ifneq ($(SKIP_WEBAPP_BUILD), true)
157         (cd webapp && npm install)
158 else
159         @echo "Skipping webapp install (SKIP_WEBAPP_BUILD var is set)"
160 endif
161         @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
162
163 .PHONY: install
164 install:
165         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
166         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
167         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh
168
169 .PHONY: uninstall
170 uninstall:
171         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
172
173 package: clean tools/syncthing vendor build
174         @mkdir -p $(PACKAGE_DIR)/$(TARGET)/www $(PACKAGE_DIR)/scripts
175         @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/$(TARGET)/
176         @cp -a webapp/dist/* $(PACKAGE_DIR)/$(TARGET)/www/
177 ifneq ($(GOOS), windows)
178                 @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)/
179 endif
180         @cp $(ROOT_SRCDIR)/conf.d/etc/xds/agent/agent-config.json $(PACKAGE_DIR)/$(TARGET)/agent-config.json.in
181         @cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
182         @echo "### Package $(PACKAGE_ZIPFILE) has been successfuly built - $(BUILD_MODE)"
183
184 .PHONY: package-all
185 package-all:
186         @echo "# Build linux amd64..."
187         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
188         @echo "# Build windows amd64..."
189         GOOS=windows GOARCH=amd64 RELEASE=1 SKIP_WEBAPP_BUILD=true make -f $(ROOT_SRCDIR)/Makefile package
190         @echo "# Build darwin amd64..."
191         GOOS=darwin GOARCH=amd64 RELEASE=1 SKIP_WEBAPP_BUILD=true make -f $(ROOT_SRCDIR)/Makefile package
192         make -f $(ROOT_SRCDIR)/Makefile clean
193
194 .PHONY: gomod
195 gomod:
196         go get
197
198 gomod/debug:
199         @echo "replace gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git => $(ROOT_SRCDIR)/../xds-common" >> $(ROOT_SRCDIR)/go.mod
200         @echo "replace gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git => $(ROOT_SRCDIR)/../xds-server" >> $(ROOT_SRCDIR)/go.mod
201         @echo "Add replace in go.mod file - done."
202
203 vendor: gomod
204         go mod vendor
205
206 vendor/debug: vendor
207         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
208                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git && \
209                 rm -rf xds-server.git && ln -s ../../../../../../xds-server xds-server.git )
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/get-syncthing.sh; }
219
220 .PHONY: tools/syncthing/copytobin
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 "  package"
240         @echo "  install"
241         @echo "  uninstall"
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'."