Optimized/Reduced packaging time
[src/xds/xds-agent.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-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)
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
66 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
67 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
68
69 # Check Go version
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))."
75
76
77 VERBOSE_1 := -v
78 VERBOSE_2 := -v -x
79
80 # Release or Debug mode
81 ifeq ($(filter 1,$(RELEASE) $(REL)),)
82         GO_LDFLAGS=
83         # disable compiler optimizations and inlining
84         GO_GCFLAGS=-N -l
85         BUILD_MODE="Debug mode"
86         WEBAPP_BUILD_RULE=build
87 else
88         # optimized code without debug info
89         GO_LDFLAGS=-s -w
90         GO_GCFLAGS=
91         BUILD_MODE="Release mode"
92         WEBAPP_BUILD_RULE=build:prod
93 endif
94
95 ifeq ($(SUB_VERSION), )
96         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).zip
97 else
98         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION)_$(SUB_VERSION).zip
99 endif
100
101
102 all: tools/syncthing build
103
104 .PHONY: build
105 build: checkgover vendor xds webapp
106
107 xds: scripts tools/syncthing/copytobin
108         @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
109         @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)" .
110
111 test: tools/glide
112         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
113
114 vet: tools/glide
115         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
116
117 fmt: tools/glide
118         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
119
120 run: build/xds tools/syncthing/copytobin
121         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log info $(XDS_AGENT_RUN_ARGS)
122
123 debug: build/xds tools/syncthing/copytobin
124         $(LOCAL_BINDIR)/$(TARGET)$(EXT) --log debug $(XDS_AGENT_DEBUG_ARGS)
125
126 .PHONY: clean
127 clean:
128         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
129
130 .PHONY: distclean
131 distclean: clean
132         cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./glide.lock ./vendor ./*.zip ./webapp/node_modules ./webapp/dist
133
134 webapp: webapp/install
135 ifneq ($(SKIP_WEBAPP_BUILD), true)
136         cd webapp && npm run $(WEBAPP_BUILD_RULE)
137 else
138         @echo "Skipping webapp build (SKIP_WEBAPP_BUILD var is set)"
139 endif
140
141 webapp/debug:
142         cd webapp && npm run watch
143
144 webapp/install:
145 ifneq ($(SKIP_WEBAPP_BUILD), true)
146         (cd webapp && npm install)
147 else
148         @echo "Skipping webapp install (SKIP_WEBAPP_BUILD var is set)"
149 endif
150         @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
151
152 .PHONY: install
153 install:
154         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
155         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
156         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh
157
158 .PHONY: uninstall
159 uninstall:
160         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
161
162 package: clean tools/syncthing vendor build
163         @mkdir -p $(PACKAGE_DIR)/$(TARGET)/www $(PACKAGE_DIR)/scripts
164         @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/$(TARGET)/
165         @cp -a webapp/dist/* $(PACKAGE_DIR)/$(TARGET)/www/
166 ifneq ($(GOOS), windows)
167                 @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)/
168 endif
169         @cp $(ROOT_SRCDIR)/conf.d/etc/xds/agent/agent-config.json $(PACKAGE_DIR)/$(TARGET)/agent-config.json.in
170         @cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
171         @echo "### Package $(PACKAGE_ZIPFILE) has been successfuly built - $(BUILD_MODE)"
172
173 .PHONY: package-all
174 package-all:
175         @echo "# Build linux amd64..."
176         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
177         @echo "# Build windows amd64..."
178         GOOS=windows GOARCH=amd64 RELEASE=1 SKIP_WEBAPP_BUILD=true make -f $(ROOT_SRCDIR)/Makefile package
179         @echo "# Build darwin amd64..."
180         GOOS=darwin GOARCH=amd64 RELEASE=1 SKIP_WEBAPP_BUILD=true make -f $(ROOT_SRCDIR)/Makefile package
181         make -f $(ROOT_SRCDIR)/Makefile clean
182
183 vendor: tools/glide glide.yaml
184         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
185
186 vendor/debug: vendor
187         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
188                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git && \
189                 rm -rf xds-server.git && ln -s ../../../../../../xds-server xds-server.git )
190
191 .PHONY: tools/glide
192 tools/glide:
193         @test -f $(LOCAL_TOOLSDIR)/glide || { \
194                 echo "Downloading glide"; \
195                 mkdir -p $(LOCAL_TOOLSDIR); \
196                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
197         }
198
199 .PHONY: tools/syncthing
200 tools/syncthing:
201         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
202         mkdir -p $(LOCAL_TOOLSDIR); \
203         DESTDIR=$(LOCAL_TOOLSDIR) \
204         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
205         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
206         ./scripts/get-syncthing.sh; }
207
208 .PHONY: tools/syncthing/copytobin
209 tools/syncthing/copytobin:
210         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
211         @mkdir -p $(LOCAL_BINDIR)
212         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
213
214 .PHONY:
215 checkgover:
216         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
217
218
219 .PHONY: help
220 help:
221         @echo "Main supported rules:"
222         @echo "  all               (default)"
223         @echo "  build"
224         @echo "  package"
225         @echo "  install"
226         @echo "  uninstall"
227         @echo "  clean"
228         @echo "  distclean"
229         @echo ""
230         @echo "Influential make variables:"
231         @echo "  V                 - Build verbosity {0,1,2}."
232         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."