1# SPDX-License-Identifier: BSD-2-Clause 2# 3# $Id: auto.obj.mk,v 1.20 2025/05/17 15:29:55 sjg Exp $ 4# 5# @(#) Copyright (c) 2004-2025, 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# relative dirs can cause trouble below 54# keep it simple and convert to absolute path now if needed 55.if ${__objdir:M/*} == "" 56# avoid ugly ${.CURDIR}/./obj etc. 57__objdir:= ${.CURDIR}/${__objdir:S,^./,,} 58.endif 59.if ${.OBJDIR:tA} != ${__objdir:tA} 60# We need to chdir, make the directory if needed 61.if !exists(${__objdir}/) && \ 62 (${.TARGETS} == "" || ${.TARGETS:Nclean*:N*clean:Ndestroy*} != "") 63# This will actually make it... 64__objdir_made != echo ${__objdir}/; umask ${OBJDIR_UMASK:U002}; \ 65 ${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \ 66 ${Mkdirs}; Mkdirs ${__objdir} 67.endif 68# This causes make to use the specified directory as .OBJDIR 69.OBJDIR: ${__objdir} 70.if ${.OBJDIR:tA} != ${__objdir:tA} 71# we did not get what we want - do we care? 72.if ${__objdir_made:Uno:M${__objdir}/*} != "" 73# we attempted to make ${__objdir} and failed 74.error could not use ${__objdir}: .OBJDIR=${.OBJDIR} 75.endif 76# apparently we can live with it 77# make sure we know what we have 78.OBJDIR: ${.CURDIR} 79.endif 80.endif 81.endif 82