summaryrefslogtreecommitdiff
path: root/mk/sys.clean-env.mk
blob: b1867c3804bc6b70dcba80b82676be1dc7f84603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# $Id: sys.clean-env.mk,v 1.20 2012/11/12 06:56:04 sjg Exp $
#
#	@(#) Copyright (c) 2009, Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

# This makefile would normally be included by sys.env.mk

# The variables used by this makefile include:
#
# MAKE_ENV_SAVE_VAR_LIST
#	The actuall list of variables from the environment that will be
#	preserved.
# MAKE_ENV_SAVE_PREFIX_LIST
#	A list of prefixes to match against the environment - the results
#	are added to MAKE_ENV_SAVE_VAR_LIST after being filtered by...
# MAKE_ENV_SAVE_EXCLUDE_LIST
#	A list of words or patterns which is turned into a list of :N
#	modifiers.

.if ${.MAKE.LEVEL} == 0 && ${MAKE_VERSION} >= 20100606
# We save any env var that starts with the words in MAKE_ENV_SAVE_PREFIX_LIST.
# This gets expanded to an egrep expression like '^(A|B|C...)'
# and added to MAKE_ENV_SAVE_VAR_LIST below.
# If any of these end up being too greedy, MAKE_ENV_SAVE_EXCLUDE_LIST
# can be used to filter.
MAKE_ENV_SAVE_PREFIX_LIST += \
	CCACHE \
	CVS \
	DEBUG \
	DISTCC \
	HOST \
	MACHINE \
	MAKE \
	MK \
	NEED_ \
	SB_ \
	SSH \
	SVN \
	USE_ \
	WITH_ \
	WITHOUT_ \


# This could be a list of vars or patterns to explicitly exclude.
MAKE_ENV_SAVE_EXCLUDE_LIST ?= _

# This is the actual list that we will save
# HOME is probably something worth clobbering eg. 
# HOME=/var/empty
MAKE_ENV_SAVE_VAR_LIST += \
	HOME \
	LOGNAME \
	OBJROOT \
	OBJTOP \
	PATH \
	SB \
	SRCTOP \
	USER \
	${_env_vars:${MAKE_ENV_SAVE_EXCLUDE_LIST:${M_ListToSkip}}}

_env_vars != env | egrep '^(${MAKE_ENV_SAVE_PREFIX_LIST:ts|})' | sed 's,=.*,,'; echo

_export_list =
.for v in ${MAKE_ENV_SAVE_VAR_LIST:O:u}
.if defined($v)
_export_list += $v
# Save current value
$v := ${$v}
.endif
.endfor

# Now, clobber the environment
.unexport-env

# This is a list of vars that we handle specially below
_tricky_env_vars = MAKEOBJDIR OBJTOP
# Export our selection - sans tricky ones
.export ${_export_list:${_tricky_env_vars:${M_ListToSkip}}}

# This next bit may need tweaking
# if you don't happen to like the way I set it.
.if defined(MAKEOBJDIR)
# We are going to set this to the equivalent of the shell's
# MAKEOBJDIR='${.CURDIR:S,${SRCTOP},${OBJTOP},}'
_srctop := ${SRCTOP:U${SB_SRC:U${SB}/src}}
_objroot := ${OBJROOT:U${SB_OBJROOT:U${SB}/${SB_OBJPREFIX}}}
_objtop := ${OBJTOP:U${_objroot}${MACHINE}}
# Take care of ${MACHINE}
.if ${MACHINE} == "host" || ${OBJTOP} == ${HOST_OBJTOP:Uno}
OBJTOP = ${_objtop:S,${HOST_TARGET}$,\${MACHINE},}
.else
OBJTOP = ${_objtop:S,${MACHINE}$,\${MACHINE},}
.endif
# Export like this
MAKEOBJDIR = $${.CURDIR:S,${_srctop},$${OBJTOP},}
#.info ${MAKE_SAVE_ENV_VARS _srctop _objroot _objtop OBJTOP MAKEOBJDIR:L:@v@${.newline}$v=${$v}@}

# Export these as-is, and do not track...
# otherwise the environment will be ruined when we evaluate them below.
.export-env ${_tricky_env_vars}

# Now evaluate for ourselves
.for v in ${_tricky_env_vars}
$v := ${$v}
.endfor
#.info ${_tricky_env_vars:@v@${.newline}$v=${$v}@}

.endif				# MAKEOBJDIR
.endif				# level 0