git: f957a32dacfd - main - Update meta.sys.mk and related local*mk

From: Simon J. Gerraty <sjg_at_FreeBSD.org>
Date: Fri, 21 Apr 2023 03:39:42 UTC
The branch main has been updated by sjg:

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

commit f957a32dacfd0e18e574d0797e4bec4bec9f15af
Author:     Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2023-04-21 03:38:53 +0000
Commit:     Simon J. Gerraty <sjg@FreeBSD.org>
CommitDate: 2023-04-21 03:38:53 +0000

    Update meta.sys.mk and related local*mk
    
    Move the setting of TARGET_SPEC_VARS to local.sys.env.mk
    so meta.sys.mk can do the processing, and include local.meta.sys.mk
    later.
    
    Move the setting of GENDIRDEPS_FILTER*_VARS from local.gendirdeps.mk
    to local.meta.sys.mk so we can automatically set DEP_* at level 1+
    to avoid syntax errors when DEP_* variables are used in conditionals
    in Makefile.depend files.
    
    Update gendirdeps.mk just to get the documentation about the above.
    No functional change.
    
    local.dirdeps.mk be more careful about adding to DIRDEPS to avoid
    unnecessary overhead, and introducing cycles in the graph.
    Also set DEP_MACHINE_CPUARCH.
    
    Reviewed by:    stevek
    Sponsored by:   Juniper Networks, Inc.
    Differential Revision:  https://reviews.freebsd.org/D39737
---
 share/mk/gendirdeps.mk       | 35 +++++++++++++++++++++++++-
 share/mk/local.dirdeps.mk    |  3 ++-
 share/mk/local.gendirdeps.mk | 16 ++----------
 share/mk/local.meta.sys.mk   | 60 +++++++++++++++++++++-----------------------
 share/mk/local.sys.env.mk    |  5 ++++
 share/mk/meta.sys.mk         | 44 +++++++++++++++++++++++++++++---
 6 files changed, 112 insertions(+), 51 deletions(-)

diff --git a/share/mk/gendirdeps.mk b/share/mk/gendirdeps.mk
index 1ff2036237ed..ec4e188cdf78 100644
--- a/share/mk/gendirdeps.mk
+++ b/share/mk/gendirdeps.mk
@@ -1,5 +1,7 @@
-# $Id: gendirdeps.mk,v 1.48 2022/09/09 17:44:29 sjg Exp $
+# $Id: gendirdeps.mk,v 1.49 2023/04/20 17:45:03 sjg Exp $
 
+# SPDX-License-Identifier: BSD-2-Clause
+#
 # Copyright (c) 2011-2020, Simon J. Gerraty
 # Copyright (c) 2010-2018, Juniper Networks, Inc.
 # All rights reserved.
@@ -41,6 +43,37 @@
 #		symlink to another filesystem.
 #		_objroot must be a prefix match for _objtop
 
+# If any of GENDIRDEPS_FILTER, GENDIRDEPS_FILTER_DIR_VARS
+# or GENDIRDEPS_FILTER_VARS are set, we use them to filter the
+# output from filemon(4).
+# Any references to variables that dirdeps.mk will set
+# such as DEP_MACHINE, DEP_RELDIR etc, should use that form.
+# Thus we want ${DEP_MACHINE} not ${MACHINE} used in DIRDEPS.
+#
+# If any manually maintained Makefile.depend files will use any
+# DEP_* variables in conditionals, precautions are needed to avoid
+# errors when Makefile.depend is read at level 1+ (ie not via
+# dirdeps.mk)
+# Using MACHINE as an example; such makefiles can do:
+#
+# 	DEP_MACHINE ?= ${MACHINE}
+# 	.if ${DEP_MACHINE} == "xyz"
+#
+# or:
+# 
+# 	.if ${DEP_MACHINE:U${MACHINE}} == "xyz"
+#
+# but it might be safer to set GENDIRDEPS_FILTER_DIR_VARS and
+# GENDIRDEPS_FILTER_VARS via local.meta.sys.mk rather than
+# local.gendirdeps.mk and then:
+#
+# 	.if ${.MAKE.LEVEL} > 0
+# 	.for V in ${GENDIRDEPS_FILTER_DIR_VARS:MDEP_*} \
+# 		${GENDIRDEPS_FILTER_VARS:MDEP_*}
+# 	$V ?= ${${V:S,DEP_,,}}
+# 	.endfor
+# 	.endif
+# 
 .MAIN: all
 
 # keep this simple
diff --git a/share/mk/local.dirdeps.mk b/share/mk/local.dirdeps.mk
index d7e5396c81ed..246c9cdef114 100644
--- a/share/mk/local.dirdeps.mk
+++ b/share/mk/local.dirdeps.mk
@@ -207,12 +207,13 @@ DIRDEPS+= ${_lib${_lib}reldir}
 
 .if ${MK_STAGING} == "yes"
 # we need targets/pseudo/stage to prep the stage tree
