Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 24d8745

Browse files
committed
Add Makefiles
1 parent f88c0fe commit 24d8745

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include Makefile.inc
2+
include mk/main.mk

Makefile.inc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Project name, used to name the binaries
2+
PKG_NAME := lambda-machine-local
3+
4+
# If true, disable optimizations and does NOT strip the binary
5+
DEBUG ?=
6+
# If true, "build" will produce a static binary (cross compile always produce static build regardless)
7+
STATIC ?=
8+
# If true, turn on verbose output for build
9+
VERBOSE ?=
10+
# Build tags
11+
BUILDTAGS ?=
12+
# Adjust number of parallel builds (XXX not used)
13+
PARALLEL ?= -1
14+
# Coverage default directory
15+
COVERAGE_DIR ?= cover
16+
# Whether to perform targets inside a docker container, or natively on the host
17+
USE_CONTAINER ?=
18+
19+
# List of cross compilation targets
20+
ifeq ($(TARGET_OS),)
21+
TARGET_OS := darwin linux windows
22+
endif
23+
24+
ifeq ($(TARGET_ARCH),)
25+
TARGET_ARCH := amd64
26+
endif
27+
28+
# Output prefix, defaults to local directory if not specified
29+
ifeq ($(PREFIX),)
30+
PREFIX := $(shell pwd)
31+
endif

