2 # Early setup of MAKEOBJDIR
4 # Default format is: /usr/obj/usr/src/[${TARGET}.${TARGET_ARCH}/]bin/sh
5 # MAKEOBJDIRPREFIX is /usr/obj
6 # OBJROOT is /usr/obj/usr/src/
7 # OBJTOP is /usr/obj/usr/src/[${TARGET}.${TARGET_ARCH}/]
8 # MAKEOBJDIR is /usr/obj/usr/src/[${TARGET}.${TARGET_ARCH}/]bin/sh
10 # MAKEOBJDIRPREFIX will override the default pattern above and internally
11 # set MAKEOBJDIR. If OBJROOT is set then MAKEOBJDIRPREFIX is rooted inside
14 # If MK_UNIFIED_OBJDIR is no then OBJROOT will always match OBJTOP.
16 # If .MAKE.LEVEL == 0 then the TARGET.TARGET_ARCH is potentially added on.
17 # If .MAKE.LEVEL > 0 and MAKEOBJDIRPREFIX is set then it will not get
18 # TARGET.TARGET_ARCH added in as it assumes that MAKEOBJDIRPREFIX is
19 # nested in the existing OBJTOP with TARGET.TARGET_ARCH in it.
21 # The expected OBJDIR is stored in __objdir for auto.obj.mk to use.
23 # AUTO_OBJ is opportunistically enabled if the computed .OBJDIR is writable
24 # by the current user. Some top-level targets disable this behavior in
28 _default_makeobjdirprefix?= /usr/obj
29 _default_makeobjdir= $${.CURDIR:S,^$${SRCTOP},$${OBJTOP},}
31 .include <bsd.mkopt.mk>
33 .if ${.MAKE.LEVEL} == 0 || empty(OBJROOT)
34 .if ${MK_UNIFIED_OBJDIR} == "no" && ${MK_DIRDEPS_BUILD} == "no"
35 # Fall back to historical behavior.
36 # We always want to set a default MAKEOBJDIRPREFIX...
37 MAKEOBJDIRPREFIX?= ${_default_makeobjdirprefix}
38 # but don't enforce TARGET.TARGET_ARCH unless we're at the top-level directory.
39 .if ${.CURDIR} == ${SRCTOP} && \
40 !(defined(TARGET) && defined(TARGET_ARCH) && \
41 ${MACHINE} == ${TARGET} && ${MACHINE_ARCH} == ${TARGET_ARCH} && \
42 !defined(CROSS_BUILD_TESTING))
43 MAKEOBJDIRPREFIX:= ${MAKEOBJDIRPREFIX}${TARGET:D/${TARGET}.${TARGET_ARCH}}
45 .endif # ${MK_UNIFIED_OBJDIR} == "no"
47 .if !empty(MAKEOBJDIRPREFIX)
48 # put things approximately where they want
49 OBJROOT:= ${MAKEOBJDIRPREFIX}${SRCTOP}/
51 # export but do not track
52 .export-env MAKEOBJDIRPREFIX
56 MAKEOBJDIR= ${_default_makeobjdir}
57 # export but do not track
58 .export-env MAKEOBJDIR
59 # Expand for our own use
60 MAKEOBJDIR:= ${MAKEOBJDIR}
62 # SB documented at http://www.crufty.net/sjg/docs/sb-tools.htm
64 SB_OBJROOT?= ${SB}/obj/
65 # this is what we use below
66 OBJROOT?= ${SB_OBJROOT}
68 OBJROOT?= ${_default_makeobjdirprefix}${SRCTOP}/
69 .if ${OBJROOT:M*/} != ""
70 OBJROOT:= ${OBJROOT:H:tA}/
72 OBJROOT:= ${OBJROOT:H:tA}/${OBJROOT:T}
74 # Must export since OBJDIR will dynamically be based on it
75 .export OBJROOT SRCTOP
78 .if ${MK_DIRDEPS_BUILD} == "no"
80 # SRCTOP == OBJROOT only happens with clever MAKEOBJDIRPREFIX=/. Don't
81 # append TARGET.TARGET_ARCH for that case since the user wants to build
83 .if ${MK_UNIFIED_OBJDIR} == "yes" && ${SRCTOP} != ${OBJROOT:tA}
84 .if defined(TARGET) && defined(TARGET_ARCH)
85 OBJTOP:= ${OBJROOT}${TARGET}.${TARGET_ARCH}
86 .elif defined(TARGET) && ${.CURDIR} == ${SRCTOP}
87 # Not enough information, just use basic OBJDIR. This can happen with some
88 # 'make universe' targets or if TARGET is not being used as expected.
91 OBJTOP:= ${OBJROOT}${MACHINE}.${MACHINE_ARCH}
94 # TARGET.TARGET_ARCH handled in OBJROOT already.
96 .endif # ${MK_UNIFIED_OBJDIR} == "yes"
97 .endif # empty(OBJTOP)
99 # Fixup OBJROOT/OBJTOP if using MAKEOBJDIRPREFIX.
100 # This intenionally comes after adding TARGET.TARGET_ARCH so that is truncated
101 # away for nested objdirs. This logic also will not trigger if the OBJROOT
102 # block above unsets MAKEOBJDIRPREFIX.
103 .if !empty(MAKEOBJDIRPREFIX)
104 OBJTOP:= ${MAKEOBJDIRPREFIX}${SRCTOP}
108 # Wait to validate MAKEOBJDIR until OBJTOP is set.
109 .if defined(MAKEOBJDIR)
110 .if ${MAKEOBJDIR:M/*} == ""
111 .error Cannot use MAKEOBJDIR=${MAKEOBJDIR}${.newline}Unset MAKEOBJDIR to get default: MAKEOBJDIR='${_default_makeobjdir}'
115 # __objdir is the expected .OBJDIR we want to use and that auto.obj.mk will
117 .if !empty(MAKEOBJDIRPREFIX)
118 .if ${.CURDIR:M${MAKEOBJDIRPREFIX}/*} != ""
119 # we are already in obj tree!
122 __objdir:= ${MAKEOBJDIRPREFIX}${.CURDIR}
124 .elif !empty(MAKEOBJDIR)
125 __objdir:= ${MAKEOBJDIR}
128 # Try to enable MK_AUTO_OBJ by default if we can write to the __objdir. Only
129 # do this if AUTO_OBJ is not disabled by the user, and this is the first make
131 .if ${.MAKE.LEVEL} == 0 && \
132 ${MK_AUTO_OBJ} == "no" && empty(.MAKEOVERRIDES:MMK_AUTO_OBJ) && \
133 !defined(WITHOUT_AUTO_OBJ) && !make(showconfig) && !make(print-dir) && \
134 !make(test-system-*) && \
135 !defined(NO_OBJ) && \
136 empty(RELDIR:Msys/*/compile/*)
137 # Find the last existing directory component and check if we can write to it.
138 # If the last component is a symlink then recurse on the new path.
141 if [ -w "$${1}" ]; then \
142 [ -d "$${1}" ] || return 1; \
149 while [ $$\# -gt 0 ]; do \
152 if [ ! -d "$${dir}$${d}/" ]; then \
153 if [ -L "$${dir}$${d}" ]; then \
154 dir="$$(readlink "$${dir}$${d}")/"; \
155 for d in "$${@}"; do \
156 dir="$${dir}$${d}/"; \
159 DirIsCreatable "$${dir%/}" || ret=$$?; \
161 elif [ -e "/$${dir}$${d}" ]; then \
167 dir="$${dir}$${d}/"; \
169 [ -w "$${dir}" ] && [ -d "$${dir}" ] && return 0; \
173 if DirIsCreatable "$${1}"; then \
180 .if ${.CURDIR} == ${__objdir} || \
181 (exists(${__objdir}) && ${.TARGETS:M*install*} == ${.TARGETS})
182 __objdir_writable?= yes
183 .elif empty(__objdir_writable)
184 __objdir_writable!= \
185 ${CheckAutoObj}; CheckAutoObj "${__objdir}" || echo no
188 __objdir_writable?= no
189 # Export the decision to sub-makes.
190 MK_AUTO_OBJ:= ${__objdir_writable}
192 .elif make(showconfig)
193 # Need to export for showconfig internally running make -dg1. It is enabled
194 # in sys.mk by default.
196 .endif # ${MK_AUTO_OBJ} == "no" && ...
198 # Assign this directory as .OBJDIR if possible.
200 # The expected OBJDIR already exists, set it as .OBJDIR.
201 .if !empty(__objdir) && exists(${__objdir})
204 # The OBJDIR we wanted does not yet exist, ensure we default to safe .CURDIR
205 # in case make started with a bogus MAKEOBJDIR, that expanded before OBJTOP
206 # was set, that happened to match some unexpected directory. Either
207 # auto.obj.mk or bsd.obj.mk will create the directory and fix .OBJDIR later.
211 # Ensure .OBJDIR=.CURDIR cases have a proper OBJTOP and .OBJDIR
212 .if defined(NO_OBJ) || ${__objdir_writable:Uunknown} == "no" || \
213 ${__objdir} == ${.CURDIR}
216 # Compare only to avoid an unneeded chdir(2), :tA purposely left out.
217 .if ${.OBJDIR} != ${.CURDIR}
220 .endif # defined(NO_OBJ)
222 HOST_OBJTOP?= ${OBJROOT}${HOST_TARGET}
224 .endif # ${MK_DIRDEPS_BUILD} == "no"