aboutsummaryrefslogtreecommitdiffstats
path: root/GNUmakefile
diff options
context:
space:
mode:
Diffstat (limited to 'GNUmakefile')
-rw-r--r--GNUmakefile168
1 files changed, 109 insertions, 59 deletions
diff --git a/GNUmakefile b/GNUmakefile
index bbf14bd..81e6f18 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -1,62 +1,104 @@
# Makefile for DEXON Consensus Core
-GOPATH = $(CURDIR)/../../../../
-ifndef BINDIR
-BINDIR := $(CURDIR)/build
+# Commands
+DOCKER ?= docker
+GO ?= go
+GOFMT ?= gofmt
+GOLINT ?= $(GOBIN)/golint
+GREP ?= grep
+INSTALL ?= install -c
+MKDIR_P ?= mkdir -p
+SHELLCHECK ?= shellcheck
+
+# Paths
+ifndef BUILDDIR
+BUILDDIR := $(CURDIR)/build
else
-BINDIR := $(abspath $(BINDIR))
-endif
-PROJECT_ROOT=github.com/dexon-foundation/dexon-consensus
-BLS_REPO = dexonfoundation/bls-go-alpine
-BLS_LIB = vendor/github.com/dexon-foundation/bls/lib/libbls384.a
-BUILDER_REPO = dexonfoundation/dexon-alpine
-
-ifeq ($(DOCKER),true)
-GO_LDFLAGS += -linkmode external -extldflags \"-static\"
+BUILDDIR := $(abspath $(BUILDDIR))
endif
+FIRST_GOPATH := $(shell $(GO) env GOPATH | cut -d: -f1)
+GOBIN ?= $(FIRST_GOPATH)/bin
+BLS_LIB = vendor/github.com/dexon-foundation/bls/lib/libbls384.a
+# Automake-like silent rules
V ?= 0
-AT_LOCAL_GO = $(AT_LOCAL_GO_$(V))
-AT_LOCAL_GO_0 = @echo " HOST GO "$1;
-AT_LOCAL_GO_1 =
-AT_DOCKER_GO = $(AT_DOCKER_GO_$(V))
-AT_DOCKER_GO_0 = @echo " DOCKER GO "$1;
-AT_DOCKER_GO_1 =
+AT_LOCAL_GO = $(AT_LOCAL_GO_$(V))
+AT_LOCAL_GO_0 = @echo " HOST GO "$1;
+AT_LOCAL_GO_1 =
+AT_DOCKER_GO = $(AT_DOCKER_GO_$(V))
+AT_DOCKER_GO_0 = @echo " DOCKER GO "$1;
+AT_DOCKER_GO_1 =
+AT_RUN = $(AT_RUN_$(V))
+AT_RUN_0 = @echo " RUN "$@;
+AT_RUN_1 =
+
+# Functions
+include functions.mk
+
+# Go build variables
+GO_IMPORT_PATH = github.com/dexon-foundation/dexon-consensus
+# Handle -tags safely
+GO_TAGS ?=
+GO_TAGS_SAFE = $(call SAFE_STRING,$(GO_TAGS))
+GO_TAGS_FLAGS = -tags $(GO_TAGS_SAFE)
+# Handle -ldflags safely
+GO_LDFLAGS =
+GO_LDFLAGS_SAFE = $(call SAFE_STRING,$(GO_LDFLAGS))
+GO_LDFLAGS_FLAGS = -ldflags $(GO_LDFLAGS_SAFE)
+# Handle -timeout
+GO_TEST_TIMEOUT := 33m
+# Produce common flags
+GO_BUILD_COMMON_FLAGS = $(GO_TAGS_FLAGS) $(GO_LDFLAGS_FLAGS)
+GO_VET_COMMON_FLAGS = $(GO_TAGS_FLAGS)
+GO_TEST_COMMON_FLAGS = -v -timeout $(GO_TEST_TIMEOUT)
+# Produce final flags
+GO_BUILD_ALL_FLAGS = $(GO_BUILD_COMMON_FLAGS) $(GO_BUILD_FLAGS)
+GO_LIST_ALL_FLAGS = $(GO_BUILD_COMMON_FLAGS) $(GO_BUILD_FLAGS)
+GO_VET_ALL_FLAGS = $(GO_VET_COMMON_FLAGS) $(GO_VET_FLAGS)
+GO_TEST_ALL_FLAGS = $(GO_TEST_COMMON_FLAGS) $(GO_BUILD_COMMON_FLAGS) $(GO_BUILD_FLAGS)
+
+# Builder images
+BUILDER_IMAGE = dexonfoundation/dexon-alpine:latest
+BLS_IMAGE = dexonfoundation/bls-go-alpine:latest
+
+ifdef BUILD_IN_DOCKER
+GO_TAGS += static
+GO_LDFLAGS += -linkmode external -extldflags -static
+GO_BUILD_COMMON_FLAGS += -buildmode=pie
+endif
define BUILD_RULE
$1: pre-build
-ifeq ($(DOCKER),true)
- $(AT_DOCKER_GO)docker run --rm \
+ifdef BUILD_IN_DOCKER
+ $(AT_DOCKER_GO)$(DOCKER) run --rm \
-v BLSDATA:/data/bls \
- -v "$(GOPATH)":/go:z \
- -v $(BINDIR):/artifacts:z \
- -e "GOPATH=/go" \
- -w /go/src/$(PROJECT_ROOT) \
- $(BUILDER_REPO):latest sh -c "\
+ -v $(FIRST_GOPATH):/go:z \
+ -v $(BUILDDIR):/artifacts:z \
+ -e GOPATH=/go \
+ -w /go/src/$(GO_IMPORT_PATH) \
+ $(BUILDER_IMAGE) sh -c $(call SAFE_STRING,\
mv -f $(BLS_LIB) $(BLS_LIB).bak; \
- cp /data/bls/libbls384.a $(BLS_LIB) ;\
- go build -o /artifacts/$1 $(PROJECT_ROOT)/cmd/$1; \
- mv -f $(BLS_LIB).bak $(BLS_LIB)"
+ cp /data/bls/libbls384.a $(BLS_LIB); \
+ go build -o /artifacts/$1 $(GO_BUILD_ALL_FLAGS) \
+ $(GO_IMPORT_PATH)/cmd/$1; \
+ mv -f $(BLS_LIB).bak $(BLS_LIB))
else
- @mkdir -p $(BINDIR)
- $(AT_LOCAL_GO)go install -ldflags '$(GO_LDFLAGS)' $(PROJECT_ROOT)/cmd/$1
- @install -c $(GOPATH)/bin/$1 $(BINDIR)
+ $(AT_LOCAL_GO)$(GO) build -o $(GOBIN)/$1 $(GO_BUILD_ALL_FLAGS) \
+ $(GO_IMPORT_PATH)/cmd/$1
+ @$(INSTALL) $(GOBIN)/$1 $(BUILDDIR)
endif
endef
-GO_TEST_TIMEOUT := 33m
-
-TEST_TARGET := go list ./... | grep -v 'vendor'
+TEST_TARGET := $(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor
ifeq ($(NO_INTEGRATION_TEST), true)
GO_TEST_TIMEOUT := 25m
- TEST_TARGET := $(TEST_TARGET) | grep -v 'integration_test'
+ TEST_TARGET := $(TEST_TARGET) | $(GREP) -v integration_test
else ifeq ($(ONLY_INTEGRATION_TEST), true)
- TEST_TARGET := $(TEST_TARGET) | grep 'integration_test'
+ TEST_TARGET := $(TEST_TARGET) | $(GREP) integration_test
endif
-GO_TEST_FLAG := -v -timeout $(GO_TEST_TIMEOUT)
ifneq ($(NO_TEST_RACE), true)
- GO_TEST_FLAG := $(GO_TEST_FLAG) -race
+ GO_TEST_COMMON_FLAGS += -race
endif
COMPONENTS = \
@@ -68,39 +110,47 @@ COMPONENTS = \
default: all
all: $(COMPONENTS)
-ifeq ($(DOCKER),true)
- @docker volume rm BLSDATA > /dev/null
+ifdef BUILD_IN_DOCKER
+ $(DOCKER) volume rm BLSDATA > /dev/null
endif
$(foreach component, $(COMPONENTS), $(eval $(call BUILD_RULE,$(component))))
pre-build: dep docker-dep
+ @$(MKDIR_P) $(BUILDDIR)
-pre-submit: dep check-format lint vet check-security test
+pre-submit: dep check-format lint vet shellcheck check-security test
dep:
- @bin/install_eth_dep.sh
- @bin/install_dkg_dep.sh
+ bin/install_eth_dep.sh
+ bin/install_dkg_dep.sh
docker-dep:
-ifeq ($(DOCKER),true)
- @docker run --rm -v BLSDATA:/data/bls $(BLS_REPO):latest \
- sh -c "cp -f /usr/lib/libbls384.a /data/bls/"
+ifdef BUILD_IN_DOCKER
+ $(DOCKER) volume create BLSDATA > /dev/null
+ $(DOCKER) run --rm -v BLSDATA:/data/bls $(BLS_IMAGE) \
+ cp -f /usr/lib/libbls384.a /data/bls/
endif
format:
- @go fmt `go list ./... | grep -v 'vendor'`
+ $(AT_RUN)$(GO) fmt \
+ $$($(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor)
lint:
- @$(GOPATH)/bin/golint -set_exit_status `go list ./... | grep -v 'vendor'`
+ $(AT_RUN)$(GOBIN)/golint -set_exit_status \
+ $$($(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor)
vet:
- @go vet `go list ./... | grep -v 'vendor'`
+ $(AT_RUN)$(GO) vet $(GO_VET_ALL_FLAGS) \
+ $$($(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor)
+
+shellcheck:
+ $(AT_RUN)$(SHELLCHECK) */*.sh */*/*.sh
check-security:
@rm -f gosec.log
- @gosec -quiet -out gosec.log ./... || true
- @if [ -a gosec.log ]; then \
+ $(AT_RUN)$(GOBIN)/gosec -quiet -out gosec.log ./... || true
+ @if [ -e gosec.log ]; then \
cat gosec.log; \
echo 'Error: security issue found'; \
exit 1; \
@@ -108,31 +158,31 @@ check-security:
test-short:
- @for pkg in `$(TEST_TARGET)`; do \
- if ! go test -short $(GO_TEST_FLAG) $$pkg; then \
+ $(AT_RUN)for pkg in $$($(TEST_TARGET)); do \
+ if ! $(GO) test -short $(GO_TEST_ALL_FLAGS) "$$pkg"; then \
echo 'Some test failed, abort'; \
exit 1; \
fi; \
done
test:
- @for pkg in `$(TEST_TARGET)`; do \
- if ! go test $(GO_TEST_FLAG) $$pkg; then \
+ $(AT_RUN)for pkg in $$($(TEST_TARGET)); do \
+ if ! $(GO) test $(GO_TEST_ALL_FLAGS) "$$pkg"; then \
echo 'Some test failed, abort'; \
exit 1; \
fi; \
done
bench:
- @for pkg in `go list ./... | grep -v 'vendor'`; do \
- if ! go test -bench=. -run=^$$ $$pkg; then \
+ $(AT_RUN)for pkg in $$($(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor); do \
+ if ! $(GO) test -bench=. -run=^$$ $(GO_TEST_nALL_FLAGS) "$$pkg"; then \
echo 'Some test failed, abort'; \
exit 1; \
fi; \
done
check-format:
- @if gofmt -l `go list -f '{{.Dir}}' ./...` | grep -q go; then \
+ $(AT_RUN)if $(GOFMT) -l $$($(GO) list -f '{{.Dir}}' $(GO_LIST_ALL_FLAGS) ./...) | $(GREP) -q go; then \
echo 'Error: source code not formatted'; \
exit 1; \
fi
@@ -144,6 +194,6 @@ test-sim: all
@cp test_config/test.toml build/test-sim/
@cd build/test-sim ; ../dexcon-simulation-peer-server -config test.toml >& server.log &
@cd build/test-sim ; ../dexcon-simulation -config test.toml >& /dev/null
- @if grep "error" build/test-sim/server.log -q -i; then \
+ @if $(GREP) "error" build/test-sim/server.log -q -i; then \
exit 1; \
fi