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