chore: add Makefile

This commit is contained in:
Emmanuel BENOîT 2024-07-26 13:27:49 +02:00
parent cf1e599be8
commit 0ddc65b43f
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
2 changed files with 131 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
bin bin
dist
go.sum go.sum
fetchcert fetchcert
fetch-certificates.yml fetch-certificates.yml

130
Makefile Normal file
View file

@ -0,0 +1,130 @@
BINNAME = fetchcert
BINDIR = $(CURDIR)/bin
DISTDIR = $(CURDIR)/dist
TARGETS = linux/amd64 linux/386 linux/arm linux/arm64
STATICFILES = README.md COPYING fetch-certificates.example.yml
SRC := $(shell find . -type f -name '*.go' -not -path './dist/*' -print) go.mod go.sum
GOHOSTOS = $(shell go env GOHOSTOS)
GOHOSTARCH ?= $(shell go env GOHOSTARCH)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
GIT_STATUS ?= $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
ifneq ($(VERSION),)
BINARY_VERSION = $(VERSION)
endif
BINARY_VERSION ?= $(GIT_TAG)
LDFLAGS += -w -s
ifneq ($(BINARY_VERSION),)
LDFLAGS += -X main.version=${BINARY_VERSION}
endif
LDFLAGS += -X main.commit=${GIT_COMMIT}
LDFLAGS += -X main.status=${GIT_STATUS}
# Use bash as the shell, mostly for -o pipefail
SHELL = /usr/bin/env bash
.PHONY: all
all: build
# ---------------
# Basic building
.PHONY: build
build: GOOS=$(GOHOSTOS)
build: GOARCH=$(GOHOSTARCH)
build: build-target symlink
.PHONY: symlink
symlink:
@ln -sf $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME) $(BINDIR)/$(BINNAME)
.PHONY: build-target
build-target: LDFLAGS += -X main.target=${GOOS}/${GOARCH}
build-target: $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME)
$(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME): $(SRC)
@mkdir -p $(BINDIR)/$(GOOS)-$(GOARCH)
@CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o $(BINDIR)/$(GOOS)-$(GOARCH)/$(BINNAME) .
.PHONY: test
test:
@go test -v ./...
.PHONY: lint
lint:
@go run -modfile=tools/go.mod github.com/golangci/golangci-lint/cmd/golangci-lint run
# ----------------
# Cross compiling
.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
# ----------
# Packaging
.PHONY: dist
dist: GOOS=$(GOHOSTOS)
dist: GOARCH=$(GOHOSTARCH)
dist: dist-target
.PHONY: dist-all
dist-all: dist-source
@set -eu -o pipefail; \
for target in $(TARGETS); do \
$(MAKE) dist-target GOOS=`echo $$target | cut -d / -f 1` GOARCH=`echo $$target | cut -d / -f 2`; \
done; \
mkdir -p $(DISTDIR)/release; \
mv $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)*.tar.xz $(DISTDIR)/release
.PHONY: dist-source
dist-source:
@set -eu -o pipefail; \
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)
.PHONY: dist-target
dist-target: build-target
@set -eu -o pipefail; \
if [ -z "$(BINARY_VERSION)" ]; then \
echo "No version set"; \
exit 1; \
fi; \
rm -rf $(DISTDIR)/$(BINNAME)-$(BINARY_VERSION)-$(GOOS)-$(GOARCH); \
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
.PHONY: clean
clean:
@rm -rf $(BINDIR) $(DISTDIR)