sys.clean-env.mk revision 296637
1153187Spjd# $Id: sys.clean-env.mk,v 1.21 2016/02/18 21:16:40 sjg Exp $ 2153187Spjd# 3153187Spjd# @(#) Copyright (c) 2009, Simon J. Gerraty 4153187Spjd# 5153187Spjd# This file is provided in the hope that it will 6153187Spjd# be of use. There is absolutely NO WARRANTY. 7153187Spjd# Permission to copy, redistribute or otherwise 8153187Spjd# use this file is hereby granted provided that 9153187Spjd# the above copyright notice and this notice are 10153187Spjd# left intact. 11153187Spjd# 12153187Spjd# Please send copies of changes and bug-fixes to: 13153187Spjd# sjg@crufty.net 14153187Spjd# 15153187Spjd 16153187Spjd# This makefile would normally be included by sys.env.mk 17153187Spjd 18153187Spjd# The variables used by this makefile include: 19153187Spjd# 20153187Spjd# MAKE_ENV_SAVE_VAR_LIST 21153187Spjd# The actuall list of variables from the environment that will be 22153187Spjd# preserved. 23153187Spjd# MAKE_ENV_SAVE_PREFIX_LIST 24153187Spjd# A list of prefixes to match against the environment - the results 25153187Spjd# are added to MAKE_ENV_SAVE_VAR_LIST after being filtered by... 26153187Spjd# MAKE_ENV_SAVE_EXCLUDE_LIST 27153187Spjd# A list of words or patterns which is turned into a list of :N 28153187Spjd# modifiers. 29153187Spjd 30153187Spjd.if ${.MAKE.LEVEL} == 0 && ${MAKE_VERSION} >= 20100606 31153187Spjd# We save any env var that starts with the words in MAKE_ENV_SAVE_PREFIX_LIST. 32153187Spjd# This gets expanded to an egrep expression like '^(A|B|C...)' 33153187Spjd# and added to MAKE_ENV_SAVE_VAR_LIST below. 34153187Spjd# If any of these end up being too greedy, MAKE_ENV_SAVE_EXCLUDE_LIST 35153187Spjd# can be used to filter. 36153187SpjdMAKE_ENV_SAVE_PREFIX_LIST += \ 37153187Spjd 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 '^(${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} == "host" || ${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 use the '$$' trick, anymore 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 'OBJ|SRC' 129.endif # MAKEOBJDIR 130.endif # level 0 131