1 ###########################################################################
2 # Copyright 2017-2018 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)
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
66 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
67 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
70 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
71 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
72 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
73 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 8 \) ] && echo true)
74 CHECKERRMSG := "ERROR: Go version 1.8.1 or higher is requested (current detected version: $(GOVERSION))."
80 # Release or Debug mode
81 ifeq ($(filter 1,$(RELEASE) $(REL)),)
83 # disable compiler optimizations and inlining
85 BUILD_MODE="Debug mode"
86 WEBAPP_BUILD_RULE=build
88 # optimized code without debug info
91 BUILD_MODE="Release mode"
92 WEBAPP_BUILD_RULE=build:prod
95 ifeq ($(SUB_VERSION), )
96 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).zip
98 # only use dot as separator to allow rpm packaging (see version .spec file)
99 PK_VER := $(subst _,.,$(subst -,,$(VERSION)))
100 PK_SBVER := $(subst _,.,$(subst -,,$(SUB_VERSION)))
101 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(PK_VER).$(PK_SBVER).zip
105 all: tools/syncthing build
108 build: checkgover vendor xds webapp
110 xds: scripts tools/syncthing/copytobin
111 @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
112 @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)" .
115 go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
118 go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
121 go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
123 run: build/xds tools/syncthing/copytobin
124 $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log info $(XDS_AGENT_RUN_ARGS)
126 debug: build/xds tools/syncthing/copytobin
127 $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log debug $(XDS_AGENT_DEBUG_ARGS)
131 rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
135 (cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./glide.lock ./vendor ./*.zip ./webapp/dist ./webapp/node_modules)
137 webapp: webapp/install
138 ifneq ($(SKIP_WEBAPP_BUILD), true)
139 cd webapp && npm run $(WEBAPP_BUILD_RULE)
141 @echo "Skipping webapp build (SKIP_WEBAPP_BUILD var is set)"
145 cd webapp && npm run watch
148 ifneq ($(SKIP_WEBAPP_BUILD), true)
149 (cd webapp && npm install)
151 @echo "Skipping webapp install (SKIP_WEBAPP_BUILD var is set)"
153 @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
157 @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
158 @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
159 export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh
163 export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
165 package: clean tools/syncthing vendor build
166 @mkdir -p $(PACKAGE_DIR)/$(TARGET)/www $(PACKAGE_DIR)/scripts
167 @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/$(TARGET)/
168 @cp -a webapp/dist/* $(PACKAGE_DIR)/$(TARGET)/www/
169 ifneq ($(GOOS), windows)
170 @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)/
172 @cp $(ROOT_SRCDIR)/conf.d/etc/xds/agent/agent-config.json $(PACKAGE_DIR)/$(TARGET)/agent-config.json.in
173 @cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
174 @echo "### Package $(PACKAGE_ZIPFILE) has been successfuly built - $(BUILD_MODE)"
178 @echo "# Build linux amd64..."
179 GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
180 @echo "# Build windows amd64..."
181 GOOS=windows GOARCH=amd64 RELEASE=1 SKIP_WEBAPP_BUILD=true make -f $(ROOT_SRCDIR)/Makefile package
182 @echo "# Build darwin amd64..."
183 GOOS=darwin GOARCH=amd64 RELEASE=1 SKIP_WEBAPP_BUILD=true make -f $(ROOT_SRCDIR)/Makefile package
184 make -f $(ROOT_SRCDIR)/Makefile clean
186 vendor: tools/glide glide.yaml
187 $(LOCAL_TOOLSDIR)/glide install --strip-vendor
190 (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
191 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git && \
192 rm -rf xds-server.git && ln -s ../../../../../../xds-server xds-server.git )
196 @test -f $(LOCAL_TOOLSDIR)/glide || { \
197 echo "Downloading glide"; \
198 mkdir -p $(LOCAL_TOOLSDIR); \
199 curl --silent --connect-timeout 60 --retry 3 -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR) sh; \
202 .PHONY: tools/syncthing
204 @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { \
205 mkdir -p $(LOCAL_TOOLSDIR); \
206 DESTDIR=$(LOCAL_TOOLSDIR) \
207 SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
208 SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
209 ./scripts/get-syncthing.sh; }
211 .PHONY: tools/syncthing/copytobin
212 tools/syncthing/copytobin:
213 @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
214 @mkdir -p $(LOCAL_BINDIR)
215 @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
219 @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
224 @echo "Main supported rules:"
225 @echo " all (default)"
233 @echo "Influential make variables:"
234 @echo " V - Build verbosity {0,1,2}."
235 @echo " BUILD_ENV_FLAGS - Environment added to 'go build'."