xref: /freebsd/share/mk/auto.obj.mk (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
15f99c0d3SBryan Drewery# $Id: auto.obj.mk,v 1.12 2015/12/16 01:57:06 sjg Exp $
27750ad47SMarcel Moolenaar#
37750ad47SMarcel Moolenaar#	@(#) Copyright (c) 2004, Simon J. Gerraty
47750ad47SMarcel Moolenaar#
57750ad47SMarcel Moolenaar#	This file is provided in the hope that it will
67750ad47SMarcel Moolenaar#	be of use.  There is absolutely NO WARRANTY.
77750ad47SMarcel Moolenaar#	Permission to copy, redistribute or otherwise
87750ad47SMarcel Moolenaar#	use this file is hereby granted provided that
97750ad47SMarcel Moolenaar#	the above copyright notice and this notice are
107750ad47SMarcel Moolenaar#	left intact.
117750ad47SMarcel Moolenaar#
127750ad47SMarcel Moolenaar#	Please send copies of changes and bug-fixes to:
137750ad47SMarcel Moolenaar#	sjg@crufty.net
147750ad47SMarcel Moolenaar#
157750ad47SMarcel Moolenaar
167750ad47SMarcel MoolenaarECHO_TRACE ?= echo
177750ad47SMarcel Moolenaar
187750ad47SMarcel Moolenaar.ifndef Mkdirs
197750ad47SMarcel Moolenaar# A race condition in some versions of mkdir, means that it can bail
207750ad47SMarcel Moolenaar# if another process made a dir that mkdir expected to.
217750ad47SMarcel Moolenaar# We repeat the mkdir -p a number of times to try and work around this.
227750ad47SMarcel Moolenaar# We stop looping as soon as the dir exists.
237750ad47SMarcel Moolenaar# If we get to the end of the loop, a plain mkdir will issue an error.
247750ad47SMarcel MoolenaarMkdirs= Mkdirs() { \
257750ad47SMarcel Moolenaar	for d in $$*; do \
267750ad47SMarcel Moolenaar		for i in 1 2 3 4 5 6; do \
277750ad47SMarcel Moolenaar			mkdir -p $$d; \
287750ad47SMarcel Moolenaar			test -d $$d && return 0; \
297750ad47SMarcel Moolenaar		done > /dev/null 2>&1; \
307750ad47SMarcel Moolenaar		mkdir $$d || exit $$?; \
317750ad47SMarcel Moolenaar	done; }
327750ad47SMarcel Moolenaar.endif
337750ad47SMarcel Moolenaar
347750ad47SMarcel Moolenaar# if MKOBJDIRS is set to auto (and NOOBJ isn't defined) do some magic...
357750ad47SMarcel Moolenaar# This will automatically create objdirs as needed.
367750ad47SMarcel Moolenaar# Skip it if we are just doing 'clean'.
37566bf19aSSimon J. Gerraty.if ${MK_AUTO_OBJ:Uno} == "yes"
38566bf19aSSimon J. GerratyMKOBJDIRS= auto
39566bf19aSSimon J. Gerraty.endif
407750ad47SMarcel Moolenaar.if !defined(NOOBJ) && !defined(NO_OBJ) && ${MKOBJDIRS:Uno} == auto
417750ad47SMarcel Moolenaar# Use __objdir here so it is easier to tweak without impacting
427750ad47SMarcel Moolenaar# the logic.
435f99c0d3SBryan Drewery.if !empty(MAKEOBJDIRPREFIX)
44d86e8e9cSSimon J. Gerraty.if ${.CURDIR:M${MAKEOBJDIRPREFIX}/*} != ""
45d86e8e9cSSimon J. Gerraty# we are already in obj tree!
46d86e8e9cSSimon J. Gerraty__objdir?= ${.CURDIR}
47d86e8e9cSSimon J. Gerraty.endif
48b56a78b6SSimon J. Gerraty__objdir?= ${MAKEOBJDIRPREFIX}${.CURDIR}
49b56a78b6SSimon J. Gerraty.endif
50b56a78b6SSimon J. Gerraty__objdir?= ${MAKEOBJDIR:Uobj}
515f99c0d3SBryan Drewery__objdir:= ${__objdir}
525f99c0d3SBryan Drewery.if ${.OBJDIR:tA} != ${__objdir:tA}
537750ad47SMarcel Moolenaar# We need to chdir, make the directory if needed
547750ad47SMarcel Moolenaar.if !exists(${__objdir}/) && \
557750ad47SMarcel Moolenaar	(${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
567750ad47SMarcel Moolenaar# This will actually make it...
577750ad47SMarcel Moolenaar__objdir_made != echo ${__objdir}/; umask ${OBJDIR_UMASK:U002}; \
587750ad47SMarcel Moolenaar        ${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \
597750ad47SMarcel Moolenaar        ${Mkdirs}; Mkdirs ${__objdir}
607750ad47SMarcel Moolenaar.endif
617750ad47SMarcel Moolenaar# This causes make to use the specified directory as .OBJDIR
627750ad47SMarcel Moolenaar.OBJDIR: ${__objdir}
63*5abdb50eSSimon J. Gerraty.if ${.OBJDIR:tA} != ${__objdir:tA}
64*5abdb50eSSimon J. Gerraty# we did not get what we want - do we care?
65*5abdb50eSSimon J. Gerraty.if ${__objdir_made:Uno:M${__objdir}/*} != ""
6660297063SSimon J. Gerraty# watch out for __objdir being relative path
6760297063SSimon J. Gerraty.if !(${__objdir:M/*} == "" && ${.OBJDIR:tA} == ${${.CURDIR}/${__objdir}:L:tA})
68ccfb9654SSimon J. Gerraty.error could not use ${__objdir}: .OBJDIR=${.OBJDIR}
697750ad47SMarcel Moolenaar.endif
707750ad47SMarcel Moolenaar.endif
71*5abdb50eSSimon J. Gerraty# apparently we can live with it
72*5abdb50eSSimon J. Gerraty# make sure we know what we have
73*5abdb50eSSimon J. Gerraty.OBJDIR: ${.CURDIR}
74*5abdb50eSSimon J. Gerraty.endif
757750ad47SMarcel Moolenaar.endif
7660297063SSimon J. Gerraty.endif
77