Update command used to extract version from tag
[src/xds/xds-cli.git] / Makefile
1  ###########################################################################
2 # Copyright 2017-2018 IoT.bzh
3 #
4 # author: Sebastien Douheret <sebastien@iot.bzh>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 ###########################################################################
18
19 # Application Name
20 TARGET=xds-cli
21
22
23 # Retrieve git tag/commit to set version & sub-version strings
24 GIT_DESC := $(shell git describe --always --tags --match "[0-9]*")
25 VERSION := $(firstword $(subst -, ,$(GIT_DESC)))
26 ifeq (-,$(findstring -,$(GIT_DESC)))
27         SUB_VERSION := $(subst $(VERSION)-,,$(GIT_DESC))
28 endif
29 ifeq ($(VERSION), )
30         VERSION := unknown-dev
31 endif
32 ifeq ($(SUB_VERSION), )
33         SUB_VERSION := $(shell date +'%Y-%m-%d_%H%M%S')
34 endif
35
36 # Configurable variables for installation (default /opt/AGL/...)
37 ifeq ($(origin DESTDIR), undefined)
38         DESTDIR := /opt/AGL/xds/cli
39 endif
40
41 HOST_GOOS=$(shell go env GOOS)
42 HOST_GOARCH=$(shell go env GOARCH)
43 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
44 REPOPATH=gerrit.automotivelinux.org/gerrit/src/xds/$(TARGET)
45
46 EXT=
47 ifeq ($(HOST_GOOS), windows)
48         EXT=.exe
49 endif
50
51 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
52 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
53 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../../../..)
54 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
55 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
56 PACKAGE_DIR := $(ROOT_SRCDIR)/package
57
58 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
59 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
60
61 # Check Go version
62 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
63 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
64 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
65 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 9 \) ] && echo true)
66 CHECKERRMSG := "ERROR: Go version 1.9.0 or higher is requested (current detected version: $(GOVERSION)). \n\
67 \t--> It may be necessary to clear glide cache with following command 'tools/linux/glide cc'"
68
69
70 VERBOSE_1 := -v
71 VERBOSE_2 := -v -x
72
73 # Release or Debug mode
74 ifeq ($(filter 1,$(RELEASE) $(REL)),)
75         GO_LDFLAGS=
76         # disable compiler optimizations and inlining
77         GO_GCFLAGS=-N -l
78         BUILD_MODE="Debug mode"
79 else
80         # optimized code without debug info
81         GO_LDFLAGS=-s -w
82         GO_GCFLAGS=
83         BUILD_MODE="Release mode"
84 endif
85
86 ifeq ($(SUB_VERSION), )
87         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).zip
88 else
89         # only use dot as separator to allow rpm packaging (see version .spec file)
90         PK_VER := $(subst _,.,$(subst -,,$(VERSION)))
91         PK_SBVER := $(subst _,.,$(subst -,,$(SUB_VERSION)))
92         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(PK_VER).$(PK_SBVER).zip
93 endif
94
95 .PHONY: all
96 all: vendor build
97
98 .PHONY: build
99 build: checkgover
100         @echo "### Build $(TARGET) (version $(VERSION), subversion $(SUB_VERSION) - $(BUILD_MODE))";
101         @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)" .
102
103 test: tools/glide
104         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
105
106 vet: tools/glide
107         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
108
109 fmt: tools/glide
110         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
111
112 .PHONY: clean
113 clean:
114         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
115
116 .PHONY: distclean
117 distclean: clean
118         rm -rf $(LOCAL_BINDIR) && (cd $(ROOT_SRCDIR) && rm -rf ./tools ./glide.lock ./vendor ./*.zip)
119
120
121 .PHONY: scripts
122 scripts:
123         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/*.sh scripts/xds-utils $(LOCAL_BINDIR)
124
125 .PHONY: release
126 release:
127         RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile clean build
128
129 package: clean vendor build
130         @mkdir -p $(PACKAGE_DIR)/$(TARGET)
131         @cp -a $(LOCAL_BINDIR)/*cli$(EXT) $(PACKAGE_DIR)/$(TARGET)
132 ifneq ($(GOOS), windows)
133         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)
134 endif
135         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
136
137 .PHONY: package-all
138 package-all:
139         @echo "# Build linux amd64..."
140         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
141         @echo "# Build windows amd64..."
142         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
143         @echo "# Build darwin amd64..."
144         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
145         make -f $(ROOT_SRCDIR)/Makefile clean
146
147 .PHONY: install
148 install:
149         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
150         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
151
152 .PHONY: uninstall
153 uninstall:
154         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
155
156 vendor: tools/glide glide.yaml
157         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
158
159 vendor/debug: vendor
160         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
161                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git && \
162                 rm -rf xds-agent.git && ln -s ../../../../../../xds-agent xds-agent.git)
163
164 .PHONY: tools/glide
165 tools/glide:
166         @test -f $(LOCAL_TOOLSDIR)/glide || { \
167                 echo "Downloading glide"; \
168                 mkdir -p $(LOCAL_TOOLSDIR); \
169                 curl --silent --connect-timeout 60 --retry 3 -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
170         }
171
172 .PHONY:
173 checkgover:
174         @test "$(CHECKGOVER)" = "true" || { echo -e $(CHECKERRMSG); exit 1; }
175
176
177 .PHONY: help
178 help:
179         @echo "Main supported rules:"
180         @echo "  all                (default)"
181         @echo "  build"
182         @echo "  release"
183         @echo "  package"
184         @echo "  install"
185         @echo "  uninstall"
186         @echo "  clean"
187         @echo "  distclean"
188         @echo ""
189         @echo "Influential make variables:"
190         @echo "  V                 - Build verbosity {0,1,2}."
191         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."