aboutsummaryrefslogtreecommitdiffstats
path: root/GNUmakefile
blob: 81e6f187ecd27fe9fd0ac0195979375b87d491d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Makefile for DEXON Consensus Core

# 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
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_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
ifdef BUILD_IN_DOCKER
    $(AT_DOCKER_GO)$(DOCKER) run --rm \
        -v BLSDATA:/data/bls \
        -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 $(GO_BUILD_ALL_FLAGS) \
                $(GO_IMPORT_PATH)/cmd/$1; \
            mv -f $(BLS_LIB).bak $(BLS_LIB))
else
    $(AT_LOCAL_GO)$(GO) build -o $(GOBIN)/$1 $(GO_BUILD_ALL_FLAGS) \
        $(GO_IMPORT_PATH)/cmd/$1
    @$(INSTALL) $(GOBIN)/$1 $(BUILDDIR)
endif
endef

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
else ifeq ($(ONLY_INTEGRATION_TEST), true)
    TEST_TARGET := $(TEST_TARGET) | $(GREP) integration_test
endif

ifneq ($(NO_TEST_RACE), true)
    GO_TEST_COMMON_FLAGS += -race
endif

COMPONENTS = \
    dexcon-simulation \
    dexcon-simulation-peer-server

.PHONY: clean default

default: all

all: $(COMPONENTS)
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 shellcheck check-security test

dep:
    bin/install_eth_dep.sh
    bin/install_dkg_dep.sh

docker-dep:
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:
    $(AT_RUN)$(GO) fmt \
        $$($(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor)

lint:
    $(AT_RUN)$(GOBIN)/golint -set_exit_status \
        $$($(GO) list $(GO_LIST_ALL_FLAGS) ./... | $(GREP) -v vendor)

vet:
    $(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
    $(AT_RUN)$(GOBIN)/gosec -quiet -out gosec.log ./... || true
    @if [ -e gosec.log ]; then \
        cat gosec.log; \
        echo 'Error: security issue found'; \
        exit 1; \
    fi


test-short:
    $(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:
    $(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:
    $(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:
    $(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

.ONESHELL:
test-sim: all
    @rm -rf build/test-sim
    @mkdir build/test-sim
    @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 \
        exit 1; \
    fi