xref: /freebsd/contrib/bmake/mk/srctop.mk (revision a91a246563dffa876a52f53a98de4af9fa364c52)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# $Id: srctop.mk,v 1.5 2024/02/17 17:26:57 sjg Exp $
4#
5#	@(#) Copyright (c) 2012, 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.if !defined(SRCTOP)
19# if using mk(1) SB will be set.
20.ifdef SB
21.if ${.CURDIR:S,${SB},,} != ${.CURDIR}
22# we are actually within SB
23.ifdef SB_SRC
24SRCTOP:= ${SB_SRC}
25.elif exists(${SB}/src)
26SRCTOP:= ${SB}/src
27.else
28SRCTOP:= ${SB}
29.endif
30.endif
31.endif
32
33.if !defined(SRCTOP)
34.for rd in share/mk build/mk mk
35.if ${_this_mk_dir:M*${rd}} != ""
36.if ${.CURDIR:S,${_this_mk_dir:${rd:C,[^/]+,H,g:S,/, ,g:ts:}},,} != ${.CURDIR}
37SRCTOP:= ${_this_mk_dir:${rd:C,[^/]+,H,g:S,/, ,g:ts:}}
38.endif
39.endif
40.endfor
41.endif
42
43.if !defined(SRCTOP)
44_SRCTOP_TEST_?= [ -f ../.sandbox-env -o -d share/mk ]
45# Linux at least has a bug where attempting to check an automounter
46# directory will hang.  So avoid looking above /a/b
47SRCTOP!= cd ${.CURDIR}; while :; do \
48		here=`pwd`; \
49		${_SRCTOP_TEST_} && { echo $$here; break; }; \
50		case $$here in /*/*/*) cd ..;; *) echo ""; break;; esac; \
51		done
52.endif
53.if defined(SRCTOP) && exists(${SRCTOP}/.)
54.export SRCTOP
55.endif
56.endif
57
58.if !defined(OBJTOP) && !empty(SRCTOP)
59.if defined(MAKEOBJDIRPREFIX) && exists(${MAKEOBJDIRPREFIX}${SRCTOP})
60OBJTOP= ${MAKEOBJDIRPREFIX}${SRCTOP}
61.elif (exists(${SRCTOP}/Makefile) || exists(${SRCTOP}/makefile))
62OBJTOP!= cd ${SRCTOP} && ${PRINTOBJDIR}
63.endif
64.if empty(OBJTOP)
65OBJTOP= ${SRCTOP}
66.endif
67.export OBJTOP
68.endif
69