Disable compiler optimizations and inlining for debugging.
[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 /usr/local/...)
31 ifeq ($(origin DESTDIR), undefined)
32         DESTDIR := /usr/local/bin
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
86         @cp agent-config.json.in $(PACKAGE_DIR)/xds-agent/agent-config.json
87         @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/xds-agent
88         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
89
90 .PHONY: package-all
91 package-all:
92         @echo "# Build linux amd64..."
93         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
94         @echo "# Build windows amd64..."
95         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
96         @echo "# Build darwin amd64..."
97         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
98         make -f $(ROOT_SRCDIR)/Makefile clean
99
100 test: tools/glide
101         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
102
103 vet: tools/glide
104         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
105
106 fmt: tools/glide
107         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
108
109 run: build/xds tools/syncthing/copytobin
110         $(LOCAL_BINDIR)/xds-agent$(EXT) --log info -c agent-config.json.in
111
112 debug: build/xds tools/syncthing/copytobin
113         $(LOCAL_BINDIR)/xds-agent$(EXT) --log debug -c agent-config.json.in
114
115 .PHONY: clean
116 clean:
117         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
118
119 .PHONY: distclean
120 distclean: clean
121         rm -rf $(LOCAL_BINDIR) tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
122
123 .PHONY: install
124 install: all
125         mkdir -p $(DESTDIR) && cp $(LOCAL_BINDIR)/* $(DESTDIR)
126
127 vendor: tools/glide glide.yaml
128         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
129
130 .PHONY: tools/glide
131 tools/glide:
132         @test -f $(LOCAL_TOOLSDIR)/glide || { \
133                 echo "Downloading glide"; \
134                 mkdir -p $(LOCAL_TOOLSDIR); \
135                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
136         }
137
138 .PHONY: tools/syncthing
139 tools/syncthing:
140         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
141         mkdir -p $(LOCAL_TOOLSDIR); \
142         DESTDIR=$(LOCAL_TOOLSDIR) \
143         SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
144         SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
145         ./scripts/get-syncthing.sh; }
146
147 .PHONY:
148 tools/syncthing/copytobin:
149         @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
150         @mkdir -p $(LOCAL_BINDIR)
151         @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
152
153 .PHONY: help
154 help:
155         @echo "Main supported rules:"
156         @echo "  all               (default)"
157         @echo "  build"
158         @echo "  package"
159         @echo "  install"
160         @echo "  clean"
161         @echo "  distclean"
162         @echo ""
163         @echo "Influential make variables:"
164         @echo "  V                 - Build verbosity {0,1,2}."
165         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."