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