Fix version / sub-version retrieval.
[src/xds/xds-server.git] / Makefile
1 # Makefile used to build XDS daemon Web Server
2
3 # Application Version
4 VERSION := 0.1.0
5
6 # Syncthing version to install
7 SYNCTHING_VERSION = 0.14.28
8 # FIXME: use master while waiting a release that include #164
9 #SYNCTHING_INOTIFY_VERSION = 0.8.5
10 SYNCTHING_INOTIFY_VERSION=master
11
12
13 # Retrieve git tag/commit to set sub-version string
14 ifeq ($(origin SUB_VERSION), undefined)
15         SUB_VERSION := $(shell git describe --exact-match --tags 2>/dev/null | sed 's/^v//')
16         ifneq ($(SUB_VERSION), )
17                 VERSION := $(firstword $(subst -, ,$(SUB_VERSION)))
18                 SUB_VERSION := $(word 2,$(subst -, ,$(SUB_VERSION)))
19         else
20                 SUB_VERSION := $(shell git describe --tags --always  | sed 's/^v//')
21                 ifeq ($(SUB_VERSION), )
22                         SUB_VERSION := unknown-dev
23                 endif
24         endif
25 endif
26
27 # Configurable variables for installation (default /usr/local/...)
28 ifeq ($(origin INSTALL_DIR), undefined)
29         INSTALL_DIR := /usr/local/bin
30 endif
31 ifeq ($(origin INSTALL_WEBAPP_DIR), undefined)
32         INSTALL_WEBAPP_DIR := $(INSTALL_DIR)/www-xds-server
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
50 PACKAGE_DIR := $(ROOT_SRCDIR)/package
51
52
53 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
54 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
55
56 VERBOSE_1 := -v
57 VERBOSE_2 := -v -x
58
59 # Release or Debug mode
60 ifeq ($(filter 1,$(RELEASE) $(REL)),)
61         GORELEASE=
62         BUILD_MODE="Debug mode"
63 else
64         # optimized code without debug info
65         GORELEASE= -s -w
66         BUILD_MODE="Release mode"
67 endif
68
69 ifeq ($(SUB_VERSION), )
70         PACKAGE_ZIPFILE := xds-server_$(ARCH)-v$(VERSION).zip
71 else
72         PACKAGE_ZIPFILE := xds-server_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip
73 endif
74
75
76 all: tools/syncthing build
77
78 .PHONY: build
79 build: xds webapp
80
81 xds:vendor scripts tools/syncthing/copytobin
82         @echo "### Build XDS server (version $(VERSION), subversion $(SUB_VERSION))";
83         @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-server$(EXT) -ldflags "$(GORELEASE) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" .
84
85 test: tools/glide
86         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
87
88 vet: tools/glide
89         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
90
91 fmt: tools/glide
92         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
93
94 run: build/xds tools/syncthing/copytobin
95         $(LOCAL_BINDIR)/xds-server$(EXT) --log info -c config.json.in
96
97 debug: build/xds webapp/debug tools/syncthing/copytobin
98         $(LOCAL_BINDIR)/xds-server$(EXT) --log debug -c config.json.in
99
100 .PHONY: clean
101 clean:
102         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
103
104 .PHONY: distclean
105 distclean: clean
106         rm -rf $(LOCAL_BINDIR) $(LOCAL_TOOLSDIR) glide.lock vendor webapp/node_modules webapp/dist
107
108 webapp: webapp/install
109         (cd webapp && gulp build)
110
111 webapp/debug:
112         (cd webapp && gulp watch &)
113
114 webapp/install:
115         (cd webapp && npm install)
116
117 .PHONY: scripts
118 scripts:
119         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/xds-server-st*.sh scripts/xds-utils $(LOCAL_BINDIR)
120
121 .PHONY: conffile
122 conffile:
123         cat config.json.in \
124                 | sed -e s,"webapp/dist","$(INSTALL_WEBAPP_DIR)",g \
125                 | sed -e s,"\./bin","",g \
126                  > $(PACKAGE_DIR)/xds-server/config.json
127
128 .PHONY: install
129 install:
130         @test -e $(LOCAL_BINDIR)/xds-server$(EXT) -a -d webapp/dist || { echo "Please execute first: make all\n"; exit 1; }
131         @test -e $(LOCAL_BINDIR)/xds-server-start.sh -a -d $(LOCAL_BINDIR)/xds-utils || { echo "Please execute first: make all\n"; exit 1; }
132         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
133         mkdir -p $(INSTALL_DIR) \
134                 && cp -a $(LOCAL_BINDIR)/* $(INSTALL_DIR)
135         mkdir -p $(INSTALL_WEBAPP_DIR) \
136                 && cp -a webapp/dist/* $(INSTALL_WEBAPP_DIR)
137
138 .PHONY: package
139 package: clean
140         INSTALL_DIR=$(PACKAGE_DIR)/xds-server make -f $(ROOT_SRCDIR)/Makefile all install
141         INSTALL_DIR=$(PACKAGE_DIR)/xds-server INSTALL_WEBAPP_DIR=www-xds-server make -f $(ROOT_SRCDIR)/Makefile conffile
142         (cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-server)
143
144 .PHONY: package-all
145 package-all:
146         @echo "# Build linux amd64..."
147         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
148         @echo "# Build windows amd64..."
149         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
150
151 vendor: tools/glide glide.yaml
152         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
153
154 tools/glide:
155         @echo "Downloading glide"
156         mkdir -p $(LOCAL_TOOLSDIR)
157         curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh
158
159 .PHONY: tools/syncthing
160 tools/syncthing:
161         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
162         mkdir -p $(LOCAL_TOOLSDIR); \
163         DESTDIR=$(LOCAL_TOOLSDIR) \
164         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
165         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
166         ./scripts/xds-utils/get-syncthing.sh; }
167
168 .PHONY:
169 tools/syncthing/copytobin:
170         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
171         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
172
173 .PHONY: help
174 help:
175         @echo "Main supported rules:"
176         @echo "  all                (default)"
177         @echo "  build"
178         @echo "  install"
179         @echo "  clean"
180         @echo "  distclean"
181         @echo ""
182         @echo "Influential make variables:"
183         @echo "  V                 - Build verbosity {0,1,2}."
184         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."