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