1# $Id: gendirdeps.mk,v 1.53 2025/05/20 17:42:49 sjg Exp $ 2 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2011-2025, Simon J. Gerraty 6# Copyright (c) 2010-2018, Juniper Networks, Inc. 7# All rights reserved. 8# 9# Redistribution and use in source and binary forms, with or without 10# modification, are permitted provided that the following conditions 11# are met: 12# 1. Redistributions of source code must retain the above copyright 13# notice, this list of conditions and the following disclaimer. 14# 2. Redistributions in binary form must reproduce the above copyright 15# notice, this list of conditions and the following disclaimer in the 16# documentation and/or other materials provided with the distribution. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30# 31# This makefile [re]generates ${.MAKE.DEPENDFILE} 32# 33 34.include <install-new.mk> 35 36# Assumptions: 37# RELDIR is the relative path from ${SRCTOP} to ${_CURDIR} 38# (SRCTOP is ${SB}/src) 39# _CURDIR is the absolute version of ${.CURDIR} 40# _OBJDIR is the absolute version of ${.OBJDIR} 41# _objroot is realpath of ${_OBJTOP} without ${MACHINE} 42# this may be different from _OBJROOT if $SB/obj is a 43# symlink to another filesystem. 44# _objroot must be a prefix match for _objtop 45 46# If any of GENDIRDEPS_FILTER, GENDIRDEPS_FILTER_DIR_VARS 47# or GENDIRDEPS_FILTER_VARS are set, we use them to filter the 48# output from filemon(4). 49# Any references to variables that dirdeps.mk will set 50# such as DEP_MACHINE, DEP_RELDIR etc, should use that form. 51# Thus we want ${DEP_MACHINE} not ${MACHINE} used in DIRDEPS. 52# 53# If any manually maintained Makefile.depend files will use any 54# DEP_* variables in conditionals, precautions are needed to avoid 55# errors when Makefile.depend is read at level 1+ (ie not via 56# dirdeps.mk) 57# Using MACHINE as an example; such makefiles can do: 58# 59# DEP_MACHINE ?= ${MACHINE} 60# .if ${DEP_MACHINE} == "xyz" 61# 62# or: 63# 64# .if ${DEP_MACHINE:U${MACHINE}} == "xyz" 65# 66# but it might be safer to set GENDIRDEPS_FILTER_DIR_VARS and 67# GENDIRDEPS_FILTER_VARS via local.meta.sys.mk rather than 68# local.gendirdeps.mk and then: 69# 70# .if ${.MAKE.LEVEL} > 0 71# .for V in ${GENDIRDEPS_FILTER_DIR_VARS:MDEP_*} \ 72# ${GENDIRDEPS_FILTER_VARS:MDEP_*} 73# $V ?= ${${V:S,DEP_,,}} 74# .endfor 75# .endif 76# 77.MAIN: all 78 79.if ${DEBUG_GENDIRDEPS:Uno:@m@${RELDIR:M$m}@} != "" 80_debug.gendirdeps = 1 81.else 82_debug.gendirdeps = 0 83.endif 84 85# keep this simple 86.MAKE.MODE = compat 87 88all: 89 90_CURDIR ?= ${.CURDIR} 91_OBJDIR ?= ${.OBJDIR} 92_OBJTOP ?= ${OBJTOP} 93_OBJROOT ?= ${OBJROOT:U${_OBJTOP:H}} 94.if ${_OBJROOT:M*/} 95_slash=/ 96.else 97_slash= 98.endif 99_objroot ?= ${_OBJROOT:tA}${_slash} 100 101_this = ${.PARSEDIR}/${.PARSEFILE} 102 103# remember what to make 104_DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T} 105 106# We do _not_ want to read our own output! 107.MAKE.DEPENDFILE = /dev/null 108 109# caller should have set this 110META_FILES ?= ${.MAKE.META.FILES} 111# this sometimes needs to be passed separately 112.if !empty(META_XTRAS) 113META_FILES += ${META_XTRAS:N\*.meta} 114.endif 115 116.if !empty(META_FILES) 117.if ${_debug.gendirdeps} && ${DEBUG_GENDIRDEPS:Mmeta*} != "" 118.info ${RELDIR}: META_FILES=${META_FILES} 119.endif 120 121.if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER) 122# so we can compare below 123.-include <${_DEPENDFILE}> 124# yes, I mean :U with no value 125_DIRDEPS := ${DIRDEPS:U:O:u} 126.endif 127 128META_FILES := ${META_FILES:T:O:u} 129 130# pickup customizations 131.-include <local.gendirdeps.mk> 132 133# these are actually prefixes that we'll skip 134# they should all be absolute paths 135SKIP_GENDIRDEPS ?= 136.if !empty(SKIP_GENDIRDEPS) 137_skip_gendirdeps = ${EGREP:Uegrep} -v '^(${SKIP_GENDIRDEPS:O:u:ts|})' | 138.else 139_skip_gendirdeps = 140.endif 141 142# Below we will turn _{VAR} into ${VAR} which keeps this simple 143# GENDIRDEPS_FILTER_DIR_VARS is a list of dirs to be substiuted for. 144# GENDIRDEPS_FILTER_VARS is more general. 145# In each case order matters. 146.if !empty(GENDIRDEPS_FILTER_DIR_VARS) 147GENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_DIR_VARS:@v@S,${$v},_{${v}},@} 148.endif 149.if !empty(GENDIRDEPS_FILTER_VARS) 150GENDIRDEPS_FILTER += ${GENDIRDEPS_FILTER_VARS:@v@S,/${$v}/,/_{${v}}/,@:NS,//,*:u} 151.endif 152 153# this (*should* be set in meta.sys.mk) 154# is the script that extracts what we want. 155META2DEPS ?= ${.PARSEDIR}/meta2deps.sh 156META2DEPS := ${META2DEPS} 157 158.if ${_debug.gendirdeps} && ${DEBUG_GENDIRDEPS:Mmeta2d*} != "" 159_time = time 160_sh_x = sh -x 161_py_d = -ddd 162.else 163_time = 164_sh_x = 165_py_d = 166.endif 167 168.if ${META2DEPS:E} == "py" 169# we can afford to do this all the time. 170DPDEPS ?= no 171META2DEPS_CMD = ${_time} ${PYTHON} ${META2DEPS} ${_py_d} 172.if ${DPDEPS:tl} != "no" 173META2DEPS_CMD += -D ${DPDEPS} 174.endif 175META2DEPS_FILTER = sed 's,^src:,${SRCTOP}/,;s,^\([^/]\),${OBJTOP}/\1,' | 176.elif ${META2DEPS:E} == "sh" 177META2DEPS_CMD = ${_time} ${_sh_x} ${META2DEPS} OBJTOP=${_OBJTOP} 178.else 179META2DEPS_CMD ?= ${META2DEPS} 180.endif 181 182.if ${TARGET_OBJ_SPEC:U${MACHINE}} != ${MACHINE} 183META2DEPS_CMD += -T ${TARGET_OBJ_SPEC} 184.endif 185META2DEPS_CMD += \ 186 -R ${RELDIR} -H ${HOST_TARGET} \ 187 ${M2D_OBJROOTS:O:u:@o@-O $o@} \ 188 ${M2D_EXCLUDES:O:u:@o@-X $o@} \ 189 190 191M2D_OBJROOTS += ${OBJTOP} ${_OBJROOT} ${_objroot} 192.if defined(SB_OBJROOT) 193M2D_OBJROOTS += ${SB_OBJROOT} 194.endif 195.if defined(STAGE_ROOT) 196M2D_OBJROOTS += ${STAGE_ROOT} 197.endif 198.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} == "" 199# meta2deps.py only groks objroot 200# so we need to give it what it expects 201# and tell it not to add machine qualifiers 202META2DEPS_ARGS += MACHINE=none 203.endif 204.if defined(SB_BACKING_SB) 205META2DEPS_CMD += -S ${SB_BACKING_SB}/src 206M2D_OBJROOTS += ${SB_BACKING_SB}/${SB_OBJPREFIX} 207.endif 208 209GENDIRDEPS_SEDCMDS += \ 210 -e 's,//*$$,,;s,\.${HOST_TARGET:Uhost}$$,.host,' \ 211 -e 's,\.${HOST_TARGET32:Uhost32}$$,.host32,' \ 212 -e 's,\.${MACHINE}$$,,' \ 213 -e 's:\.${TARGET_SPEC:U${MACHINE}}$$::' 214 215# we are only interested in the dirs 216# specifically those we read something from. 217# we canonicalize them to keep things simple 218# if we are using a split-fs sandbox, it gets a little messier. 219_objtop := ${_OBJTOP:tA} 220 221# some people put *.meta in META_XTRAS to make sure we get here 222_meta_files := ${META_FILES:N\*.meta:O:u} 223# assume a big list 224_meta_files_arg= @meta.list 225.if empty(_meta_files) && ${META_FILES:M\*.meta} != "" 226# XXX this should be considered a bad idea, 227# since we cannot ignore stale .meta 228x != cd ${_OBJDIR} && find . -name '*.meta' -print -o \( -type d ! -name . -prune \) | sed 's,^./,,' > meta.list; echo 229.elif ${_meta_files:[#]} > 500 230.export _meta_files 231x != echo; for m in $$_meta_files; do echo $$m; done > meta.list 232# _meta_files is consuming a lot of env space 233# that can impact command line length, 234# and we do not need it any more 235.undef _meta_files 236.unexport _meta_files 237.else 238_meta_files_arg:= ${_meta_files} 239.endif 240 241dir_list != cd ${_OBJDIR} && \ 242 ${META2DEPS_CMD} MACHINE=${MACHINE} \ 243 SRCTOP=${SRCTOP} RELDIR=${RELDIR} CURDIR=${_CURDIR} \ 244 ${META2DEPS_ARGS} \ 245 ${_meta_files_arg} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \ 246 sed ${GENDIRDEPS_SEDCMDS} 247 248.if ${dir_list:M*ERROR\:*} != "" 249.warning ${dir_list:C,.*(ERROR),\1,W} 250.warning Skipping ${_DEPENDFILE:S,${SRCTOP}/,,} 251# we are not going to update anything 252.else 253dpadd_dir_list= 254.if !empty(DPADD) 255_nonlibs := ${DPADD:T:Nlib*:N*include} 256.if !empty(_nonlibs) 257ddep_list = 258.for f in ${_nonlibs:@x@${DPADD:M*/$x}@} 259.if exists($f.dirdep) 260ddep_list += $f.dirdep 261.elif exists(${f:H}.dirdep) 262ddep_list += ${f:H}.dirdep 263.else 264dir_list += ${f:H:tA} 265dpadd_dir_list += ${f:H:tA} 266.endif 267.endfor 268.if !empty(ddep_list) 269ddeps != cat ${ddep_list:O:u} | ${META2DEPS_FILTER} ${_skip_gendirdeps} \ 270 sed ${GENDIRDEPS_SEDCMDS} 271 272.if ${_debug.gendirdeps} 273.info ${RELDIR}: raw_dir_list='${dir_list}' 274.info ${RELDIR}: ddeps='${ddeps}' 275.endif 276dir_list += ${ddeps} 277.endif 278.endif 279.endif 280 281# DIRDEPS represent things that had to have been built first 282# so they should all be undir OBJTOP. 283# Note that ${_OBJTOP}/bsd/include/machine will get reported 284# to us as $SRCTOP/bsd/sys/$MACHINE_ARCH/include meaning we 285# will want to visit bsd/include 286# so we add 287# ${"${dir_list:M*bsd/sys/${MACHINE_ARCH}/include}":?bsd/include:} 288# to GENDIRDEPS_DIR_LIST_XTRAS 289_objtops = ${OBJTOP} ${_OBJTOP} ${_objtop} 290_objtops := ${_objtops:O:u} 291dirdep_list = \ 292 ${_objtops:@o@${dir_list:M$o*/*:C,$o[^/]*/,,}@} \ 293 ${GENDIRDEPS_DIR_LIST_XTRAS} 294 295# sort longest first 296M2D_OBJROOTS := ${M2D_OBJROOTS:O:u:[-1..1]} 297 298# anything we use from an object dir other than ours 299# needs to be qualified with its .<machine> suffix 300# (we used the pseudo machine "host" for the HOST_TARGET). 301skip_ql= ${SRCTOP}* ${_objtops:@o@$o*@} 302.for o in ${M2D_OBJROOTS:${skip_ql:${M_ListToSkip}}} 303# we need := so only skip_ql to this point applies 304ql.$o := ${dir_list:${skip_ql:${M_ListToSkip}}:M$o*/*/*:C,$o([^/]+)/(.*),\2.\1,:S,.${HOST_TARGET},.host,} 305qualdir_list += ${ql.$o} 306.if ${_debug.gendirdeps} 307.info ${RELDIR}: o=$o ${ql.$o qualdir_list:L:@v@$v=${$v}@} 308.endif 309skip_ql+= $o* 310.endfor 311 312dirdep_list := ${dirdep_list:O:u} 313qualdir_list := ${qualdir_list:N*.${MACHINE}:O:u} 314 315DIRDEPS = \ 316 ${dirdep_list:N${RELDIR}:N${RELDIR}/*} \ 317 ${qualdir_list:N${RELDIR}.*:N${RELDIR}/*} 318 319# We only consider things below $RELDIR/ if they have a makefile. 320# This is the same test that _DIRDEP_USE applies. 321# We have do a double test with dirdep_list as it _may_ contain 322# qualified dirs - if we got anything from a stage dir. 323# qualdir_list we know are all qualified. 324# It would be nice do peform this check for all of DIRDEPS, 325# but we cannot assume that all of the tree is present, 326# in fact we can only assume that RELDIR is. 327DIRDEPS += \ 328 ${dirdep_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/$d/$m):?$d:${exists(${SRCTOP}/${d:R}/$m):?$d:}}@}@} \ 329 ${qualdir_list:M${RELDIR}/*:@d@${.MAKE.MAKEFILE_PREFERENCE:@m@${exists(${SRCTOP}/${d:R}/$m):?$d:}@}@} 330 331# what modifiers do we allow in GENDIRDEPS_FILTER 332GENDIRDEPS_FILTER_MASK += @CMNS 333DIRDEPS := ${DIRDEPS:${GENDIRDEPS_FILTER:UNno:M[${GENDIRDEPS_FILTER_MASK:O:u:ts}]*:ts:}:C,//+,/,g:O:u} 334 335.if ${_debug.gendirdeps} 336.info ${RELDIR}: M2D_OBJROOTS=${M2D_OBJROOTS} 337.info ${RELDIR}: M2D_EXCLUDES=${M2D_EXCLUDES} 338.info ${RELDIR}: dir_list='${dir_list}' 339.info ${RELDIR}: dpadd_dir_list='${dpadd_dir_list}' 340.info ${RELDIR}: dirdep_list='${dirdep_list}' 341.info ${RELDIR}: qualdir_list='${qualdir_list}' 342.info ${RELDIR}: SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS}' 343.info ${RELDIR}: GENDIRDEPS_FILTER='${GENDIRDEPS_FILTER}' 344.info ${RELDIR}: FORCE_DPADD='${DPADD}' 345.info ${RELDIR}: DIRDEPS='${DIRDEPS}' 346.endif 347 348# SRC_DIRDEPS is for checkout logic 349src_dirdep_list = \ 350 ${dir_list:M${SRCTOP}/*:S,${SRCTOP}/,,} 351 352SRC_DIRDEPS = \ 353 ${src_dirdep_list:N${RELDIR}:N${RELDIR}/*:C,(/h)/.*,,} 354 355SRC_DIRDEPS := ${SRC_DIRDEPS:${GENDIRDEPS_SRC_FILTER:UN/*:ts:}:C,//+,/,g:O:u} 356 357# if you want to capture SRC_DIRDEPS in .MAKE.DEPENDFILE put 358# SRC_DIRDEPS_FILE = ${_DEPENDFILE} 359# in local.gendirdeps.mk 360.if ${SRC_DIRDEPS_FILE:Uno:tl} != "no" 361ECHO_SRC_DIRDEPS = echo 'SRC_DIRDEPS = \'; echo '${SRC_DIRDEPS:@d@ $d \\${.newline}@}'; echo; 362 363.if ${SRC_DIRDEPS_FILE:T} == ${_DEPENDFILE:T} 364_include_src_dirdeps = ${ECHO_SRC_DIRDEPS} 365.else 366all: ${SRC_DIRDEPS_FILE} 367.if !target(${SRC_DIRDEPS_FILE}) 368${SRC_DIRDEPS_FILE}: ${META_FILES} ${_this} ${META2DEPS} 369 @(${ECHO_SRC_DIRDEPS}) > $@ 370.endif 371.endif 372.endif 373_include_src_dirdeps ?= 374 375all: ${_DEPENDFILE} 376 377# if this is going to exist it would be there by now 378.if !exists(.depend) 379CAT_DEPEND = /dev/null 380.endif 381CAT_DEPEND ?= .depend 382 383.if !empty(_DIRDEPS) && ${DIRDEPS} != ${_DIRDEPS} 384# we may have changed a filter 385.PHONY: ${_DEPENDFILE} 386.endif 387 388# set this to 'no' and we will not capture any 389# local depends 390LOCAL_DEPENDS_GUARD ?= _{.MAKE.LEVEL} > 0 391 392# 'cat .depend' should suffice, but if we are mixing build modes 393# .depend may contain things we don't want. 394# The sed command at the end of the stream, allows for the filters 395# to output _{VAR} tokens which we will turn into proper ${VAR} references. 396${_DEPENDFILE}: .NOMETA ${CAT_DEPEND:M.depend} ${META_FILES:O:u:@m@${exists($m):?$m:}@} ${_this} ${META2DEPS} 397 @(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \ 398 echo 'DIRDEPS = \'; \ 399 echo '${DIRDEPS:@d@ $d \\${.newline}@}'; echo; \ 400 ${_include_src_dirdeps} \ 401 echo '.include <dirdeps.mk>'; \ 402 [ "${LOCAL_DEPENDS_GUARD:[1]:tl}" != no ] || exit 0; \ 403 echo; \ 404 echo '.if ${LOCAL_DEPENDS_GUARD}'; \ 405 echo '# local dependencies - needed for -jN in clean tree'; \ 406 [ -s ${CAT_DEPEND} ] && { grep : ${CAT_DEPEND} | grep -v '[/\\]'; }; \ 407 echo '.endif' ) | sed 's,_\([{(]\),$$\1,g' > $@.new${.MAKE.PID} 408 @${InstallNew}; InstallNew -s $@.new${.MAKE.PID} 409 410.endif # meta2deps failed 411.elif !empty(SUBDIR) 412 413DIRDEPS := ${SUBDIR:S,^,${RELDIR}/,:O:u} 414 415all: ${_DEPENDFILE} 416 417${_DEPENDFILE}: .NOMETA ${MAKEFILE} ${_this} 418 @(${GENDIRDEPS_HEADER} echo '# Autogenerated - do NOT edit!'; echo; \ 419 echo 'DIRDEPS = \'; \ 420 echo '${DIRDEPS:@d@ $d \\${.newline}@}'; echo; \ 421 echo '.include <dirdeps.mk>'; \ 422 echo ) | sed 's,_\([{(]\),$$\1,g' > $@.new 423 @${InstallNew}; InstallNew $@.new 424 425.else 426 427# nothing to do 428all ${_DEPENDFILE}: 429 430.endif 431${_DEPENDFILE}: .PRECIOUS 432 433# don't waste time looking for ways to make .meta files 434.SUFFIXES: 435