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