Skip to content

Commit a8c829c

Browse files
committed
build: Refactor OPAL_VAR_SCOPE system
Refactor the OPAL_VAR_SCOPE_PUSH and OPAL_VAR_SCOPE_POP macros to be wrappers around shell functions to reduce code duplication in the output configure. This patch reduces the size of the output configrue by approximately 30% on MacOS. Move the scope stack from shell to m4, as all the inputs are currently string literals, so we can have the same functionality with less work in the resulting configure script. With the scope stack in m4, also perform variable name conflicts during autogen time. This allows for us to detect more conflicts, as there were times that the parent macro pushed a variable name in the scope but didn't initialize it before calling a child macro with a conflict, which was a bit of a ticking timebomb. Note that we do still search the current shell environment during OPAL_VAR_SCOPE_PUSH, as macros may use a variable without pushing it into a scope. FInally, be more aggressive about cleaning up the environment after ourselves. Signed-off-by: Brian Barrett <bbarrett@amazon.com>
1 parent 99365ec commit a8c829c

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

config/opal_functions.m4

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -496,60 +496,68 @@ dnl #######################################################################
496496
dnl #######################################################################
497497
dnl #######################################################################
498498

499-
# Declare some variables; use OPAL_VAR_SCOPE_POP to ensure that they
500-
# are cleaned up / undefined.
501-
AC_DEFUN([OPAL_VAR_SCOPE_PUSH],[
502-
# Is the private index set? If not, set it.
503-
if test "x$opal_scope_index" = "x"; then
504-
opal_scope_index=1
505-
fi
506-
499+
AC_DEFUN([OPAL_VAR_SCOPE_INIT],
500+
[opal_var_scope_push()
501+
{
502+
opal_var_scope_push_lineno=$[]1
503+
shift
507504
# First, check to see if any of these variables are already set.
508505
# This is a simple sanity check to ensure we're not already
509506
# overwriting pre-existing variables (that have a non-empty
510507
# value). It's not a perfect check, but at least it's something.
511-
for opal_var in $1; do
512-
opal_str="opal_str=\"\$$opal_var\""
513-
eval $opal_str
514-
515-
if test "x$opal_str" != "x"; then
516-
AC_MSG_WARN([Found configure shell variable clash at line $LINENO!])
517-
AC_MSG_WARN([[OPAL_VAR_SCOPE_PUSH] called on "$opal_var",])
518-
AC_MSG_WARN([but it is already defined with value "$opal_str"])
519-
AC_MSG_WARN([This usually indicates an error in configure.])
520-
AC_MSG_ERROR([Cannot continue])
521-
fi
508+
for opal_var_scope_tmp_var in $[]@; do
509+
AS_VAR_SET_IF([$opal_var_scope_tmp_var],
510+
[AS_VAR_COPY([opal_var_scope_tmp_var_val], [$opal_var_scope_tmp_var])
511+
AC_MSG_WARN([Found configure shell variable clash at line $opal_var_scope_push_lineno!])
512+
AC_MSG_WARN([[OPAL_VAR_SCOPE_PUSH] called on "$opal_var_scope_tmp_var",])
513+
AC_MSG_WARN([but it is already defined with value "$opal_var_scope_tmp_var_val"])
514+
AC_MSG_WARN([This usually indicates an error in configure.])
515+
AC_MSG_ERROR([Cannot continue])])
522516
done
517+
AS_UNSET([opal_var_scope_push_lineno])
518+
AS_UNSET([opal_var_scope_tmp_var])
519+
AS_UNSET([opal_var_scope_tmp_var_val])
520+
}
523521

524-
# Ok, we passed the simple sanity check. Save all these names so
525-
# that we can unset them at the end of the scope.
526-
opal_str="opal_scope_$opal_scope_index=\"$1\""
527-
eval $opal_str
528-
unset opal_str
529-
530-
env | grep opal_scope
531-
opal_scope_index=`expr $opal_scope_index + 1`
522+
opal_var_scope_pop()
523+
{
524+
# Iterate over all the variables and unset them all
525+
for opal_var_scope_tmp_var in $[]@; do
526+
AS_UNSET([$opal_var_scope_tmp_var])
527+
done
528+
AS_UNSET([opal_var_scope_tmp_var])
529+
}])
530+
531+
# OPAL_VAR_SCOPE_PUSH(vars list)
532+
# ------------------------------
533+
# Scope-check that the vars in the space-separated vars list are not already
534+
# in use. Generate a configure-time error if a conflict is found. Note that
535+
# the in use check is defined as "defined", so even if a var in vars list is
536+
# set outside of OPAL_VAR_SCOPE_PUSH, the check will still trip.
537+
AC_DEFUN([OPAL_VAR_SCOPE_PUSH],[
538+
AC_REQUIRE([OPAL_VAR_SCOPE_INIT])dnl
539+
m4_pushdef([opal_var_scope_stack], [$1])dnl
540+
m4_foreach_w([opal_var_scope_var], [$1],
541+
[m4_set_add([opal_var_scope_active_set], opal_var_scope_var,
542+
[], [m4_fatal([OPAL_VAR_SCOPE_PUSH found the variable ]opal_var_scope_var[
543+
active in a previous scope.])])])dnl
544+
opal_var_scope_push ${LINENO} $1
532545
])dnl
533546

534-
# Unset a bunch of variables that were previously set
547+
# OPAL_VAR_SCOPE_POP()
548+
# --------------------
549+
# Unset the last set of variables set in OPAL_VAR_SCOPE_POP. Every call to
550+
# OPAL_VAR_SCOPE_PUSH should have a matched call to this macro.
535551
AC_DEFUN([OPAL_VAR_SCOPE_POP],[
536-
# Unwind the index
537-
opal_scope_index=`expr $opal_scope_index - 1`
538-
opal_scope_test=`expr $opal_scope_index \> 0`
539-
if test "$opal_scope_test" = "0"; then
540-
AC_MSG_WARN([[OPAL_VAR_SCOPE_POP] popped too many OPAL configure scopes.])
541-
AC_MSG_WARN([This usually indicates an error in configure.])
542-
AC_MSG_ERROR([Cannot continue])
543-
fi
544-
545-
# Get the variable names from that index
546-
opal_str="opal_str=\"\$opal_scope_$opal_scope_index\""
547-
eval $opal_str
548-
549-
# Iterate over all the variables and unset them all
550-
for opal_var in $opal_str; do
551-
unset $opal_var
552-
done
552+
AC_REQUIRE([OPAL_VAR_SCOPE_INIT])dnl
553+
m4_ifdef([opal_var_scope_stack], [],
554+
[m4_fatal([OPAL_VAR_SCOPE_POP was called without a defined
555+
variable stack. This usually means that OPAL_VAR_SCOPE_POP was called more
556+
times than OPAL_VAR_SCOPE_PUSH.])])dnl
557+
m4_foreach_w([opal_var_scope_var], opal_var_scope_stack,
558+
[m4_set_remove([opal_var_scope_active_set], opal_var_scope_var)])dnl
559+
opal_var_scope_pop opal_var_scope_stack
560+
m4_popdef([opal_var_scope_stack])dnl
553561
])dnl
554562

555563

0 commit comments

Comments
 (0)