mk/build.mk

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extension = $(patsubst windows,.exe,$(filter windows,$(1)))
2+
3+
# Valid target combinations
4+
VALID_OS_ARCH := "[darwin/amd64][linux/amd64][windows/amd64]"
5+
6+
os.darwin := Darwin
7+
os.linux := Linux
8+
os.windows := Windows
9+
10+
arch.amd64 := x86_64
11+
12+
define gocross
13+
$(if $(findstring [$(1)/$(2)],$(VALID_OS_ARCH)), \
14+
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 \
15+
$(GO) build \
16+
-o $(PREFIX)/bin/lambda-machine-local-${os.$(1)}-${arch.$(2)}$(call extension,$(GOOS)) \
17+
-a $(VERBOSE_GO) -tags "static_build netgo $(BUILDTAGS)" -installsuffix netgo \
18+
-ldflags "$(GO_LDFLAGS) -extldflags -static" $(GO_GCFLAGS) ./cmd/machine.go;)
19+
endef
20+
21+
build-clean:
22+
rm -Rf $(PREFIX)/bin/*
23+
24+
build-x: $(shell find . -type f -name '*.go')
25+
$(foreach GOARCH,$(TARGET_ARCH),$(foreach GOOS,$(TARGET_OS),$(call gocross,$(GOOS),$(GOARCH))))
26+
27+
$(PREFIX)/bin/lambda-machine-local$(call extension,$(GOOS)): $(shell find . -type f -name '*.go')
28+
$(GO) build \
29+
-o $@ \
30+
$(VERBOSE_GO) -tags "$(BUILDTAGS)" \
31+
-ldflags "$(GO_LDFLAGS)" $(GO_GCFLAGS) ./cmd/machine.go
32+
33+
build: $(PREFIX)/bin/lambda-machine-local$(call extension,$(GOOS))

mk/coverage.mk

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# COVERAGE_OUTPUT dir is a temp dir (OSX/Linux compatible), unless explicitly specified through env COVERAGE_DIR
2+
COVERAGE_OUTPUT := $(COVERAGE_DIR)
3+
ifeq ($(COVERAGE_OUTPUT),)
4+
COVERAGE_OUTPUT := $(shell mktemp -d 2>/dev/null || mktemp -d -t machine-coverage)
5+
endif
6+
7+
# Final cover file, html, and mode
8+
COVERAGE_PROFILE := $(COVERAGE_OUTPUT)/profile.out
9+
COVERAGE_HTML := $(COVERAGE_OUTPUT)/index.html
10+
COVERAGE_MODE := set
11+
12+
# Goveralls dependency
13+
GOVERALLS_BIN := $(GOPATH)/bin/goveralls
14+
GOVERALLS := $(shell [ -x $(GOVERALLS_BIN) ] && echo $(GOVERALLS_BIN) || echo '')
15+
16+
# Generate coverage
17+
coverage-generate: $(COVERAGE_PROFILE)
18+
19+
# Send the results to coveralls
20+
coverage-send: $(COVERAGE_PROFILE)
21+
$(if $(GOVERALLS), , $(error Please install goveralls: go get github.com/mattn/goveralls))
22+
@$(GOVERALLS) -service travis-ci -coverprofile="$(COVERAGE_PROFILE)"
23+
24+
# Generate html report
25+
coverage-html: $(COVERAGE_HTML)
26+
27+
# Serve over http - useful only if building remote/headless
28+
coverage-serve: $(COVERAGE_HTML)
29+
@cd "$(COVERAGE_OUTPUT)" && python -m SimpleHTTPServer 8000
30+
31+
# Clean up coverage coverage output
32+
coverage-clean:
33+
@rm -Rf "$(COVERAGE_OUTPUT)/coverage"
34+
@rm -f "$(COVERAGE_HTML)"
35+
@rm -f "$(COVERAGE_PROFILE)"
36+
37+
$(COVERAGE_PROFILE): $(shell find . -type f -name '*.go')
38+
@mkdir -p "$(COVERAGE_OUTPUT)/coverage"
39+
@$(foreach PKG,$(PKGS), go test $(VERBOSE_GO) -tags "$(BUILDTAGS)" -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_OUTPUT)/coverage/`echo $(PKG) | tr "/" "-"`.cover" "$(PKG)";)
40+
@echo "mode: $(COVERAGE_MODE)" > "$(COVERAGE_PROFILE)"
41+
@grep -h -v "^mode:" "$(COVERAGE_OUTPUT)/coverage"/*.cover >> "$(COVERAGE_PROFILE)"
42+
43+
$(COVERAGE_HTML): $(COVERAGE_PROFILE)
44+
$(GO) tool cover -html="$(COVERAGE_PROFILE)" -o "$(COVERAGE_HTML)"

mk/main.mk

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Initialize version and gc flags
2+
GO_LDFLAGS := -X `go list ./version`.GitCommit=`git rev-parse --short HEAD 2>/dev/null`
3+
GO_GCFLAGS :=
4+
5+
# Full package list
6+
PKGS := $(shell go list -tags "$(BUILDTAGS)" ./... | grep -v "/vendor/" | grep -v "/cmd")
7+
8+
# Resolving binary dependencies for specific targets
9+
GOLINT_BIN := $(GOPATH)/bin/golint
10+
GOLINT := $(shell [ -x $(GOLINT_BIN) ] && echo $(GOLINT_BIN) || echo '')
11+
12+
GODEP_BIN := $(GOPATH)/bin/godep
13+
GODEP := $(shell [ -x $(GODEP_BIN) ] && echo $(GODEP_BIN) || echo '')
14+
15+
# Honor debug
16+
ifeq ($(DEBUG),true)
17+
# Disable function inlining and variable registerization
18+
GO_GCFLAGS := -gcflags "-N -l"
19+
else
20+
# Turn of DWARF debugging information and strip the binary otherwise
21+
GO_LDFLAGS := $(GO_LDFLAGS) -w -s
22+
endif
23+
24+
# Honor static
25+
ifeq ($(STATIC),true)
26+
# Append to the version
27+
GO_LDFLAGS := $(GO_LDFLAGS) -extldflags -static
28+
endif
29+
30+
# Honor verbose
31+
VERBOSE_GO :=
32+
GO := go
33+
ifeq ($(VERBOSE),true)
34+
VERBOSE_GO := -v
35+
endif
36+
37+
include mk/build.mk
38+
include mk/coverage.mk
39+
include mk/test.mk
40+
include mk/validate.mk
41+
42+
.all_build: build build-clean build-x
43+
.all_coverage: coverage-generate coverage-html coverage-send coverage-serve coverage-clean
44+
.all_test: test-short test-long
45+
.all_validate: fmt vet lint
46+
47+
default: build
48+
49+
install:
50+
cp $(PREFIX)/bin/lambda-machine-local /usr/local/bin
51+
52+
clean: coverage-clean build-clean
53+
test: fmt test-short lint vet
54+
validate: fmt lint vet test-long
55+
56+
.PHONY: .all_build .all_coverage .all_test .all_validate test build validate clean

mk/test.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Quick test. You can bypass long tests using: `if testing.Short() { t.Skip("Skipping in short mode.") }`
2+
test-short:
3+
$(GO) test $(VERBOSE_GO) -test.short -tags "$(BUILDTAGS)" $(PKGS)
4+
5+
# Runs long tests also, plus race detection
6+
test-long:
7+
$(GO) test $(VERBOSE_GO) -race -tags "$(BUILDTAGS)" $(PKGS)

mk/validate.mk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Validate DCO on all history
2+
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
3+
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
4+
5+
fmt:
6+
@test -z "$$(gofmt -s -l . 2>&1 | grep -v vendor/)"
7+
8+
vet:
9+
@test -z "$$(go vet $(PKGS) 2>&1)"
10+
11+
lint:
12+
$(if $(GOLINT), , \
13+
$(error Please install golint: go get -u github.com/golang/lint/golint))
14+
@test -z "$$($(GOLINT) ./... 2>&1 | grep -v vendor/ | grep -v "cli/" | grep -v "should have comment")"

0 commit comments

Comments
 (0)