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