ed58a296e06b2699d50041a494c00c731f5430ef
[src/xds/xds-gdb.git] / Makefile
1 # Makefile used to build xds-gdb commands
2
3 # Application Version
4 VERSION := 0.1.0
5 TARGET=xds-gdb
6
7 # Retrieve git tag/commit to set sub-version string
8 ifeq ($(origin SUB_VERSION), undefined)
9         SUB_VERSION := $(shell git describe --exact-match --tags 2>/dev/null | sed 's/^v//')
10         ifneq ($(SUB_VERSION), )
11                 VERSION := $(firstword $(subst -, ,$(SUB_VERSION)))
12                 SUB_VERSION := $(word 2,$(subst -, ,$(SUB_VERSION)))
13         endif
14         ifeq ($(SUB_VERSION), )
15                 SUB_VERSION := $(shell git rev-parse --short HEAD)
16                 ifeq ($(SUB_VERSION), )
17                         SUB_VERSION := unknown-dev
18                 endif
19         endif
20 endif
21
22 HOST_GOOS=$(shell go env GOOS)
23 HOST_GOARCH=$(shell go env GOARCH)
24 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
25 REPOPATH=github.com/iotbzh/$(TARGET)
26
27 EXT=
28 ifeq ($(HOST_GOOS), windows)
29         EXT=.exe
30 endif
31
32
33 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
34 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
35 BINDIR := $(ROOT_SRCDIR)/bin
36 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
37 PACKAGE_DIR := $(ROOT_SRCDIR)/package
38
39 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
40 export PATH := $(PATH):$(ROOT_SRCDIR)/tools
41
42 VERBOSE_1 := -v
43 VERBOSE_2 := -v -x
44
45 # Release or Debug mode
46 ifeq ($(filter 1,$(RELEASE) $(REL)),)
47         GO_LDFLAGS=
48         # disable compiler optimizations and inlining
49         GO_GCFLAGS=-N -l
50         BUILD_MODE="Debug mode"
51 else
52         # optimized code without debug info
53         GO_LDFLAGS=-s -w
54         GO_GCFLAGS=
55         BUILD_MODE="Release mode"
56 endif
57
58
59 ifeq ($(SUB_VERSION), )
60         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-v$(VERSION).zip
61 else
62         PACKAGE_ZIPFILE := $(TARGET)_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip
63 endif
64
65 .PHONY: all
66 all: vendor build
67
68 .PHONY: build
69 build:
70         @echo "### Build $(TARGET) (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
71         @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(BINDIR)/$(TARGET)$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
72
73 test: tools/glide
74         go test --race $(shell ./tools/glide novendor)
75
76 vet: tools/glide
77         go vet $(shell ./tools/glide novendor)
78
79 fmt: tools/glide
80         go fmt $(shell ./tools/glide novendor)
81
82 .PHONY: clean
83 clean:
84         rm -rf $(BINDIR)/* debug $(ROOT_GOPRJ)/pkg/*/$(REPOPATH) $(PACKAGE_DIR)
85
86 distclean: clean
87         rm -rf $(BINDIR) tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
88
89 .PHONY: release
90 release:
91         RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile clean build
92
93 package: clean vendor build
94         @mkdir -p $(PACKAGE_DIR)/$(TARGET)
95         @cp -a $(BINDIR)/*gdb$(EXT) $(PACKAGE_DIR)/$(TARGET)
96         @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/$(TARGET)
97         cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./$(TARGET)
98
99 .PHONY: package-all
100 package-all:
101         @echo "# Build linux amd64..."
102         GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
103         @echo "# Build windows amd64..."
104         GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
105         @echo "# Build darwin amd64..."
106         GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
107         make -f $(ROOT_SRCDIR)/Makefile clean
108
109 vendor: tools/glide glide.yaml
110         ./tools/glide install --strip-vendor
111
112 vendor/debug: vendor
113         (cd vendor/github.com/iotbzh && \
114                 rm -rf xds-common && ln -s ../../../../xds-common && \
115                 rm -rf xds-server && ln -s ../../../../xds-server )
116
117 tools/glide:
118         @echo "Downloading glide"
119         mkdir -p tools
120         curl --silent -L https://glide.sh/get | GOBIN=./tools  sh
121
122 help:
123         @echo "Main supported rules:"
124         @echo "  all               (default)"
125         @echo "  release"
126         @echo "  clean"
127         @echo "  package"
128         @echo "  distclean"
129         @echo ""
130         @echo "Influential make variables:"
131         @echo "  V                 - Build verbosity {0,1,2}."
132         @echo "  BUILD_ENV_FLAGS   - Environment added to 'go build'."