xref: /src/contrib/bmake/mk/srctop.mk (revision c60f6422ffae3ea85e7b10bad950ad27c463af18)
1# $Id: srctop.mk,v 1.6 2025/08/09 22:42:24 sjg Exp $
2#
3#	@(#) Copyright (c) 2012, Simon J. Gerraty
4#
5#	SPDX-License-Identifier: BSD-2-Clause
6#
7#	Please send copies of changes and bug-fixes to:
8#	sjg@crufty.net
9#
10
11.if !defined(SRCTOP)
12# if using mk(1) SB will be set.
13.ifdef SB
14.if ${.CURDIR:S,${SB},,} != ${.CURDIR}
15# we are actually within SB
16.ifdef SB_SRC
17SRCTOP:= ${SB_SRC}
18.elif exists(${SB}/src)
19SRCTOP:= ${SB}/src
20.else
21SRCTOP:= ${SB}
22.endif
23.endif
24.endif
25
26.if !defined(SRCTOP)
27.for rd in share/mk build/mk mk
28.if ${_this_mk_dir:M*${rd}} != ""
29.if ${.CURDIR:S,${_this_mk_dir:${rd:C,[^/]+,H,g:S,/, ,g:ts:}},,} != ${.CURDIR}
30SRCTOP:= ${_this_mk_dir:${rd:C,[^/]+,H,g:S,/, ,g:ts:}}
31.endif
32.endif
33.endfor
34.endif
35
36.if !defined(SRCTOP)
37_SRCTOP_TEST_?= [ -f ../.sandbox-env -o -d share/mk ]
38# Linux at least has a bug where attempting to check an automounter
39# directory will hang.  So avoid looking above /a/b
40SRCTOP!= cd ${.CURDIR}; while :; do \
41		here=`pwd`; \
42		${_SRCTOP_TEST_} && { echo $$here; break; }; \
43		case $$here in /*/*/*) cd ..;; *) echo ""; break;; esac; \
44		done
45.endif
46.if defined(SRCTOP) && exists(${SRCTOP}/.)
47.export SRCTOP
48.endif
49.endif
50
51.if !defined(OBJTOP) && !empty(SRCTOP)
52.if defined(MAKEOBJDIRPREFIX) && exists(${MAKEOBJDIRPREFIX}${SRCTOP})
53OBJTOP= ${MAKEOBJDIRPREFIX}${SRCTOP}
54.elif (exists(${SRCTOP}/Makefile) || exists(${SRCTOP}/makefile))
55OBJTOP!= cd ${SRCTOP} && ${PRINTOBJDIR}
56.endif
57.if empty(OBJTOP)
58OBJTOP= ${SRCTOP}
59.endif
60.export OBJTOP
61.endif
62