1246149Ssjg# $Id: auto.obj.mk,v 1.8 2011/08/08 17:35:20 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'.
37246149Ssjg.if !defined(NOOBJ) && !defined(NO_OBJ) && ${MKOBJDIRS:Uno} == auto
38246149Ssjg# Use __objdir here so it is easier to tweak without impacting
39246149Ssjg# the logic.
40246149Ssjg__objdir?= ${MAKEOBJDIR}
41246149Ssjg.if ${.OBJDIR} != ${__objdir}
42246149Ssjg# We need to chdir, make the directory if needed
43246149Ssjg.if !exists(${__objdir}/) && \
44246149Ssjg	(${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
45246149Ssjg# This will actually make it... 
46246149Ssjg__objdir_made != echo ${__objdir}/; umask ${OBJDIR_UMASK:U002}; \
47246149Ssjg        ${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \
48246149Ssjg        ${Mkdirs}; Mkdirs ${__objdir}
49246149Ssjg.endif
50246149Ssjg# This causes make to use the specified directory as .OBJDIR
51246149Ssjg.OBJDIR: ${__objdir}
52246149Ssjg.if ${.OBJDIR} != ${__objdir} && ${__objdir_made:Uno:M${__objdir}/*} != ""
53246149Ssjg.error could not use ${__objdir}
54246149Ssjg.endif
55246149Ssjg.endif
56246149Ssjg.endif
57