xref: /freebsd/contrib/bmake/mk/sys.clean-env.mk (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# $Id: sys.clean-env.mk,v 1.26 2024/02/17 17:26:57 sjg Exp $
4#
5#	@(#) Copyright (c) 2009, 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# This makefile would normally be included by sys.env.mk
19
20# The variables used by this makefile include:
21#
22# MAKE_ENV_SAVE_VAR_LIST
23#	The actuall list of variables from the environment that will be
24#	preserved.
25# MAKE_ENV_SAVE_PREFIX_LIST
26#	A list of prefixes to match against the environment - the results
27#	are added to MAKE_ENV_SAVE_VAR_LIST after being filtered by...
28# MAKE_ENV_SAVE_EXCLUDE_LIST
29#	A list of words or patterns which is turned into a list of :N
30#	modifiers.
31
32.if ${.MAKE.LEVEL} == 0 && ${MAKE_VERSION} >= 20100606
33# We save any env var that starts with the words in MAKE_ENV_SAVE_PREFIX_LIST.
34# This gets expanded to an egrep expression like '^(A|B|C...)'
35# and added to MAKE_ENV_SAVE_VAR_LIST below.
36# If any of these end up being too greedy, MAKE_ENV_SAVE_EXCLUDE_LIST
37# can be used to filter.
38MAKE_ENV_SAVE_PREFIX_LIST += \
39	CCACHE \
40	CVS \
41	DEBUG \
42	DISTCC \
43	HOST \
44	MACHINE \
45	MAKE \
46	MK \
47	NEED_ \
48	SB_ \
49	SSH \
50	SVN \
51	USE_ \
52	WITH_ \
53	WITHOUT_ \
54
55
56# This could be a list of vars or patterns to explicitly exclude.
57MAKE_ENV_SAVE_EXCLUDE_LIST += _
58
59# This is the actual list that we will save
60# HOME is probably something worth clobbering eg.
61# HOME=/var/empty
62MAKE_ENV_SAVE_VAR_LIST += \
63	HOME \
64	LOGNAME \
65	OBJROOT \
66	OBJTOP \
67	PATH \
68	SB \
69	SRCTOP \
70	USER \
71	${_env_vars:${MAKE_ENV_SAVE_EXCLUDE_LIST:${M_ListToSkip}}}
72
73_env_vars != env | ${EGREP:Uegrep} '^(${MAKE_ENV_SAVE_PREFIX_LIST:ts|})' | sed 's,=.*,,'; echo
74
75_export_list =
76.for v in ${MAKE_ENV_SAVE_VAR_LIST:O:u}
77.if defined($v)
78_export_list += $v
79# Save current value
80$v := ${$v}
81.endif
82.endfor
83
84# Now, clobber the environment
85.unexport-env
86
87# This is a list of vars that we handle specially below
88_tricky_env_vars = MAKEOBJDIR OBJTOP
89# Export our selection - sans tricky ones
90.export ${_export_list:${_tricky_env_vars:${M_ListToSkip}}}
91
92# This next bit may need tweaking
93# if you don't happen to like the way I set it.
94.if defined(MAKEOBJDIR)
95# We are going to set this to the equivalent of the shell's
96# MAKEOBJDIR='${.CURDIR:S,${SRCTOP},${OBJTOP},}'
97_srctop := ${SRCTOP:U${SB_SRC:U${SB}/src}}
98_objroot := ${OBJROOT:U${SB_OBJROOT:U${SB}/${SB_OBJPREFIX}}}
99.if ${MAKE_VERSION} < 20160218
100_objtop := ${OBJTOP:U${_objroot}${MACHINE}}
101# Take care of ${MACHINE}
102.if ${MACHINE:Nhost*} == "" || ${OBJTOP} == ${HOST_OBJTOP:Uno}
103OBJTOP = ${_objtop:S,${HOST_TARGET}$,\${MACHINE},}
104.else
105OBJTOP = ${_objtop:S,${MACHINE}$,\${MACHINE},}
106.endif
107# Export like this
108MAKEOBJDIR = $${.CURDIR:S,${_srctop},$${OBJTOP},}
109#.info ${MAKE_SAVE_ENV_VARS _srctop _objroot _objtop OBJTOP MAKEOBJDIR:L:@v@${.newline}$v=${$v}@}
110
111# Export these as-is, and do not track...
112# otherwise the environment will be ruined when we evaluate them below.
113.export-env ${_tricky_env_vars}
114
115# Now evaluate for ourselves
116.for v in ${_tricky_env_vars}
117$v := ${$v}
118.endfor
119.else
120# we cannot rely on the '$$' trick (depending on .MAKE.SAVE_DOLLARS)
121# but we can export a literal (unexpanded) value
122SRCTOP := ${_srctop}
123OBJROOT := ${_objroot}
124OBJTOP = ${OBJROOT}${MACHINE}
125MAKEOBJDIR = ${.CURDIR:S,${SRCTOP},${OBJTOP},}
126.export-literal SRCTOP OBJROOT ${_tricky_env_vars}
127.endif
128#.info ${_tricky_env_vars:@v@${.newline}$v=${$v}@}
129#showenv:
130#	@env | ${EGREP:Uegrep} 'OBJ|SRC'
131.endif				# MAKEOBJDIR
132.endif				# level 0
133