1*3cbdda60SSimon J. Gerraty# $Id: meta.autodep.mk,v 1.32 2012/11/13 00:44:26 sjg Exp $ 2*3cbdda60SSimon J. Gerraty 3*3cbdda60SSimon J. Gerraty# 4*3cbdda60SSimon J. Gerraty# @(#) Copyright (c) 2010, Simon J. Gerraty 5*3cbdda60SSimon J. Gerraty# 6*3cbdda60SSimon J. Gerraty# This file is provided in the hope that it will 7*3cbdda60SSimon J. Gerraty# be of use. There is absolutely NO WARRANTY. 8*3cbdda60SSimon J. Gerraty# Permission to copy, redistribute or otherwise 9*3cbdda60SSimon J. Gerraty# use this file is hereby granted provided that 10*3cbdda60SSimon J. Gerraty# the above copyright notice and this notice are 11*3cbdda60SSimon J. Gerraty# left intact. 12*3cbdda60SSimon J. Gerraty# 13*3cbdda60SSimon J. Gerraty# Please send copies of changes and bug-fixes to: 14*3cbdda60SSimon J. Gerraty# sjg@crufty.net 15*3cbdda60SSimon J. Gerraty# 16*3cbdda60SSimon J. Gerraty 17*3cbdda60SSimon J. Gerraty_this ?= ${.PARSEFILE} 18*3cbdda60SSimon J. Gerraty.if !target(__${_this}__) 19*3cbdda60SSimon J. Gerraty__${_this}__: .NOTMAIN 20*3cbdda60SSimon J. Gerraty 21*3cbdda60SSimon J. Gerraty.-include "local.autodep.mk" 22*3cbdda60SSimon J. Gerraty 23*3cbdda60SSimon J. Gerraty.if defined(SRCS) 24*3cbdda60SSimon J. Gerraty# it would be nice to be able to query .SUFFIXES 25*3cbdda60SSimon J. GerratyOBJ_EXTENSIONS+= .o .po .lo .So 26*3cbdda60SSimon J. Gerraty 27*3cbdda60SSimon J. Gerraty# explicit dependencies help short-circuit .SUFFIX searches 28*3cbdda60SSimon J. GerratySRCS_DEP_FILTER+= N*.[hly] 29*3cbdda60SSimon J. Gerraty.for s in ${SRCS:${SRCS_DEP_FILTER:O:u:ts:}} 30*3cbdda60SSimon J. Gerraty.for e in ${OBJ_EXTENSIONS:O:u} 31*3cbdda60SSimon J. Gerraty.if !target(${s:T:R}$e) 32*3cbdda60SSimon J. Gerraty${s:T:R}$e: $s 33*3cbdda60SSimon J. Gerraty.endif 34*3cbdda60SSimon J. Gerraty.endfor 35*3cbdda60SSimon J. Gerraty.endfor 36*3cbdda60SSimon J. Gerraty.endif 37*3cbdda60SSimon J. Gerraty 38*3cbdda60SSimon J. Gerraty.if make(gendirdeps) 39*3cbdda60SSimon J. Gerraty# you are supposed to know what you are doing! 40*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE = yes 41*3cbdda60SSimon J. Gerraty.elif !empty(.TARGETS) && !make(all) 42*3cbdda60SSimon J. Gerraty# do not update the *depend* files 43*3cbdda60SSimon J. Gerraty# unless we are building the entire directory or the default target. 44*3cbdda60SSimon J. Gerraty# NO means don't update .depend - or Makefile.depend* 45*3cbdda60SSimon J. Gerraty# no means update .depend but not Makefile.depend* 46*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE = NO 47*3cbdda60SSimon J. Gerraty.elif ${.MAKEFLAGS:M-k} != "" 48*3cbdda60SSimon J. Gerraty# it is a bad idea to update anything 49*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE = NO 50*3cbdda60SSimon J. Gerraty.endif 51*3cbdda60SSimon J. Gerraty 52*3cbdda60SSimon J. Gerraty_CURDIR ?= ${.CURDIR} 53*3cbdda60SSimon J. Gerraty_DEPENDFILE := ${_CURDIR}/${.MAKE.DEPENDFILE:T} 54*3cbdda60SSimon J. Gerraty 55*3cbdda60SSimon J. Gerraty.if ${.MAKE.LEVEL} == 0 56*3cbdda60SSimon J. Gerraty.if ${BUILD_AT_LEVEL0:Uyes:tl} == "no" 57*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE = NO 58*3cbdda60SSimon J. Gerraty.endif 59*3cbdda60SSimon J. Gerraty.endif 60*3cbdda60SSimon J. Gerraty.if !exists(${_DEPENDFILE}) 61*3cbdda60SSimon J. Gerraty_bootstrap_dirdeps = yes 62*3cbdda60SSimon J. Gerraty.endif 63*3cbdda60SSimon J. Gerraty_bootstrap_dirdeps ?= no 64*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE ?= yes 65*3cbdda60SSimon J. Gerraty 66*3cbdda60SSimon J. Gerraty.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != "" 67*3cbdda60SSimon J. Gerraty.info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE} 68*3cbdda60SSimon J. Gerraty.endif 69*3cbdda60SSimon J. Gerraty 70*3cbdda60SSimon J. Gerraty.if !empty(XMAKE_META_FILE) 71*3cbdda60SSimon J. Gerraty.if exists(${.OBJDIR}/${XMAKE_META_FILE}) 72*3cbdda60SSimon J. Gerraty# we cannot get accurate dependencies from an update build 73*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE = NO 74*3cbdda60SSimon J. Gerraty.else 75*3cbdda60SSimon J. GerratyMETA_XTRAS += ${XMAKE_META_FILE} 76*3cbdda60SSimon J. Gerraty.endif 77*3cbdda60SSimon J. Gerraty.endif 78*3cbdda60SSimon J. Gerraty 79*3cbdda60SSimon J. Gerraty.if ${_bootstrap_dirdeps} == "yes" || exists(${_DEPENDFILE}) 80*3cbdda60SSimon J. Gerraty# if it isn't supposed to be touched by us the Makefile should have 81*3cbdda60SSimon J. Gerraty# UPDATE_DEPENDFILE = no 82*3cbdda60SSimon J. GerratyWANT_UPDATE_DEPENDFILE ?= yes 83*3cbdda60SSimon J. Gerraty.endif 84*3cbdda60SSimon J. Gerraty 85*3cbdda60SSimon J. Gerraty.if ${WANT_UPDATE_DEPENDFILE:Uno:tl} != "no" 86*3cbdda60SSimon J. Gerraty.if ${.MAKE.MODE:Mmeta*} == "" || ${.MAKE.MODE:M*read*} != "" 87*3cbdda60SSimon J. GerratyUPDATE_DEPENDFILE = no 88*3cbdda60SSimon J. Gerraty.endif 89*3cbdda60SSimon J. Gerraty 90*3cbdda60SSimon J. Gerraty.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != "" 91*3cbdda60SSimon J. Gerraty.info ${_DEPENDFILE:S,${SRCTOP}/,,} update=${UPDATE_DEPENDFILE} 92*3cbdda60SSimon J. Gerraty.endif 93*3cbdda60SSimon J. Gerraty 94*3cbdda60SSimon J. Gerraty.if ${UPDATE_DEPENDFILE:tl} == "yes" 95*3cbdda60SSimon J. Gerraty# sometimes we want .meta files generated to aid debugging/error detection 96*3cbdda60SSimon J. Gerraty# but do not want to consider them for dependencies 97*3cbdda60SSimon J. Gerraty# for example the result of running configure 98*3cbdda60SSimon J. Gerraty# just make sure this is not empty 99*3cbdda60SSimon J. GerratyMETA_FILE_FILTER ?= N.meta 100*3cbdda60SSimon J. Gerraty 101*3cbdda60SSimon J. Gerraty.if !empty(DPADD) 102*3cbdda60SSimon J. Gerraty# if we have any non-libs in DPADD, 103*3cbdda60SSimon J. Gerraty# they probably need to be paid attention to 104*3cbdda60SSimon J. Gerraty.if !empty(DPLIBS) 105*3cbdda60SSimon J. GerratyFORCE_DPADD = ${DPADD:${DPLIBS:${M_ListToSkip}}:${DPADD_LAST:${M_ListToSkip}}} 106*3cbdda60SSimon J. Gerraty.else 107*3cbdda60SSimon J. Gerraty_nonlibs := ${DPADD:T:Nlib*:N*include} 108*3cbdda60SSimon J. Gerraty.if !empty(_nonlibs) 109*3cbdda60SSimon J. GerratyFORCE_DPADD += ${_nonlibs:@x@${DPADD:M*/$x}@} 110*3cbdda60SSimon J. Gerraty.endif 111*3cbdda60SSimon J. Gerraty.endif 112*3cbdda60SSimon J. Gerraty.endif 113*3cbdda60SSimon J. Gerraty 114*3cbdda60SSimon J. Gerraty.if !make(gendirdeps) 115*3cbdda60SSimon J. Gerraty.END: gendirdeps 116*3cbdda60SSimon J. Gerraty.endif 117*3cbdda60SSimon J. Gerraty 118*3cbdda60SSimon J. Gerraty# if we don't have OBJS, then .depend isn't useful 119*3cbdda60SSimon J. Gerraty.if !target(.depend) && (!empty(OBJS) || ${.ALLTARGETS:M*.o} != "") 120*3cbdda60SSimon J. Gerraty# some makefiles and/or targets contain 121*3cbdda60SSimon J. Gerraty# circular dependencies if you dig too deep 122*3cbdda60SSimon J. Gerraty# (as meta mode is apt to do) 123*3cbdda60SSimon J. Gerraty# so we provide a means of supressing them. 124*3cbdda60SSimon J. Gerraty# the input to the loop below is target: dependency 125*3cbdda60SSimon J. Gerraty# with just one dependency per line. 126*3cbdda60SSimon J. Gerraty# Also some targets are not really local, or use random names. 127*3cbdda60SSimon J. Gerraty# Use local.autodep.mk to provide local additions! 128*3cbdda60SSimon J. GerratySUPPRESS_DEPEND += \ 129*3cbdda60SSimon J. Gerraty ${SB:S,/,_,g}* \ 130*3cbdda60SSimon J. Gerraty *:y.tab.c \ 131*3cbdda60SSimon J. Gerraty *.c:*.c \ 132*3cbdda60SSimon J. Gerraty *.h:*.h 133*3cbdda60SSimon J. Gerraty 134*3cbdda60SSimon J. Gerraty.NOPATH: .depend 135*3cbdda60SSimon J. Gerraty# we use ${.MAKE.META.CREATED} to trigger an update but 136*3cbdda60SSimon J. Gerraty# we process using ${.MAKE.META.FILES} 137*3cbdda60SSimon J. Gerraty# the double $$ defers initial evaluation 138*3cbdda60SSimon J. Gerraty# if necessary, we fake .po dependencies, just so the result 139*3cbdda60SSimon J. Gerraty# in Makefile.depend* is stable 140*3cbdda60SSimon J. Gerraty# The current objdir may be refered to in various ways 141*3cbdda60SSimon J. GerratyOBJDIR_REFS += ${.OBJDIR} ${.OBJDIR:tA} ${_OBJDIR} ${RELOBJTOP}/${RELDIR} 142*3cbdda60SSimon J. Gerraty_depend = .depend 143*3cbdda60SSimon J. Gerraty# it would be nice to be able to get .SUFFIXES as ${.SUFFIXES} 144*3cbdda60SSimon J. Gerraty# we actually only care about the .SUFFIXES of files that might be 145*3cbdda60SSimon J. Gerraty# generated by tools like yacc. 146*3cbdda60SSimon J. GerratyDEPEND_SUFFIXES += .c .h .cpp .hpp .cxx .hxx .cc .hh 147*3cbdda60SSimon J. Gerraty.depend: .NOMETA $${.MAKE.META.CREATED} ${_this} 148*3cbdda60SSimon J. Gerraty @echo "Updating $@: ${.OODATE:T:[1..8]}" 149*3cbdda60SSimon J. Gerraty @egrep -i '^R .*\.(${DEPEND_SUFFIXES:tl:O:u:S,^.,,:ts|})$$' /dev/null ${.MAKE.META.FILES:T:O:u:${META_FILE_FILTER:ts:}:M*o.meta} | \ 150*3cbdda60SSimon J. Gerraty sed -e 's, \./, ,${OBJDIR_REFS:O:u:@d@;s, $d/, ,@};/\//d' \ 151*3cbdda60SSimon J. Gerraty -e 's,^\([^/][^/]*\).meta...[0-9]* ,\1: ,' | \ 152*3cbdda60SSimon J. Gerraty sort -u | \ 153*3cbdda60SSimon J. Gerraty while read t d; do \ 154*3cbdda60SSimon J. Gerraty case "$$d:" in $$t) continue;; esac; \ 155*3cbdda60SSimon J. Gerraty case "$$t$$d" in ${SUPPRESS_DEPEND:U.:O:u:ts|}) continue;; esac; \ 156*3cbdda60SSimon J. Gerraty echo $$t $$d; \ 157*3cbdda60SSimon J. Gerraty done > $@.${.MAKE.PID} 158*3cbdda60SSimon J. Gerraty @case "${.MAKE.META.FILES:T:M*.po.*}" in \ 159*3cbdda60SSimon J. Gerraty *.po.*) mv $@.${.MAKE.PID} $@;; \ 160*3cbdda60SSimon J. Gerraty *) { cat $@.${.MAKE.PID}; \ 161*3cbdda60SSimon J. Gerraty sed 's,\.So:,.o:,;s,\.o:,.po:,' $@.${.MAKE.PID}; } | sort -u > $@; \ 162*3cbdda60SSimon J. Gerraty rm -f $@.${.MAKE.PID};; \ 163*3cbdda60SSimon J. Gerraty esac 164*3cbdda60SSimon J. Gerraty.else 165*3cbdda60SSimon J. Gerraty# make sure this exists 166*3cbdda60SSimon J. Gerraty.depend: 167*3cbdda60SSimon J. Gerraty# do _not_ assume that .depend is in any fit state for us to use 168*3cbdda60SSimon J. GerratyCAT_DEPEND = /dev/null 169*3cbdda60SSimon J. Gerraty.if ${.MAKE.LEVEL} > 0 170*3cbdda60SSimon J. Gerraty.export CAT_DEPEND 171*3cbdda60SSimon J. Gerraty.endif 172*3cbdda60SSimon J. Gerraty_depend = 173*3cbdda60SSimon J. Gerraty.endif 174*3cbdda60SSimon J. Gerraty 175*3cbdda60SSimon J. Gerraty.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != "" 176*3cbdda60SSimon J. Gerraty.info ${_DEPENDFILE:S,${SRCTOP}/,,} _depend=${_depend} 177*3cbdda60SSimon J. Gerraty.endif 178*3cbdda60SSimon J. Gerraty 179*3cbdda60SSimon J. Gerratygendirdeps: ${_DEPENDFILE} 180*3cbdda60SSimon J. Gerraty 181*3cbdda60SSimon J. Gerraty.if !target(${_DEPENDFILE}) 182*3cbdda60SSimon J. Gerraty.if ${_bootstrap_dirdeps} == "yes" 183*3cbdda60SSimon J. Gerraty# We are boot-strapping a new directory 184*3cbdda60SSimon J. Gerraty# Use DPADD to seed DIRDEPS 185*3cbdda60SSimon J. Gerraty.if !empty(DPADD) 186*3cbdda60SSimon J. Gerraty# anything which matches ${_OBJROOT}* but not ${_OBJTOP}* 187*3cbdda60SSimon J. Gerraty# needs to be qualified in DIRDEPS 188*3cbdda60SSimon J. Gerraty# The pseudo machine "host" is used for HOST_TARGET 189*3cbdda60SSimon J. GerratyDIRDEPS = \ 190*3cbdda60SSimon J. Gerraty ${DPADD:M${_OBJTOP}*:H:C,${_OBJTOP}[^/]*/,,:N.:O:u} \ 191*3cbdda60SSimon J. Gerraty ${DPADD:M${_OBJROOT}*:N${_OBJTOP}*:H:S,${_OBJROOT},,:C,^([^/]+)/(.*),\2.\1,:S,${HOST_TARGET}$,host,:N.*:O:u} 192*3cbdda60SSimon J. Gerraty 193*3cbdda60SSimon J. Gerraty.endif 194*3cbdda60SSimon J. Gerraty.endif 195*3cbdda60SSimon J. Gerraty 196*3cbdda60SSimon J. Gerraty_gendirdeps_mutex = 197*3cbdda60SSimon J. Gerraty.if defined(NEED_GENDIRDEPS_MUTEX) 198*3cbdda60SSimon J. Gerraty# If a src dir gets built with multiple object dirs, 199*3cbdda60SSimon J. Gerraty# we need a mutex. Obviously, this is best avoided. 200*3cbdda60SSimon J. Gerraty# Note if .MAKE.DEPENDFILE is common for all ${MACHINE} 201*3cbdda60SSimon J. Gerraty# you either need to mutex, or ensure only one machine builds at a time! 202*3cbdda60SSimon J. Gerraty# lockf is an example of a suitable tool 203*3cbdda60SSimon J. GerratyLOCKF ?= /usr/bin/lockf 204*3cbdda60SSimon J. Gerraty.if exists(${LOCKF}) 205*3cbdda60SSimon J. GerratyGENDIRDEPS_MUTEXER ?= ${LOCKF} -k 206*3cbdda60SSimon J. Gerraty.endif 207*3cbdda60SSimon J. Gerraty.if empty(GENDIRDEPS_MUTEXER) 208*3cbdda60SSimon J. Gerraty.error NEED_GENDIRDEPS_MUTEX defined, but GENDIRDEPS_MUTEXER not set 209*3cbdda60SSimon J. Gerraty.else 210*3cbdda60SSimon J. Gerraty_gendirdeps_mutex = ${GENDIRDEPS_MUTEXER} ${GENDIRDEPS_MUTEX:U${_CURDIR}/Makefile} 211*3cbdda60SSimon J. Gerraty.endif 212*3cbdda60SSimon J. Gerraty.endif 213*3cbdda60SSimon J. Gerraty 214*3cbdda60SSimon J. Gerraty# If we have META_XTRAS we most likely did not create them 215*3cbdda60SSimon J. Gerraty# but we need to behave as if we did. 216*3cbdda60SSimon J. Gerraty# Avoid adding glob patterns to .MAKE.META.CREATED though. 217*3cbdda60SSimon J. Gerraty.MAKE.META.CREATED += ${META_XTRAS:N*\**:O:u} 218*3cbdda60SSimon J. Gerraty 219*3cbdda60SSimon J. Gerraty.if make(gendirdeps) 220*3cbdda60SSimon J. GerratyMETA_FILES = *.meta 221*3cbdda60SSimon J. Gerraty.elif ${OPTIMIZE_OBJECT_META_FILES:Uno:tl} == "no" 222*3cbdda60SSimon J. GerratyMETA_FILES = ${.MAKE.META.FILES:T:N.depend*:O:u} 223*3cbdda60SSimon J. Gerraty.else 224*3cbdda60SSimon J. Gerraty# if we have 1000's of .o.meta, .So.meta etc we need only look at one set 225*3cbdda60SSimon J. Gerraty# it is left as an exercise for the reader to work out what this does 226*3cbdda60SSimon J. GerratyMETA_FILES = ${.MAKE.META.FILES:T:N.depend*:N*o.meta:O:u} \ 227*3cbdda60SSimon J. Gerraty ${.MAKE.META.FILES:T:M*.${.MAKE.META.FILES:M*o.meta:R:E:O:u:[1]}.meta:O:u} 228*3cbdda60SSimon J. Gerraty.endif 229*3cbdda60SSimon J. Gerraty 230*3cbdda60SSimon J. Gerraty.if ${DEBUG_AUTODEP:Uno:@m@${RELDIR:M$m}@} != "" 231*3cbdda60SSimon J. Gerraty.info ${_DEPENDFILE:S,${SRCTOP}/,,}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} xtras=${META_XTRAS} 232*3cbdda60SSimon J. Gerraty.endif 233*3cbdda60SSimon J. Gerraty 234*3cbdda60SSimon J. Gerraty.if ${.MAKE.LEVEL} > 0 && !empty(GENDIRDEPS_FILTER) 235*3cbdda60SSimon J. Gerraty.export GENDIRDEPS_FILTER 236*3cbdda60SSimon J. Gerraty.endif 237*3cbdda60SSimon J. Gerraty 238*3cbdda60SSimon J. Gerraty# we might have .../ in MAKESYSPATH 239*3cbdda60SSimon J. Gerraty_makesyspath:= ${_PARSEDIR} 240*3cbdda60SSimon J. Gerraty${_DEPENDFILE}: ${_depend} ${.PARSEDIR}/gendirdeps.mk ${META2DEPS} $${.MAKE.META.CREATED} 241*3cbdda60SSimon J. Gerraty @echo Checking $@: ${.OODATE:T:[1..8]} 242*3cbdda60SSimon J. Gerraty @(cd . && \ 243*3cbdda60SSimon J. Gerraty SKIP_GENDIRDEPS='${SKIP_GENDIRDEPS:O:u}' \ 244*3cbdda60SSimon J. Gerraty DPADD='${FORCE_DPADD:O:u}' ${_gendirdeps_mutex} \ 245*3cbdda60SSimon J. Gerraty MAKESYSPATH=${_makesyspath} \ 246*3cbdda60SSimon J. Gerraty ${.MAKE} -f gendirdeps.mk RELDIR=${RELDIR} _DEPENDFILE=${_DEPENDFILE} \ 247*3cbdda60SSimon J. Gerraty META_FILES='${META_XTRAS:T:O:u} ${META_FILES:T:O:u:${META_FILE_FILTER:ts:}}') 248*3cbdda60SSimon J. Gerraty @test -s $@ && touch $@; : 249*3cbdda60SSimon J. Gerraty.endif 250*3cbdda60SSimon J. Gerraty 251*3cbdda60SSimon J. Gerraty.endif 252*3cbdda60SSimon J. Gerraty.endif 253*3cbdda60SSimon J. Gerraty 254*3cbdda60SSimon J. Gerraty.if ${_bootstrap_dirdeps} == "yes" 255*3cbdda60SSimon J. Gerraty# make sure this is included at least once 256*3cbdda60SSimon J. Gerraty.include <dirdeps.mk> 257*3cbdda60SSimon J. Gerraty.else 258*3cbdda60SSimon J. Gerraty${_DEPENDFILE}: .PRECIOUS 259*3cbdda60SSimon J. Gerraty.endif 260*3cbdda60SSimon J. Gerraty 261*3cbdda60SSimon J. GerratyCLEANFILES += *.meta filemon.* *.db 262*3cbdda60SSimon J. Gerraty.endif 263