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