-.if ${DEP_RELDIR} != "targets/pseudo/stage"
+.if ${DEP_RELDIR:N.:N${SRCTOP}:N*pseudo/stage} != ""
 DIRDEPS += targets/pseudo/stage
 .endif
 .endif
 
 DEP_MACHINE_ARCH = ${MACHINE_ARCH.${DEP_MACHINE}}
+DEP_MACHINE_CPUARCH = ${DEP_MACHINE_ARCH:${__TO_CPUARCH}}
 CSU_DIR.${DEP_MACHINE_ARCH} ?= csu/${DEP_MACHINE_ARCH}
 CSU_DIR := ${CSU_DIR.${DEP_MACHINE_ARCH}}
 BOOT_MACHINE_DIR:= ${BOOT_MACHINE_DIR.${DEP_MACHINE}}
diff --git a/share/mk/local.gendirdeps.mk b/share/mk/local.gendirdeps.mk
index c8aa3ce05910..c224c7c64bd2 100644
--- a/share/mk/local.gendirdeps.mk
+++ b/share/mk/local.gendirdeps.mk
@@ -53,20 +53,8 @@ GENDIRDEPS_FILTER+=	${GENDIRDEPS_FILTER_HOST_TOOLS}
 GENDIRDEPS_FILTER+= ${GENDIRDEPS_FILTER.${MACHINE}:U}
 
 # gendirdeps.mk will turn _{VAR} into ${VAR} which keeps this simple
