1 ###########################################################################
2 # Copyright 2017-2019 IoT.bzh
4 # author: Sebastien Douheret <sebastien@iot.bzh>
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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 ###########################################################################
23 # Syncthing version to install
24 SYNCTHING_VERSION = 0.14.38
25 SYNCTHING_INOTIFY_VERSION = 0.8.7
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))
35 VERSION := unknown-dev
37 ifeq ($(SUB_VERSION), )
38 SUB_VERSION := $(shell date +'%Y-%m-%d_%H%M%S')
41 # Configurable variables for installation (default /opt/AGL/...)
42 ifeq ($(origin DESTDIR), undefined)
43 DESTDIR := /opt/AGL/xds/agent
45 ifeq ($(origin DESTDIR_WWW), undefined)
46 DESTDIR_WWW := $(DESTDIR)/www
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)
55 ifeq ($(HOST_GOOS), windows)
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/... .
68 export GOPATH := $(ROOT_GOPRJ):$(shell go env GOPATH)
69 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
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))."
82 # Release or Debug mode
83 ifeq ($(filter 1,$(RELEASE) $(REL)),)
85 # disable compiler optimizations and inlining
87 BUILD_MODE="Debug mode"
88 WEBAPP_BUILD_RULE=build
90 # optimized code without debug info
93 BUILD_MODE="Release mode"
94 WEBAPP_BUILD_RULE=build:prod
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)))
103 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).$(NB_COMMIT).zip
106 all: tools/syncthing build
109 build: checkgover gomod xds webapp
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)" .
118 go test --race $(PACKAGE_LIST)
121 go vet $(PACKAGE_LIST)
124 go fmt $(PACKAGE_LIST)
126 run: build/xds tools/syncthing/copytobin
127 $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log info $(XDS_AGENT_RUN_ARGS)
129 debug: build/xds tools/syncthing/copytobin
130 $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log debug $(XDS_AGENT_DEBUG_ARGS)
134 rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
138 (cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./vendor ./*.zip ./webapp/dist ./webapp/node_modules)
142 clean-lock: distclean
143 (cd $(ROOT_SRCDIR) && rm -f ./go.sum ./webapp/package-lock.json)
145 webapp: webapp/install
146 ifneq ($(SKIP_WEBAPP_BUILD), true)
147 cd webapp && npm run $(WEBAPP_BUILD_RULE)
149 @echo "Skipping webapp build (SKIP_WEBAPP_BUILD var is set)"
153 cd webapp && npm run watch
156 ifneq ($(SKIP_WEBAPP_BUILD), true)
157 (cd webapp && npm install)
159 @echo "Skipping webapp install (SKIP_WEBAPP_BUILD var is set)"
161 @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
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
171 export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
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)/
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)"
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
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."
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 )
211 .PHONY: 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; }
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)
228 @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
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; }
236 @echo "Main supported rules:"
237 @echo " all (default)"
245 @echo "Influential make variables:"
246 @echo " V - Build verbosity {0,1,2}."
247 @echo " BUILD_ENV_FLAGS - Environment added to 'go build'."