]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/mk/src.sys.obj.mk
Handle MAKEOBJDIRPREFIX when already inside of .OBJDIR.
[FreeBSD/FreeBSD.git] / share / mk / src.sys.obj.mk
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...
32 MAKEOBJDIRPREFIX?=      ${_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))
38 MAKEOBJDIRPREFIX:=      ${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
44 OBJROOT:=       ${MAKEOBJDIRPREFIX}${SRCTOP}/
45 MAKEOBJDIRPREFIX=
46 # export but do not track
47 .export-env MAKEOBJDIRPREFIX
48 .endif
49 .if empty(MAKEOBJDIR)
50 # OBJTOP set below
51 MAKEOBJDIR=     ${_default_makeobjdir}
52 # export but do not track
53 .export-env MAKEOBJDIR
54 # Expand for our own use
55 MAKEOBJDIR:=    ${MAKEOBJDIR}
56 .endif
57 # SB documented at http://www.crufty.net/sjg/docs/sb-tools.htm
58 .if !empty(SB)
59 SB_OBJROOT?=    ${SB}/obj/
60 # this is what we use below
61 OBJROOT?=       ${SB_OBJROOT}
62 .endif
63 OBJROOT?=       ${_default_makeobjdirprefix}${SRCTOP}/
64 .if ${OBJROOT:M*/} != ""
65 OBJROOT:=       ${OBJROOT:H:tA}/
66 .else
67 OBJROOT:=       ${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 empty(OBJTOP)
74 # SRCTOP == OBJROOT only happens with clever MAKEOBJDIRPREFIX=/.  Don't
75 # append TARGET.TARGET_ARCH for that case since the user wants to build
76 # in the source tree.
77 .if ${MK_UNIFIED_OBJDIR} == "yes" && ${SRCTOP} != ${OBJROOT:tA}
78 OBJTOP:=        ${OBJROOT}${TARGET:D${TARGET}.${TARGET_ARCH}:U${MACHINE}.${MACHINE_ARCH}}
79 .else
80 # TARGET.TARGET_ARCH handled in OBJROOT already.
81 OBJTOP:=        ${OBJROOT:H}
82 .endif  # ${MK_UNIFIED_OBJDIR} == "yes"
83 .endif
84
85 # Fixup OBJROOT/OBJTOP if using MAKEOBJDIRPREFIX but leave it alone
86 # for DIRDEPS_BUILD which really wants to know the absolute top at
87 # all times.  This intenionally comes after adding TARGET.TARGET_ARCH
88 # so that is truncated away for nested objdirs.  This logic also
89 # will not trigger if the OBJROOT block above unsets MAKEOBJDIRPREFIX.
90 .if !empty(MAKEOBJDIRPREFIX) && ${MK_DIRDEPS_BUILD} == "no"
91 OBJTOP:=        ${MAKEOBJDIRPREFIX}${SRCTOP}
92 OBJROOT:=       ${OBJTOP}/
93 .endif
94
95 # Wait to validate MAKEOBJDIR until OBJTOP is set.
96 .if defined(MAKEOBJDIR)
97 .if ${MAKEOBJDIR:M/*} == ""
98 .error Cannot use MAKEOBJDIR=${MAKEOBJDIR}${.newline}Unset MAKEOBJDIR to get default:  MAKEOBJDIR='${_default_makeobjdir}'
99 .endif
100 .endif
101
102 # __objdir is the expected .OBJDIR we want to use and that auto.obj.mk will
103 # try to create.
104 .if !empty(MAKEOBJDIRPREFIX)
105 .if ${.CURDIR:M${MAKEOBJDIRPREFIX}/*} != ""
106 # we are already in obj tree!
107 __objdir=       ${.CURDIR}
108 .else
109 __objdir:=      ${MAKEOBJDIRPREFIX}${.CURDIR}
110 .endif
111 .elif !empty(MAKEOBJDIR)
112 __objdir:=      ${MAKEOBJDIR}
113 .endif
114
115 # Try to enable MK_AUTO_OBJ by default if we can write to the __objdir.  Only
116 # do this if AUTO_OBJ is not disabled by the user, and this is the first make
117 # ran.
118 .if ${.MAKE.LEVEL} == 0 && \
119     ${MK_AUTO_OBJ} == "no" && empty(.MAKEOVERRIDES:MMK_AUTO_OBJ) && \
120     !defined(WITHOUT_AUTO_OBJ) && !make(showconfig) && !make(print-dir) && \
121     !defined(NO_OBJ)
122 # Find the last existing directory component and check if we can write to it.
123 # If the last component is a symlink then recurse on the new path.
124 CheckAutoObj= \
125 DirIsCreatable() { \
126         [ -w "$${1}" ] && return 0; \
127         d="$${1}"; \
128         IFS=/; \
129         set -- $${d}; \
130         unset dir; \
131         while [ $$\# -gt 0 ]; do \
132                 d="$${1}"; \
133                 shift; \
134                 if [ ! -d "$${dir}$${d}/" ]; then \
135                         if [ -L "$${dir}$${d}" ]; then \
136                                 dir="$$(readlink "$${dir}$${d}")/"; \
137                                 for d in "$${@}"; do \
138                                         dir="$${dir}$${d}/"; \
139                                 done; \
140                                 ret=0; \
141                                 DirIsCreatable "$${dir%/}" || ret=$$?; \
142                                 return $${ret}; \
143                         else \
144                                 break; \
145                         fi; \
146                 fi; \
147                 dir="$${dir}$${d}/"; \
148         done; \
149         [ -w "$${dir}" ]; \
150 }; \
151 CheckAutoObj() { \
152         if DirIsCreatable "$${1}"; then \
153                 echo yes; \
154         else \
155                 echo no; \
156         fi; \
157 }
158 .if !empty(__objdir)
159 .if ${.CURDIR} == ${__objdir}
160 __objdir_writable?= yes
161 .else
162 __objdir_writable!= \
163         ${CheckAutoObj}; CheckAutoObj "${__objdir}" || echo no
164 .endif
165 .endif
166 __objdir_writable?= no
167 # Export the decision to sub-makes.
168 MK_AUTO_OBJ:=   ${__objdir_writable}
169 .export MK_AUTO_OBJ
170 .elif make(showconfig)
171 # Need to export for showconfig internally running make -dg1.  It is enabled
172 # in sys.mk by default.
173 .export MK_AUTO_OBJ
174 .endif  # ${MK_AUTO_OBJ} == "no" && ...
175
176 # Assign this directory as .OBJDIR if possible.
177 #
178 # The expected OBJDIR already exists, set it as .OBJDIR.
179 .if !empty(__objdir) && exists(${__objdir})
180 .OBJDIR: ${__objdir}
181 # Special case to work around bmake bug.  If the top-level .OBJDIR does not yet
182 # exist and MAKEOBJDIR is passed into environment and yield a blank value,
183 # bmake will incorrectly set .OBJDIR=${SRCTOP}/ rather than the expected
184 # ${SRCTOP} to match ${.CURDIR}.
185 .elif ${MAKE_VERSION} <= 20170720 && \
186     ${.CURDIR} == ${SRCTOP} && ${.OBJDIR} == ${SRCTOP}/
187 .OBJDIR: ${.CURDIR}
188 .else
189 # The OBJDIR we wanted does not yet exist, ensure we default to safe .CURDIR
190 # in case make started with a bogus MAKEOBJDIR, that expanded before OBJTOP
191 # was set, that happened to match some unexpected directory.  Either
192 # auto.obj.mk or bsd.obj.mk will create the directory and fix .OBJDIR later.
193 .OBJDIR: ${.CURDIR}
194 .endif
195
196 # Ensure .OBJDIR=.CURDIR cases have a proper OBJTOP and .OBJDIR
197 .if defined(NO_OBJ) || ${__objdir_writable:Uunknown} == "no" || \
198     ${__objdir} == ${.CURDIR}
199 OBJTOP=         ${SRCTOP}
200 OBJROOT=        ${SRCTOP}/
201 # Compare only to avoid an unneeded chdir(2), :tA purposely left out.
202 .if ${.OBJDIR} != ${.CURDIR}
203 .OBJDIR:        ${.CURDIR}
204 .endif
205 .endif  # defined(NO_OBJ)