1# SPDX-License-Identifier: BSD-2-Clause
2#
3# $Id: obj.mk,v 1.19 2024/02/19 00:06:19 sjg Exp $
4#
5#	@(#) Copyright (c) 1999-2010, 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
18# should be set properly in sys.mk
19_this ?= ${.PARSEFILE:S,bsd.,,}
20
21.if !target(__${_this}__)
22__${_this}__: .NOTMAIN
23
24.include <init.mk>
25
26ECHO_TRACE ?= echo
27
28.if ${MK_OBJDIRS} == "no"
29obj:
30objlink:
31objwarn:
32.else
33
34# this has to match how make behaves
35.if defined(MAKEOBJDIRPREFIX) || defined(MAKEOBJDIR)
36.if defined(MAKEOBJDIRPREFIX)
37__objdir:= ${MAKEOBJDIRPREFIX}${.CURDIR}
38.else
39__objdir:= ${MAKEOBJDIR}
40.endif
41.else
42__objdir= ${__objlink}
43.endif
44
45.if defined(OBJMACHINE)
46__objlink=	obj.${MACHINE}
47.else
48__objlink=	obj
49.endif
50
51.if ${MK_AUTO_OBJ} == "yes"
52.-include "auto.obj.mk"
53.endif
54
55.NOPATH:	${__objdir}
56.PHONY:		obj
57
58obj: _SUBDIRUSE
59	@if test ! -d ${__objdir}/.; then \
60		mkdir -p ${__objdir}; \
61		if test ! -d ${__objdir}; then \
62			mkdir ${__objdir}; exit 1; \
63		fi; \
64		${ECHO_TRACE} "[Creating objdir ${__objdir}...]" >&2; \
65	fi
66
67.if !target(_SUBDIRUSE)
68# this just allows us to be included by ourselves
69_SUBDIRUSE:
70.endif
71
72# so we can interact with freebsd makefiles
73.if !target(objwarn)
74objwarn:
75.if ${.OBJDIR} == ${.CURDIR}
76	@echo "Warning Object directory is ${.CURDIR}"
77.elif ${.OBJDIR} != ${__objdir}
78	@echo "Warning Object directory is ${.OBJDIR} vs. ${__objdir}"
79.endif
80.endif
81
82.if !target(objlink)
83objlink:
84.if ${__objdir:T} != ${__objlink}
85	@if test -d ${__objdir}/.; then \
86		${RM} -f ${.CURDIR}/${__objlink}; \
87		${LN} -s ${__objdir} ${.CURDIR}/${__objlink}; \
88		echo "${__objlink} -> ${__objdir}"; \
89	else \
90		echo "No ${__objdir} to link to - do a 'make obj'"; \
91	fi
92.endif
93.endif
94.endif
95
96_CURDIR?= ${.CURDIR}
97_OBJDIR?= ${.OBJDIR}
98
99.if !target(print-objdir)
100print-objdir:
101	@echo ${_OBJDIR}
102.endif
103
104.if !target(whereobj)
105whereobj:
106	@echo ${_OBJDIR}
107.endif
108
109.if !target(destroy)
110.if ${.CURDIR} != ${.OBJDIR}
111destroy:
112	(cd ${_CURDIR} && rm -rf ${_OBJDIR})
113.else
114destroy:  clean
115.endif
116.endif
117
118.endif
119