2024-07-19 14:55:23 +02:00
|
|
|
BINNAME = gomonop
|
2024-07-19 14:29:51 +02:00
|
|
|
BINDIR = $(CURDIR)/bin
|
2024-07-19 15:10:50 +02:00
|
|
|
DISTDIR = $(CURDIR)/dist
|
2024-07-19 14:29:51 +02:00
|
|
|
TARGETS = linux/amd64 linux/386 linux/arm linux/arm64
|
2024-07-19 15:20:14 +02:00
|
|
|
STATICFILES = README.md LICENSE
|
2024-07-19 14:29:51 +02:00
|
|
|
|
2024-07-19 15:36:50 +02:00
|
|
|
SRC := $(shell find . -type f -name '*.go' -not -path './dist/*' -print) go.mod go.sum
|
2024-07-19 14:29:51 +02:00
|
|
|
GOHOSTOS = $(shell go env GOHOSTOS)
|
|
|
|
GOHOSTARCH ?= $(shell go env GOHOSTARCH)
|
|
|
|
|
|
|
|
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
|
2024-07-19 15:36:50 +02:00
|
|
|
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
|
|
|
|
GIT_STATUS ?= $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
|
2024-07-19 14:29:51 +02:00
|
|
|
|
|
|
|
ifneq ($(VERSION),)
|
|
|
|
BINARY_VERSION = $(VERSION)
|
|
|
|
endif
|
|
|
|
BINARY_VERSION ?= $(GIT_TAG)
|
|
|
|
|
|
|
|
LDFLAGS += -w -s
|
|
|
|
ifneq ($(BINARY_VERSION),)
|
2024-07-19 14:55:23 +02:00
|
|
|
LDFLAGS += -X nocternity.net/gomonop/pkg/version.version=${BINARY_VERSION}
|
2024-07-19 14:29:51 +02:00
|
|
|
endif
|
2024-07-19 14:55:23 +02:00
|
|
|
LDFLAGS += -X nocternity.net/gomonop/pkg/version.commit=${GIT_COMMIT}
|
|
|
|
LDFLAGS += -X nocternity.net/gomonop/pkg/version.status=${GIT_STATUS}
|
2024-07-19 14:29:51 +02:00
|
|
|
|
2024-07-19 17:52:26 +02:00
|
|
|
# Use bash as the shell, mostly for -o pipefail
|
|
|
|
SHELL = /usr/bin/env bash
|
|
|
|
|
2024-07-19 14:29:51 +02:00
|
|
|
|
|
|
|
.PHONY: all
|
|
|
|
all: build
|
|
|
|
|
2024-07-19 15:10:50 +02:00
|
|
|
# ---------------
|
|
|
|
# Basic building
|
|
|
|
|
2024-07-19 14:29:51 +02:00
|
|
|
.PHONY: build
|
|
|
|
build: GOOS=$(GOHOSTOS)
|
|
|
|
build: GOARCH=$(GOHOSTARCH)
|
|
|
|
build: build-target symlink
|
|
|
|
|
|
|
|
.PHONY: symlink
|
|
|
|
symlink:
|
2024-07-19 14:55:23 +02:00
|
|
|
@ln -sf $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME) $(BINDIR)/$(BINNAME)
|
2024-07-19 14:29:51 +02:00
|
|
|
|
|
|
|
.PHONY: build-target
|
2024-07-19 14:55:23 +02:00
|
|
|
build-target: LDFLAGS += -X nocternity.net/gomonop/pkg/version.target=${GOOS}/${GOARCH}
|
|
|
|
build-target: $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME)
|
2024-07-19 14:29:51 +02:00
|
|
|
|
2024-07-19 14:55:23 +02:00
|
|
|
$(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME): $(SRC)
|
|
|
|
@mkdir -p $(BINDIR)/$(GOOS)-$(GOARCH)
|
|
|
|
@CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME) .
|
2024-07-19 14:29:51 +02:00
|
|
|
|
|
|
|
.PHONY: test
|
|
|
|
test: build
|
|
|
|
@go test -v ./...
|
|
|
|
|
2024-07-19 15:10:50 +02:00
|
|
|
# ----------------
|
|
|
|
# Cross compiling
|
|
|
|
|
2024-07-19 14:29:51 +02:00
|
|
|
.PHONY: build-cross
|
|
|
|
build-cross:
|
|
|
|
@for target in $(TARGETS); do \
|
|
|
|
$(MAKE) build-target GOOS=`echo $$target | cut -d / -f 1` GOARCH=`echo $$target | cut -d / -f 2`; \
|
|
|
|
done
|
|
|
|
|
2024-07-19 15:10:50 +02:00
|
|
|
# ----------
|
|
|
|
# Packaging
|
|
|
|
|
|
|
|
.PHONY: dist
|
|
|
|
dist: GOOS=$(GOHOSTOS)
|
|
|
|
dist: GOARCH=$(GOHOSTARCH)
|
|
|
|
dist: dist-target
|
|
|
|
|
|
|
|
.PHONY: dist-all
|
2024-07-19 15:36:50 +02:00
|
|
|
dist-all: dist-source
|
2024-07-19 17:48:13 +02:00
|
|
|
@set -eu -o pipefail; \
|
|
|
|
for target in $(TARGETS); do \
|
2024-07-19 15:10:50 +02:00
|
|
|
$(MAKE) dist-target GOOS=`echo $$target | cut -d / -f 1` GOARCH=`echo $$target | cut -d / -f 2`; \
|
2024-07-19 17:48:13 +02:00
|
|
|
done; \
|
|
|
|
mkdir -p $(DISTDIR)/release; \
|
|
|
|
mv $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)-*.tar.xz $(DISTDIR)/release
|
2024-07-19 15:10:50 +02:00
|
|
|
|
2024-07-19 15:36:50 +02:00
|
|
|
.PHONY: dist-source
|
|
|
|
dist-source:
|
2024-07-19 17:48:13 +02:00
|
|
|
@set -eu -o pipefail; \
|
2024-07-19 15:36:50 +02:00
|
|
|
if [ -z "$(BINARY_VERSION)" ]; then \
|
|
|
|
echo "No version set"; \
|
|
|
|
exit 1; \
|
|
|
|
fi; \
|
|
|
|
rm -rf $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION); \
|
|
|
|
mkdir -p $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION); \
|
|
|
|
for f in $(SRC) $(STATICFILES); do \
|
|
|
|
install -m 644 -D $$f $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)/$$f; \
|
|
|
|
done; \
|
|
|
|
( \
|
|
|
|
echo "VERSION = ${BINARY_VERSION}"; \
|
|
|
|
echo "GIT_COMMIT = ${GIT_COMMIT}"; \
|
|
|
|
echo "GIT_STATUS = ${GIT_STATUS}"; \
|
|
|
|
cat Makefile; \
|
|
|
|
) > $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)/Makefile; \
|
|
|
|
cd $(DISTDIR); \
|
|
|
|
tar cJf $(BINNAME)-$(BINARY_VERSION).tar.xz $(BINNAME)-$(BINARY_VERSION)
|
|
|
|
|
2024-07-19 15:10:50 +02:00
|
|
|
.PHONY: dist-target
|
|
|
|
dist-target: build-target
|
2024-07-19 17:48:13 +02:00
|
|
|
@set -eu -o pipefail; \
|
2024-07-19 15:10:50 +02:00
|
|
|
if [ -z "$(BINARY_VERSION)" ]; then \
|
|
|
|
echo "No version set"; \
|
|
|
|
exit 1; \
|
|
|
|
fi; \
|
2024-07-19 15:36:50 +02:00
|
|
|
rm -rf $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH); \
|
2024-07-19 15:10:50 +02:00
|
|
|
mkdir -p $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH); \
|
|
|
|
cp $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME) $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH); \
|
|
|
|
for f in $(STATICFILES); do \
|
|
|
|
cp $$f $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH); \
|
|
|
|
done; \
|
|
|
|
cd $(DISTDIR); \
|
|
|
|
tar cJf $(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH).tar.xz $(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH)
|
|
|
|
|
|
|
|
# --------
|
|
|
|
# Cleanup
|
|
|
|
|
2024-07-19 14:29:51 +02:00
|
|
|
.PHONY: clean
|
|
|
|
clean:
|
2024-07-19 15:10:50 +02:00
|
|
|
@rm -rf $(BINDIR) $(DISTDIR)
|