xref: /freebsd/share/mk/sys.dirdeps.mk (revision f697b9d03d998da8ec69f2028dd7702a42adc3fc)
1f974ced3SSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
2f974ced3SSimon J. Gerraty#
3*f697b9d0SSimon J. Gerraty# $Id: sys.dirdeps.mk,v 1.15 2024/04/18 17:18:31 sjg Exp $
4bee3d4bfSSimon J. Gerraty#
5bee3d4bfSSimon J. Gerraty#	@(#) Copyright (c) 2012-2023, Simon J. Gerraty
6bee3d4bfSSimon J. Gerraty#
7bee3d4bfSSimon J. Gerraty#	This file is provided in the hope that it will
8bee3d4bfSSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
9bee3d4bfSSimon J. Gerraty#	Permission to copy, redistribute or otherwise
10bee3d4bfSSimon J. Gerraty#	use this file is hereby granted provided that
11bee3d4bfSSimon J. Gerraty#	the above copyright notice and this notice are
12bee3d4bfSSimon J. Gerraty#	left intact.
13bee3d4bfSSimon J. Gerraty#
14bee3d4bfSSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
15bee3d4bfSSimon J. Gerraty#	sjg@crufty.net
16bee3d4bfSSimon J. Gerraty#
17bee3d4bfSSimon J. Gerraty
18bee3d4bfSSimon J. Gerraty# Originally DIRDEPS_BUILD and META_MODE were the same thing.
19bee3d4bfSSimon J. Gerraty# So, much of this was done in *meta.sys.mk and local*mk
20bee3d4bfSSimon J. Gerraty# but properly belongs here.
21bee3d4bfSSimon J. Gerraty
22bee3d4bfSSimon J. Gerraty# Include from [local.]sys.mk - if doing DIRDEPS_BUILD
23bee3d4bfSSimon J. Gerraty# we should not be here otherwise
24bee3d4bfSSimon J. GerratyMK_DIRDEPS_BUILD ?= yes
25bee3d4bfSSimon J. Gerraty# these are all implied
26bee3d4bfSSimon J. GerratyMK_AUTO_OBJ ?= yes
27bee3d4bfSSimon J. GerratyMK_META_MODE ?= yes
28bee3d4bfSSimon J. GerratyMK_STAGING ?= yes
29bee3d4bfSSimon J. Gerraty
30bee3d4bfSSimon J. Gerraty_PARSEDIR ?= ${.PARSEDIR:tA}
31bee3d4bfSSimon J. Gerraty
32bee3d4bfSSimon J. Gerraty.-include <local.sys.dirdeps.env.mk>
33bee3d4bfSSimon J. Gerraty
34bee3d4bfSSimon J. Gerraty.if ${.MAKE.LEVEL} == 0
35bee3d4bfSSimon J. Gerraty# make sure dirdeps target exists and do it first
36f974ced3SSimon J. Gerraty# init.mk will set .MAIN to 'dirdeps' if appropriate
37f974ced3SSimon J. Gerraty# as will dirdeps-targets.mk for top-level builds.
38f974ced3SSimon J. Gerraty# This allows a Makefile to have more control.
39bee3d4bfSSimon J. Gerratydirdeps:
40bee3d4bfSSimon J. Gerraty.NOPATH: dirdeps
41bee3d4bfSSimon J. Gerratyall: dirdeps .WAIT
42bee3d4bfSSimon J. Gerraty.endif
43bee3d4bfSSimon J. Gerraty
44bee3d4bfSSimon J. Gerraty.if empty(SRCTOP)
45bee3d4bfSSimon J. Gerraty# fallback assumes share/mk!
46bee3d4bfSSimon J. GerratySRCTOP := ${SB_SRC:U${.PARSEDIR:tA:H:H}}
47bee3d4bfSSimon J. Gerraty.export SRCTOP
48bee3d4bfSSimon J. Gerraty.endif
49bee3d4bfSSimon J. Gerraty
50bee3d4bfSSimon J. Gerraty# fake SB if not using mk wrapper
517d86c8b8SSimon J. Gerraty# SB documented at http://www.crufty.net/sjg/docs/sb-tools.htm
52bee3d4bfSSimon J. Gerraty.if !defined(SB)
53bee3d4bfSSimon J. GerratySB := ${SRCTOP:H}
54bee3d4bfSSimon J. Gerraty.export SB
55bee3d4bfSSimon J. Gerraty.endif
56bee3d4bfSSimon J. Gerraty
57bee3d4bfSSimon J. Gerraty.if empty(OBJROOT)
58bee3d4bfSSimon J. GerratyOBJROOT := ${SB_OBJROOT:U${MAKEOBJDIRPREFIX:U${SB}/obj}/}
59bee3d4bfSSimon J. Gerraty.export OBJROOT
60bee3d4bfSSimon J. Gerraty.endif
617d86c8b8SSimon J. Gerraty# we expect OBJROOT to end with / (- can work too)
627d86c8b8SSimon J. Gerraty.if ${OBJROOT:M*[/-]} == ""
637d86c8b8SSimon J. GerratyOBJROOT := ${OBJROOT}/
647d86c8b8SSimon J. Gerraty.endif
65bee3d4bfSSimon J. Gerraty
66bee3d4bfSSimon J. Gerraty.if empty(STAGE_ROOT)
67bee3d4bfSSimon J. GerratySTAGE_ROOT ?= ${OBJROOT}stage
68bee3d4bfSSimon J. Gerraty.export STAGE_ROOT
69bee3d4bfSSimon J. Gerraty.endif
70bee3d4bfSSimon J. Gerraty
71bee3d4bfSSimon J. Gerraty# We should be included before meta.sys.mk
72bee3d4bfSSimon J. Gerraty# If TARGET_SPEC_VARS is other than just MACHINE
73bee3d4bfSSimon J. Gerraty# it should be set by now.
74bee3d4bfSSimon J. Gerraty# TARGET_SPEC must not contain any '.'s.
75bee3d4bfSSimon J. GerratyTARGET_SPEC_VARS ?= MACHINE
76bee3d4bfSSimon J. Gerraty
77bee3d4bfSSimon J. Gerraty.if ${TARGET_SPEC:Uno:M*,*} != ""
78bee3d4bfSSimon J. Gerraty# deal with TARGET_SPEC from env
79bee3d4bfSSimon J. Gerraty_tspec := ${TARGET_SPEC:S/,/ /g}
80bee3d4bfSSimon J. Gerraty.for i in ${TARGET_SPEC_VARS:${M_RANGE:Urange}}
81bee3d4bfSSimon J. Gerraty${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
82bee3d4bfSSimon J. Gerraty.endfor
83bee3d4bfSSimon J. Gerraty# We need to stop that TARGET_SPEC affecting any submakes
84bee3d4bfSSimon J. GerratyTARGET_SPEC=
85bee3d4bfSSimon J. Gerraty# so export but do not track
86bee3d4bfSSimon J. Gerraty.export-env TARGET_SPEC
87bee3d4bfSSimon J. Gerraty.export ${TARGET_SPEC_VARS}
88bee3d4bfSSimon J. Gerraty.for v in ${TARGET_SPEC_VARS:O:u}
89bee3d4bfSSimon J. Gerraty.if empty($v)
90bee3d4bfSSimon J. Gerraty.undef $v
91bee3d4bfSSimon J. Gerraty.endif
92bee3d4bfSSimon J. Gerraty.endfor
93bee3d4bfSSimon J. Gerraty.endif
94bee3d4bfSSimon J. Gerraty
95bee3d4bfSSimon J. Gerraty# Now make sure we know what TARGET_SPEC is
96bee3d4bfSSimon J. Gerraty# as we may need it to find Makefile.depend*
97bee3d4bfSSimon J. Gerraty.if ${MACHINE:Mhost*} != ""
98bee3d4bfSSimon J. Gerraty# host is special
99bee3d4bfSSimon J. GerratyTARGET_SPEC = ${MACHINE}
100bee3d4bfSSimon J. Gerraty.else
101bee3d4bfSSimon J. GerratyTARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
102bee3d4bfSSimon J. Gerraty.endif
103bee3d4bfSSimon J. Gerraty
104bee3d4bfSSimon J. Gerraty.if ${TARGET_SPEC_VARS:[#]} > 1
1057d86c8b8SSimon J. GerratyTARGET_SPEC_VARSr := ${TARGET_SPEC_VARS:[-1..1]}
1067d86c8b8SSimon J. Gerraty# alternatives might be
1077d86c8b8SSimon J. Gerraty# TARGET_OBJ_SPEC = ${TARGET_SPEC_VARSr:@v@${$v:U}@:ts/}
1087d86c8b8SSimon J. Gerraty# TARGET_OBJ_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts/}
109bee3d4bfSSimon J. GerratyTARGET_OBJ_SPEC ?= ${TARGET_SPEC_VARS:@v@${$v:U}@:ts.}
110bee3d4bfSSimon J. Gerraty.else
111bee3d4bfSSimon J. GerratyTARGET_OBJ_SPEC ?= ${MACHINE}
112bee3d4bfSSimon J. Gerraty.endif
113bee3d4bfSSimon J. Gerraty
114bee3d4bfSSimon J. GerratyMAKE_PRINT_VAR_ON_ERROR += ${TARGET_SPEC_VARS}
115bee3d4bfSSimon J. Gerraty
116bee3d4bfSSimon J. Gerraty.if !defined(MACHINE0)
117bee3d4bfSSimon J. Gerraty# it can be handy to know which MACHINE kicked off the build
118bee3d4bfSSimon J. Gerraty# for example, if using Makefild.depend for multiple machines,
119bee3d4bfSSimon J. Gerraty# allowing only MACHINE0 to update can keep things simple.
120bee3d4bfSSimon J. GerratyMACHINE0 := ${MACHINE}
121bee3d4bfSSimon J. Gerraty.export MACHINE0
122bee3d4bfSSimon J. Gerraty.endif
123bee3d4bfSSimon J. Gerraty
124bee3d4bfSSimon J. GerratyMACHINE_OBJ.host = ${HOST_TARGET}
125bee3d4bfSSimon J. GerratyMACHINE_OBJ.host32 = ${HOST_TARGET32}
126bee3d4bfSSimon J. GerratyMACHINE_OBJ.${MACHINE} ?= ${TARGET_OBJ_SPEC}
127bee3d4bfSSimon J. GerratyMACHINE_OBJDIR = ${MACHINE_OBJ.${MACHINE}}
128bee3d4bfSSimon J. Gerraty
1297d86c8b8SSimon J. Gerraty# we likely want to override env for OBJTOP
1307d86c8b8SSimon J. Gerraty.if ${MACHINE} == "host"
1317d86c8b8SSimon J. GerratyOBJTOP = ${HOST_OBJTOP}
1327d86c8b8SSimon J. Gerraty.elif ${MACHINE} == "host32"
1337d86c8b8SSimon J. GerratyOBJTOP = ${HOST_OBJTOP32}
1347d86c8b8SSimon J. Gerraty.else
1357d86c8b8SSimon J. GerratyOBJTOP = ${OBJROOT}${MACHINE_OBJDIR}
1367d86c8b8SSimon J. Gerraty.endif
1377d86c8b8SSimon J. Gerraty.if ${.MAKE.LEVEL} > 0
1387d86c8b8SSimon J. Gerraty# should not change from level 1 onwards
1397d86c8b8SSimon J. Gerraty# this only matters for cases like bmake/unit-tests
1407d86c8b8SSimon J. Gerraty# where we do ${MAKE} -r
1417d86c8b8SSimon J. Gerraty.export OBJTOP
1427d86c8b8SSimon J. Gerraty.endif
1437d86c8b8SSimon J. Gerraty
1447d86c8b8SSimon J. Gerraty.if ${MAKEOBJDIR:U:M*/*} == ""
145bee3d4bfSSimon J. Gerraty# we do not use MAKEOBJDIRPREFIX
1467d86c8b8SSimon J. Gerraty# though we may have used it above to initialize OBJROOT
147bee3d4bfSSimon J. Gerraty.undef MAKEOBJDIRPREFIX
1487d86c8b8SSimon J. Gerraty# this is what we expected in env
1497d86c8b8SSimon J. GerratyMAKEOBJDIR = $${.CURDIR:S,^$${SRCTOP},$${OBJTOP},}
1507d86c8b8SSimon J. Gerraty# export that but do not track
1517d86c8b8SSimon J. Gerraty.export-env MAKEOBJDIR
1527d86c8b8SSimon J. Gerraty# this what we need here
1537d86c8b8SSimon J. GerratyMAKEOBJDIR = ${.CURDIR:S,${SRCTOP},${OBJTOP},}
1547d86c8b8SSimon J. Gerraty.endif
155bee3d4bfSSimon J. Gerraty
156bee3d4bfSSimon J. GerratySTAGE_MACHINE ?= ${MACHINE_OBJDIR}
157bee3d4bfSSimon J. GerratySTAGE_OBJTOP ?= ${STAGE_ROOT}/${STAGE_MACHINE}
158bee3d4bfSSimon J. GerratySTAGE_COMMON_OBJTOP ?= ${STAGE_ROOT}/common
159bee3d4bfSSimon J. GerratySTAGE_HOST_OBJTOP ?= ${STAGE_ROOT}/${HOST_TARGET}
160bee3d4bfSSimon J. GerratySTAGE_HOST_OBJTOP32 ?= ${STAGE_ROOT}/${HOST_TARGET32}
161bee3d4bfSSimon J. Gerraty
162bee3d4bfSSimon J. GerratySTAGE_INCLUDEDIR ?= ${STAGE_OBJTOP}${INCLUDEDIR:U/usr/include}
163bee3d4bfSSimon J. GerratySTAGE_LIBDIR ?= ${STAGE_OBJTOP}${LIBDIR:U/lib}
164bee3d4bfSSimon J. Gerraty
165bee3d4bfSSimon J. GerratyTIME_STAMP_FMT ?= @ %s [%Y-%m-%d %T] ${:U}
166bee3d4bfSSimon J. GerratyDATE_TIME_STAMP ?= `date '+${TIME_STAMP_FMT}'`
167bee3d4bfSSimon J. GerratyTIME_STAMP ?= ${TIME_STAMP_FMT:localtime}
168bee3d4bfSSimon J. Gerraty
169bee3d4bfSSimon J. Gerraty.if ${MK_TIME_STAMPS:Uyes} == "yes"
170bee3d4bfSSimon J. GerratyTRACER = ${TIME_STAMP}
171bee3d4bfSSimon J. GerratyECHO_DIR = echo ${TIME_STAMP}
172bee3d4bfSSimon J. GerratyECHO_TRACE = echo ${TIME_STAMP}
173bee3d4bfSSimon J. Gerraty.endif
174bee3d4bfSSimon J. Gerraty
175bee3d4bfSSimon J. Gerraty.if ${.CURDIR} == ${SRCTOP}
176bee3d4bfSSimon J. GerratyRELDIR= .
177bee3d4bfSSimon J. GerratyRELTOP= .
178bee3d4bfSSimon J. Gerraty.elif ${.CURDIR:M${SRCTOP}/*}
179bee3d4bfSSimon J. GerratyRELDIR:= ${.CURDIR:S,${SRCTOP}/,,}
180bee3d4bfSSimon J. Gerraty.else
181bee3d4bfSSimon J. GerratyRELDIR:= ${.OBJDIR:S,${OBJTOP}/,,}
182bee3d4bfSSimon J. Gerraty.endif
183bee3d4bfSSimon J. GerratyRELTOP?= ${RELDIR:C,[^/]+,..,g}
184bee3d4bfSSimon J. GerratyRELOBJTOP?= ${RELTOP}
185bee3d4bfSSimon J. GerratyRELSRCTOP?= ${RELTOP}
186bee3d4bfSSimon J. Gerraty
187bee3d4bfSSimon J. Gerraty# this does all the smarts of setting .MAKE.DEPENDFILE
188bee3d4bfSSimon J. Gerraty.-include <sys.dependfile.mk>
189bee3d4bfSSimon J. Gerraty
190bee3d4bfSSimon J. Gerraty.-include <local.sys.dirdeps.mk>
191bee3d4bfSSimon J. Gerraty
192bee3d4bfSSimon J. Gerraty# check if we got anything sane
193bee3d4bfSSimon J. Gerraty.if ${.MAKE.DEPENDFILE} == ".depend"
194bee3d4bfSSimon J. Gerraty.undef .MAKE.DEPENDFILE
195bee3d4bfSSimon J. Gerraty.endif
196bee3d4bfSSimon J. Gerraty# just in case
197bee3d4bfSSimon J. Gerraty.MAKE.DEPENDFILE ?= Makefile.depend
198bee3d4bfSSimon J. Gerraty
199*f697b9d0SSimon J. Gerraty# Makefile.depend* often refer to DEP_MACHINE etc,
200*f697b9d0SSimon J. Gerraty# we need defaults for both first include in a leaf dir
201*f697b9d0SSimon J. Gerraty# and when level > 0
202*f697b9d0SSimon J. Gerraty# so ensure DEP_* for TARGET_SPEC_VARS and RELDIR are set
203*f697b9d0SSimon J. Gerraty.for V in ${TARGET_SPEC_VARS} RELDIR
204*f697b9d0SSimon J. GerratyDEP_$V ?= ${$V}
205bee3d4bfSSimon J. Gerraty.endfor
206