xref: /freebsd/share/mk/auto.obj.mk (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1# $FreeBSD$
2# $Id: auto.obj.mk,v 1.12 2015/12/16 01:57:06 sjg Exp $
3#
4#	@(#) Copyright (c) 2004, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17ECHO_TRACE ?= echo
18
19.ifndef Mkdirs
20# A race condition in some versions of mkdir, means that it can bail
21# if another process made a dir that mkdir expected to.
22# We repeat the mkdir -p a number of times to try and work around this.
23# We stop looping as soon as the dir exists.
24# If we get to the end of the loop, a plain mkdir will issue an error.
25Mkdirs= Mkdirs() { \
26	for d in $$*; do \
27		for i in 1 2 3 4 5 6; do \
28			mkdir -p $$d; \
29			test -d $$d && return 0; \
30		done > /dev/null 2>&1; \
31		mkdir $$d || exit $$?; \
32	done; }
33.endif
34
35# if MKOBJDIRS is set to auto (and NOOBJ isn't defined) do some magic...
36# This will automatically create objdirs as needed.
37# Skip it if we are just doing 'clean'.
38.if ${MK_AUTO_OBJ:Uno} == "yes"
39MKOBJDIRS= auto
40.endif
41.if !defined(NOOBJ) && !defined(NO_OBJ) && ${MKOBJDIRS:Uno} == auto
42# Use __objdir here so it is easier to tweak without impacting
43# the logic.
44.if !empty(MAKEOBJDIRPREFIX)
45.if ${.CURDIR:M${MAKEOBJDIRPREFIX}/*} != ""
46# we are already in obj tree!
47__objdir?= ${.CURDIR}
48.endif
49__objdir?= ${MAKEOBJDIRPREFIX}${.CURDIR}
50.endif
51__objdir?= ${MAKEOBJDIR:Uobj}
52__objdir:= ${__objdir}
53.if ${.OBJDIR:tA} != ${__objdir:tA}
54# We need to chdir, make the directory if needed
55.if !exists(${__objdir}/) && \
56	(${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
57# This will actually make it...
58__objdir_made != echo ${__objdir}/; umask ${OBJDIR_UMASK:U002}; \
59        ${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \
60        ${Mkdirs}; Mkdirs ${__objdir}
61.endif
62# This causes make to use the specified directory as .OBJDIR
63.OBJDIR: ${__objdir}
64.if ${.OBJDIR:tA} != ${__objdir:tA}
65# we did not get what we want - do we care?
66.if ${__objdir_made:Uno:M${__objdir}/*} != ""
67# watch out for __objdir being relative path
68.if !(${__objdir:M/*} == "" && ${.OBJDIR:tA} == ${${.CURDIR}/${__objdir}:L:tA})
69.error could not use ${__objdir}: .OBJDIR=${.OBJDIR}
70.endif
71.endif
72# apparently we can live with it
73# make sure we know what we have
74.OBJDIR: ${.CURDIR}
75.endif
76.endif
77.endif
78