1# $Id: dirdeps.mk,v 1.22 2012/04/25 15:12:29 sjg Exp $ 2 3# Copyright (c) 2010-2012, Juniper Networks, Inc. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions 7# are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 26# Much of the complexity here is for supporting cross-building. 27# If a tree does not support that, simply using plain Makefile.depend 28# should provide sufficient clue. 29# Otherwise the recommendation is to use Makefile.depend.${MACHINE} 30# as expected below. 31 32# Note: this file gets multiply included. 33# This is what we do with DIRDEPS 34 35# DIRDEPS: 36# This is a list of directories - relative to SRCTOP, it is only 37# of interest to .MAKE.LEVEL 0. 38# In some cases the entry may be qualified with a .<machine> 39# suffix, for example to force building something for the pseudo 40# machines "host" or "common" regardless of current ${MACHINE}. 41# All unqualified entries end up being qualified with .${MACHINE} 42# and _DIRDEPS_USE below, uses the suffix to set MACHINE 43# correctly when visiting each entry. 44# 45# Each entry is also converted into a set of paths to look for 46# Makefile.depend.<machine> to learn the dependencies of each. 47# Each Makefile.depend.<machine> sets DEP_RELDIR to be the 48# the RELDIR (path relative to SRCTOP) for its directory, and 49# DEP_MACHINE to its suffix (<machine>), further since 50# each Makefile.depend.<machine> includes dirdeps.mk, this 51# processing is recursive and results in .MAKE.LEVEL 0 learning the 52# dependencies of the tree wrt the initial directory (_DEP_RELDIR). 53# 54# BUILD_AT_LEVEL0 55# Indicates whether .MAKE.LEVEL 0 builds anything: 56# if "no" sub-makes are used to build everything, 57# if "yes" sub-makes are only used to build for other machines. 58 59.if ${.MAKE.LEVEL} == 0 60# only the first instance is interested in all this 61 62# First off, we want to know what ${MACHINE} to build for. 63# This can be complicated if we are using a mixture of ${MACHINE} specific 64# and non-specific Makefile.depend* 65 66.if !target(_DIRDEP_USE) 67# do some setup we only need once 68_CURDIR ?= ${.CURDIR} 69 70.if !defined(.MAKE.DEPENDFILE_PREFERENCE) 71# this makes the logic below neater? 72.MAKE.DEPENDFILE_PREFERENCE = ${_CURDIR}/${.MAKE.DEPENDFILE:T} 73.if ${.MAKE.DEPENDFILE:E} == "${MACHINE}" 74.MAKE.DEPENDFILE_PREFERENCE += ${_CURDIR}/${.MAKE.DEPENDFILE:T:R} 75.endif 76.endif 77 78_default_dependfile := ${.MAKE.DEPENDFILE_PREFERENCE:[1]:T} 79_machine_dependfiles := ${.MAKE.DEPENDFILE_PREFERENCE:M*.${MACHINE}} 80 81# for machine specific dependfiles we require ${MACHINE} to be at the end 82# also for the sake of sanity we require a common prefix 83.if !defined(.MAKE.DEPENDFILE_PREFIX) 84.if !empty(_machine_dependfiles) 85.MAKE.DEPENDFILE_PREFIX := ${_machine_dependfiles:[1]:T:R} 86.else 87.MAKE.DEPENDFILE_PREFIX := ${_default_dependfile:T} 88.endif 89.endif 90 91 92# this is how we identify non-machine specific dependfiles 93N_notmachine := ${.MAKE.DEPENDFILE_PREFERENCE:E:N${MACHINE}:${M_ListToSkip}} 94 95.endif # !target(_DIRDEP_USE) 96 97_last_dependfile := ${.MAKE.MAKEFILES:M*/${.MAKE.DEPENDFILE_PREFIX}*:[-1]} 98 99# Note: if a makefile is read many times, the above 100# will not work, so we also test for DEP_MACHINE==depend below. 101.if empty(_last_dependfile) 102# we haven't included one yet 103DEP_MACHINE ?= ${TARGET_MACHINE:U${MACHINE}} 104# else it should be correctly set by ${.MAKE.DEPENDFILE} 105.elif ${_last_dependfile:E:${N_notmachine}} == "" || ${DEP_MACHINE:Uno:${N_notmachine}} == "" 106# don't rely on manually maintained files to be correct 107DEP_MACHINE := ${_DEP_MACHINE:U${MACHINE}} 108.else 109# just in case 110DEP_MACHINE ?= ${_last_dependfile:E} 111.endif 112 113# pickup customizations 114# as below you can use !target(_DIRDEP_USE) to protect things 115# which should only be done once. 116.-include "local.dirdeps.mk" 117 118# the first time we are included the _DIRDEP_USE target will not be defined 119# we can use this as a clue to do initialization and other one time things. 120.if !target(_DIRDEP_USE) 121# make sure this target exists 122dirdeps: 123 124# We normally expect to be included by Makefile.depend.* 125# which sets the DEP_* macros below. 126DEP_RELDIR ?= ${RELDIR} 127 128# this can cause lots of output! 129# set to a set of glob expressions that might match RELDIR 130DEBUG_DIRDEPS ?= no 131 132# remember the initial value of DEP_RELDIR - we test for it below. 133_DEP_RELDIR := ${DEP_RELDIR} 134 135# things we skip for host tools 136SKIP_HOSTDIR ?= 137 138NSkipHostDir = ${SKIP_HOSTDIR:N*.host:S,$,.host,:N.host:${M_ListToSkip}} 139NSkipHostDep = ${SKIP_HOSTDIR:R:@d@*/$d*.host@:${M_ListToSkip}} 140 141# things we always skip 142# SKIP_DIRDEPS allows for adding entries on command line. 143SKIP_DIR += .host *.WAIT ${SKIP_DIRDEPS} 144 145.ifdef HOSTPROG 146SKIP_DIR += ${SKIP_HOSTDIR} 147.endif 148 149NSkipDir = ${SKIP_DIR:${M_ListToSkip}} 150 151.if defined(NO_DIRDEPS) || defined(NODIRDEPS) 152# confine ourselves to the original dir 153DIRDEPS_FILTER += M${_DEP_RELDIR}* 154.endif 155 156# we supress SUBDIR when visiting the leaves 157# we assume sys.mk will set MACHINE_ARCH 158_DIRDEP_USE: .USE .MAKE 159 @for m in ${.MAKE.MAKEFILE_PREFERENCE}; do \ 160 test -s ${.TARGET:R}/$$m || continue; \ 161 echo "${TRACER}Checking ${.TARGET:R} for ${.TARGET:E} ..."; \ 162 MACHINE=${.TARGET:E} MACHINE_ARCH= NO_SUBDIR=1 \ 163 ${.MAKE} -C ${.TARGET:R} || exit 1; \ 164 break; \ 165 done 166 167.ifdef ALL_MACHINES 168# this is how you limit it to only the machines we have been built for 169# previously. 170.if empty(ONLY_MACHINE_LIST) 171.if !empty(ALL_MACHINE_LIST) 172# ALL_MACHINE_LIST is the list of all legal machines - ignore anything else 173_machine_list != cd ${_CURDIR} && 'ls' -1 ${ALL_MACHINE_LIST:O:u:@m@${.MAKE.DEPENDFILE:T:R}.$m@} 2> /dev/null; echo 174.else 175_machine_list != 'ls' -1 ${_CURDIR}/${.MAKE.DEPENDFILE_PREFIX}.* 2> /dev/null; echo 176.endif 177_only_machines := ${_machine_list:${NIgnoreFiles:UN*.bak}:E:O:u} 178.else 179_only_machines := ${ONLY_MACHINE_LIST} 180.endif 181 182.if empty(_only_machines) 183# we must be boot-strapping 184_only_machines := ${TARGET_MACHINE:U${ALL_MACHINE_LIST:U${DEP_MACHINE}}} 185.endif 186 187.else # ! ALL_MACHINES 188# if ONLY_MACHINE_LIST is set, we are limited to that 189# if TARGET_MACHINE is set - it is really the same as ONLY_MACHINE_LIST 190# otherwise DEP_MACHINE is it - so DEP_MACHINE will match. 191_only_machines := ${ONLY_MACHINE_LIST:U${TARGET_MACHINE:U${DEP_MACHINE}}:M${DEP_MACHINE}} 192.endif 193 194.if !empty(NOT_MACHINE_LIST) 195_only_machines := ${_only_machines:${NOT_MACHINE_LIST:${M_ListToSkip}}} 196.endif 197 198# make sure we have a starting place? 199DIRDEPS ?= ${RELDIR} 200.endif # target 201 202_debug_reldir := ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.${DEP_MACHINE}:L:M$x}@} 203_debug_search := ${DEBUG_DIRDEPS:@x@${DEP_RELDIR:M$x}${${DEP_RELDIR}.depend:L:M$x}@} 204 205# the rest is done repeatedly for every Makefile.depend we read. 206# if we are anything but the original dir we care only about the 207# machine type we were included for.. 208 209.if ${DEP_RELDIR} == "." 210_this_dir := ${SRCTOP} 211.else 212_this_dir := ${SRCTOP}/${DEP_RELDIR} 213.endif 214 215# on rare occasions, there can be a need for extra help 216_dep_hack := ${_this_dir}/${.MAKE.DEPENDFILE_PREFIX}.inc 217.-include "${_dep_hack}" 218 219.if ${DEP_RELDIR} != ${_DEP_RELDIR} || ${DEP_MACHINE} != ${MACHINE} 220# this should be all 221_machines := ${DEP_MACHINE} 222.else 223# this is the machine list we actually use below 224_machines := ${_only_machines} 225 226.if defined(HOSTPROG) || ${DEP_MACHINE} == "host" 227# we need to build this guy's dependencies for host as well. 228_machines += host 229.endif 230 231_machines := ${_machines:O:u} 232.endif 233 234_build_dirs = 235 236.if ${DEP_RELDIR} == ${_DEP_RELDIR} 237# pickup other machines for this dir if necessary 238.if ${BUILD_AT_LEVEL0:Uyes} == "no" 239_build_dirs += ${_machines:@m@${_CURDIR}.$m@} 240.else 241_build_dirs += ${_machines:N${DEP_MACHINE}:@m@${_CURDIR}.$m@} 242.if ${DEP_MACHINE} == ${MACHINE} 243# pickup local dependencies now 244.-include <.depend> 245.endif 246.endif 247.endif 248 249.if !empty(_debug_reldir) 250.info ${DEP_RELDIR}.${DEP_MACHINE}: _last_dependfile='${_last_dependfile}' 251.info ${DEP_RELDIR}.${DEP_MACHINE}: DIRDEPS='${DIRDEPS}' 252.info ${DEP_RELDIR}.${DEP_MACHINE}: _machines='${_machines}' 253.endif 254 255.if !empty(DIRDEPS) 256 257# this is what we start with 258__depdirs := ${DIRDEPS:${NSkipDir}:${DIRDEPS_FILTER:ts:}:O:u:@d@${SRCTOP}/$d@} 259 260# some entries may be qualified with .<machine> 261# the :M*/*/*.* just tries to limit the dirs we check to likely ones. 262# the ${d:E:M*/*} ensures we don't consider junos/usr.sbin/mgd 263__qual_depdirs := ${__depdirs:M*/*/*.*:@d@${exists($d):?:${"${d:E:M*/*}":?:${exists(${d:R}):?$d:}}}@} 264__unqual_depdirs := ${__depdirs:${__qual_depdirs:Uno:${M_ListToSkip}}} 265 266.if ${DEP_RELDIR} == ${_DEP_RELDIR} 267# if it was called out - we likely need it. 268__hostdpadd := ${DPADD:U.:M${HOST_OBJTOP}/*:S,${HOST_OBJTOP}/,,:H:${NSkipDir}:${DIRDEPS_FILTER:ts:}:S,$,.host,:N.*:@d@${SRCTOP}/$d@} 269__qual_depdirs += ${__hostdpadd} 270.endif 271 272.if !empty(_debug_reldir) 273.info depdirs=${__depdirs} 274.info qualified=${__qual_depdirs} 275.info unqualified=${__unqual_depdirs} 276.endif 277 278# _build_dirs is what we will feed to _DIRDEP_USE 279_build_dirs += \ 280 ${__qual_depdirs:M*.host:${NSkipHostDir}:N.host} \ 281 ${__qual_depdirs:N*.host} \ 282 ${_machines:@m@${__unqual_depdirs:@d@$d.$m@}@} 283 284_build_dirs := ${_build_dirs:O:u} 285 286# this is where we will pick up more dependencies from 287# the inner inline loops look complex, but save a significant 288# amount of memory compared to a .for loop. 289_depdir_files = 290.for d in ${_build_dirs} 291.if exists($d) 292# easy, we're building for ${MACHINE} 293_depdir_files += ${.MAKE.DEPENDFILE_PREFERENCE:T:@m@${exists($d/$m):?$d/$m:}@:[1]} 294.elif exists(${d:R}) && ${d:R:T} == ${d:T:R} 295# a little more complex - building for another machine 296# we will ensure the file is qualified with a machine 297# so that if necessary _DEP_MACHINE can be set below 298_depdir_files += ${.MAKE.DEPENDFILE_PREFERENCE:T:S,.${MACHINE}$,.${d:E},:@m@${exists(${d:R}/$m):?${d:R}/$m:}@:[1]:@m@${"${m:M*.${d:E}}":?$m:$m.${d:E}}@} 299.endif 300.endfor 301 302# clean up 303_depdir_files := ${_depdir_files:O:u} 304 305.endif # empty DIRDEPS 306 307# Normally if doing make -V something, 308# we do not want to waste time chasing DIRDEPS 309# but if we want to count the number of Makefile.depend* read, we do. 310.if ${.MAKEFLAGS:M-V${_V_READ_DIRDEPS}} == "" 311.if !empty(_build_dirs) 312# this makes it all happen 313dirdeps: ${_build_dirs} 314${_build_dirs}: _DIRDEP_USE 315 316.if !empty(_debug_reldir) 317.info ${DEP_RELDIR}.${DEP_MACHINE}: ${_build_dirs} 318.endif 319 320.for m in ${_machines} 321# it would be nice to do :N${.TARGET} 322.if !empty(__qual_depdirs) 323.for q in ${__qual_depdirs:E:O:u:N$m} 324.if !empty(_debug_reldir) || ${DEBUG_DIRDEPS:@x@${${DEP_RELDIR}.$m:L:M$x}${${DEP_RELDIR}.$q:L:M$x}@} != "" 325.info ${DEP_RELDIR}.$m: ${_build_dirs:M*.$q} 326.endif 327${_this_dir}.$m: ${_build_dirs:M*.$q} 328.endfor 329.endif 330.if !empty(_debug_reldir) 331.info ${DEP_RELDIR}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m} 332.endif 333${_this_dir}.$m: ${_build_dirs:M*.$m:N${_this_dir}.$m} 334.endfor 335 336.endif 337 338.for d in ${_depdir_files} 339.if ${.MAKE.MAKEFILES:M${d}} == "" 340.if !empty(_debug_search) 341.info Looking for $d 342.endif 343.if exists($d) 344.include <$d> 345.elif exists(${d:R}) 346# an unqualified file exists, we qualified it above so we can set _DEP_MACHINE 347# it might be manually maintained and shared by all machine types 348# tell it the machine we are interested in. 349_DEP_MACHINE := ${d:E} 350.if !empty(_debug_reldir) 351.info loading ${d:R} for ${_DEP_MACHINE} 352.endif 353# pretend we read $d, so we don't come by here again. 354.MAKE.MAKEFILES += $d 355.include <${d:R}> 356.endif 357.endif 358.endfor 359.endif # -V 360 361.elif ${.MAKE.LEVEL} > 42 362.error You should have stopped recursing by now. 363.else 364_DEP_RELDIR := ${DEP_RELDIR} 365# pickup local dependencies 366.-include <.depend> 367.endif 368 369