help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Suggestion for "errexit" equivalent for $(shell ...)


From: DSB
Subject: Re: Suggestion for "errexit" equivalent for $(shell ...)
Date: Wed, 27 Dec 2023 08:55:54 -0500

I see that you've researched this thoroughly and aren't asking for
suggestions other than an answer to your .SHELL_ERREXIT question but I
still thought I'd offer the macro I wrote to solve this. It's not as easy
as a flag but at least it's a line-for-line replacement.

# Behave like $(shell ...) but abort make if the shell exits nonzero.
# Default to "pipefail" mode; use an explicit "set +o pipefail" to override.
# Usage: $(call .shell-e,<command>)
.shell-e = $(shell set -o pipefail; $1)$(if $(filter $(or
$(.SHELLSTATUS),0),0),,$(error Error: [$(.SHELLSTATUS)] "$1"))

On Wed, Dec 27, 2023 at 2:55 AM paul d <paul.david@redbubble.com> wrote:

> Hi,
>
> This is my first posting to the GNU Make mailing lists, so please let me
> know if i've contravened any expectations.
>
> ## Background
>
> I am a somewhat heavy user of GNU Make, and frequently want to do
> something like the following:
>
>     FILE_SHA = $(shell sha256sum src/file.c)
>     deploy-frobnicate:
>         rsync src/file.c remote:dist/file-${FILE_SHA}.c
>
> Obviously this is a toy example, but bear with me.  In almost all of my
> own use-cases it makes no sense to continue execution if the command passed
> to $(shell ...) exits nonzero.  In fact, it is often destructive, because
> empty strings being used in subsequent commands frequently risk doing the
> wrong thing for my situation.
>
> I'm aware that there are workarounds such as the pattern of testing
> .SHELLSTATUS after having invoked $(shell), but i find this extremely
> cumbersome.  For example, this can work,
>
>     FOO := $(shell exit 12)
>     ifneq ($(.SHELLSTATUS),0)
>     $(error Shell failed with $(.SHELLSTATUS))
>     endif
>
> However, that only works if i am disciplined and never use lazy assignment
> -- for slow commands that's not a good trade-off.  There's also the
> approach of setting .ONESHELL, but i'm not sure i'd like to rewrite all my
> Makefiles such that that's feasible.  Also, as far as i understand, that
> won't help me when i'm wanting to set a "top-level" variable to the stdout
> of a shell command, which is what i'm most often doing.
>
> ## Finally getting to the point
>
> I'm writing to ask whether there's any chance that a patch might be
> accepted if i add a configurable setting (for example, similar to
> .ONESHELL, maybe an option called .SHELL_ERREXIT, to match Bash's `-o
> errexit` option) which will cause any $(shell ...) invocation exiting
> nonzero to halt Make with an error message.  Of course such a flag would
> have to default to Make's current behaviour of entirely ignoring exit codes.
>
> I haven't looked at the code extremely thoroughly, but it looks like i'd
> have to modify func_shell_base in function.c, as well as do whatever needs
> to be done to introduce, document, and handle an option flag such as i've
> described.  I'd be happy to have a stab at it, but i wouldn't want to start
> working on a patchset if there's zero chance the maintainers would be
> interested in the feature.
>
> Hope that all makes sense, kind regards, happy holidays,
>
> paul
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]