From 331e7c13fa73fd1711b6629d7b2c6d5b17b30e1e Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Mon, 1 Dec 2025 14:18:10 +0000 Subject: [PATCH] Makefile: share $(DATA) on internal/external, keep record of build flags This means you could make precompile, then make INTERNAL=on then make INTERNAL=off and each time it would re-generate the go files only since the flags have changed. --- Makefile | 20 ++++++++++++++++---- internal.go | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a2c4b1a..8d05fa1 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ E2EE ?= on TAGS := -tags " ifeq ($(INTERNAL), on) - DATA := data + DATA := build/data COMPDEPS := $(BUILDDEPS) else DATA := build/data @@ -102,6 +102,13 @@ else SWAGINSTALL := endif +# FLAG HASHING: To rebuild on flag change. +# credit for idea to https://bnikolic.co.uk/blog/sh/make/unix/2021/07/08/makefile.html +rebuildFlags := GOESBUILD GOBINARY VERSION COMMIT UPDATER INTERNAL TRAY E2EE TAGS DEBUG RACE +rebuildVals := $(foreach v,$(rebuildFlags),$(v)=$($(v))) +rebuildHash := $(strip $(shell echo $(rebuildVals) | sha256sum | cut -d " " -f1)) +rebuildHashFile := $(DATA)/buildhash-$(rebuildHash).txt + CONFIG_BASE = config/config-base.yaml # CONFIG_DESCRIPTION = $(DATA)/config-base.json @@ -219,11 +226,16 @@ $(COPY_TARGET): $(INLINE_TARGET) $(STATIC_SRC) $(LANG_SRC) $(CONFIG_BASE) BUILDDEPS := $(DATA) $(CONFIG_DEFAULT) $(EMAIL_TARGET) $(COPY_TARGET) $(SWAGGER_TARGET) $(INLINE_TARGET) $(CSS_FULLTARGET) $(TYPESCRIPT_TARGET) precompile: $(BUILDDEPS) -COMPDEPS = +COMPDEPS = $(rebuildHashFile) ifeq ($(INTERNAL), on) - COMPDEPS = $(BUILDDEPS) + COMPDEPS = $(BUILDDEPS) $(rebuildHashFile) endif +$(rebuildHashFile): + $(info recording new flags $(rebuildVals)) + rm -f $(DATA)/buildhash-*.txt + touch $(rebuildHashFile) + GO_SRC = $(shell find ./ -name "*.go") GO_TARGET = build/jfa-go $(GO_TARGET): $(COMPDEPS) $(SWAGGER_TARGET) $(GO_SRC) go.mod go.sum @@ -236,7 +248,7 @@ $(GO_TARGET): $(COMPDEPS) $(SWAGGER_TARGET) $(GO_SRC) go.mod go.sum test: $(BUILDDEPS) $(COMPDEPS) $(SWAGGER_TARGET) $(GO_SRC) go.mod go.sum $(GOBINARY) test -ldflags="$(LDFLAGS)" $(TAGS) -p 1 -all: $(BUILDDEPS) $(GO_TARGET) +all: $(BUILDDEPS) $(GO_TARGET) $(rebuildHashFile) compress: upx --lzma $(GO_TARGET) diff --git a/internal.go b/internal.go index 0e521c5..0d69865 100644 --- a/internal.go +++ b/internal.go @@ -14,7 +14,7 @@ const binaryType = "internal" func BuildTagsExternal() {} -//go:embed data data/html data/web data/web/css data/web/js +//go:embed build/data build/data/html build/data/web build/data/web/css build/data/web/js var loFS embed.FS //go:embed lang/common lang/admin lang/email lang/form lang/setup lang/pwreset lang/telegram @@ -38,6 +38,6 @@ func FSJoin(elem ...string) string { func loadFilesystems(rootDir string, logger *logger.Logger) { langFS = rewriteFS{laFS, "lang/"} - localFS = rewriteFS{loFS, "data/"} + localFS = rewriteFS{loFS, "build/data/"} logger.Println("Using internal storage") }