Used xdsserver api v1.0.0-rc1
[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         WEBAPP_BUILD_RULE=build
65 else
66         # optimized code without debug info
67         GO_LDFLAGS=-s -w
68         GO_GCFLAGS=
69         BUILD_MODE="Release mode"
70         WEBAPP_BUILD_RULE=build:prod
71 endif
72
73 ifeq ($(SUB_VERSION), )
74         PACKAGE_ZIPFILE := xds-agent_$(ARCH)-$(VERSION).zip
75 else
76         PACKAGE_ZIPFILE := xds-agent_$(ARCH)-$(VERSION)_$(SUB_VERSION).zip
77 endif
78
79
80 all: tools/syncthing build
81
82 .PHONY: build
83 build: checkgover vendor xds webapp
84
85 xds: scripts tools/syncthing/copytobin
86         @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
87         @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)" .
88
89 test: tools/glide
90         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
91
92 vet: tools/glide
93         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
94
95 fmt: tools/glide
96         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
97
98 run: build/xds tools/syncthing/copytobin
99         $(LOCAL_BINDIR)/xds-agent$(EXT) --log info -c agent-config.json.in
100
101 debug: build/xds tools/syncthing/copytobin
102         $(LOCAL_BINDIR)/xds-agent$(EXT) --log debug -c agent-config.json.in
103
104 .PHONY: clean
105 clean:
106         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
107
108 .PHONY: distclean
109 distclean: clean
110         cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./glide.lock ./vendor ./*.zip ./webapp/node_modules ./webapp/dist
111
112 webapp: webapp/install
113         cd webapp && npm run $(WEBAPP_BUILD_RULE)
114
115 webapp/debug:
116         cd webapp && npm run watch
117
118 webapp/install:
119         (cd webapp && npm install)
120         @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
121
122 .PHONY: install
123 install:
124         @test -e $(LOCAL_BINDIR)/xds-agent$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
125         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
126         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh
127
128 .PHONY: uninstall
129 uninstall:
130         export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
131
132 package: clean tools/syncthing vendor build
133         @mkdir -p $(PACKAGE_DIR)/xds-agent/www $(PACKAGE_DIR)/scripts
134         @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/xds-agent/
135         @cp -a webapp/dist/* $(PACKAGE_DIR)/xds-agent/www/
136         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/xds-agent/
137         @cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
138         @echo "### Package $(PACKAGE_ZIPFILE) has been successfuly built - $(BUILD_MODE)"
139
140 .PHONY: package-all
141 package-all:
142         @echo "# Build linux amd64..."
143         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
144         @echo "# Build windows amd64..."
145         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
146         @echo "# Build darwin amd64..."
147         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
148         make -f $(ROOT_SRCDIR)/Makefile clean
149
150 vendor: tools/glide glide.yaml
151         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
152
153 vendor/debug: vendor
154         (cd vendor/github.com/iotbzh && \
155                 rm -rf xds-common && ln -s ../../../../xds-common \
156                 rm -rf xds-server && ln -s ../../../../xds-server )
157
158 .PHONY: tools/glide
159 tools/glide:
160         @test -f $(LOCAL_TOOLSDIR)/glide || { \
161                 echo "Downloading glide"; \
162                 mkdir -p $(LOCAL_TOOLSDIR); \
163                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
164         }
165
166 .PHONY: tools/syncthing
167 tools/syncthing:
168         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
169         mkdir -p $(LOCAL_TOOLSDIR); \
170         DESTDIR=$(LOCAL_TOOLSDIR) \
171         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
172         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
173         ./scripts/get-syncthing.sh; }
174
175 .PHONY: tools/syncthing/copytobin
176 tools/syncthing/copytobin:
177         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
178         @mkdir -p $(LOCAL_BINDIR)
179         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
180
181 .PHONY:
182 checkgover:
183         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
184
185
186 .PHONY: help
187 help:
188         @echo "Main supported rules:"
189         @echo "  all               (default)"
190         @echo "  build"
191         @echo "  package"
192         @echo "  install"
193         @echo "  uninstall"
194         @echo "  clean"
195         @echo "  distclean"
196         @echo ""
197         @echo "Influential make variables:"
198         @echo "  V                 - Build verbosity {0,1,2}."
199         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."