threads / patch / 45340

patchMakefile: detect errors in running spatch

Subject: [PATCH] Makefile: detect errors in running spatch

## tl;dr

4 messages between Mar 10, 2017 and Mar 10, 2017. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

Jeff King· Mar 10, 2017, 08:31 UTC · lore

The "make coccicheck" target runs spatch against each source file. But it does so in a for loop, so "make" never sees the exit code of spatch. Worse, it redirects stderr to a log file, so the user has no indication of any failure. And then to top it all off, because we touched the patch file's mtime, make will refuse to repeat the command because it think the target is up-to-date.

So for example:
  $ make coccicheck SPATCH=does-not-exist
      SPATCH contrib/coccinelle/free.cocci
      SPATCH contrib/coccinelle/qsort.cocci
      SPATCH contrib/coccinelle/xstrdup_or_null.cocci
      SPATCH contrib/coccinelle/swap.cocci
      SPATCH contrib/coccinelle/strbuf.cocci
      SPATCH contrib/coccinelle/object_id.cocci
      SPATCH contrib/coccinelle/array.cocci
  $ make coccicheck SPATCH=does-not-exist
  make: Nothing to be done for 'coccicheck'.
With this patch, you get:
  $ make coccicheck SPATCH=does-not-exist
       SPATCH contrib/coccinelle/free.cocci
  /bin/sh: 4: does-not-exist: not found
  Makefile:2338: recipe for target 'contrib/coccinelle/free.cocci.patch' failed
  make: *** [contrib/coccinelle/free.cocci.patch] Error 1

It also dumps the log on failure, so any errors from spatch itself (like syntax errors in our .cocci files) will be seen by the user.

Signed-off-by: Jeff King <peff@peff.net>
---
This shell code is getting a bit unwieldy to stick inside the Makefile,
with all the line continuation and $-escaping.  It might be worth moving
it into a helper script.

It also doesn't help that shells are awkward at passing status out of a for-loop. I think the most "make-ish" way of doing this would actually be to lose the for loop and have a per-cocci-per-source target.

I don't know if that would make the patches harder to apply. The results aren't full patches, so I assume you usually do some kind of munging on them? I resorted to:

  make coccicheck SPATCH='spatch --in-place'
 Makefile | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
Show changes to Makefile +10 −2
diff --git a/Makefile b/Makefile
index 9ec6065cc..d97633892 100644
--- a/Makefile
+++ b/Makefile
@@ -2336,9 +2336,17 @@ check: common-cmds.h
 C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))
 %.cocci.patch: %.cocci $(C_SOURCES)
 	@echo '    ' SPATCH $<; \
+	ret=0; \
 	for f in $(C_SOURCES); do \
-		$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \
-	done >$@ 2>$@.log; \
+		$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS) || \
+			{ ret=$$?; break; }; \
+	done >$@+ 2>$@.log; \
+	if test $$ret != 0; \
+	then \
+		cat $@.log; \
+		exit 1; \
+	fi; \
+	mv $@+ $@; \
 	if test -s $@; \
 	then \
 		echo '    ' SPATCH result: $@; \
-- 
2.12.0.450.gd7e60cc16
René Scharfe· Mar 10, 2017, 17:03 UTC · re: Jeff King · lore

Re: [PATCH] Makefile: detect errors in running spatch

Am 10.03.2017 um 09:31 schrieb Jeff King:
Show 28 quoted lines
> The "make coccicheck" target runs spatch against each source
> file. But it does so in a for loop, so "make" never sees the
> exit code of spatch. Worse, it redirects stderr to a log
> file, so the user has no indication of any failure. And then
> to top it all off, because we touched the patch file's
> mtime, make will refuse to repeat the command because it
> think the target is up-to-date.
>
> So for example:
>
>   $ make coccicheck SPATCH=does-not-exist
>       SPATCH contrib/coccinelle/free.cocci
>       SPATCH contrib/coccinelle/qsort.cocci
>       SPATCH contrib/coccinelle/xstrdup_or_null.cocci
>       SPATCH contrib/coccinelle/swap.cocci
>       SPATCH contrib/coccinelle/strbuf.cocci
>       SPATCH contrib/coccinelle/object_id.cocci
>       SPATCH contrib/coccinelle/array.cocci
>   $ make coccicheck SPATCH=does-not-exist
>   make: Nothing to be done for 'coccicheck'.
>
> With this patch, you get:
>
>   $ make coccicheck SPATCH=does-not-exist
>        SPATCH contrib/coccinelle/free.cocci
>   /bin/sh: 4: does-not-exist: not found
>   Makefile:2338: recipe for target 'contrib/coccinelle/free.cocci.patch' failed
>   make: *** [contrib/coccinelle/free.cocci.patch] Error 1

That's nice. The current version is just a contraption that does the bare minimum of work.

Show 9 quoted lines
> It also dumps the log on failure, so any errors from spatch
> itself (like syntax errors in our .cocci files) will be seen
> by the user.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> This shell code is getting a bit unwieldy to stick inside the Makefile,
> with all the line continuation and $-escaping.  It might be worth moving
> it into a helper script.
There is one for the kernel 
(https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/coccicheck). 
  It's quite big, though.
