Always set sub-version
[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 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 # for backward compatibility
20 ifneq ($(origin INSTALL_DIR), undefined)
21         DESTDIR := $(INSTALL_DIR)
22 endif
23 ifneq ($(origin INSTALL_WEBAPP_DIR), undefined)
24         DESTDIR_WWW := $(INSTALL_WEBAPP_DIR)
25 endif
26
27 # Configurable variables for installation (default /opt/AGL/...)
28 ifeq ($(origin DESTDIR), undefined)
29         DESTDIR := /opt/AGL/xds/server
30 endif
31 ifeq ($(origin DESTDIR_WWW), undefined)
32         DESTDIR_WWW := $(DESTDIR)/www
33 endif
34
35 HOST_GOOS=$(shell go env GOOS)
36 HOST_GOARCH=$(shell go env GOARCH)
37 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
38 REPOPATH=github.com/iotbzh/xds-server
39
40 EXT=
41 ifeq ($(HOST_GOOS), windows)
42         EXT=.exe
43 endif
44
45 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
46 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
47 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
48 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
49 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
50 PACKAGE_DIR := $(ROOT_SRCDIR)/package
51
52 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
53 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
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-server_$(ARCH)-v$(VERSION).zip
73 else
74         PACKAGE_ZIPFILE := xds-server_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip
75 endif
76
77
78 all: tools/syncthing build
79
80 .PHONY: build
81 build: vendor xds webapp
82
83 xds: scripts tools/syncthing/copytobin
84         @echo "### Build XDS server (version $(VERSION), subversion $(SUB_VERSION), $(BUILD_MODE))";
85         @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-server$(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-server$(EXT) --log info -c config.json.in
98
99 debug: build/xds tools/syncthing/copytobin
100         $(LOCAL_BINDIR)/xds-server$(EXT) --log debug -c config.json.in
101
102 .PHONY: clean
103 clean:
104         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
105
106 .PHONY: distclean
107 distclean: clean
108         rm -rf $(LOCAL_BINDIR) $(ROOT_SRCDIR)/tools glide.lock vendor $(ROOT_SRCDIR)/webapp/dist $(ROOT_SRCDIR)/webapp/node_modules
109
110 webapp: webapp/install
111         mkdir -p $(ROOT_SRCDIR)/webapp/dist $(ROOT_SRCDIR)/webapp/dist/fonts
112         (cd $(ROOT_SRCDIR)/webapp && cp -a ./assets ./src/index.html ./node_modules/font-awesome/css/font-awesome.min.css ./dist/)
113         (cd $(ROOT_SRCDIR)/webapp && cp -a ./node_modules/font-awesome/fonts/* ./dist/fonts/)
114
115 webapp/install:
116         (cd webapp && npm install)
117
118 .PHONY: scripts
119 scripts:
120         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/xds-server-st*.sh scripts/xds-utils $(LOCAL_BINDIR)
121
122 .PHONY: conffile
123 conffile:
124         cat config.json.in \
125                 | sed -e s,"webapp/dist","$(DESTDIR_WWW)",g \
126                 | sed -e s,"\./bin","",g \
127                  > $(DESTDIR)/config.json.in
128
129 .PHONY: install
130 install:
131         @test -e $(LOCAL_BINDIR)/xds-server$(EXT) -a -d webapp/dist || { echo "Please execute first: make all\n"; exit 1; }
132         @test -e $(LOCAL_BINDIR)/xds-server-start.sh -a -d $(LOCAL_BINDIR)/xds-utils || { echo "Please execute first: make all\n"; exit 1; }
133         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
134         mkdir -p $(DESTDIR) \
135                 && cp -a $(LOCAL_BINDIR)/* $(DESTDIR)
136         mkdir -p $(DESTDIR_WWW) \
137                 && cp -a webapp/dist/* $(DESTDIR_WWW)
138
139 .PHONY: _package
140 _package: clean
141         make -f $(ROOT_SRCDIR)/Makefile all install  DESTDIR=$(PACKAGE_DIR)/xds-server
142         make -f $(ROOT_SRCDIR)/Makefile conffile  DESTDIR=$(PACKAGE_DIR)/xds-server DESTDIR_WWW=www
143         cp -r $(ROOT_SRCDIR)/conf.d $(PACKAGE_DIR)/xds-server
144         rm -f $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE)
145         (cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-server)
146
147 # On support Linux for now
148 .PHONY: package
149 package:
150         @echo "# Build linux amd64..."
151         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile _package
152         make -f $(ROOT_SRCDIR)/Makefile clean
153
154 .PHONY: package-all
155 package-all: package
156
157 vendor: tools/glide glide.yaml
158         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
159
160 vendor/debug: vendor
161         (cd vendor/github.com/iotbzh && \
162                 rm -rf xds-common && ln -s ../../../../xds-common )
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/xds-utils/get-syncthing.sh; }
180
181 .PHONY:
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: help
188 help:
189         @echo "Main supported rules:"
190         @echo "  all                (default)"
191         @echo "  build"
192         @echo "  package"
193         @echo "  install"
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'."