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
This commit is contained in:
Mark Johnston 2025-08-21 14:06:19 +00:00
parent 027be99b1f
commit 3a239e46c4
6 changed files with 72 additions and 90 deletions

View file

@ -22,6 +22,7 @@ FILES= \
bsd.confs.mk \
bsd.cpu.mk \
bsd.crunchgen.mk \
bsd.debug.mk \
bsd.dep.mk \
bsd.dirs.mk \
bsd.doc.mk \

View file

@ -20,6 +20,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

46
share/mk/bsd.debug.mk Normal file
View file

@ -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

View file

@ -1,4 +1,3 @@
.include <bsd.init.mk>
.include <bsd.compiler.mk>
.include <bsd.linker.mk>
@ -45,23 +44,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}
@ -130,18 +112,6 @@ CXXFLAGS+= -fzero-call-used-regs=${ZEROREG_TYPE}
# 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
@ -156,6 +126,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" ||\
@ -272,16 +243,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)
@ -392,8 +363,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} \
@ -403,14 +374,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} \
@ -501,6 +464,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>

View file

@ -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]
@ -197,7 +201,8 @@ LIBMODE?= ${NOBINMODE}
DEBUGDIR?= /usr/lib/debug
DEBUGMODE?= ${NOBINMODE}
DEBUGOWN?= ${BINOWN}
DEBUGGRP?= ${BINGRP}
# Share files
SHAREDIR?= /usr/share

View file

@ -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
@ -109,20 +93,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)
@ -159,6 +129,9 @@ PROG_FULL= ${PROG}
.if defined(PROG)
PROGNAME?= ${PROG}
.if ${MK_DEBUG_FILES} != "no"
DEBUGFILE= ${PROGNAME}.debug
.endif
.if defined(SRCS)
@ -223,11 +196,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
@ -266,9 +240,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)
@ -308,19 +279,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)
@ -391,6 +355,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>