f786abd1595aab2db3c8761184251a023bff021b
[src/xds/xds-gdb.git] / Makefile
1  ###########################################################################
2 # Copyright 2017 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 SUB_VERSION := $(subst $(VERSION)-,,$(GIT_DESC))
27 ifeq ($(VERSION), )
28         VERSION := unknown-dev
29 endif
30 ifeq ($(SUB_VERSION), )
31         SUB_VERSION := $(shell date +'%Y-%m-%d_%H%M%S')
32 endif
33
34 # Configurable variables for installation (default /opt/AGL/...)
35 ifeq ($(origin DESTDIR), undefined)
36         DESTDIR := /opt/AGL/xds/gdb
37 endif
38
39 HOST_GOOS=$(shell go env GOOS)
40 HOST_GOARCH=$(shell go env GOARCH)
41 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
42 REPOPATH=github.com/iotbzh/$(TARGET)
43
44 EXT=
45 ifeq ($(HOST_GOOS), windows)
46         EXT=.exe
47 endif
48
49 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
50 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
51 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
52 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
53 LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
54 PACKAGE_DIR := $(ROOT_SRCDIR)/package
55
56 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
57 export PATH := $(PATH):$(LOCAL_TOOLSDIR)
58
59 VERBOSE_1 := -v
60 VERBOSE_2 := -v -x
61
62 # Release or Debug mode
63 ifeq ($(filter 1,$(RELEASE) $(REL)),)
64         GO_LDFLAGS=
65         # disable compiler optimizations and inlining
66         GO_GCFLAGS=-N -l
67         BUILD_MODE="Debug mode"
68 else
69         # optimized code without debug info
70         GO_LDFLAGS=-s -w
71         GO_GCFLAGS=
72         BUILD_MODE="Release mode"
73 endif
74
75
76 ifeq ($(SUB_VERSION), )
77         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION).zip
78 else
79         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-$(VERSION)_$(SUB_VERSION).zip
80 endif
81
82 .PHONY: all
83 all: vendor build
84
85 .PHONY: build
86 build:
87         @echo "### Build $(TARGET) (version $(VERSION), subversion $(SUB_VERSION) - $(BUILD_MODE))";
88         @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)" .
89
90 test: tools/glide
91         go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
92
93 vet: tools/glide
94         go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
95
96 fmt: tools/glide
97         go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
98
99 .PHONY: clean
100 clean:
101         rm -rf $(LOCAL_BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
102
103 .PHONY: distclean
104 distclean: clean
105         rm -rf $(LOCAL_BINDIR) $(ROOT_SRCDIR)/tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
106
107 .PHONY: release
108 release:
109         RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile clean build
110
111 package: clean vendor build
112         @mkdir -p $(PACKAGE_DIR)/$(TARGET)
113         @cp -a $(LOCAL_BINDIR)/*gdb$(EXT) $(PACKAGE_DIR)/$(TARGET)
114         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)
115         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
116
117 .PHONY: package-all
118 package-all:
119         @echo "# Build linux amd64..."
120         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
121         @echo "# Build windows amd64..."
122         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
123         @echo "# Build darwin amd64..."
124         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
125         make -f $(ROOT_SRCDIR)/Makefile clean
126
127 .PHONY: install
128 install:
129         @test -e $(LOCAL_BINDIR)/$(TARGET)$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
130         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
131
132 .PHONY: uninstall
133 uninstall:
134         export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
135
136 vendor: tools/glide glide.yaml
137         $(LOCAL_TOOLSDIR)/glide install --strip-vendor
138
139 vendor/debug: vendor
140         (cd vendor/github.com/iotbzh && \
141                 rm -rf xds-common && ln -s ../../../../xds-common && \
142                 rm -rf xds-agent && ln -s ../../../../xds-agent )
143
144 .PHONY: tools/glide
145 tools/glide:
146         @test -f $(LOCAL_TOOLSDIR)/glide || { \
147                 echo "Downloading glide"; \
148                 mkdir -p $(LOCAL_TOOLSDIR); \
149                 curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
150         }
151
152 help:
153         @echo "Main supported rules:"
154         @echo "  all               (default)"
155         @echo "  build"
156         @echo "  release"
157         @echo "  clean"
158         @echo "  package"
159         @echo "  install / uninstall"
160         @echo "  distclean"
161         @echo ""
162         @echo "Influential make variables:"
163         @echo "  V                 - Build verbosity {0,1,2}."
164         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."