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