Fix build release and packaging
[src/xds/xds-agent.git] / Makefile
index 7ba9163..7e8bd6c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,27 +1,35 @@
 # Makefile used to build XDS daemon Web Server
 
 # Application Version
-VERSION := 0.0.1
+VERSION := 0.2.0
 
 # Syncthing version to install
 SYNCTHING_VERSION = 0.14.28
-# FIXME: use patched version while waiting integration of #165
-#SYNCTHING_INOTIFY_VERSION = 0.8.5
-SYNCTHING_INOTIFY_VERSION = master
+SYNCTHING_INOTIFY_VERSION = 0.8.6
 
 
 
 # Retrieve git tag/commit to set sub-version string
 ifeq ($(origin SUB_VERSION), undefined)
-       SUB_VERSION := $(shell git describe --tags --always | sed 's/^v//')
+       SUB_VERSION := $(shell git describe --exact-match --tags 2>/dev/null | sed 's/^v//')
+       ifneq ($(SUB_VERSION), )
+               VERSION := $(firstword $(subst -, ,$(SUB_VERSION)))
+               SUB_VERSION := $(word 2,$(subst -, ,$(SUB_VERSION)))
+       endif
        ifeq ($(SUB_VERSION), )
-               SUB_VERSION=unknown-dev
+               SUB_VERSION := $(shell git rev-parse --short HEAD)
+               ifeq ($(SUB_VERSION), )
+                       SUB_VERSION := unknown-dev
+               endif
        endif
 endif
 
-# Configurable variables for installation (default /usr/local/...)
-ifeq ($(origin INSTALL_DIR), undefined)
-       INSTALL_DIR := /usr/local/bin
+# for backward compatibility
+DESTDIR := $(INSTALL_DIR)
+
+# Configurable variables for installation (default /opt/AGL/xds/agent)
+ifeq ($(origin DESTDIR), undefined)
+       DESTDIR := /opt/AGL/xds/agent
 endif
 
 HOST_GOOS=$(shell go env GOOS)
@@ -29,11 +37,16 @@ HOST_GOARCH=$(shell go env GOARCH)
 ARCH=$(HOST_GOOS)-$(HOST_GOARCH)
 REPOPATH=github.com/iotbzh/xds-agent
 
+EXT=
+ifeq ($(HOST_GOOS), windows)
+       EXT=.exe
+endif
+
 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
 ROOT_SRCDIR := $(patsubst %/,%,$(dir $(mkfile_path)))
 ROOT_GOPRJ := $(abspath $(ROOT_SRCDIR)/../../../..)
 LOCAL_BINDIR := $(ROOT_SRCDIR)/bin
-LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools
+LOCAL_TOOLSDIR := $(ROOT_SRCDIR)/tools/${HOST_GOOS}
 PACKAGE_DIR := $(ROOT_SRCDIR)/package
 
 export GOPATH := $(shell go env GOPATH):$(ROOT_GOPRJ)
@@ -42,32 +55,62 @@ export PATH := $(PATH):$(LOCAL_TOOLSDIR)
 VERBOSE_1 := -v
 VERBOSE_2 := -v -x
 
-all: tools/syncthing build
+# Release or Debug mode
+ifeq ($(filter 1,$(RELEASE) $(REL)),)
+       GO_LDFLAGS=
+       # disable compiler optimizations and inlining
+       GO_GCFLAGS=-N -l
+       BUILD_MODE="Debug mode"
+else
+       # optimized code without debug info
+       GO_LDFLAGS=-s -w
+       GO_GCFLAGS=
+       BUILD_MODE="Release mode"
+endif
+
+ifeq ($(SUB_VERSION), )
+       PACKAGE_ZIPFILE := xds-agent_$(ARCH)-v$(VERSION).zip
+else
+       PACKAGE_ZIPFILE := xds-agent_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip
+endif
+
 
-build: vendor tools/syncthing/copytobin
-       @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION))";
-       @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-agent -ldflags "-X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" .
+all: tools/syncthing vendor build
 
-package: clean build
-       @mkdir -p $(PACKAGE_DIR)/xds-agent
-       @cp agent-config.json.in $(PACKAGE_DIR)/xds-agent/agent-config.json
+build: tools/syncthing/copytobin
+       @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
+       @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-agent$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
+
+package: clean tools/syncthing vendor build
+       @mkdir -p $(PACKAGE_DIR)/xds-agent $(PACKAGE_DIR)/scripts
        @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/xds-agent
