Update .gitreview file
[src/xds/xds-cli.git] / Makefile
1  ###########################################################################
2 # Copyright 2017-2019 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 PACKAGE_LIST=.
58
59 export GO111MODULE=on
60 export GOPATH := $(ROOT_GOPRJ):$(shell go env GOPATH)
61 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
62
63 # Check Go version
64 GOVERSION := $(shell go version |grep -o '[0-9\.]*'|head -n 1)
65 GOVERMAJ := $(shell echo $(GOVERSION) |cut -f1 -d.)
66 GOVERMIN := $(shell echo $(GOVERSION) |cut -f2 -d.)
67 CHECKGOVER := $(shell [ $(GOVERMAJ) -gt 1 -o \( $(GOVERMAJ) -eq 1 -a $(GOVERMIN) -ge 12 \) ] && echo true)
68 CHECKERRMSG := "ERROR: Go version 1.12 or higher is requested (current detected version: $(GOVERSION))."
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 # Build Package name (model: <target>_<arch>-<version>.<nb_commit_from_last_tag>.zip)
88 ifeq (-g,$(findstring -g,$(GIT_DESC)))
89         NB_COMMIT=$(firstword $(subst -, ,$(SUB_VERSION)))
90 else
91         NB_COMMIT=0
92 endif
93 PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).$(NB_COMMIT).zip
94
95 .PHONY: all
96 all: gomod 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 .PHONY: test
104 test: checkgorace
105         go test --race . -v
106
107 vet:
108         go vet $(PACKAGE_LIST)
109
110 fmt:
111         go fmt $(PACKAGE_LIST)
112
113 .PHONY: clean
114 clean:
115         rm -rf $(LOCAL_BINDIR)/* $(ROOT_SRCDIR)/debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
116
117 .PHONY: distclean
118 distclean: clean
119         (cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./vendor ./*.zip)
120          go clean -modcache
121
122 .PHONY: clean-lock
123 clean-lock: distclean
124         (cd $(ROOT_SRCDIR) && rm -f ./go.sum)
125
126 .PHONY: scripts
127 scripts:
128         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/*.sh scripts/xds-utils $(LOCAL_BINDIR)
129
130 .PHONY: release
131 release:
132         RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile clean build
133
134 package: clean vendor build
135         @mkdir -p $(PACKAGE_DIR)/$(TARGET)
136         @cp -a $(LOCAL_BINDIR)/*cli$(EXT) $(PACKAGE_DIR)/$(TARGET)
137 ifneq ($(GOOS), windows)
138         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)
139 endif
140         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
141
142 .PHONY: package-all
143 package-all:
144         @echo "# Build linux amd64..."
145         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
146         @echo "# Build windows amd64..."
147         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
148         @echo "# Build darwin amd64..."
149         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
150         make -f $(ROOT_SRCDIR)/Makefile clean
151
152 .PHONY: install
153 install:
154         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
155         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
156
157 .PHONY: uninstall
158 uninstall:
159         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
160
161 .PHONY: gomod
162 gomod:
163         go get
164
165 gomod/debug:
166         @echo "replace gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git => $(ROOT_SRCDIR)/../xds-common" >> $(ROOT_SRCDIR)/go.mod
167         @echo "replace gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git => $(ROOT_SRCDIR)/../xds-agent" >> $(ROOT_SRCDIR)/go.mod
168         @echo "Add replace in go.mod file - done."
169
170 vendor: gomod
171         go mod vendor
172
173 vendor/debug: vendor
174         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
175                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git && \
176                 rm -rf xds-agent.git && ln -s ../../../../../../xds-agent xds-agent.git)
177
178 .PHONY:
179 checkgover:
180         @test "$(CHECKGOVER)" = "true" || { echo -e $(CHECKERRMSG); exit 1; }
181
182 .PHONY:
183 checkgorace: checkgover
184         @ls $(shell go env GOROOT)/src/runtime/race/*.syso 1> /dev/null 2>&1 || { echo "ERROR: go-race package mandatory to run test. Please install it, for example: zypper install go-race"; exit 1; }
185
186 .PHONY: help
187 help:
188         @echo "Main supported rules:"
189         @echo "  all                (default)"
190         @echo "  build"
191         @echo "  release"
192         @echo "  package"
193         @echo "  install"
194         @echo "  uninstall"
195         @echo "  clean"
196         @echo "  distclean"
197         @echo ""
198         @echo "Influential make variables:"
199         @echo "  V                 - Build verbosity {0,1,2}."
200         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."