-# order of this list matters!
-GENDIRDEPS_FILTER_DIR_VARS+= \
-       CSU_DIR \
-       BOOT_MACHINE_DIR
-
-# order of this list matters!
-GENDIRDEPS_FILTER_VARS+= \
-       KERNEL_NAME \
-       MACHINE_CPUARCH \
-       MACHINE_ARCH \
-       MACHINE
-
-GENDIRDEPS_FILTER+= ${GENDIRDEPS_FILTER_DIR_VARS:@v@S,${$v},_{${v}},@}
-GENDIRDEPS_FILTER+= ${GENDIRDEPS_FILTER_VARS:@v@S,/${$v}/,/_{${v}}/,@:NS,//,*:u}
+# see local.meta.sys.mk for GENDIRDEPS_FILTER_DIR_VARS and
+# GENDIRDEPS_FILTER_VARS
 
 # avoid churn for now
 LOCAL_DEPENDS_GUARD= _{DEP_RELDIR} == _{_DEP_RELDIR}
diff --git a/share/mk/local.meta.sys.mk b/share/mk/local.meta.sys.mk
index 6d54d9c5881c..6e03a99d7dfe 100644
--- a/share/mk/local.meta.sys.mk
+++ b/share/mk/local.meta.sys.mk
@@ -1,7 +1,6 @@
 # $FreeBSD$
 
 # local configuration specific to meta mode
-# XXX some of this should be in meta.sys.mk
 # we assume that MK_DIRDEPS_BUILD=yes
 
 # we need this until there is an alternative
@@ -34,7 +33,6 @@ MACHINE_ARCH.$m?= ${MACHINE_ARCH_LIST.$m:[1]}
 BOOT_MACHINE_DIR.$m ?= boot/$m
 .endfor
 
-.ifndef _TARGET_SPEC
 .if empty(MACHINE_ARCH)
 .if !empty(TARGET_ARCH)
 MACHINE_ARCH= ${TARGET_ARCH}
@@ -44,36 +42,6 @@ MACHINE_ARCH= ${MACHINE_ARCH.${MACHINE}}
 .endif
 MACHINE_ARCH?= ${MACHINE_ARCH.${MACHINE}}
 MACHINE_ARCH:= ${MACHINE_ARCH}
-.else
-# we got here via dirdeps
-MACHINE_ARCH:= ${MACHINE_ARCH.${MACHINE}}
-.endif
-
-# now because for universe we want to potentially
-# build for multiple MACHINE_ARCH per MACHINE
-# we need more than MACHINE in TARGET_SPEC
-TARGET_SPEC_VARS?= MACHINE MACHINE_ARCH
-# see dirdeps.mk
-.if ${TARGET_SPEC:Uno:M*,*} != ""
-_tspec := ${TARGET_SPEC:S/,/ /g}
-MACHINE := ${_tspec:[1]}
-MACHINE_ARCH := ${_tspec:[2]}
-# etc.
-# We need to stop that TARGET_SPEC affecting any submakes
-# and deal with MACHINE=${TARGET_SPEC} in the environment.
-TARGET_SPEC=
-# export but do not track
-.export-env TARGET_SPEC 
-.export ${TARGET_SPEC_VARS}
-.for v in ${TARGET_SPEC_VARS:O:u}
-.if empty($v)
-.undef $v
-.endif
-.endfor
-.endif
-# make sure we know what TARGET_SPEC is
-# as we may need it to find Makefile.depend*
-TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
 
 # to be consistent with src/Makefile just concatenate with '.'s
 TARGET_OBJ_SPEC:= ${TARGET_SPEC:S;,;.;g}
@@ -96,6 +64,7 @@ TARGET_MACHINE= host
 .endif
 .if ${MACHINE} == "host"
 OBJTOP := ${HOST_OBJTOP}
+MACHINE_ARCH= ${MACHINE_ARCH.${MACHINE}}
 .endif
 
 .if ${.MAKE.LEVEL} == 0 || empty(PYTHON)
@@ -267,3 +236,30 @@ META_MODE+=	missing-meta=yes
 .if empty(META_MODE:Mnofilemon)
 META_MODE+=	missing-filemon=yes
 .endif
+
+.if ${MK_DIRDEPS_BUILD} == "yes"
+# we set these here, rather than local.gendirdeps.mk
+# so we can ensure any DEP_* values that might be used in
+# conditionals do not cause syntax errors when Makefile.depend
+# is included at level 1+
+
+# order of this list matters!
+GENDIRDEPS_FILTER_DIR_VARS+= \
+       CSU_DIR \
+       BOOT_MACHINE_DIR
+
+# order of this list matters!
+GENDIRDEPS_FILTER_VARS+= \
+       KERNEL_NAME \
+       DEP_MACHINE_CPUARCH \
+       DEP_MACHINE_ARCH \
+       DEP_MACHINE
+
+.if ${.MAKE.LEVEL} > 0
+.for V in ${GENDIRDEPS_FILTER_DIR_VARS:MDEP_*:O:u} \
+	${GENDIRDEPS_FILTER_VARS:MDEP_*:O:u}
+$V?= ${${V:S,DEP_,,}}
+.endfor
+.endif
+
+.endif
diff --git a/share/mk/local.sys.env.mk b/share/mk/local.sys.env.mk
index e3348537db41..626aafe61270 100644
--- a/share/mk/local.sys.env.mk
+++ b/share/mk/local.sys.env.mk
@@ -50,3 +50,8 @@ ECHO_TRACE?=	true
 
 .include "src.sys.env.mk"
 .-include <site.sys.env.mk>
+
+# For universe we want to potentially
+# build for multiple MACHINE_ARCH per MACHINE
+# so we need more than MACHINE in TARGET_SPEC
+TARGET_SPEC_VARS?= MACHINE MACHINE_ARCH
diff --git a/share/mk/meta.sys.mk b/share/mk/meta.sys.mk
index cccab7ca952d..1fc58a226cb1 100644
--- a/share/mk/meta.sys.mk
+++ b/share/mk/meta.sys.mk
@@ -1,5 +1,4 @@
-# $FreeBSD$
-# $Id: meta.sys.mk,v 1.42 2021/12/13 05:50:55 sjg Exp $
+# $Id: meta.sys.mk,v 1.46 2023/04/18 18:43:00 sjg Exp $
 
 #
 #	@(#) Copyright (c) 2010-2021, Simon J. Gerraty
@@ -21,7 +20,44 @@
 .if ${MAKE_VERSION:U0} > 20100901
 .if !target(.ERROR)
 
-.-include <local.meta.sys.mk>
+.-include <local.meta.sys.env.mk>
+
+# If TARGET_SPEC_VARS is other than just MACHINE
+# it should be set by now.
+# TARGET_SPEC must not contain any '.'s.
+TARGET_SPEC_VARS ?= MACHINE
+
+.if !target(_meta_tspec_env_done_)
+_meta_tspec_env_done_: .NOTMAIN
+# Allow for local.meta.sys.env.mk to have done this
+
+.if ${TARGET_SPEC:Uno:M*,*} != ""
+# deal with TARGET_SPEC from env
+_tspec := ${TARGET_SPEC:S/,/ /g}
+.for i in ${TARGET_SPEC_VARS:${M_RANGE:Urange}}
+${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
+.endfor
+# We need to stop that TARGET_SPEC affecting any submakes
+TARGET_SPEC=
+# so export but do not track
+.export-env TARGET_SPEC
+.export ${TARGET_SPEC_VARS}
+.for v in ${TARGET_SPEC_VARS:O:u}
+.if empty($v)
+.undef $v
+.endif
+.endfor
+.endif
+.endif
+
+# Now make sure we know what TARGET_SPEC is
+# as we may need it to find Makefile.depend*
+.if ${MACHINE:Mhost*} != ""
+# host is special
+TARGET_SPEC = ${MACHINE}
+.else
+TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
+.endif
 
 # absolute path to what we are reading.
 _PARSEDIR = ${.PARSEDIR:tA}
@@ -178,3 +214,5 @@ META_NOPHONY= .PHONY
 META_NOECHO= echo
 .endif
 .endif
+
+.-include <local.meta.sys.mk>