xref: /freebsd/contrib/bmake/mk/auto.obj.mk (revision c59c3bf34db360695f07735bebc76a768cac5afc)
1*c59c3bf3SSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
2*c59c3bf3SSimon J. Gerraty#
3*c59c3bf3SSimon J. Gerraty# $Id: auto.obj.mk,v 1.17 2024/02/17 17:26:57 sjg Exp $
43cbdda60SSimon J. Gerraty#
53cbdda60SSimon J. Gerraty#	@(#) Copyright (c) 2004, Simon J. Gerraty
63cbdda60SSimon J. Gerraty#
73cbdda60SSimon J. Gerraty#	This file is provided in the hope that it will
83cbdda60SSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
93cbdda60SSimon J. Gerraty#	Permission to copy, redistribute or otherwise
103cbdda60SSimon J. Gerraty#	use this file is hereby granted provided that
113cbdda60SSimon J. Gerraty#	the above copyright notice and this notice are
123cbdda60SSimon J. Gerraty#	left intact.
133cbdda60SSimon J. Gerraty#
143cbdda60SSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
153cbdda60SSimon J. Gerraty#	sjg@crufty.net
163cbdda60SSimon J. Gerraty#
173cbdda60SSimon J. Gerraty
183cbdda60SSimon J. GerratyECHO_TRACE ?= echo
193cbdda60SSimon J. Gerraty
203cbdda60SSimon J. Gerraty.ifndef Mkdirs
213cbdda60SSimon J. Gerraty# A race condition in some versions of mkdir, means that it can bail
223cbdda60SSimon J. Gerraty# if another process made a dir that mkdir expected to.
233cbdda60SSimon J. Gerraty# We repeat the mkdir -p a number of times to try and work around this.
243cbdda60SSimon J. Gerraty# We stop looping as soon as the dir exists.
253cbdda60SSimon J. Gerraty# If we get to the end of the loop, a plain mkdir will issue an error.
263cbdda60SSimon J. GerratyMkdirs= Mkdirs() { \
273cbdda60SSimon J. Gerraty	for d in $$*; do \
283cbdda60SSimon J. Gerraty		for i in 1 2 3 4 5 6; do \
293cbdda60SSimon J. Gerraty			mkdir -p $$d; \
303cbdda60SSimon J. Gerraty			test -d $$d && return 0; \
313cbdda60SSimon J. Gerraty		done > /dev/null 2>&1; \
323cbdda60SSimon J. Gerraty		mkdir $$d || exit $$?; \
333cbdda60SSimon J. Gerraty	done; }
343cbdda60SSimon J. Gerraty.endif
353cbdda60SSimon J. Gerraty
363cbdda60SSimon J. Gerraty# if MKOBJDIRS is set to auto (and NOOBJ isn't defined) do some magic...
373cbdda60SSimon J. Gerraty# This will automatically create objdirs as needed.
383cbdda60SSimon J. Gerraty# Skip it if we are just doing 'clean'.
39db29cad8SSimon J. Gerraty.if ${MK_AUTO_OBJ:Uno} == "yes"
40db29cad8SSimon J. GerratyMKOBJDIRS= auto
41db29cad8SSimon J. Gerraty.endif
423cbdda60SSimon J. Gerraty.if !defined(NOOBJ) && !defined(NO_OBJ) && ${MKOBJDIRS:Uno} == auto
433cbdda60SSimon J. Gerraty# Use __objdir here so it is easier to tweak without impacting
443cbdda60SSimon J. Gerraty# the logic.
456e050540SSimon J. Gerraty.if !empty(MAKEOBJDIRPREFIX)
46e1cee40dSSimon J. Gerraty.if ${.CURDIR:M${MAKEOBJDIRPREFIX}/*} != ""
47e1cee40dSSimon J. Gerraty# we are already in obj tree!
48e1cee40dSSimon J. Gerraty__objdir?= ${.CURDIR}
49e1cee40dSSimon J. Gerraty.endif
504c620fe5SSimon J. Gerraty__objdir?= ${MAKEOBJDIRPREFIX}${.CURDIR}
514c620fe5SSimon J. Gerraty.endif
524c620fe5SSimon J. Gerraty__objdir?= ${MAKEOBJDIR:Uobj}
536e050540SSimon J. Gerraty__objdir:= ${__objdir}
546e050540SSimon J. Gerraty.if ${.OBJDIR:tA} != ${__objdir:tA}
553cbdda60SSimon J. Gerraty# We need to chdir, make the directory if needed
563cbdda60SSimon J. Gerraty.if !exists(${__objdir}/) && \
573cbdda60SSimon J. Gerraty	(${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "")
583cbdda60SSimon J. Gerraty# This will actually make it...
593cbdda60SSimon J. Gerraty__objdir_made != echo ${__objdir}/; umask ${OBJDIR_UMASK:U002}; \
603cbdda60SSimon J. Gerraty        ${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \
613cbdda60SSimon J. Gerraty        ${Mkdirs}; Mkdirs ${__objdir}
623cbdda60SSimon J. Gerraty.endif
633cbdda60SSimon J. Gerraty# This causes make to use the specified directory as .OBJDIR
643cbdda60SSimon J. Gerraty.OBJDIR: ${__objdir}
6550d2e745SSimon J. Gerraty.if ${.OBJDIR:tA} != ${__objdir:tA}
6650d2e745SSimon J. Gerraty# we did not get what we want - do we care?
6750d2e745SSimon J. Gerraty.if ${__objdir_made:Uno:M${__objdir}/*} != ""
68b46b9039SSimon J. Gerraty# watch out for __objdir being relative path
69b46b9039SSimon J. Gerraty.if !(${__objdir:M/*} == "" && ${.OBJDIR:tA} == ${${.CURDIR}/${__objdir}:L:tA})
70db29cad8SSimon J. Gerraty.error could not use ${__objdir}: .OBJDIR=${.OBJDIR}
713cbdda60SSimon J. Gerraty.endif
723cbdda60SSimon J. Gerraty.endif
7350d2e745SSimon J. Gerraty# apparently we can live with it
7450d2e745SSimon J. Gerraty# make sure we know what we have
7550d2e745SSimon J. Gerraty.OBJDIR: ${.CURDIR}
7650d2e745SSimon J. Gerraty.endif
773cbdda60SSimon J. Gerraty.endif
78b46b9039SSimon J. Gerraty.endif
79