Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 30 Sep 2025 21:14:39 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 8545586793f2 - stable/14 - share/mk: Deduplicate some handling of debug info
Message-ID:  <202509302114.58ULEdeo010317@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by markj:

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

commit 8545586793f22fd7f67f8c46ec6fa12b766d0446
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-08-21 14:06:19 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-09-30 21:14:29 +0000

    share/mk: Deduplicate some handling of debug info
    
    bsd.prog.mk and bsd.lib.mk contain a bunch of duplicated logic used to
    handle DEBUG_FLAGS and standalone debug file info (enabled by
    MK_DEBUG_FILES).  In anticipation of adding more duplicated logic, let's
    try factoring it out into a separate bsd.debug.mk first.
    
    bsd.debug.mk now handles the following:
    - MK_ASSERT_DEBUG
    - installation rules for debug files (the consumer has to set DEBUGFILE)
    - updating CFLAGS and CXXFLAGS based on DEBUG_FLAGS
    - optionally stripping installed files
    
    No functional change intended.
    
    Reviewed by:    bdrewery
    Sponsored by:   The FreeBSD Foundation
    Sponsored by:   Klara, Inc.
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D51805
    
    (cherry picked from commit 3a239e46c4aa1dc62a74b40f6a0feb030f089a0c)
---
 share/mk/Makefile     |  1 +
 share/mk/bsd.README   |  1 +
 share/mk/bsd.debug.mk | 46 ++++++++++++++++++++++++++++++++++++++++++
 share/mk/bsd.lib.mk   | 51 ++++++++---------------------------------------
 share/mk/bsd.own.mk   |  7 ++++++-
 share/mk/bsd.prog.mk  | 55 ++++++++++-----------------------------------------
 6 files changed, 72 insertions(+), 89 deletions(-)

diff --git a/share/mk/Makefile b/share/mk/Makefile
index 21d512c74c80..e3d2410fb34f 100644
--- a/share/mk/Makefile
+++ b/share/mk/Makefile
@@ -23,6 +23,7 @@ FILES=	\
 	bsd.confs.mk \
 	bsd.cpu.mk \
 	bsd.crunchgen.mk \
+	bsd.debug.mk \
 	bsd.dep.mk \
 	bsd.dirs.mk \
 	bsd.doc.mk \
diff --git a/share/mk/bsd.README b/share/mk/bsd.README
index b68d7b66402d..d04012666fba 100644
--- a/share/mk/bsd.README
+++ b/share/mk/bsd.README
@@ -21,6 +21,7 @@ bsd.compiler.mk		- defined based on current compiler
 bsd.confs.mk		- install of configuration files
 bsd.cpu.mk		- sets CPU/arch-related variables (included from sys.mk)
 bsd.crunchgen.mk	- building crunched binaries using crunchgen(1)
+bsd.debug.mk		- handling debug options for bsd.{prog,lib}.mk
 bsd.dep.mk		- handle Makefile dependencies
 bsd.dirs.mk		- handle directory creation
 bsd.doc.mk		- building troff system documents
diff --git a/share/mk/bsd.debug.mk b/share/mk/bsd.debug.mk
new file mode 100644
index 000000000000..fc80bc103061
--- /dev/null
+++ b/share/mk/bsd.debug.mk
@@ -0,0 +1,46 @@
+#
+# This file configures debug options for compiled targets.  It is meant
+# to consolidate common logic in bsd.prog.mk and bsd.lib.mk.  It should
+# not be included directly by Makefiles.
+#
+
+.include <bsd.opts.mk>
+
+.if ${MK_ASSERT_DEBUG} == "no"
+CFLAGS+= -DNDEBUG
+# XXX: shouldn't we ensure that !asserts marks potentially unused variables as
+# __unused instead of disabling -Werror globally?
+MK_WERROR=	no
+.endif
+
+.if defined(DEBUG_FLAGS)
+CFLAGS+=${DEBUG_FLAGS}
+CXXFLAGS+=${DEBUG_FLAGS}
+
+.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
+CTFFLAGS+= -g
+.endif
+.else
+STRIP?= -s
+.endif
+
+.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
+    empty(DEBUG_FLAGS:M-gdwarf*)
+.if !${COMPILER_FEATURES:Mcompressed-debug}
+CFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
+CXXFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
+.else
+CFLAGS+= ${DEBUG_FILES_CFLAGS}
+CXXFLAGS+= ${DEBUG_FILES_CFLAGS}
+.endif
+CTFFLAGS+= -g
+.endif
+
+_debuginstall:
+.if ${MK_DEBUG_FILES} != "no" && defined(DEBUGFILE)
+.if defined(DEBUGMKDIR)
+	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -d ${DESTDIR}${DEBUGFILEDIR}/
+.endif
+	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -o ${DEBUGOWN} -g ${DEBUGGRP} -m ${DEBUGMODE} \
+	    ${DEBUGFILE} ${DESTDIR}${DEBUGFILEDIR}/${DEBUGFILE}
+.endif
diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk
index d6a7786e17fd..f2ed34b11edc 100644
--- a/share/mk/bsd.lib.mk
+++ b/share/mk/bsd.lib.mk
@@ -47,23 +47,6 @@ SONAME?=	${SHLIB_NAME}
 CFLAGS+=	${CRUNCH_CFLAGS}
 .endif
 
