xref: /freebsd/contrib/bmake/mk/dirdeps-cache-update.mk (revision c59c3bf34db360695f07735bebc76a768cac5afc)
1*c59c3bf3SSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
2*c59c3bf3SSimon J. Gerraty#
3*c59c3bf3SSimon J. Gerraty# $Id: dirdeps-cache-update.mk,v 1.23 2024/02/17 17:26:57 sjg Exp $
42c3632d1SSimon J. Gerraty#
52c3632d1SSimon J. Gerraty#	@(#) Copyright (c) 2020, Simon J. Gerraty
62c3632d1SSimon J. Gerraty#
72c3632d1SSimon J. Gerraty#	This file is provided in the hope that it will
82c3632d1SSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
92c3632d1SSimon J. Gerraty#	Permission to copy, redistribute or otherwise
102c3632d1SSimon J. Gerraty#	use this file is hereby granted provided that
112c3632d1SSimon J. Gerraty#	the above copyright notice and this notice are
122c3632d1SSimon J. Gerraty#	left intact.
132c3632d1SSimon J. Gerraty#
142c3632d1SSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
152c3632d1SSimon J. Gerraty#	sjg@crufty.net
162c3632d1SSimon J. Gerraty#
172c3632d1SSimon J. Gerraty
182c3632d1SSimon J. Gerraty##
192c3632d1SSimon J. Gerraty#
202c3632d1SSimon J. Gerraty# This makefile deals with the updating of STATIC_DIRDEPS_CACHE.
212c3632d1SSimon J. Gerraty# Some targets are so huge that computing dirdeps takes a significant
222c3632d1SSimon J. Gerraty# amount of time.  For such targets a STATIC_DIRDEPS_CACHE can make
232c3632d1SSimon J. Gerraty# sense.
242c3632d1SSimon J. Gerraty#
252c3632d1SSimon J. Gerraty# If the target is represented by targets/pseudo/production
262c3632d1SSimon J. Gerraty# it's normal DIRDEPS would be in
272c3632d1SSimon J. Gerraty# targets/pseudo/production/Makefile.depend
282c3632d1SSimon J. Gerraty# and STATIC_DIRDEPS_CACHE would be
292c3632d1SSimon J. Gerraty# targets/pseudo/production/Makefile.dirdeps.cache
302c3632d1SSimon J. Gerraty# which is simply initialized by copying dirdeps.cache.production
312c3632d1SSimon J. Gerraty# from $OBJTOP
322c3632d1SSimon J. Gerraty#
332c3632d1SSimon J. Gerraty# When dirdeps-targets.mk is initializing DIRDEPS it will look for
342c3632d1SSimon J. Gerraty# Makefile.dirdeps.cache and unless told not to
352c3632d1SSimon J. Gerraty# (MK_STATIC_DIRDEPS_CACHE=no) will use it as DIRDEPS_CACHE.
362c3632d1SSimon J. Gerraty#
372c3632d1SSimon J. Gerraty# If MK_STATIC_DIRDEPS_CACHE_UPDATE is "yes", then this makefile
382c3632d1SSimon J. Gerraty# comes into play.
392c3632d1SSimon J. Gerraty#
402c3632d1SSimon J. Gerraty# We usually get included from local.dirdeps.mk
412c3632d1SSimon J. Gerraty# as well as Makefile.depend of RELDIR with a static Makefile.dirdeps.cache
422c3632d1SSimon J. Gerraty#
432c3632d1SSimon J. Gerraty# If we see that STATIC_DIRDEPS_CACHE is in use, we need to hook a
442c3632d1SSimon J. Gerraty# cache-update target into the build to regenerate dirdeps.cache
452c3632d1SSimon J. Gerraty# in parallel with the rest of the build.
462c3632d1SSimon J. Gerraty# If MK_STATIC_DIRDEPS_CACHE_UPDATE_IMMEDIATE is "yes" we update
472c3632d1SSimon J. Gerraty# STATIC_DIRDEPS_CACHE as soon as the update is ready,
482c3632d1SSimon J. Gerraty# otherwise it will be done at the end of the build.
492c3632d1SSimon J. Gerraty#
502c3632d1SSimon J. Gerraty# If STATIC_DIRDEPS_CACHE is not in use, but a DIRDEPS_CACHE is,
512c3632d1SSimon J. Gerraty# then we need do nothing except export STATIC_DIRDEPS_CACHE and
522c3632d1SSimon J. Gerraty# DYNAMIC_DIRDEPS_CACHE for use when we are include during the visit
532c3632d1SSimon J. Gerraty# to the ultimate target (targets/pseudo/production).
542c3632d1SSimon J. Gerraty#
552c3632d1SSimon J. Gerraty# Regardless of which happens, when included at .MAKE.LEVEL > 0
562c3632d1SSimon J. Gerraty# for a target other than cache-update we simply copy
572c3632d1SSimon J. Gerraty# DYNAMIC_DIRDEPS_CACHE to STATIC_DIRDEPS_CACHE with some optional
582c3632d1SSimon J. Gerraty# filtering.
592c3632d1SSimon J. Gerraty#
602c3632d1SSimon J. Gerraty# If we are included for the target cache-update we take care of
612c3632d1SSimon J. Gerraty# running dirdeps.mk again to generate the DYNAMIC_DIRDEPS_CACHE.
622c3632d1SSimon J. Gerraty#
632c3632d1SSimon J. Gerraty
642c3632d1SSimon J. Gerraty.if !target(_${.PARSEFILE}_)
652c3632d1SSimon J. Gerraty_${.PARSEFILE}_: .NOTMAIN
662c3632d1SSimon J. Gerraty
672c3632d1SSimon J. GerratySTATIC_CACHE_SED += \
682c3632d1SSimon J. Gerraty	-e '/Autogenerated/s,-.*,- edit with care!,' \
692c3632d1SSimon J. Gerraty	-e '/cache-update/d'
702c3632d1SSimon J. Gerraty
712c3632d1SSimon J. GerratySTATIC_DIRDEPS_CACHE_UPDATE_SCRIPT ?= \
722c3632d1SSimon J. Gerraty	{ echo Saving ${DYNAMIC_DIRDEPS_CACHE} as ${STATIC_DIRDEPS_CACHE}; \
732c3632d1SSimon J. Gerraty        sed ${STATIC_CACHE_SED} ${DYNAMIC_DIRDEPS_CACHE} > ${STATIC_DIRDEPS_CACHE}; }
742c3632d1SSimon J. Gerraty.endif
752c3632d1SSimon J. Gerraty
762c3632d1SSimon J. Gerraty.if ${MK_DIRDEPS_CACHE:Uno} == "yes"
772c3632d1SSimon J. Gerraty.if ${MK_STATIC_DIRDEPS_CACHE_UPDATE:Uno} == "yes"
782c3632d1SSimon J. Gerraty.if ${_debug_reldir:U0} || ${DEBUG_DIRDEPS:U:Mcache*} != ""
792c3632d1SSimon J. Gerraty_debug_cache = 1
802c3632d1SSimon J. Gerraty.else
812c3632d1SSimon J. Gerraty_debug_cache = 0
822c3632d1SSimon J. Gerraty.endif
832c3632d1SSimon J. Gerraty
842c3632d1SSimon J. Gerraty.if ${.MAKE.LEVEL} == 0 && !make(cache-update)
852c3632d1SSimon J. Gerraty
862c3632d1SSimon J. Gerraty.if ${_debug_cache}
872c3632d1SSimon J. Gerraty.info ${MK_STATIC_DIRDEPS_CACHE_UPDATE MK_STATIC_DIRDEPS_CACHE MK_DIRDEPS_CACHE DIRDEPS_CACHE STATIC_DIRDEPS_CACHE:L:@v@$v=${$v}@}
882c3632d1SSimon J. Gerraty.endif
892c3632d1SSimon J. Gerraty
902c3632d1SSimon J. Gerraty.if ${MK_STATIC_DIRDEPS_CACHE} == "yes" && defined(STATIC_DIRDEPS_CACHE) && exists(${STATIC_DIRDEPS_CACHE})
91956e45f6SSimon J. Gerraty.if !make(dirdeps) && !target(cache_update_dirdep)
922c3632d1SSimon J. Gerraty# We are using static cache and this is the only look we will get.
932c3632d1SSimon J. Gerraty# We want to generate an updated cache while we build
942c3632d1SSimon J. Gerraty# so need to hook cache-update to dirdeps now.
952c3632d1SSimon J. Gerraty# Note: we are running as a sibling to dirdeps-cached,
962c3632d1SSimon J. Gerraty# attempting to do this in that context is problematic.
972c3632d1SSimon J. Gerraty
982c3632d1SSimon J. Gerraty# One of these should exist - to actually kick off the cache generation
992c3632d1SSimon J. Gerraty.for d in ${STATIC_DIRDEPS_CACHE:H}/cache-update ${STATIC_DIRDEPS_CACHE:H:H}/cache-update ${STATIC_DIRDEPS_CACHE:H:H:H}/cache-update
1002c3632d1SSimon J. Gerraty.if exists($d)
1012c3632d1SSimon J. Gerratycache_update_dirdep ?= $d.${TARGET_SPEC}
1022c3632d1SSimon J. Gerraty.endif
1032c3632d1SSimon J. Gerraty.endfor
104956e45f6SSimon J. Gerratydirdeps cache_update_dirdep: ${cache_update_dirdep}
1052c3632d1SSimon J. Gerraty${cache_update_dirdep}: _DIRDEP_USE
1062c3632d1SSimon J. GerratyDYNAMIC_DIRDEPS_CACHE := ${OBJTOP}/dirdeps.cache.${STATIC_DIRDEPS_CACHE:H:T}-update
1072c3632d1SSimon J. Gerraty.export DYNAMIC_DIRDEPS_CACHE STATIC_DIRDEPS_CACHE
1082c3632d1SSimon J. Gerraty.endif	# make(dirdeps)
1092c3632d1SSimon J. Gerraty.endif	# MK_*
1102c3632d1SSimon J. Gerraty
1112c3632d1SSimon J. Gerraty.endif	# .MAKE.LEVEL 0
1122c3632d1SSimon J. Gerraty
1132c3632d1SSimon J. Gerraty.if ${.MAKE.LEVEL} > 0 && ${.CURDIR:T} == "cache-update"
1142c3632d1SSimon J. Gerraty# we are the background update shim
1152c3632d1SSimon J. Gerraty
1162c3632d1SSimon J. Gerraty.if ${_debug_cache}
1172c3632d1SSimon J. Gerraty.info level ${.MAKE.LEVEL}: ${MK_DIRDEPS_CACHE DYNAMIC_DIRDEPS_CACHE STATIC_DIRDEPS_CACHE:L:@v@$v=${$v}@}
1182c3632d1SSimon J. Gerraty.endif
1192c3632d1SSimon J. Gerraty
1202c3632d1SSimon J. Gerratyall: cache-build
1212c3632d1SSimon J. Gerratycache-build: .META
1222c3632d1SSimon J. Gerraty	@set -x; MAKELEVEL=0 \
1232c3632d1SSimon J. Gerraty	${.MAKE} -C ${SRCTOP} -f ${RELDIR}/Makefile cache-update \
1242c3632d1SSimon J. Gerraty	-DWITHOUT_STATIC_DIRDEPS_CACHE_UPDATE
1252c3632d1SSimon J. Gerraty
1262c3632d1SSimon J. Gerraty.endif	# cache-update
1272c3632d1SSimon J. Gerraty
1282c3632d1SSimon J. Gerraty.elif ${.MAKE.LEVEL} == 0 && make(cache-update) && !target(cache-update)
1292c3632d1SSimon J. Gerraty# we were invoked above
1302c3632d1SSimon J. Gerraty# we just leverage dirdeps.mk
1312c3632d1SSimon J. GerratyBUILD_DIRDEPS_TARGETS := ${STATIC_DIRDEPS_CACHE:H:T}
1322c3632d1SSimon J. GerratyDIRDEPS := ${STATIC_DIRDEPS_CACHE:H:S,^${SRCTOP}/,,}.${TARGET_SPEC}
1332c3632d1SSimon J. GerratyDIRDEPS_CACHE := ${DYNAMIC_DIRDEPS_CACHE}
1342c3632d1SSimon J. Gerraty
1352c3632d1SSimon J. Gerraty.if ${DEBUG_DIRDEPS:U:Mcache*} != ""
1362c3632d1SSimon J. Gerraty.info level 0: ${MK_DIRDEPS_CACHE DIRDEPS_CACHE DIRDEPS:L:@v@$v=${$v}@}
1372c3632d1SSimon J. Gerraty.endif
1382c3632d1SSimon J. Gerraty
1392c3632d1SSimon J. Gerraty# so cache-built below can check on us
1402c3632d1SSimon J. Gerratyx!= echo; echo ${.MAKE.PID} > ${DIRDEPS_CACHE}.new.pid
1412c3632d1SSimon J. Gerraty
1422c3632d1SSimon J. Gerratycache-update: ${DIRDEPS_CACHE}
1432c3632d1SSimon J. Gerraty	@rm -f ${DIRDEPS_CACHE}.new.pid
1442c3632d1SSimon J. Gerraty.if ${MK_STATIC_DIRDEPS_CACHE_UPDATE_IMMEDIATE:Uno} == "yes"
1452c3632d1SSimon J. Gerraty	${STATIC_DIRDEPS_CACHE_UPDATE_SCRIPT}
1462c3632d1SSimon J. Gerraty.endif
1472c3632d1SSimon J. Gerraty
1482c3632d1SSimon J. Gerratyall:
1492c3632d1SSimon J. Gerraty
1502c3632d1SSimon J. Gerraty.include <dirdeps.mk>
1512c3632d1SSimon J. Gerraty
1522c3632d1SSimon J. Gerraty.endif	# MK_STATIC_DIRDEPS_CACHE_UPDATE
1532c3632d1SSimon J. Gerraty.endif	# MK_DIRDEPS_CACHE
1542c3632d1SSimon J. Gerraty
1552c3632d1SSimon J. Gerraty.if ${.MAKE.LEVEL} > 0 && ${MK_STATIC_DIRDEPS_CACHE_UPDATE:Uno} == "yes" && \
1562c3632d1SSimon J. Gerraty	${STATIC_DIRDEPS_CACHE:Uno:H} == "${SRCTOP}/${RELDIR}"
1572c3632d1SSimon J. Gerraty.if !defined(DYNAMIC_DIRDEPS_CACHE)
1582c3632d1SSimon J. Gerratyall:
1592c3632d1SSimon J. Gerraty.else
1602c3632d1SSimon J. Gerraty# This is the easy bit, time to save the cache
1612c3632d1SSimon J. Gerraty
1622c3632d1SSimon J. Gerratyall: cache-update
1632c3632d1SSimon J. Gerraty
1642c3632d1SSimon J. Gerraty# ensure the cache update is completed
1652c3632d1SSimon J. Gerratycache-built:
1662c3632d1SSimon J. Gerraty	@test -s ${DYNAMIC_DIRDEPS_CACHE}.new || exit 0; \
1672c3632d1SSimon J. Gerraty	pid=`cat ${DYNAMIC_DIRDEPS_CACHE}.new.pid 2> /dev/null`; \
1682c3632d1SSimon J. Gerraty	test $${pid:-0} -gt 1 || exit 0; \
1692c3632d1SSimon J. Gerraty	echo "Waiting for $$pid to finish ${DYNAMIC_DIRDEPS_CACHE} ..."; \
1702c3632d1SSimon J. Gerraty	while 'kill' -0 $$pid; do sleep 30; done > /dev/null 2>&1
1712c3632d1SSimon J. Gerraty
1722c3632d1SSimon J. Gerratycache-update: cache-built
1732c3632d1SSimon J. Gerraty.if ${MK_STATIC_DIRDEPS_CACHE_UPDATE_IMMEDIATE:Uno} == "no"
1742c3632d1SSimon J. Gerraty	@test ! -s ${DYNAMIC_DIRDEPS_CACHE} || \
1752c3632d1SSimon J. Gerraty	${STATIC_DIRDEPS_CACHE_UPDATE_SCRIPT}
1762c3632d1SSimon J. Gerraty.endif
1772c3632d1SSimon J. Gerraty
1782c3632d1SSimon J. Gerraty.endif
1792c3632d1SSimon J. Gerraty.endif
180