Skip to content

Commit 26067d9

Browse files
committed
feat: updated common.mk to include setup and self-update
1 parent 3111815 commit 26067d9

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

common.mk

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
SHELL = /usr/bin/env bash -o pipefail
44
.SHELLFLAGS = -ec
55

6+
# Capture the path to this file before any other includes can shift MAKEFILE_LIST.
7+
_COMMON_MK_PATH := $(lastword $(MAKEFILE_LIST))
8+
69
# Set MAKEFLAGS to suppress entering/leaving directory messages
710
MAKEFLAGS += --no-print-directory
811

@@ -214,3 +217,60 @@ $(LOCALGOBIN)/ocm: $(LOCALGOBIN) $(TOOL_LOCK)
214217
version=$$(cut -d@ -f2 <<< $$module); \
215218
test -s $@ && grep -q "$$version" $(LOCALGOBIN)/.ocm-version 2>/dev/null || \
216219
curl -s https://ocm.software/install.sh | VERSION_OCM=$$version bash -s -- $(LOCALGOBIN) && echo $$version > $(LOCALGOBIN)/.ocm-version
220+
221+
# Rewrites the common.mk: rule in the calling project's Makefile to the current bootstrap format.
222+
# Run once per repository to migrate from any older recipe shape.
223+
.PHONY: update-common-mk-bootstrap
224+
update-common-mk-bootstrap: ## Rewrite the common.mk: rule in Makefile to the current bootstrap format
225+
@tmp=Makefile.tmp; \
226+
awk ' \
227+
BEGIN { found = 0 } \
228+
/^common\.mk:$$/ { \
229+
found = 1; \
230+
print; \
231+
print "\t@[ -f .common.mk-download ] || \\"; \
232+
print "\t\tcurl --fail -sSL https://raw.githubusercontent.com/opendefensecloud/dev-kit/$$(DEV_KIT_VERSION)/common.mk \\"; \
233+
print "\t\t -o .common.mk-download"; \
234+
print "\tmv .common.mk-download $$@"; \
235+
print "\tprintf \047%s\047 \047$$(DEV_KIT_VERSION)\047 > .common.mk-version"; \
236+
print "\ttouch .common.mk-checked"; \
237+
skip = 1; next \
238+
} \
239+
skip && /^\t/ { next } \
240+
{ skip = 0; print } \
241+
END { exit(found ? 0 : 1) } \
242+
' Makefile > "$$tmp" && mv "$$tmp" Makefile || { rm -f "$$tmp"; echo "error: common.mk: rule not found in Makefile" >&2; exit 1; }
243+
@echo "Updated common.mk: bootstrap in Makefile"
244+
245+
# ── Self-update ────────────────────────────────────────────────────────────────
246+
# DEV_KIT_VERSION must be set by the including Makefile before -include common.mk.
247+
# This fallback is for environments where common.mk is used standalone.
248+
DEV_KIT_VERSION ?= main
249+
250+
# Performs a content-based staleness check at most once per hour.
251+
# If the remote content differs from the local file, deletes this file so that
252+
# Make's include-file-remake mechanism triggers the project's common.mk: rule on
253+
# its next restart — picking up the pre-downloaded .common.mk-download file.
254+
_COMMON_MK_SELF_UPDATE := $(shell \
255+
stored=$$(cat .common.mk-version 2>/dev/null); \
256+
if [ "$$stored" != "$(DEV_KIT_VERSION)" ]; then \
257+
rm -f .common.mk-checked .common.mk-download; \
258+
elif find .common.mk-checked -mmin -60 2>/dev/null | grep -q .; then \
259+
exit 0; \
260+
fi; \
261+
if curl --fail -sSL \
262+
'https://raw.githubusercontent.com/opendefensecloud/dev-kit/$(DEV_KIT_VERSION)/common.mk' \
263+
-o .common.mk-download 2>/dev/null; then \
264+
remote=$$(sha256sum .common.mk-download | cut -d' ' -f1); \
265+
local_hash=$$(sha256sum '$(_COMMON_MK_PATH)' 2>/dev/null | cut -d' ' -f1); \
266+
printf '%s' '$(DEV_KIT_VERSION)' > .common.mk-version; \
267+
if [ "$$remote" = "$$local_hash" ]; then \
268+
rm -f .common.mk-download; \
269+
touch .common.mk-checked; \
270+
else \
271+
rm -f '$(_COMMON_MK_PATH)'; \
272+
fi; \
273+
else \
274+
echo >&2 'warning: could not fetch common.mk update, using cached version'; \
275+
touch .common.mk-checked; \
276+
fi)

0 commit comments

Comments
 (0)