Show 7 quoted lines
> It also doesn't help that shells are awkward at passing status out of a
> for-loop. I think the most "make-ish" way of doing this would actually
> be to lose the for loop and have a per-cocci-per-source target.
>
> I don't know if that would make the patches harder to apply. The results
> aren't full patches, so I assume you usually do some kind of munging on
> them?
They work with patch -p0.
We can get rid of the loop by using the spatch options --use-gitgrep and 
--dir.  I can't find the former one in the docs, though, so I'm not sure 
if it only works with certain versions or what exactly it is even doing. 
  It seems to have the side effect of producing git-style patches 
(applicable with patch -p1) at least.
> I resorted to:
>
>   make coccicheck SPATCH='spatch --in-place'
Using SPATCH_FLAGS for adding an option in such case would be a bit simpler.
Show 28 quoted lines
>  Makefile | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 9ec6065cc..d97633892 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2336,9 +2336,17 @@ check: common-cmds.h
>  C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))
>  %.cocci.patch: %.cocci $(C_SOURCES)
>  	@echo '    ' SPATCH $<; \
> +	ret=0; \
>  	for f in $(C_SOURCES); do \
> -		$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \
> -	done >$@ 2>$@.log; \
> +		$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS) || \
> +			{ ret=$$?; break; }; \
> +	done >$@+ 2>$@.log; \
> +	if test $$ret != 0; \
> +	then \
> +		cat $@.log; \
> +		exit 1; \
> +	fi; \
> +	mv $@+ $@; \
>  	if test -s $@; \
>  	then \
>  		echo '    ' SPATCH result: $@; \
>
Jeff King· Mar 10, 2017, 18:12 UTC · re: René Scharfe · lore

Re: [PATCH] Makefile: detect errors in running spatch

On Fri, Mar 10, 2017 at 06:03:47PM +0100, René Scharfe wrote:
Show 6 quoted lines
> > This shell code is getting a bit unwieldy to stick inside the Makefile,
> > with all the line continuation and $-escaping.  It might be worth moving
> > it into a helper script.
> 
> There is one for the kernel (https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/coccicheck).
> It's quite big, though.

Yeah, there's a lot going on there that I don't think we care about (though I am new to coccinelle, so maybe I would grow to appreciate the features).

I was thinking of just moving the current Makefile snippet into a script. That lets us avoid the irritating quoting. And we can use things like functions, which would make the $?-handling in the loop less tedious (because we can return straight out of the loop).

Show 5 quoted lines
> > I don't know if that would make the patches harder to apply. The results
> > aren't full patches, so I assume you usually do some kind of munging on
> > them?
> 
> They work with patch -p0.

Hrm, you're right. I tried it earlier based on the commit message from the original "coccicheck" commit, but I got "only garbage was found". But now it works. I must have screwed it up (perhaps tab-completion stopped at "copy.cocci" instead of "copy.cocci.patch").

> >   make coccicheck SPATCH='spatch --in-place'
> 
> Using SPATCH_FLAGS for adding an option in such case would be a bit simpler.
That too. :)
Show 5 quoted lines
> We can get rid of the loop by using the spatch options --use-gitgrep and
> --dir.  I can't find the former one in the docs, though, so I'm not sure if
> it only works with certain versions or what exactly it is even doing.  It
> seems to have the side effect of producing git-style patches (applicable
> with patch -p1) at least.

I have no objections to pursuing that. But I think with my patch, it's workable for now, so there's no rush.

-Peff
Junio C Hamano· Mar 10, 2017, 20:16 UTC · re: Jeff King · lore

Re: [PATCH] Makefile: detect errors in running spatch

Jeff King <peff@peff.net> writes:
> It also doesn't help that shells are awkward at passing status out of a
> for-loop. I think the most "make-ish" way of doing this would actually
> be to lose the for loop and have a per-cocci-per-source target.

As we assume we can freely use GNUmake facilities, another option, (i.e. the most "gnumake-ish" way) may be to have it unroll the loop with $(foreach,...) so that the shell just sees a series of commands.

Show 33 quoted lines
> I don't know if that would make the patches harder to apply. The results
> aren't full patches, so I assume you usually do some kind of munging on
> them? I resorted to:
>
>   make coccicheck SPATCH='spatch --in-place'
>
>  Makefile | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 9ec6065cc..d97633892 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2336,9 +2336,17 @@ check: common-cmds.h
>  C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))
>  %.cocci.patch: %.cocci $(C_SOURCES)
>  	@echo '    ' SPATCH $<; \
> +	ret=0; \
>  	for f in $(C_SOURCES); do \
> -		$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \
> -	done >$@ 2>$@.log; \
> +		$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS) || \
> +			{ ret=$$?; break; }; \
> +	done >$@+ 2>$@.log; \
> +	if test $$ret != 0; \
> +	then \
> +		cat $@.log; \
> +		exit 1; \
> +	fi; \
> +	mv $@+ $@; \
>  	if test -s $@; \
>  	then \
>  		echo '    ' SPATCH result: $@; \

← back to recent threads