Check go version and print error if not >1.8.1
[src/xds/xds-agent.git] / Makefile
1 # Makefile used to build XDS Agent
2
3 # Syncthing version to install
4 SYNCTHING_VERSION = 0.14.38
5 SYNCTHING_INOTIFY_VERSION = 0.8.7
6
7
8 # Retrieve git tag/commit to set version & sub-version strings
9 GIT_DESC := $(shell git describe --always --tags)
10 VERSION := $(firstword $(subst -, ,$(GIT_DESC)))
11 SUB_VERSION := $(subst $(VERSION)-,,$(GIT_DESC))
12 ifeq ($(VERSION), )
13         VERSION := unknown-dev
14 endif
15 ifeq ($(SUB_VERSION), )
16         SUB_VERSION := $(shell date +'%Y-%m-%d_%H%M%S')
17 endif
18
19 # Configurable variables for installation (default /opt/AGL/...)
20 ifeq ($(origin DESTDIR), undefined)
21         DESTDIR := /opt/AGL/xds/agent
22 endif
23 ifeq ($(origin DESTDIR_WWW), undefined)
24         DESTDIR_WWW := $(DESTDIR)/www
25 endif
26
27 HOST_GOOS=$(shell go env GOOS)
28 HOST_GOARCH=$(shell go env GOARCH)
29 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
30 REPOPATH=github.com/iotbzh/xds-agent
31
32 EXT=
33 ifeq ($(HOST_GOOS), windows)
34         EXT=.exe
35 endif
36
37 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
38 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
39 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
40 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
41 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
42 PACKAGE_DIR := $(ROOT_SRCDIR)/package
43
44 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
45 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
46
47 # Check Go version
48 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
49 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
50 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
51 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 8 \) ] && echo true)
52 CHECKERRMSG := "ERROR: Go version 1.8.1 or higher is requested (current detected version: $(GOVERSION))."
53
54
55 VERBOSE_1 := -v
56 VERBOSE_2 := -v -x
57
58 # Release or Debug mode
59 ifeq ($(filter 1,$(RELEASE) $(REL)),)
60         GO_LDFLAGS=
61         # disable compiler optimizations and inlining
62         GO_GCFLAGS=-N -l
63         BUILD_MODE="Debug mode"
64 else
65         # optimized code without debug info
66         GO_LDFLAGS=-s -w
67         GO_GCFLAGS=
68         BUILD_MODE="Release mode"
69 endif
70
71 ifeq ($(SUB_VERSION), )
72         PACKAGE_ZIPFILE := xds-agent_$(ARCH)-v$(VERSION).zip
73 else
74         PACKAGE_ZIPFILE := xds-agent_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip
75 endif
76
77
78 all: tools/syncthing build
79
80 .PHONY: build
81 build: checkgover vendor xds webapp
82
83 xds: scripts tools/syncthing/copytobin
84         @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
85         @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-agent$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
86
87 test: tools/glide
88         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
89
90 vet: tools/glide
91         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
92
93 fmt: tools/glide
94         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
95
96 run: build/xds tools/syncthing/copytobin
97         $(LOCAL_BINDIR)/xds-agent$(EXT) --log info -c agent-config.json.in
98
99 debug: build/xds tools/syncthing/copytobin
100         $(LOCAL_BINDIR)/xds-agent$(EXT) --log debug -c agent-config.json.in
101
102 .PHONY: clean
103 clean:
104         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
105
106 .PHONY: distclean
107 distclean: clean
108         cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./glide.lock ./vendor ./*.zip ./webapp/node_modules ./webapp/dist
109
110 webapp: webapp/install
111         (cd webapp && npm run build)
112
113 webapp/debug:
114         (cd webapp && npm run watch)
115
116 webapp/install:
117         (cd webapp && npm install)
118         @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
119
120 .PHONY: install
121 install:
122         @test -e $(LOCAL_BINDIR)/xds-agent$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
123         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
124         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh
125
126 .PHONY: uninstall
127 uninstall:
128         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
129
130 package: clean tools/syncthing vendor build
131         @mkdir -p $(PACKAGE_DIR)/xds-agent $(PACKAGE_DIR)/scripts
132         @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/xds-agent
133         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/xds-agent
134         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
135
136 .PHONY: package-all
137 package-all:
138         @echo "# Build linux amd64..."
139         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
140         @echo "# Build windows amd64..."
141         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
142         @echo "# Build darwin amd64..."
143         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
144         make -f $(ROOT_SRCDIR)/Makefile clean
145
146 vendor: tools/glide glide.yaml
147         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
148
149 vendor/debug: vendor
150         (cd vendor/github.com/iotbzh && \
151                 rm -rf xds-common && ln -s ../../../../xds-common \
152                 rm -rf xds-server && ln -s ../../../../xds-server )
153
154 .PHONY: tools/glide
155 tools/glide:
156         @test -f $(LOCAL_TOOLSDIR)/glide || { \
157                 echo "Downloading glide"; \
158                 mkdir -p $(LOCAL_TOOLSDIR); \
159                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
160         }
161
162 .PHONY: tools/syncthing
163 tools/syncthing:
164         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
165         mkdir -p $(LOCAL_TOOLSDIR); \
166         DESTDIR=$(LOCAL_TOOLSDIR) \
167         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
168         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
169         ./scripts/get-syncthing.sh; }
170
171 .PHONY: tools/syncthing/copytobin
172 tools/syncthing/copytobin:
173         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
174         @mkdir -p $(LOCAL_BINDIR)
175         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
176
177 .PHONY:
178 checkgover:
179         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
180
181
182 .PHONY: help
183 help:
184         @echo "Main supported rules:"
185         @echo "  all               (default)"
186         @echo "  build"
187         @echo "  package"
188         @echo "  install"
189         @echo "  uninstall"
190         @echo "  clean"
191         @echo "  distclean"
192         @echo ""
193         @echo "Influential make variables:"
194         @echo "  V                 - Build verbosity {0,1,2}."
195         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."