-
Notifications
You must be signed in to change notification settings - Fork 105
/
Makefile
125 lines (105 loc) · 4.47 KB
/
Makefile
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
# Package options.
NAME := go-php
DESCRIPTION := PHP bindings for the Go programming language
IMPORT_PATH := github.com/deuill/$(NAME)
VERSION := $(shell git describe --tags --always --dirty="-dev")
# Generic build options.
PHP_VERSION := 7.0.30
STATIC := false
DOCKER_IMAGE := deuill/$(NAME):$(PHP_VERSION)
# Go build options.
GO := go
TAGS := -tags 'php$(word 1,$(subst ., ,$(PHP_VERSION))) $(if $(findstring true,$(STATIC)),static)'
# Install options.
PREFIX := /usr
# Default Makefile options.
VERBOSE :=
# Variables to pass down to sub-invocations of 'make'.
MAKE_OPTIONS := PHP_VERSION=$(PHP_VERSION) GO=$(GO) PREFIX=$(PREFIX) VERBOSE=$(VERBOSE) STATIC=$(STATIC)
## Build binary distribution for library.
build: .build/env/GOPATH/.ok
@echo "Building '$(NAME)'..."
$Q $(GO) install $(if $(VERBOSE),-v) $(TAGS) $(IMPORT_PATH)
## Run test for all local packages or specified PACKAGE.
test: .build/env/GOPATH/.ok
@echo "Running tests for '$(NAME)'..."
$Q $(GO) test -race $(if $(VERBOSE),-v) $(TAGS) $(if $(PACKAGE),$(PACKAGE),$(PACKAGES))
@echo "Running 'vet' for '$(NAME)'..."
$Q $(GO) vet $(if $(VERBOSE),-v) $(TAGS) $(if $(PACKAGE),$(PACKAGE),$(PACKAGES))
## Create test coverage report for all local packages or specified PACKAGE.
cover: .build/env/GOPATH/.ok
@echo "Creating code coverage report for '$(NAME)'..."
$Q rm -Rf .build/tmp && mkdir -p .build/tmp
$Q for pkg in $(if $(PACKAGE),$(PACKAGE),$(PACKAGES)); do \
name=`echo $$pkg.cover | tr '/' '.'`; \
imports=`go list -f '{{ join .Imports " " }}' $$pkg`; \
coverpkg=`echo "$$imports $(PACKAGES)" | tr ' ' '\n' | sort | uniq -d | tr '\n' ','`; \
$(GO) test $(if $(VERBOSE),-v) $(TAGS) -coverpkg $$coverpkg$$pkg -coverprofile .build/tmp/$$name $$pkg; done
$Q awk "$$COVERAGE_MERGE" .build/tmp/*.cover > .build/tmp/cover.merged
$Q $(GO) tool cover -html .build/tmp/cover.merged -o .build/tmp/coverage.html
@echo "Coverage report written to '.build/tmp/coverage.html'"
@echo "Total coverage for '$(NAME)':"
$Q $(GO) tool cover -func .build/tmp/cover.merged
## Remove temporary files and packages required for build.
clean:
@echo "Cleaning '$(NAME)'..."
$Q $(GO) clean
$Q rm -Rf .build
## Show usage information for this Makefile.
help:
@printf "$(BOLD)$(DESCRIPTION)$(RESET)\n\n"
@printf "This Makefile contains tasks for processing auxiliary actions, such as\n"
@printf "building binaries, packages, or running tests against the test suite.\n\n"
@printf "$(UNDERLINE)Available Tasks$(RESET)\n\n"
@awk -F \
':|##' '/^##/ {c=$$2; getline; printf "$(BLUE)%10s$(RESET) %s\n", $$1, c}' \
$(MAKEFILE_LIST)
@printf "\n"
.PHONY: build test cover clean
.DEFAULT:
$Q $(MAKE) -s -f $(MAKEFILE) help
# Pull or build Docker image for PHP version specified.
docker-image:
$Q docker image pull $(DOCKER_IMAGE) || \
docker build --build-arg=PHP_VERSION=$(PHP_VERSION) --build-arg=STATIC=$(STATIC) \
-t $(DOCKER_IMAGE) -f Dockerfile . \
# Run Make target in Docker container. For instance, to run 'test', call as 'docker-test'.
docker-%: docker-image
$Q docker run --rm -e GOPATH="/tmp/go" \
-v "$(CURDIR):/tmp/go/src/$(IMPORT_PATH)" $(DOCKER_IMAGE) \
"$(MAKE) -C /tmp/go/src/$(IMPORT_PATH) $(word 2,$(subst -, ,$@)) $(MAKE_OPTIONS)"
.build/env/GOPATH/.ok:
$Q mkdir -p "$(dir .build/env/GOPATH/src/$(IMPORT_PATH))" && touch $@
$Q ln -s ../../../../../.. ".build/env/GOPATH/src/$(IMPORT_PATH)"
MAKEFILE := $(lastword $(MAKEFILE_LIST))
Q := $(if $(VERBOSE),,@)
PACKAGES = $(shell ( \
cd $(CURDIR)/.build/env/GOPATH/src/$(IMPORT_PATH) && \
GOPATH=$(CURDIR)/.build/env/GOPATH go list ./... | grep -v "vendor" \
))
export GOPATH := $(CURDIR)/.build/env/GOPATH
BOLD = \033[1m
UNDERLINE = \033[4m
BLUE = \033[36m
RESET = \033[0m
define COVERAGE_MERGE
/^mode: (set|count|atomic)/ {
if ($$2 == "set") mode = "set"
next
}
/^mode: / {
printf "Unknown mode '%s' in %s, line %d", $$2, FILENAME, FNR | "cat >&2"
exit 1
}
{
val = $$NF; $$NF = ""
blocks[$$0] += val
}
END {
printf "mode: %s\n", (mode == "set") ? "set" : "count"
for (b in blocks) {
printf "%s%d\n", b, (mode == "set" && blocks[b] > 1) ? 1 : blocks[b]
}
}
endef
export COVERAGE_MERGE