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