CC=go
BUILD=build -v -trimpath
VERSION=$(shell cat VERSION)
GCFLAGS=-gcflags '-e'
LDFLAGS=-ldflags '-buildid=$(VERSION) -X main.version=$(VERSION) -s -w'
PROGRAM=uvgo
CMD=-o $(PROGRAM) main.go
ENV=GOGC=0 CGO_ENABLED=0 GO111MODULE=on

all: $(PROGRAM)

setup:
	$(CC) install github.com/client9/misspell/cmd/misspell@latest
	$(CC) install honnef.co/go/tools/cmd/staticcheck@latest

release: clean gofmt codeqa
	$(ENV) $(CC) $(BUILD) $(GCFLAGS) $(LDFLAGS) $(CMD)

release-vendor: clean gofmt codeqa
	$(ENV) $(CC) $(BUILD) -mod=vendor $(GCFLAGS) $(LDFLAGS) $(CMD)

vendor: clean gofmt
	$(ENV) $(CC) $(BUILD) -mod=vendor $(GCFLAGS) $(LDFLAGS) $(CMD)

$(PROGRAM): clean gofmt
	$(ENV) $(CC) $(BUILD) $(GCFLAGS) $(LDFLAGS) $(CMD)

clean:
	$(CC) clean -x

gofmt:
	$(CC) fmt ./...

codeqa: govet misspell staticcheck test

govet:
	$(CC) vet ./...

update:
	$(ENV) $(CC) get -u ./...

misspell:
	$(GOPATH)/bin/misspell Makefile README.md main.go

staticcheck:
	$(GOPATH)/bin/staticcheck ./...

test:
ifeq ($(shell go env GOARCH), $(shell go env GOHOSTARCH))
ifeq ($(shell go env GOOS), $(shell go env GOHOSTOS))
	$(CC) test ./...
else
	$(info skipping tests of other platforms)
endif
else
	$(info skipping tests of other platforms)
endif

.PHONY: setup release release-vendor vendor build clean codeqa gofmt govet misspell staticcheck test
