Check go version because xds-common request go > v1.8.1
[src/xds/xds-cli.git] / Makefile
1 # Makefile used to build xds-cli commands
2
3 # Application Name
4 TARGET=xds-cli
5
6
7 # Retrieve git tag/commit to set version & sub-version strings
8 GIT_DESC := $(shell git describe --always --tags)
9 VERSION := $(firstword $(subst -, ,$(GIT_DESC)))
10 ifeq (-,$(findstring -,$(GIT_DESC)))
11 SUB_VERSION := $(subst $(VERSION)-,,$(GIT_DESC))
12 endif
13 ifeq ($(VERSION), )
14         VERSION := unknown-dev
15 endif
16 ifeq ($(SUB_VERSION), )
17         SUB_VERSION := $(shell date +'%Y-%m-%d_%H%M%S')
18 endif
19
20 # Configurable variables for installation (default /opt/AGL/...)
21 ifeq ($(origin DESTDIR), undefined)
22         DESTDIR := /opt/AGL/xds/cli
23 endif
24
25 HOST_GOOS=$(shell go env GOOS)
26 HOST_GOARCH=$(shell go env GOARCH)
27 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
28 REPOPATH=github.com/iotbzh/$(TARGET)
29
30 EXT=
31 ifeq ($(HOST_GOOS), windows)
32         EXT=.exe
33 endif
34
35 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
36 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
37 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
38 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
39 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
40 PACKAGE_DIR := $(ROOT_SRCDIR)/package
41
42 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
43 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
44
45 # Check Go version
46 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
47 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
48 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
49 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 8 \) ] && echo true)
50 CHECKERRMSG := "ERROR: Go version 1.8.1 or higher is requested (current detected version: $(GOVERSION))."
51
52
53 VERBOSE_1 := -v
54 VERBOSE_2 := -v -x
55
56 # Release or Debug mode
57 ifeq ($(filter 1,$(RELEASE) $(REL)),)
58         GO_LDFLAGS=
59         # disable compiler optimizations and inlining
60         GO_GCFLAGS=-N -l
61         BUILD_MODE="Debug mode"
62 else
63         # optimized code without debug info
64         GO_LDFLAGS=-s -w
65         GO_GCFLAGS=
66         BUILD_MODE="Release mode"
67 endif
68
69
70 ifeq ($(SUB_VERSION), )
71         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).zip
72 else
73         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION)_$(SUB_VERSION).zip
74 endif
75
76 .PHONY: all
77 all: vendor build
78
79 .PHONY: build
80 build: checkgover
81         @echo "### Build $(TARGET) (version $(VERSION), subversion $(SUB_VERSION) - $(BUILD_MODE))";
82         @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/$(TARGET)$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
83
84 test: tools/glide
85         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
86
87 vet: tools/glide
88         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
89
90 fmt: tools/glide
91         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
92
93 .PHONY: clean
94 clean:
95         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
96
97 .PHONY: distclean
98 distclean: clean
99         rm -rf $(LOCAL_BINDIR) $(ROOT_SRCDIR)/tools glide.lock vendor
100
101
102 .PHONY: scripts
103 scripts:
104         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/*.sh scripts/xds-utils $(LOCAL_BINDIR)
105
106 .PHONY: release
107 release:
108         RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile clean build
109
110 package: clean vendor build
111         @mkdir -p $(PACKAGE_DIR)/$(TARGET)
112         @cp -a $(LOCAL_BINDIR)/*cli$(EXT) $(PACKAGE_DIR)/$(TARGET)
113         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)
114         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
115
116 .PHONY: package-all
117 package-all:
118         @echo "# Build linux amd64..."
119         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
120         @echo "# Build windows amd64..."
121         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
122         @echo "# Build darwin amd64..."
123         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
124         make -f $(ROOT_SRCDIR)/Makefile clean
125
126 .PHONY: install
127 install:
128         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
129         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
130
131 .PHONY: uninstall
132 uninstall:
133         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
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                 rm -rf xds-agent && ln -s ../../../../xds-agent )
142
143 .PHONY: tools/glide
144 tools/glide:
145         @test -f $(LOCAL_TOOLSDIR)/glide || { \
146                 echo "Downloading glide"; \
147                 mkdir -p $(LOCAL_TOOLSDIR); \
148                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
149         }
150
151 .PHONY:
152 checkgover:
153         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
154
155
156 .PHONY: help
157 help:
158         @echo "Main supported rules:"
159         @echo "  all               (default)"
160         @echo "  build"
161         @echo "  release"
162         @echo "  clean"
163         @echo "  package"
164         @echo "  install / uninstall"
165         @echo "  distclean"
166         @echo ""
167         @echo "Influential make variables:"
168         @echo "  V                 - Build verbosity {0,1,2}."
169         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."