git: ef82bdc864c5 - stable/15 - Makefile: Don't allow install{world,kernel} with pkgbase

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Thu, 23 Oct 2025 00:37:49 UTC
The branch stable/15 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=ef82bdc864c5750c6c42a45696aa35c1a1cff10a

commit ef82bdc864c5750c6c42a45696aa35c1a1cff10a
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-10-17 19:06:17 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-10-23 00:36:50 +0000

    Makefile: Don't allow install{world,kernel} with pkgbase
    
    Using these targets on a pkgbase system will cause the installed system
    to become out of sync with the package database, which is almost certain
    to cause issues the next time pkg(8) is used.
    
    To prevent users doing this accidentally, disallow install* if we detect
    that FreeBSD-runtime is installed in the target.  The check can be
    overridden with DESTDIR=/ for users who are sure they want to do this.
    
    MFC after:      3 days
    Reviewed by:    emaste
    Sponsored by:   https://www.patreon.com/bsdivy
    Differential Revision:  https://reviews.freebsd.org/D52879
    
    (cherry picked from commit 74a6bb524e5b9ad654a9acc5da5c195f406717b5)
---
 Makefile.inc1 | 39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/Makefile.inc1 b/Makefile.inc1
index 21adf215c0aa..dcd4fd23c087 100644
--- a/Makefile.inc1
+++ b/Makefile.inc1
@@ -600,7 +600,16 @@ PKG_VERSION:=	${_PKG_REVISION}${EXTRA_REVISION:C/[[:space:]]//g}
 .endif
 .endif	# !defined(_MKSHOWCONFIG)
 
+PKG_NAME_PREFIX?=	FreeBSD
+PKG_MAINTAINER?=	re@FreeBSD.org
+PKG_WWW?=		https://www.FreeBSD.org
+PKG_WORKERS_COUNT?=	1
+
 .if make(*package*)
+.export PKG_NAME_PREFIX
+.export PKG_MAINTAINER
+.export PKG_WWW
+
 .if !defined(PKG_TIMESTAMP)
 .if !empty(GIT_CMD) && exists(${GIT_CMD}) && exists(${SRCDIR}/.git)
 SOURCE_DATE_EPOCH!=	${GIT_CMD} -C ${SRCDIR} show -s --format=%ct HEAD
@@ -611,14 +620,6 @@ SOURCE_DATE_EPOCH=	${TIMEEPOCHNOW:gmtime}
 .else
 SOURCE_DATE_EPOCH=	${PKG_TIMESTAMP}
 .endif
-PKG_WORKERS_COUNT?=	1
-
-PKG_NAME_PREFIX?=	FreeBSD
-PKG_MAINTAINER?=	re@FreeBSD.org
-PKG_WWW?=		https://www.FreeBSD.org
-.export PKG_NAME_PREFIX
-.export PKG_MAINTAINER
-.export PKG_WWW
 .endif
 
 .if !defined(_MKSHOWCONFIG)
@@ -1366,6 +1367,28 @@ __installcheck_DESTDIR: .PHONY
 .endif
 .endif
 
+#
+# Don't allow installworld or installkernel on a pkgbase system.  This avoids
+# accidentally updating a pkgbase system with install{world,kernel}, causing
+# the installed system to become out of date with the package database.
+#
+# Skip the check if DESTDIR is defined on the assumption the user knows what
+# they're doing.  This means the check can be disabled for the running system
+# using DESTDIR=/.
+#
+.if !make(distributeworld) && !defined(DESTDIR)
+_installcheck_world: __installcheck_PKG
+_installcheck_kernel: __installcheck_PKG
+__installcheck_PKG: .PHONY
+.if exists(${LOCALBASE}/sbin/pkg-static)
+	@if ${LOCALBASE}/sbin/pkg-static info -e ${PKG_NAME_PREFIX}-runtime; then \
+		echo >&2 "ERROR: This target should not be used on a system installed from packages." ; \
+		echo >&2 "       To override this check, set DESTDIR=/."; \
+		false; \
+	fi
+.endif
+.endif
+
 .if !defined(DB_FROM_SRC)
 #
 # Check for missing UIDs/GIDs.