From 98da874c43035a490cdca81331724f233a3d0c9a Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sat, 18 Jun 2022 20:25:30 -0400 Subject: [SV 10593] Export variables to $(shell ...) commands Export all variables, including exported makefile variables, when invoking a shell for the $(shell ...) function. If we detect a recursive variable expansion, silently ignore that variable and do not export it. We do print a debug message. * NEWS: Announce the potential backward-incompatibility. * doc/make.texi (Shell Function): Document the export behavior. * src/main.c (main): Add "shell-export" to .FEATURES. * src/job.h: New function to free struct childbase. * src/job.c (free_childbase): Implement it; call from free_child. * src/function.c (func_shell_base): Use target_environment() to obtain the proper environment for the shell function. Use free_childbase() to free memory. (windows32_openpipe): Don't reset the environment: the caller already provided a proper PATH variable in envp. * src/variable.c (target_environment): If we detect a recursive expansion and we're called from func_shell, ignore the variable. (sync_Path_environment): Simplify and reduce memory allocation. * tests/scripts/functions/shell: Add tests for this. --- doc/make.texi | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'doc') diff --git a/doc/make.texi b/doc/make.texi index 943c0941..085e9c24 100644 --- a/doc/make.texi +++ b/doc/make.texi @@ -8616,6 +8616,23 @@ using a very strange shell, this has the same result as @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file exists).@refill +All variables that are marked as @code{export} will also be passed to the +shell started by the @code{shell} function. It is possible to create a +variable expansion loop: consider this @file{makefile}: + +@example +export HI = $(shell echo hi) +all: ; @@echo $$HI +@end example + +When @code{make} wants to run the recipe it must add the variable @var{HI} to +the environment; to do so it must be expanded. The value of this variable +requires an invocation of the @code{shell} function, and to invoke it we must +create its environment. Since @var{HI} is exported, we need to expand it to +create its environment. And so on. In this obscure case @code{make} will not +add recursively-expanded variables to the @code{shell} environment rather than +fail with an error. + @node Guile Function, , Shell Function, Functions @section The @code{guile} Function @findex guile -- cgit v1.2.1