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