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