xref: /freebsd/share/mk/src.sys.obj.mk (revision 5c2bacde581387d5c9a8315d192a2a9b0934410f)
1# $FreeBSD$
2#
3# Early setup of MAKEOBJDIR
4#
5# Default format is: /usr/obj/usr/src/[${TARGET}.${TARGET_ARCH}/]bin/sh
6#  MAKEOBJDIRPREFIX is	/usr/obj
7#  OBJROOT is		/usr/obj/usr/src/
8#  OBJTOP is		/usr/obj/usr/src/[${TARGET}.${TARGET_ARCH}/]
9#  MAKEOBJDIR is	/usr/obj/usr/src/[${TARGET}.${TARGET_ARCH}/]bin/sh
10#
11#  MAKEOBJDIRPREFIX will override the default pattern above and internally
12#  set MAKEOBJDIR.  If OBJROOT is set then MAKEOBJDIRPREFIX is rooted inside
13#  of there.
14#
15#  If MK_UNIFIED_OBJDIR is no then OBJROOT will always match OBJTOP.
16#
17#  If .MAKE.LEVEL == 0 then the TARGET.TARGET_ARCH is potentially added on.
18#  If .MAKE.LEVEL >  0 and MAKEOBJDIRPREFIX is set then it will not get
19#  TARGET.TARGET_ARCH added in as it assumes that MAKEOBJDIRPREFIX is
20#  nested in the existing OBJTOP with TARGET.TARGET_ARCH in it.
21#
22
23_default_makeobjdirprefix?=	/usr/obj
24_default_makeobjdir=	$${.CURDIR:S,^$${SRCTOP},$${OBJTOP},}
25
26.include <bsd.mkopt.mk>
27
28.if ${.MAKE.LEVEL} == 0 || empty(OBJROOT)
29.if ${MK_UNIFIED_OBJDIR} == "no"
30# Fall back to historical behavior.
31# We always want to set a default MAKEOBJDIRPREFIX...
32MAKEOBJDIRPREFIX?=	${_default_makeobjdirprefix}
33# but don't enforce TARGET.TARGET_ARCH unless we're at the top-level directory.
34.if ${.CURDIR} == ${SRCTOP} && \
35    !(defined(TARGET) && defined(TARGET_ARCH) && \
36    ${MACHINE} == ${TARGET} && ${MACHINE_ARCH} == ${TARGET_ARCH} && \
37    !defined(CROSS_BUILD_TESTING))
38MAKEOBJDIRPREFIX:=	${MAKEOBJDIRPREFIX}${TARGET:D/${TARGET}.${TARGET_ARCH}}
39.endif
40.endif	# ${MK_UNIFIED_OBJDIR} == "no"
41
42.if !empty(MAKEOBJDIRPREFIX)
43# put things approximately where they want
44OBJROOT:=	${MAKEOBJDIRPREFIX}${SRCTOP}/
45MAKEOBJDIRPREFIX=
46# export but do not track
47.export-env MAKEOBJDIRPREFIX
48.endif
49.if empty(MAKEOBJDIR)
50# OBJTOP set below
51MAKEOBJDIR=	${_default_makeobjdir}
52# export but do not track
53.export-env MAKEOBJDIR
54# Expand for our own use
55MAKEOBJDIR:=	${MAKEOBJDIR}
56.endif
57# SB documented at http://www.crufty.net/sjg/docs/sb-tools.htm
58.if !empty(SB)
59SB_OBJROOT?=	${SB}/obj/
60# this is what we use below
61OBJROOT?=	${SB_OBJROOT}
62.endif
63OBJROOT?=	${_default_makeobjdirprefix}${SRCTOP}/
64.if ${OBJROOT:M*/} != ""
65OBJROOT:=	${OBJROOT:H:tA}/
66.else
67OBJROOT:=	${OBJROOT:H:tA}/${OBJROOT:T}
68.endif
69# Must export since OBJDIR will dynamically be based on it
70.export OBJROOT SRCTOP
71.endif
72
73.if ${MK_UNIFIED_OBJDIR} == "yes"
74OBJTOP:=	${OBJROOT}${TARGET:D${TARGET}.${TARGET_ARCH}:U${MACHINE}.${MACHINE_ARCH}}
75.else
76# TARGET.TARGET_ARCH handled in OBJROOT already.
77OBJTOP:=	${OBJROOT:H}
78.endif	# ${MK_UNIFIED_OBJDIR} == "yes"
79
80# Fixup OBJROOT/OBJTOP if using MAKEOBJDIRPREFIX but leave it alone
81# for DIRDEPS_BUILD which really wants to know the absolute top at
82# all times.  This intenionally comes after adding TARGET.TARGET_ARCH
83# so that is truncated away for nested objdirs.  This logic also
84# will not trigger if the OBJROOT block above unsets MAKEOBJDIRPREFIX.
85.if !empty(MAKEOBJDIRPREFIX) && ${MK_DIRDEPS_BUILD} == "no"
86OBJTOP:=	${MAKEOBJDIRPREFIX}${SRCTOP}
87OBJROOT:=	${OBJTOP}/
88.endif
89
90# Wait to validate MAKEOBJDIR until OBJTOP is set.
91.if defined(MAKEOBJDIR)
92.if ${MAKEOBJDIR:M/*} == ""
93.error Cannot use MAKEOBJDIR=${MAKEOBJDIR}${.newline}Unset MAKEOBJDIR to get default:  MAKEOBJDIR='${_default_makeobjdir}'
94.endif
95.endif
96
97# __objdir is the expected .OBJDIR we want to use and that auto.obj.mk will
98# try to create.
99.if !empty(MAKEOBJDIRPREFIX)
100__objdir:=	${MAKEOBJDIRPREFIX}${.CURDIR}
101.elif !empty(MAKEOBJDIR)
102__objdir:=	${MAKEOBJDIR}
103.endif
104
105# Try to enable MK_AUTO_OBJ by default if we can write to the __objdir.  Only
106# do this if AUTO_OBJ is not disabled by the user, not cleaning, and this is
107# the first make ran.
108.if 0 && ${.MAKE.LEVEL} == 0 && \
109    ${MK_AUTO_OBJ} == "no" && empty(.MAKEOVERRIDES:MMK_AUTO_OBJ) && \
110    !defined(WITHOUT_AUTO_OBJ) && !make(showconfig) && !make(print-dir) && \
111    !defined(NO_OBJ) && \
112    (${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
113# Find the last existing directory component and check if we can write to it.
114# If the last component is a symlink then recurse on the new path.
115CheckAutoObj= \
116DirIsCreatable() { \
117	[ -w "$${1}" ] && return 0; \
118	d="$${1}"; \
119	IFS=/; \
120	set -- $${d}; \
121	unset dir; \
122	while [ $$\# -gt 0 ]; do \
123		d="$${1}"; \
124		shift; \
125		if [ ! -d "$${dir}$${d}/" ]; then \
126			if [ -L "$${dir}$${d}" ]; then \
127				dir="$$(readlink "$${dir}$${d}")/"; \
128				for d in "$${@}"; do \
129					dir="$${dir}$${d}/"; \
130				done; \
131				ret=0; \
132				DirIsCreatable "$${dir%/}" || ret=$$?; \
133				return $${ret}; \
134			else \
135				break; \
136			fi; \
137		fi; \
138		dir="$${dir}$${d}/"; \
139	done; \
140	[ -w "$${dir}" ]; \
141}; \
142CheckAutoObj() { \
143	if DirIsCreatable "$${1}"; then \
144		echo yes; \
145	else \
146		echo no; \
147	fi; \
148}
149.if !empty(__objdir)
150__objdir_writable!= \
151	${CheckAutoObj}; CheckAutoObj "${__objdir}" || echo no
152.endif
153__objdir_writable?= no
154# Export the decision to sub-makes.
155MK_AUTO_OBJ:=	${__objdir_writable}
156.export MK_AUTO_OBJ
157.elif make(showconfig)
158# Need to export for showconfig internally running make -dg1.  It is enabled
159# in sys.mk by default.
160.export MK_AUTO_OBJ
161.endif	# ${MK_AUTO_OBJ} == "no" && ...
162
163# Assign this directory as .OBJDIR if possible.
164#
165# The expected OBJDIR already exists, set it as .OBJDIR.
166.if !empty(__objdir) && exists(${__objdir})
167.OBJDIR: ${__objdir}
168# Special case to work around bmake bug.  If the top-level .OBJDIR does not yet
169# exist and MAKEOBJDIR is passed into environment and yield a blank value,
170# bmake will incorrectly set .OBJDIR=${SRCTOP}/ rather than the expected
171# ${SRCTOP} to match ${.CURDIR}.
172.elif ${MAKE_VERSION} <= 20170720 && \
173    ${.CURDIR} == ${SRCTOP} && ${.OBJDIR} == ${SRCTOP}/
174.OBJDIR: ${.CURDIR}
175.else
176# The OBJDIR we wanted does not yet exist, ensure we default to safe .CURDIR
177# in case make started with a bogus MAKEOBJDIR, that expanded before OBJTOP
178# was set, that happened to match some unexpected directory.  Either
179# auto.obj.mk or bsd.obj.mk will create the directory and fix .OBJDIR later.
180.OBJDIR: ${.CURDIR}
181.endif
182