# dkekey: make DKE store keys on your own machine. MIT licensed.

GO      ?= go
VERSION := $(shell sed -n 's/^const version = "\(.*\)"$$/\1/p' version.go)

# Every binary is built the same way, so anyone with the same Go release can
# rebuild one from this source and get the same bytes: no C toolchain, no
# build paths, no build id and no version-control stamp are embedded.
FLAGS   := -trimpath -buildvcs=false -ldflags='-s -w -buildid='
TARGETS := linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64

.PHONY: test build dist clean

test:
	$(GO) vet ./...
	$(GO) test ./...

# For this machine.
build:
	CGO_ENABLED=0 $(GO) build $(FLAGS) -o dkekey .

# For every platform a release is built for, into dist/<version>/.
dist:
	@mkdir -p dist/$(VERSION)
	@for t in $(TARGETS); do \
	  os=$${t%/*}; arch=$${t#*/}; ext=; [ $$os = windows ] && ext=.exe; \
	  out=dist/$(VERSION)/dkekey-$(VERSION)-$$os-$$arch$$ext; \
	  CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch $(GO) build $(FLAGS) -o $$out . || exit 1; \
	  echo $$out; \
	done

clean:
	rm -f dkekey dkekey.exe
	rm -rf dist
