150476Speter# $FreeBSD: releng/10.3/share/mk/bsd.obj.mk 292240 2015-12-15 01:37:46Z bdrewery $
215353Swosch#
318566Sbde# The include file <bsd.obj.mk> handles creating the 'obj' directory
431859Sbde# and cleaning up object files, etc.
515353Swosch#
615353Swosch# +++ variables +++
715353Swosch#
833815Sbde# CLEANDIRS	Additional directories to remove for the clean target.
915948Swosch#
1033815Sbde# CLEANFILES	Additional files to remove for the clean target.
1133815Sbde#
12139761Skrion# MAKEOBJDIR 	A pathname for the directory where the targets
1399202Sru#		are built.  Note: MAKEOBJDIR is an *environment* variable
1499202Sru#		and works properly only if set as an environment variable,
1518566Sbde#		not as a global or command line variable!
1615353Swosch#
1718566Sbde#		E.g. use `env MAKEOBJDIR=temp-obj make'
1818566Sbde#
1918566Sbde# MAKEOBJDIRPREFIX  Specifies somewhere other than /usr/obj to root the object
2099202Sru#		tree.  Note: MAKEOBJDIRPREFIX is an *environment* variable
2199202Sru#		and works properly only if set as an environment variable,
2218566Sbde#		not as a global or command line variable!
2318566Sbde#
2418340Sswallace#		E.g. use `env MAKEOBJDIRPREFIX=/somewhere/obj make'
2515948Swosch#
26139103Sru# NO_OBJ	Do not create object directories.  This should not be set
2718566Sbde#		if anything is built.
2815353Swosch#
2915353Swosch# +++ targets +++
3015353Swosch#
3115353Swosch#	clean:
3233815Sbde#		remove ${CLEANFILES}; remove ${CLEANDIRS} and all contents.
3315353Swosch#
3415353Swosch#	cleandir:
3516663Sjkh#		remove the build directory (and all its contents) created by obj
3615353Swosch#
3715353Swosch#	obj:
3816663Sjkh#		create build directory.
3915353Swosch#
4014801Swosch
4194940Sru.if !target(__<bsd.obj.mk>__)
4294940Sru__<bsd.obj.mk>__:
4394940Sru.include <bsd.own.mk>
4494940Sru
4564784Ssheldonh.if defined(MAKEOBJDIRPREFIX)
4618340SswallaceCANONICALOBJDIR:=${MAKEOBJDIRPREFIX}${.CURDIR}
47241298Smarcel.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
48241298SmarcelCANONICALOBJDIR:=${MAKEOBJDIR}
49292240SbdreweryOBJTOP?= ${MAKEOBJDIR}
5018340Sswallace.else
5118340SswallaceCANONICALOBJDIR:=/usr/obj${.CURDIR}
5218340Sswallace.endif
5314801Swosch
54292240SbdreweryOBJTOP?= ${.OBJDIR:S,${.CURDIR},,}${SRCTOP}
55292240Sbdrewery
5618340Sswallace#
5776896Sru# Warn of unorthodox object directory.
5818340Sswallace#
5976896Sru# The following directories are tried in order for ${.OBJDIR}:
6076896Sru#
6176896Sru# 1.  ${MAKEOBJDIRPREFIX}/`pwd`
6276896Sru# 2.  ${MAKEOBJDIR}
6376896Sru# 3.  obj.${MACHINE}
6476896Sru# 4.  obj
6576896Sru# 5.  /usr/obj/`pwd`
6676896Sru# 6.  ${.CURDIR}
6776896Sru#
6876896Sru# If ${.OBJDIR} is constructed using canonical cases 1 or 5, or
6976896Sru# case 2 (using MAKEOBJDIR), don't issue a warning.  Otherwise,
7076896Sru# issue a warning differentiating between cases 6 and (3 or 4).
7176896Sru#
7218340Sswallaceobjwarn:
73139103Sru.if !defined(NO_OBJ) && ${.OBJDIR} != ${CANONICALOBJDIR} && \
7476896Sru    !(defined(MAKEOBJDIRPREFIX) && exists(${CANONICALOBJDIR}/)) && \
7576896Sru    !(defined(MAKEOBJDIR) && exists(${MAKEOBJDIR}/))
7618340Sswallace.if ${.OBJDIR} == ${.CURDIR}
7718373Speter	@${ECHO} "Warning: Object directory not changed from original ${.CURDIR}"
7876896Sru.elif exists(${.CURDIR}/obj.${MACHINE}/) || exists(${.CURDIR}/obj/)
7918373Speter	@${ECHO} "Warning: Using ${.OBJDIR} as object directory instead of\
8018340Sswallace		canonical ${CANONICALOBJDIR}"
8118340Sswallace.endif
8218340Sswallace.endif
8318340Sswallace
84139103Sru.if !defined(NO_OBJ)
8598546Sru.if !target(obj)
86128188Sruobj: .PHONY
8725316Sbde	@if ! test -d ${CANONICALOBJDIR}/; then \
8818340Sswallace		mkdir -p ${CANONICALOBJDIR}; \
8925316Sbde		if ! test -d ${CANONICALOBJDIR}/; then \
9018340Sswallace			${ECHO} "Unable to create ${CANONICALOBJDIR}."; \
9116663Sjkh			exit 1; \
9216663Sjkh		fi; \
9318340Sswallace		${ECHO} "${CANONICALOBJDIR} created for ${.CURDIR}"; \
9416663Sjkh	fi
95280329Sdim.for dir in ${SRCS:H:O:u}
96280329Sdim	@if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
97280329Sdim		mkdir -p ${CANONICALOBJDIR}/${dir}; \
98280329Sdim		if ! test -d ${CANONICALOBJDIR}/${dir}/; then \
99280329Sdim			${ECHO} "Unable to create ${CANONICALOBJDIR}/${dir}."; \
100280329Sdim			exit 1; \
101280329Sdim		fi; \
102280329Sdim		${ECHO} "${CANONICALOBJDIR}/${dir} created for ${.CURDIR}"; \
103280329Sdim	fi
104280329Sdim.endfor
10514801Swosch.endif
10614801Swosch
10744922Sbde.if !target(objlink)
10895306Sruobjlink:
10944922Sbde	@if test -d ${CANONICALOBJDIR}/; then \
11044922Sbde		rm -f ${.CURDIR}/obj; \
11144922Sbde		ln -s ${CANONICALOBJDIR} ${.CURDIR}/obj; \
11244922Sbde	else \
11344922Sbde		echo "No ${CANONICALOBJDIR} to link to - do a make obj."; \
11444922Sbde	fi
11544922Sbde.endif
116144893Sharti.endif # !defined(NO_OBJ)
11744922Sbde
11815166Swosch#
11917116Spst# where would that obj directory be?
12017116Spst#
12117116Spst.if !target(whereobj)
12217116Spstwhereobj:
12336641Speter	@echo ${.OBJDIR}
12417116Spst.endif
12517116Spst
126124061Sgreen.if ${CANONICALOBJDIR} != ${.CURDIR} && exists(${CANONICALOBJDIR}/)
12716663Sjkhcleanobj:
12876021Sru	@rm -rf ${CANONICALOBJDIR}
12976021Sru.else
130124061Sgreencleanobj: clean cleandepend
13176021Sru.endif
132102660Skris	@if [ -L ${.CURDIR}/obj ]; then rm -f ${.CURDIR}/obj; fi
13315166Swosch
134241298Smarcel# Tell bmake not to look for generated files via .PATH
135241298Smarcel.if !empty(CLEANFILES)
136241298Smarcel.NOPATH: ${CLEANFILES}
137241298Smarcel.endif
138241298Smarcel
13931859Sbde.if !target(clean)
14095306Sruclean:
14131859Sbde.if defined(CLEANFILES) && !empty(CLEANFILES)
142139761Skrion	rm -f ${CLEANFILES}
14316663Sjkh.endif
14433815Sbde.if defined(CLEANDIRS) && !empty(CLEANDIRS)
14533815Sbde	rm -rf ${CLEANDIRS}
14615166Swosch.endif
14733815Sbde.endif
14815166Swosch
14995306Srucleandir: cleanobj
15035789Sbde
15195306Sru.include <bsd.subdir.mk>
15235789Sbde
153144893Sharti.endif # !target(__<bsd.obj.mk>__)
154