use make’s dependency handling to speed-up rebuilds

make allows us to run build targets based on dependencies and will only
execute targets if the dependencies have changed. This drastically
speeds up rebuilds if some targets have already been executed and
are still up to date.
This commit is contained in:
Konrad Mohrfeldt 2021-06-26 20:25:02 +02:00
parent 07478a588c
commit 89bfe74f50

View file

@ -3,6 +3,16 @@ VERSION := $(shell git describe --tags --abbrev=0)
BUILDSTR := ${VERSION} (\#${LAST_COMMIT} $(shell date -u +"%Y-%m-%dT%H:%M:%S%z"))
YARN ?= yarn
GOPATH ?= $(HOME)/go
STUFFBIN ?= $(GOPATH)/bin/stuffbin
FRONTEND_YARN_MODULES = frontend/node_modules
FRONTEND_DIST = frontend/dist
FRONTEND_DEPS = \
$(FRONTEND_YARN_MODULES) \
frontend/package.json \
frontend/vue.config.js \
frontend/babel.config.js \
$(shell find frontend/fontello frontend/public frontend/src -type f)
BIN := listmonk
STATIC := config.toml.sample \
@ -13,26 +23,32 @@ STATIC := config.toml.sample \
frontend/dist/frontend:/frontend \
i18n:/i18n
# Install dependencies for building.
.PHONY: deps
deps:
.PHONY: build
build: $(BIN)
$(STUFFBIN):
go get -u github.com/knadh/stuffbin/...
$(FRONTEND_YARN_MODULES): frontend/package.json frontend/yarn.lock
cd frontend && $(YARN) install
touch --no-create $(FRONTEND_YARN_MODULES)
# Build the backend to ./listmonk.
.PHONY: build
build:
$(BIN): $(shell find cmd -type f -name "*.go")
CGO_ENABLED=0 go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" cmd/*.go
# Run the backend.
.PHONY: run
run: build
run: $(BIN)
./${BIN}
# Build the JS frontend into frontend/dist.
.PHONY: build-frontend
build-frontend:
$(FRONTEND_DIST): $(FRONTEND_DEPS)
export VUE_APP_VERSION="${VERSION}" && cd frontend && $(YARN) build
touch --no-create $(FRONTEND_DIST)
.PHONY: build-frontend
build-frontend: $(FRONTEND_DIST)
# Run the JS frontend server in dev mode.
.PHONY: run-frontend
@ -47,14 +63,14 @@ test:
# Bundle all static assets including the JS frontend into the ./listmonk binary
# using stuffbin (installed with make deps).
.PHONY: dist
dist: build build-frontend
stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC}
dist: $(STUFFBIN) build build-frontend
$(STUFFBIN) -a stuff -in ${BIN} -out ${BIN} ${STATIC}
# pack-releases runns stuffbin packing on the given binary. This is used
# in the .goreleaser post-build hook.
.PHONY: pack-bin
pack-bin:
stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC}
pack-bin: $(STUFFBIN)
$(STUFFBIN) -a stuff -in ${BIN} -out ${BIN} ${STATIC}
# Use goreleaser to do a dry run producing local builds.
.PHONY: release-dry