1246149Ssjg# $Id: sys.clean-env.mk,v 1.20 2012/11/12 06:56:04 sjg Exp $
2246149Ssjg#
3246149Ssjg#	@(#) Copyright (c) 2009, Simon J. Gerraty
4246149Ssjg#
5246149Ssjg#	This file is provided in the hope that it will
6246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
7246149Ssjg#	Permission to copy, redistribute or otherwise
8246149Ssjg#	use this file is hereby granted provided that 
9246149Ssjg#	the above copyright notice and this notice are
10246149Ssjg#	left intact. 
11246149Ssjg#      
12246149Ssjg#	Please send copies of changes and bug-fixes to:
13246149Ssjg#	sjg@crufty.net
14246149Ssjg#
15246149Ssjg
16246149Ssjg# This makefile would normally be included by sys.env.mk
17246149Ssjg
18246149Ssjg# The variables used by this makefile include:
19246149Ssjg#
20246149Ssjg# MAKE_ENV_SAVE_VAR_LIST
21246149Ssjg#	The actuall list of variables from the environment that will be
22246149Ssjg#	preserved.
23246149Ssjg# MAKE_ENV_SAVE_PREFIX_LIST
24246149Ssjg#	A list of prefixes to match against the environment - the results
25246149Ssjg#	are added to MAKE_ENV_SAVE_VAR_LIST after being filtered by...
26246149Ssjg# MAKE_ENV_SAVE_EXCLUDE_LIST
27246149Ssjg#	A list of words or patterns which is turned into a list of :N
28246149Ssjg#	modifiers.
29246149Ssjg
30246149Ssjg.if ${.MAKE.LEVEL} == 0 && ${MAKE_VERSION} >= 20100606
31246149Ssjg# We save any env var that starts with the words in MAKE_ENV_SAVE_PREFIX_LIST.
32246149Ssjg# This gets expanded to an egrep expression like '^(A|B|C...)'
33246149Ssjg# and added to MAKE_ENV_SAVE_VAR_LIST below.
34246149Ssjg# If any of these end up being too greedy, MAKE_ENV_SAVE_EXCLUDE_LIST
35246149Ssjg# can be used to filter.
36246149SsjgMAKE_ENV_SAVE_PREFIX_LIST += \
37246149Ssjg	CCACHE \
38246149Ssjg	CVS \
39246149Ssjg	DEBUG \
40246149Ssjg	DISTCC \
41246149Ssjg	HOST \
42246149Ssjg	MACHINE \
43246149Ssjg	MAKE \
44246149Ssjg	MK \
45246149Ssjg	NEED_ \
46246149Ssjg	SB_ \
47246149Ssjg	SSH \
48246149Ssjg	SVN \
49246149Ssjg	USE_ \
50246149Ssjg	WITH_ \
51246149Ssjg	WITHOUT_ \
52246149Ssjg
53246149Ssjg
54246149Ssjg# This could be a list of vars or patterns to explicitly exclude.
55246149SsjgMAKE_ENV_SAVE_EXCLUDE_LIST ?= _
56246149Ssjg
57246149Ssjg# This is the actual list that we will save
58246149Ssjg# HOME is probably something worth clobbering eg. 
59246149Ssjg# HOME=/var/empty
60246149SsjgMAKE_ENV_SAVE_VAR_LIST += \
61246149Ssjg	HOME \
62246149Ssjg	LOGNAME \
63246149Ssjg	OBJROOT \
64246149Ssjg	OBJTOP \
65246149Ssjg	PATH \
66246149Ssjg	SB \
67246149Ssjg	SRCTOP \
68246149Ssjg	USER \
69246149Ssjg	${_env_vars:${MAKE_ENV_SAVE_EXCLUDE_LIST:${M_ListToSkip}}}
70246149Ssjg
71246149Ssjg_env_vars != env | egrep '^(${MAKE_ENV_SAVE_PREFIX_LIST:ts|})' | sed 's,=.*,,'; echo
72246149Ssjg
73246149Ssjg_export_list =
74246149Ssjg.for v in ${MAKE_ENV_SAVE_VAR_LIST:O:u}
75246149Ssjg.if defined($v)
76246149Ssjg_export_list += $v
77246149Ssjg# Save current value
78246149Ssjg$v := ${$v}
79246149Ssjg.endif
80246149Ssjg.endfor
81246149Ssjg
82246149Ssjg# Now, clobber the environment
83246149Ssjg.unexport-env
84246149Ssjg
85246149Ssjg# This is a list of vars that we handle specially below
86246149Ssjg_tricky_env_vars = MAKEOBJDIR OBJTOP
87246149Ssjg# Export our selection - sans tricky ones
88246149Ssjg.export ${_export_list:${_tricky_env_vars:${M_ListToSkip}}}
89246149Ssjg
90246149Ssjg# This next bit may need tweaking
91246149Ssjg# if you don't happen to like the way I set it.
92246149Ssjg.if defined(MAKEOBJDIR)
93246149Ssjg# We are going to set this to the equivalent of the shell's
94246149Ssjg# MAKEOBJDIR='${.CURDIR:S,${SRCTOP},${OBJTOP},}'
95246149Ssjg_srctop := ${SRCTOP:U${SB_SRC:U${SB}/src}}
96246149Ssjg_objroot := ${OBJROOT:U${SB_OBJROOT:U${SB}/${SB_OBJPREFIX}}}
97246149Ssjg_objtop := ${OBJTOP:U${_objroot}${MACHINE}}
98246149Ssjg# Take care of ${MACHINE}
99246149Ssjg.if ${MACHINE} == "host" || ${OBJTOP} == ${HOST_OBJTOP:Uno}
100246149SsjgOBJTOP = ${_objtop:S,${HOST_TARGET}$,\${MACHINE},}
101246149Ssjg.else
102246149SsjgOBJTOP = ${_objtop:S,${MACHINE}$,\${MACHINE},}
103246149Ssjg.endif
104246149Ssjg# Export like this
105246149SsjgMAKEOBJDIR = $${.CURDIR:S,${_srctop},$${OBJTOP},}
106246149Ssjg#.info ${MAKE_SAVE_ENV_VARS _srctop _objroot _objtop OBJTOP MAKEOBJDIR:L:@v@${.newline}$v=${$v}@}
107246149Ssjg
108246149Ssjg# Export these as-is, and do not track...
109246149Ssjg# otherwise the environment will be ruined when we evaluate them below.
110246149Ssjg.export-env ${_tricky_env_vars}
111246149Ssjg
112246149Ssjg# Now evaluate for ourselves
113246149Ssjg.for v in ${_tricky_env_vars}
114246149Ssjg$v := ${$v}
115246149Ssjg.endfor
116246149Ssjg#.info ${_tricky_env_vars:@v@${.newline}$v=${$v}@}
117246149Ssjg
118246149Ssjg.endif				# MAKEOBJDIR
119246149Ssjg.endif				# level 0
120