-       cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/xds-agent_$(ARCH)-v$(VERSION)_$(SUB_VERSION).zip ./xds-agent
+       @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/xds-agent
+       cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
+
+.PHONY: package-all
+package-all:
+       @echo "# Build linux amd64..."
+       GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
+       @echo "# Build windows amd64..."
+       GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
+       @echo "# Build darwin amd64..."
+       GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
+       make -f $(ROOT_SRCDIR)/Makefile clean
 
 test: tools/glide
-       go test --race $(shell ./tools/glide novendor)
+       go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
 
 vet: tools/glide
-       go vet $(shell ./tools/glide novendor)
+       go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor)
 
 fmt: tools/glide
-       go fmt $(shell ./tools/glide novendor)
+       go fmt $(shell $(LOCAL_TOOLSDIR)/glide novendor)
 
 run: build/xds tools/syncthing/copytobin
-       $(LOCAL_BINDIR)/xds-agent --log info -c agent-config.json.in
+       $(LOCAL_BINDIR)/xds-agent$(EXT) --log info -c agent-config.json.in
 
 debug: build/xds tools/syncthing/copytobin
-       $(LOCAL_BINDIR)/xds-agent --log debug -c agent-config.json.in
+       $(LOCAL_BINDIR)/xds-agent$(EXT) --log debug -c agent-config.json.in
 
 .PHONY: clean
 clean:
@@ -75,23 +118,33 @@ clean:
 
 .PHONY: distclean
 distclean: clean
-       rm -rf $(LOCAL_BINDIR) tools glide.lock vendor
+       rm -rf $(LOCAL_BINDIR) tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
 
 .PHONY: install
-install: all
-       mkdir -p $(INSTALL_DIR) && cp $(LOCAL_BINDIR)/* $(INSTALL_DIR)
+install:
+       @test -e $(LOCAL_BINDIR)/xds-agent$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
+       @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo        "Please execute first: make all\n"; exit 1; }
+       export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
+
+.PHONY: uninstall
+uninstall:
+       export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
 
 vendor: tools/glide glide.yaml
-       ./tools/glide install --strip-vendor
+       $(LOCAL_TOOLSDIR)/glide install --strip-vendor
 
+.PHONY: tools/glide
 tools/glide:
-       @echo "Downloading glide"
-       mkdir -p tools
-       curl --silent -L https://glide.sh/get | GOBIN=./tools  sh
+       @test -f $(LOCAL_TOOLSDIR)/glide || { \
+               echo "Downloading glide"; \
+               mkdir -p $(LOCAL_TOOLSDIR); \
+               curl --silent -L https://glide.sh/get | GOBIN=$(LOCAL_TOOLSDIR)  sh; \
+       }
 
 .PHONY: tools/syncthing
 tools/syncthing:
-       @test -e $(LOCAL_TOOLSDIR)/syncthing -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify  || { \
+       @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT)  || { \
+       mkdir -p $(LOCAL_TOOLSDIR); \
        DESTDIR=$(LOCAL_TOOLSDIR) \
        SYNCTHING_VERSION=$(SYNCTHING_VERSION) \
        SYNCTHING_INOTIFY_VERSION=$(SYNCTHING_INOTIFY_VERSION) \
@@ -99,15 +152,18 @@ tools/syncthing:
 
 .PHONY:
 tools/syncthing/copytobin:
-       @test -e $(LOCAL_TOOLSDIR)/syncthing -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
-       @cp -f $(LOCAL_TOOLSDIR)/syncthing* $(LOCAL_BINDIR)
+       @test -e $(LOCAL_TOOLSDIR)/syncthing$(EXT) -a -e $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) || { echo "Please execute first: make tools/syncthing\n"; exit 1; }
+       @mkdir -p $(LOCAL_BINDIR)
+       @cp -f $(LOCAL_TOOLSDIR)/syncthing$(EXT) $(LOCAL_TOOLSDIR)/syncthing-inotify$(EXT) $(LOCAL_BINDIR)
 
 .PHONY: help
 help:
        @echo "Main supported rules:"
-       @echo "  build               (default)"
+       @echo "  all               (default)"
+       @echo "  build"
        @echo "  package"
        @echo "  install"
+       @echo "  uninstall"
        @echo "  clean"
        @echo "  distclean"
        @echo ""