xref: /freebsd/contrib/bmake/mk/sys.dirdeps.mk (revision 315ee00f)
1# $Id: sys.dirdeps.mk,v 1.12 2023/05/14 16:16:03 sjg Exp $
2#
3#	@(#) Copyright (c) 2012-2023, Simon J. Gerraty
4#
5#	This file is provided in the hope that it will
6#	be of use.  There is absolutely NO WARRANTY.
7#	Permission to copy, redistribute or otherwise
8#	use this file is hereby granted provided that
9#	the above copyright notice and this notice are
10#	left intact.
11#
12#	Please send copies of changes and bug-fixes to:
13#	sjg@crufty.net
14#
15
16# Originally DIRDEPS_BUILD and META_MODE were the same thing.
17# So, much of this was done in *meta.sys.mk and local*mk
18# but properly belongs here.
19
20# Include from [local.]sys.mk - if doing DIRDEPS_BUILD
21# we should not be here otherwise
22MK_DIRDEPS_BUILD ?= yes
23# these are all implied
24MK_AUTO_OBJ ?= yes
25MK_META_MODE ?= yes
26MK_STAGING ?= yes
27
28_PARSEDIR ?= ${.PARSEDIR:tA}
29
30.-include <local.sys.dirdeps.env.mk>
31
32.if ${.MAKE.LEVEL} == 0
33# make sure dirdeps target exists and do it first
34dirdeps:
35# first .MAIN is what counts
36.MAIN: dirdeps
37.NOPATH: dirdeps
38all: dirdeps .WAIT
39.endif
40
41.if empty(SRCTOP)
42# fallback assumes share/mk!
43SRCTOP := ${SB_SRC:U${.PARSEDIR:tA:H:H}}
44.export SRCTOP
45.endif
46
47# fake SB if not using mk wrapper
48# SB documented at http://www.crufty.net/sjg/docs/sb-tools.htm
49.if !defined(SB)
50SB := ${SRCTOP:H}
51.export SB
52.endif
53
54.if empty(OBJROOT)
55OBJROOT := ${SB_OBJROOT:U${MAKEOBJDIRPREFIX:U${SB}/obj}/}
56.export OBJROOT
57.endif
58# we expect OBJROOT to end with / (- can work too)
59.if ${OBJROOT:M*[/-]} == ""
60OBJROOT := ${OBJROOT}/
61.endif
62
63.if empty(STAGE_ROOT)
64STAGE_ROOT ?= ${OBJROOT}stage
65.export STAGE_ROOT
66.endif
67
68# We should be included before meta.sys.mk
69# If TARGET_SPEC_VARS is other than just MACHINE
70# it should be set by now.
71# TARGET_SPEC must not contain any '.'s.
72TARGET_SPEC_VARS ?= MACHINE
73
74.if ${TARGET_SPEC:Uno:M*,*} != ""
75# deal with TARGET_SPEC from env
76_tspec := ${TARGET_SPEC:S/,/ /g}
77.for i in ${TARGET_SPEC_VARS:${M_RANGE:Urange}}
78${TARGET_SPEC_VARS:[$i]} := ${_tspec:[$i]}
79.endfor
80# We need to stop that TARGET_SPEC affecting any submakes
81TARGET_SPEC=
82# so export but do not track
83.export-env TARGET_SPEC
84.export ${TARGET_SPEC_VARS}
85.for v in ${TARGET_SPEC_VARS:O:u}
86.if empty($v)
87.undef $v
88.endif
89.endfor
90.endif
91
92# Now make sure we know what TARGET_SPEC is
93# as we may need it to find Makefile.depend*
94.if ${MACHINE:Mhost*} != ""
95# host is special
96TARGET_SPEC = ${MACHINE}
97.else
98TARGET_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts,}
99.endif
100
101.if ${TARGET_SPEC_VARS:[#]} > 1
102TARGET_SPEC_VARSr := ${TARGET_SPEC_VARS:[-1..1]}
103# alternatives might be
104# TARGET_OBJ_SPEC = ${TARGET_SPEC_VARSr:@v@${$v:U}@:ts/}
105# TARGET_OBJ_SPEC = ${TARGET_SPEC_VARS:@v@${$v:U}@:ts/}
106TARGET_OBJ_SPEC ?= ${TARGET_SPEC_VARS:@v@${$v:U}@:ts.}
107.else
108TARGET_OBJ_SPEC ?= ${MACHINE}
109.endif
110
111MAKE_PRINT_VAR_ON_ERROR += ${TARGET_SPEC_VARS}
112
113.if !defined(MACHINE0)
114# it can be handy to know which MACHINE kicked off the build
115# for example, if using Makefild.depend for multiple machines,
116# allowing only MACHINE0 to update can keep things simple.
117MACHINE0 := ${MACHINE}
118.export MACHINE0
119.endif
120
121MACHINE_OBJ.host = ${HOST_TARGET}
122MACHINE_OBJ.host32 = ${HOST_TARGET32}
123MACHINE_OBJ.${MACHINE} ?= ${TARGET_OBJ_SPEC}
124MACHINE_OBJDIR = ${MACHINE_OBJ.${MACHINE}}
125
126# we likely want to override env for OBJTOP
127.if ${MACHINE} == "host"
128OBJTOP = ${HOST_OBJTOP}
129.elif ${MACHINE} == "host32"
130OBJTOP = ${HOST_OBJTOP32}
131.else
132OBJTOP = ${OBJROOT}${MACHINE_OBJDIR}
133.endif
134.if ${.MAKE.LEVEL} > 0
135# should not change from level 1 onwards
136# this only matters for cases like bmake/unit-tests
137# where we do ${MAKE} -r
138.export OBJTOP
139.endif
140
141.if ${MAKEOBJDIR:U:M*/*} == ""
142# we do not use MAKEOBJDIRPREFIX
143# though we may have used it above to initialize OBJROOT
144.undef MAKEOBJDIRPREFIX
145# this is what we expected in env
146MAKEOBJDIR = $${.CURDIR:S,^$${SRCTOP},$${OBJTOP},}
147# export that but do not track
148.export-env MAKEOBJDIR
149# this what we need here
150MAKEOBJDIR = ${.CURDIR:S,${SRCTOP},${OBJTOP},}
151.endif
152
153STAGE_MACHINE ?= ${MACHINE_OBJDIR}
154STAGE_OBJTOP ?= ${STAGE_ROOT}/${STAGE_MACHINE}
155STAGE_COMMON_OBJTOP ?= ${STAGE_ROOT}/common
156STAGE_HOST_OBJTOP ?= ${STAGE_ROOT}/${HOST_TARGET}
157STAGE_HOST_OBJTOP32 ?= ${STAGE_ROOT}/${HOST_TARGET32}
158
159STAGE_INCLUDEDIR ?= ${STAGE_OBJTOP}${INCLUDEDIR:U/usr/include}
160STAGE_LIBDIR ?= ${STAGE_OBJTOP}${LIBDIR:U/lib}
161
162TIME_STAMP_FMT ?= @ %s [%Y-%m-%d %T] ${:U}
163DATE_TIME_STAMP ?= `date '+${TIME_STAMP_FMT}'`
164TIME_STAMP ?= ${TIME_STAMP_FMT:localtime}
165
166.if ${MK_TIME_STAMPS:Uyes} == "yes"
167TRACER = ${TIME_STAMP}
168ECHO_DIR = echo ${TIME_STAMP}
169ECHO_TRACE = echo ${TIME_STAMP}
170.endif
171
172.if ${.CURDIR} == ${SRCTOP}
173RELDIR= .
174RELTOP= .
175.elif ${.CURDIR:M${SRCTOP}/*}
176RELDIR:= ${.CURDIR:S,${SRCTOP}/,,}
177.else
178RELDIR:= ${.OBJDIR:S,${OBJTOP}/,,}
179.endif
180RELTOP?= ${RELDIR:C,[^/]+,..,g}
181RELOBJTOP?= ${RELTOP}
182RELSRCTOP?= ${RELTOP}
183
184# this does all the smarts of setting .MAKE.DEPENDFILE
185.-include <sys.dependfile.mk>
186
187.-include <local.sys.dirdeps.mk>
188
189# check if we got anything sane
190.if ${.MAKE.DEPENDFILE} == ".depend"
191.undef .MAKE.DEPENDFILE
192.endif
193# just in case
194.MAKE.DEPENDFILE ?= Makefile.depend
195
196.if ${.MAKE.LEVEL} > 0
197# Makefile.depend* also get read at level 1+
198# and often refer to DEP_MACHINE etc,
199# so ensure DEP_* (for TARGET_SPEC_VARS anyway) are set
200.for V in ${TARGET_SPEC_VARS}
201DEP_$V = ${$V}
202.endfor
203.endif
204