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