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