Fiw webapp/install rule.
[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 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         else
18                 SUB_VERSION := $(shell git describe --tags --always  | sed 's/^v//')
19                 ifeq ($(SUB_VERSION), )
20                         SUB_VERSION := unknown-dev
21                 endif
22         endif
23 endif
24
25 # for backward compatibility
26 ifneq ($(origin INSTALL_DIR), undefined)
27         DESTDIR := $(INSTALL_DIR)
28 endif
29 ifneq ($(origin INSTALL_WEBAPP_DIR), undefined)
30         DESTDIR_WWW := $(INSTALL_WEBAPP_DIR)
31 endif
32
33 # Configurable variables for installation (default /usr/local/...)
34 ifeq ($(origin DESTDIR), undefined)
35         DESTDIR := /usr/local/bin
36 endif
37 ifeq ($(origin DESTDIR_WWW), undefined)
38         DESTDIR_WWW := $(DESTDIR)/www-xds-server
39 endif
40
41 HOST_GOOS=$(shell go env GOOS)
42 HOST_GOARCH=$(shell go env GOARCH)
43 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
44 REPOPATH=github.com/iotbzh/xds-server
45
46 EXT=
47 ifeq ($(HOST_GOOS), windows)
48         EXT=.exe
49 endif
50
51 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
52 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
53 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
54 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
55 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
56 PACKAGE_DIR := $(ROOT_SRCDIR)/package
57
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         GORELEASE=
68         BUILD_MODE="Debug mode"
69 else
70         # optimized code without debug info
71         GORELEASE= -s -w
72         BUILD_MODE="Release mode"
73 endif
74
75 ifeq ($(SUB_VERSION), )
76         PACKAGE_ZIPFILE := xds-server_$(ARCH)-v$(VERSION).zip
77 else
78         PACKAGE_ZIPFILE := xds-server_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip
79 endif
80
81
82 all: tools/syncthing build
83
84 .PHONY: build
85 build: xds webapp
86
87 xds:vendor scripts tools/syncthing/copytobin
88         @echo "### Build XDS server (version $(VERSION), subversion $(SUB_VERSION))";
89         @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)" .
90
91 test: tools/glide
92         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
93
94 vet: tools/glide
95         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
96
97 fmt: tools/glide
98         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
99
100 run: build/xds tools/syncthing/copytobin
101         $(LOCAL_BINDIR)/xds-server$(EXT) --log info -c config.json.in
102
103 debug: build/xds webapp/debug tools/syncthing/copytobin
104         $(LOCAL_BINDIR)/xds-server$(EXT) --log debug -c config.json.in
105
106 .PHONY: clean
107 clean:
108         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
109
110 .PHONY: distclean
111 distclean: clean
112         rm -rf $(LOCAL_BINDIR) $(ROOT_SRCDIR)/tools glide.lock vendor webapp/node_modules webapp/dist
113
114 webapp: webapp/install
115         (cd webapp && gulp build)
116
117 webapp/debug:
118         (cd webapp && gulp watch &)
119
120 webapp/install:
121         (cd webapp && npm install)
122         @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
123
124 .PHONY: scripts
125 scripts:
126         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/xds-server-st*.sh scripts/xds-utils $(LOCAL_BINDIR)
127
128 .PHONY: conffile
129 conffile:
130         cat config.json.in \
131                 | sed -e s,"webapp/dist","$(DESTDIR_WWW)",g \
132                 | sed -e s,"\./bin","",g \
133                  > $(DESTDIR)/config.json
134
135 .PHONY: install
136 install:
137         @test -e $(LOCAL_BINDIR)/xds-server$(EXT) -a -d webapp/dist || { echo "Please execute first: make all\n"; exit 1; }
138         @test -e $(LOCAL_BINDIR)/xds-server-start.sh -a -d $(LOCAL_BINDIR)/xds-utils || { echo "Please execute first: make all\n"; exit 1; }
139         @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
140         mkdir -p $(DESTDIR) \
141                 && cp -a $(LOCAL_BINDIR)/* $(DESTDIR)
142         mkdir -p $(DESTDIR_WWW) \
143                 && cp -a webapp/dist/* $(DESTDIR_WWW)
144
145 .PHONY: package
146 package: clean
147         make -f $(ROOT_SRCDIR)/Makefile all install  DESTDIR=$(PACKAGE_DIR)/xds-server
148         make -f $(ROOT_SRCDIR)/Makefile conffile  DESTDIR=$(PACKAGE_DIR)/xds-server DESTDIR_WWW=www-xds-server
149         (cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-server)
150
151 .PHONY: package-all
152 package-all:
153         @echo "# Build linux amd64..."
154         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
155         @echo "# Build windows amd64..."
156         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
157
158 vendor: tools/glide glide.yaml
159         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
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         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
182
183 .PHONY: help
184 help:
185         @echo "Main supported rules:"
186         @echo "  all                (default)"
187         @echo "  build"
188         @echo "  install"
189         @echo "  clean"
190         @echo "  distclean"
191         @echo ""
192         @echo "Influential make variables:"
193         @echo "  V                 - Build verbosity {0,1,2}."
194         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."