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