Update GOPATH in VSCode project (now in gerrit)
[src/xds/xds-gdb.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-gdb
22
23 # Retrieve git tag/commit to set version & sub-version strings
24 GIT_DESC := $(shell git describe --always --tags)
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/gdb
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 8 \) ] && echo true)
66 CHECKERRMSG := "ERROR: Go version 1.8.1 or higher is requested (current detected version: $(GOVERSION))."
67
68
69 VERBOSE_1 := -v
70 VERBOSE_2 := -v -x
71
72 # Release or Debug mode
73 ifeq ($(filter 1,$(RELEASE) $(REL)),)
74         GO_LDFLAGS=
75         # disable compiler optimizations and inlining
76         GO_GCFLAGS=-N -l
77         BUILD_MODE="Debug mode"
78 else
79         # optimized code without debug info
80         GO_LDFLAGS=-s -w
81         GO_GCFLAGS=
82         BUILD_MODE="Release mode"
83 endif
84
85
86 ifeq ($(SUB_VERSION), )
87         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).zip
88 else
89         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION)_$(SUB_VERSION).zip
90 endif
91
92 .PHONY: all
93 all: vendor build
94
95 .PHONY: build
96 build: checkgover
97         @echo "### Build $(TARGET) (version $(VERSION), subversion $(SUB_VERSION) - $(BUILD_MODE))";
98         @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)" .
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 .PHONY: clean
110 clean:
111         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
112
113 .PHONY: distclean
114 distclean: clean
115         rm -rf $(LOCAL_BINDIR) $(ROOT_SRCDIR)/tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
116
117 .PHONY: scripts
118 scripts:
119         @mkdir -p $(LOCAL_BINDIR) && cp -rf scripts/*.sh scripts/xds-utils $(LOCAL_BINDIR)
120
121 .PHONY: release
122 release:
123         RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile clean build
124
125 package: clean vendor build
126         @mkdir -p $(PACKAGE_DIR)/$(TARGET)
127         @cp -a $(LOCAL_BINDIR)/*gdb$(EXT) $(PACKAGE_DIR)/$(TARGET)
128 ifneq ($(GOOS), windows)
129         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)
130 endif
131         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
132
133 .PHONY: package-all
134 package-all:
135         @echo "# Build linux amd64..."
136         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
137         @echo "# Build windows amd64..."
138         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
139         @echo "# Build darwin amd64..."
140         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
141         make -f $(ROOT_SRCDIR)/Makefile clean
142
143 .PHONY: install
144 install:
145         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
146         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
147
148 .PHONY: uninstall
149 uninstall:
150         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
151
152 vendor: tools/glide glide.yaml
153         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
154
155 vendor/debug: vendor
156         (cd vendor/gerrit.automotivelinux.org/gerrit/src/xds && \
157                 rm -rf xds-common.git && ln -s ../../../../../../xds-common xds-common.git && \
158                 rm -rf xds-agent.git && ln -s ../../../../../../xds-agent xds-agent.git )
159
160 .PHONY: tools/glide
161 tools/glide:
162         @test -f $(LOCAL_TOOLSDIR)/glide || { \
163                 echo "Downloading glide"; \
164                 mkdir -p $(LOCAL_TOOLSDIR); \
165                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
166         }
167
168 .PHONY:
169 checkgover:
170         @test "$(CHECKGOVER)" = "true" || { echo $(CHECKERRMSG); exit 1; }
171
172
173 .PHONY: help
174 help:
175         @echo "Main supported rules:"
176         @echo "  all               (default)"
177         @echo "  build"
178         @echo "  release"
179         @echo "  clean"
180         @echo "  package"
181         @echo "  install / uninstall"
182         @echo "  distclean"
183         @echo ""
184         @echo "Influential make variables:"
185         @echo "  V                 - Build verbosity {0,1,2}."
186         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."