1331246Ssjg# $Id: auto.obj.mk,v 1.15 2017/11/04 21:05:04 sjg Exp $
2246149Ssjg#
3246149Ssjg#	@(#) Copyright (c) 2004, Simon J. Gerraty
4246149Ssjg#
5246149Ssjg#	This file is provided in the hope that it will
6246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
7246149Ssjg#	Permission to copy, redistribute or otherwise
8246149Ssjg#	use this file is hereby granted provided that 
9246149Ssjg#	the above copyright notice and this notice are
10246149Ssjg#	left intact. 
11246149Ssjg#      
12246149Ssjg#	Please send copies of changes and bug-fixes to:
13246149Ssjg#	sjg@crufty.net
14246149Ssjg#
15246149Ssjg
16246149SsjgECHO_TRACE ?= echo
17246149Ssjg
18246149Ssjg.ifndef Mkdirs
19246149Ssjg# A race condition in some versions of mkdir, means that it can bail 
20246149Ssjg# if another process made a dir that mkdir expected to.
21246149Ssjg# We repeat the mkdir -p a number of times to try and work around this.
22246149Ssjg# We stop looping as soon as the dir exists.
23246149Ssjg# If we get to the end of the loop, a plain mkdir will issue an error.
24246149SsjgMkdirs= Mkdirs() { \
25246149Ssjg	for d in $$*; do \
26246149Ssjg		for i in 1 2 3 4 5 6; do \
27246149Ssjg			mkdir -p $$d; \
28246149Ssjg			test -d $$d && return 0; \
29246149Ssjg		done > /dev/null 2>&1; \
30246149Ssjg		mkdir $$d || exit $$?; \
31246149Ssjg	done; }
32246149Ssjg.endif
33246149Ssjg
34246149Ssjg# if MKOBJDIRS is set to auto (and NOOBJ isn't defined) do some magic...
35246149Ssjg# This will automatically create objdirs as needed.
36246149Ssjg# Skip it if we are just doing 'clean'.
37281812Ssjg.if ${MK_AUTO_OBJ:Uno} == "yes"
38281812SsjgMKOBJDIRS= auto
39281812Ssjg.endif
40246149Ssjg.if !defined(NOOBJ) && !defined(NO_OBJ) && ${MKOBJDIRS:Uno} == auto
41246149Ssjg# Use __objdir here so it is easier to tweak without impacting
42246149Ssjg# the logic.
43292733Ssjg.if !empty(MAKEOBJDIRPREFIX)
44319884Ssjg.if ${.CURDIR:M${MAKEOBJDIRPREFIX}/*} != ""
45319884Ssjg# we are already in obj tree!
46319884Ssjg__objdir?= ${.CURDIR}
47319884Ssjg.endif
48289842Ssjg__objdir?= ${MAKEOBJDIRPREFIX}${.CURDIR}
49289842Ssjg.endif
50289842Ssjg__objdir?= ${MAKEOBJDIR:Uobj}
51292733Ssjg__objdir:= ${__objdir}
52292733Ssjg.if ${.OBJDIR:tA} != ${__objdir:tA}
53246149Ssjg# We need to chdir, make the directory if needed
54246149Ssjg.if !exists(${__objdir}/) && \
55246149Ssjg	(${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
56246149Ssjg# This will actually make it... 
57246149Ssjg__objdir_made != echo ${__objdir}/; umask ${OBJDIR_UMASK:U002}; \
58246149Ssjg        ${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \
59246149Ssjg        ${Mkdirs}; Mkdirs ${__objdir}
60246149Ssjg.endif
61246149Ssjg# This causes make to use the specified directory as .OBJDIR
62246149Ssjg.OBJDIR: ${__objdir}
63331246Ssjg.if ${.OBJDIR:tA} != ${__objdir:tA}
64331246Ssjg# we did not get what we want - do we care?
65331246Ssjg.if ${__objdir_made:Uno:M${__objdir}/*} != ""
66319884Ssjg# watch out for __objdir being relative path
67319884Ssjg.if !(${__objdir:M/*} == "" && ${.OBJDIR:tA} == ${${.CURDIR}/${__objdir}:L:tA})
68281812Ssjg.error could not use ${__objdir}: .OBJDIR=${.OBJDIR}
69246149Ssjg.endif
70246149Ssjg.endif
71331246Ssjg# apparently we can live with it
72331246Ssjg# make sure we know what we have
73331246Ssjg.OBJDIR: ${.CURDIR}
74246149Ssjg.endif
75319884Ssjg.endif
76331246Ssjg.endif
77