-.if ${MK_ASSERT_DEBUG} == "no"
-CFLAGS+= -DNDEBUG
-# XXX: shouldn't we ensure that !asserts marks potentially unused variables as
-# __unused instead of disabling -Werror globally?
-MK_WERROR=	no
-.endif
-
-.if defined(DEBUG_FLAGS)
-CFLAGS+= ${DEBUG_FLAGS}
-
-.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
-CTFFLAGS+= -g
-.endif
-.else
-STRIP?=	-s
-.endif
-
 .for _libcompat in ${_ALL_libcompats}
 .if ${SHLIBDIR:M*/lib${_libcompat}} || ${SHLIBDIR:M*/lib${_libcompat}/*}
 TAGS+=	lib${_libcompat}
@@ -124,18 +107,6 @@ CXXFLAGS+= -ftrivial-auto-var-init=pattern
 # bsd.sanitizer.mk is not installed, so don't require it (e.g. for ports).
 .sinclude "bsd.sanitizer.mk"
 
-.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
-    empty(DEBUG_FLAGS:M-gdwarf*)
-.if !${COMPILER_FEATURES:Mcompressed-debug}
-CFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
-CXXFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
-.else
-CFLAGS+= ${DEBUG_FILES_CFLAGS}
-CXXFLAGS+= ${DEBUG_FILES_CFLAGS}
-.endif
-CTFFLAGS+= -g
-.endif
-
 .if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == ""
 CFLAGS += -mno-relax
 .endif
@@ -150,6 +121,7 @@ _SHLIBDIR:=${SHLIBDIR}
 .if defined(SHLIB_NAME)
 .if ${MK_DEBUG_FILES} != "no"
 SHLIB_NAME_FULL=${SHLIB_NAME}.full
+DEBUGFILE= ${SHLIB_NAME}.debug
 # Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
 .if ${_SHLIBDIR} == "/boot" ||\
     ${SHLIBDIR:C%/lib(/.*)?$%/lib%} == "/lib" ||\
@@ -276,16 +248,16 @@ ${SHLIB_NAME_FULL}: ${SOBJS}
 .endif
 
 .if ${MK_DEBUG_FILES} != "no"
-CLEANFILES+=	${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
-${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${SHLIB_NAME}.debug
-	${OBJCOPY} --strip-debug --add-gnu-debuglink=${SHLIB_NAME}.debug \
+CLEANFILES+=	${SHLIB_NAME_FULL} ${DEBUGFILE}
+${SHLIB_NAME}: ${SHLIB_NAME_FULL} ${DEBUGFILE}
+	${OBJCOPY} --strip-debug --add-gnu-debuglink=${DEBUGFILE} \
 	    ${SHLIB_NAME_FULL} ${.TARGET}
 .if defined(SHLIB_LINK) && !commands(${SHLIB_LINK:R}.ld)
 	# Note: This uses ln instead of ${INSTALL_LIBSYMLINK} since we are in OBJDIR
 	@${LN:Uln} -fs ${SHLIB_NAME} ${SHLIB_LINK}
 .endif
 
-${SHLIB_NAME}.debug: ${SHLIB_NAME_FULL}
+${DEBUGFILE}: ${SHLIB_NAME_FULL}
 	${OBJCOPY} --only-keep-debug ${SHLIB_NAME_FULL} ${.TARGET}
 .endif
 .endif #defined(SHLIB_NAME)
@@ -396,8 +368,8 @@ installpcfiles-${pcfile}: ${pcfile}
 installpcfiles: .PHONY
 
 .if !defined(INTERNALLIB)
-realinstall: _libinstall installpcfiles
-.ORDER: beforeinstall _libinstall
+realinstall: _libinstall installpcfiles _debuginstall
+.ORDER: beforeinstall _libinstall _debuginstall
 _libinstall:
 .if defined(LIB) && !empty(LIB) && ${MK_INSTALLLIB} != "no"
 	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dev} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
@@ -411,14 +383,6 @@ _libinstall:
 	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
 	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
 	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}/
-.if ${MK_DEBUG_FILES} != "no"
-.if defined(DEBUGMKDIR)
-	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -d ${DESTDIR}${DEBUGFILEDIR}/
-.endif
-	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -o ${LIBOWN} -g ${LIBGRP} -m ${DEBUGMODE} \
-	    ${_INSTALLFLAGS} \
-	    ${SHLIB_NAME}.debug ${DESTDIR}${DEBUGFILEDIR}/
-.endif
 .if defined(SHLIB_LINK)
 .if commands(${SHLIB_LINK:R}.ld)
 	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dev} -S -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
@@ -512,6 +476,7 @@ SUBDIR_TARGETS+=	check
 TESTS_LD_LIBRARY_PATH+=	${.OBJDIR}
 .endif
 
+.include <bsd.debug.mk>
 .include <bsd.dep.mk>
 .include <bsd.clang-analyze.mk>
 .include <bsd.obj.mk>
diff --git a/share/mk/bsd.own.mk b/share/mk/bsd.own.mk
index aec88d64850f..ad6e07bfb7ba 100644
--- a/share/mk/bsd.own.mk
+++ b/share/mk/bsd.own.mk
@@ -44,6 +44,10 @@
 #
 # DEBUGMODE	Mode for debug files. [${NOBINMODE}]
 #
+# DEBUGOWN      Owner for debug info files. [root]
+#
+# DEBUGGRP      Group for debug info files. [wheel]
+#
 #
 # KMODDIR	Base path for loadable kernel modules
 #		(see kld(4)). [/boot/modules]
@@ -199,7 +203,8 @@ LIBMODE?=	${NOBINMODE}
 
 DEBUGDIR?=	/usr/lib/debug
 DEBUGMODE?=	${NOBINMODE}
-
+DEBUGOWN?=	${BINOWN}
+DEBUGGRP?=	${BINGRP}
 
 # Share files
 SHAREDIR?=	/usr/share
diff --git a/share/mk/bsd.prog.mk b/share/mk/bsd.prog.mk
index 0d3bac406e23..fa15370a82c6 100644
--- a/share/mk/bsd.prog.mk
+++ b/share/mk/bsd.prog.mk
@@ -12,22 +12,6 @@
 CFLAGS+=${COPTS}
 .endif
 
-.if ${MK_ASSERT_DEBUG} == "no"
-CFLAGS+= -DNDEBUG
-# XXX: shouldn't we ensure that !asserts marks potentially unused variables as
-# __unused instead of disabling -Werror globally?
-MK_WERROR=	no
-.endif
-
-.if defined(DEBUG_FLAGS)
-CFLAGS+=${DEBUG_FLAGS}
-CXXFLAGS+=${DEBUG_FLAGS}
-
-.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
-CTFFLAGS+= -g
-.endif
-.endif
-
 .if defined(PROG_CXX)
 PROG=	${PROG_CXX}
 .endif
@@ -101,20 +85,6 @@ CFLAGS += -mno-relax
 
 .if defined(CRUNCH_CFLAGS)
 CFLAGS+=${CRUNCH_CFLAGS}
-.else
-.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
-    empty(DEBUG_FLAGS:M-gdwarf-*)
-.if !${COMPILER_FEATURES:Mcompressed-debug}
-CFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
-.else
-CFLAGS+= ${DEBUG_FILES_CFLAGS}
-.endif
-CTFFLAGS+= -g
-.endif
-.endif
-
-.if !defined(DEBUG_FLAGS)
-STRIP?=	-s
 .endif
 
 .if defined(NO_ROOT)
@@ -151,6 +121,9 @@ PROG_FULL=	${PROG}
 
 .if defined(PROG)
 PROGNAME?=	${PROG}
+.if ${MK_DEBUG_FILES} != "no"
+DEBUGFILE= ${PROGNAME}.debug
+.endif
 
 .if defined(SRCS)
 
@@ -215,11 +188,12 @@ ${PROG_FULL}: ${OBJS}
 .endif # !defined(SRCS)
 
 .if ${MK_DEBUG_FILES} != "no"
-${PROG}: ${PROG_FULL} ${PROGNAME}.debug
-	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \
+CLEANFILES+= ${PROG_FULL} ${DEBUGFILE}
+${PROG}: ${PROG_FULL} ${DEBUGFILE}
+	${OBJCOPY} --strip-debug --add-gnu-debuglink=${DEBUGFILE} \
 	    ${PROG_FULL} ${.TARGET}
 
-${PROGNAME}.debug: ${PROG_FULL}
+${DEBUGFILE}: ${PROG_FULL}
 	${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET}
 .endif
 
@@ -258,9 +232,6 @@ all: all-man
 
 .if defined(PROG)
 CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll
-.if ${MK_DEBUG_FILES} != "no"
-CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug
-.endif
 .endif
 
 .if defined(OBJS)
@@ -300,19 +271,12 @@ _INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
 .endfor
 
 .if !target(realinstall) && !defined(INTERNALPROG)
-realinstall: _proginstall
-.ORDER: beforeinstall _proginstall
+realinstall: _proginstall _debuginstall
+.ORDER: beforeinstall _proginstall _debuginstall
 _proginstall:
 .if defined(PROG)
 	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
 	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
-.if ${MK_DEBUG_FILES} != "no"
-.if defined(DEBUGMKDIR)
-	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -d ${DESTDIR}${DEBUGFILEDIR}/
-.endif
-	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
-	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
-.endif
 .endif
 .endif	# !target(realinstall)
 
@@ -383,6 +347,7 @@ TESTS_PATH+=		${.OBJDIR}
 OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
 .endif
 
+.include <bsd.debug.mk>
 .include <bsd.dep.mk>
 .include <bsd.clang-analyze.mk>
 .include <bsd.obj.mk>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202509302114.58ULEdeo010317>