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