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