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