git: 10afc5eada47 - main - Makefile.libcompat: Handle MK_FOO varying for native and compat arches

From: Jessica Clarke <jrtc27_at_FreeBSD.org>
Date: Tue, 27 Jun 2023 23:11:53 UTC
The branch main has been updated by jrtc27:

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

commit 10afc5eada472f7903238c7021a00f722f49a789
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2023-06-27 23:10:30 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2023-06-27 23:10:30 +0000

    Makefile.libcompat: Handle MK_FOO varying for native and compat arches
    
    Currently Makefile.libcompat queries a few MK_FOO variables to determine
    what is being built. However, it is plausible (and indeed, downstream
    in CheriBSD, this is the case) that these may vary between the native
    and the compat architecture. In order to correctly determine their
    values for the compat architecture, we need to defer their evaluation
    until we are in the compat sub-make where src.opts.mk will give us the
    right value for the compat MACHINE_ARCH.
    
    Reviewed by:    brooks, jhb, imp
    Obtained from:  CheriBSD
    Differential Revision:  https://reviews.freebsd.org/D40570
---
 Makefile.libcompat | 56 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 33 insertions(+), 23 deletions(-)

diff --git a/Makefile.libcompat b/Makefile.libcompat
index 05e77a980b6a..11668bf54eda 100644
--- a/Makefile.libcompat
+++ b/Makefile.libcompat
@@ -39,20 +39,6 @@ LIBCOMPATIMAKE+=	${LIBCOMPATWMAKE:NINSTALL=*:NDESTDIR=*} \
 			MK_INCLUDES=no \
 			-DLIBRARIES_ONLY
 
-_LC_LIBDIRS.yes=		lib
-_LC_LIBDIRS.yes+=		gnu/lib
-_LC_LIBDIRS.${MK_CDDL:tl}+=	cddl/lib
-_LC_LIBDIRS.${MK_CRYPT:tl}+=	secure/lib
-_LC_LIBDIRS.${MK_KERBEROS:tl}+=	kerberos5/lib
-
-.if ${MK_FILE} != "no"
-_libmagic=	lib/libmagic
-.endif
-.if ${MK_PMC} != "no"
-_jevents=	lib/libpmc/pmu-events
-.endif
-
-
 # Shared logic
 build${libcompat}: .PHONY
 	@echo
@@ -65,16 +51,14 @@ build${libcompat}: .PHONY
 	${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
 	    -p ${WORLDTMP}/usr/lib/debug/usr >/dev/null
 .endif
-.for _dir in lib/ncurses/tinfo ${_libmagic} ${_jevents}
-.for _t in ${_obj} build-tools
-	${_+_}cd ${.CURDIR}/${_dir}; \
+	${_+_}cd ${.CURDIR}; \
 	    WORLDTMP=${WORLDTMP} \
 	    MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}" \
-	    ${MAKE} DESTDIR= \
+	    ${MAKE} -f Makefile.inc1 \
+	    DESTDIR= \
 	    OBJTOP=${LIBCOMPAT_OBJTOP} \
 	    OBJROOT='$${OBJTOP}/' \
 	    MAKEOBJDIRPREFIX= \
-	    DIRPRFX=${_dir}/ \
 	    -DNO_LINT \
 	    -DNO_CPU_CFLAGS \
 	    MK_ASAN=no \
@@ -83,15 +67,41 @@ build${libcompat}: .PHONY
 	    MK_SSP=no \
 	    MK_UBSAN=no \
 	    MK_WERROR=no \
-	    ${_t}
-.endfor
-.endfor
+	    _lc_build-tools
 	${_+_}cd ${.CURDIR}; \
 	    ${LIBCOMPATWMAKE} -f Makefile.inc1 -DNO_FSCHG libraries
 
 distribute${libcompat} install${libcompat}: .PHONY
+	${_+_}cd ${.CURDIR}; \
+	    ${LIBCOMPATIMAKE} -f Makefile.inc1 _lc_${.TARGET:S/${libcompat}$//}
+
+# These depend on MK_*, which could vary between the native and compat
+# architectures. Thus targets using them are run within a compat sub-make
+# rather than the native make environment.
+
+_LC_LIBDIRS.yes=		lib
+_LC_LIBDIRS.yes+=		gnu/lib
+_LC_LIBDIRS.${MK_CDDL:tl}+=	cddl/lib
+_LC_LIBDIRS.${MK_CRYPT:tl}+=	secure/lib
+_LC_LIBDIRS.${MK_KERBEROS:tl}+=	kerberos5/lib
+
+.if ${MK_FILE} != "no"
+_libmagic=	lib/libmagic
+.endif
+.if ${MK_PMC} != "no"
+_jevents=	lib/libpmc/pmu-events
+.endif
+
+_lc_build-tools:
+.for _dir in lib/ncurses/tinfo ${_libmagic} ${_jevents}
+.for _t in ${_obj} build-tools
+	${_+_}cd ${.CURDIR}/${_dir}; ${MAKE} DIRPRFX=${_dir}/ ${_t}
+.endfor
+.endfor
+
+_lc_distribute _lc_install:
 .for _dir in ${_LC_LIBDIRS.yes}
-	${_+_}cd ${.CURDIR}/${_dir}; ${LIBCOMPATIMAKE} ${.TARGET:S/${libcompat}$//}
+	${_+_}cd ${.CURDIR}/${_dir}; ${MAKE} ${.TARGET:S/^_lc_//}
 .endfor
 
 .endif # !targets(__<${_this:T}>__)