{"thread":{"id":"45340","subject":"[PATCH] Makefile: detect errors in running spatch","startedAt":"2017-03-10T08:31:32Z","lastAt":"2017-03-10T20:17:18Z","messageCount":4,"participants":["Jeff King","René Scharfe","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"313738","messageId":"20170310083117.cbflqx7zbe4s7cqv@sigill.intra.peff.net","threadId":"45340","inReplyTo":null,"subject":"[PATCH] Makefile: detect errors in running spatch","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-10T08:31:18Z","receivedAt":"2017-03-10T08:31:32Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"The \"make coccicheck\" target runs spatch against each source\nfile. But it does so in a for loop, so \"make\" never sees the\nexit code of spatch. Worse, it redirects stderr to a log\nfile, so the user has no indication of any failure. And then\nto top it all off, because we touched the patch file's\nmtime, make will refuse to repeat the command because it\nthink the target is up-to-date.\n\nSo for example:\n\n  $ make coccicheck SPATCH=does-not-exist\n      SPATCH contrib/coccinelle/free.cocci\n      SPATCH contrib/coccinelle/qsort.cocci\n      SPATCH contrib/coccinelle/xstrdup_or_null.cocci\n      SPATCH contrib/coccinelle/swap.cocci\n      SPATCH contrib/coccinelle/strbuf.cocci\n      SPATCH contrib/coccinelle/object_id.cocci\n      SPATCH contrib/coccinelle/array.cocci\n  $ make coccicheck SPATCH=does-not-exist\n  make: Nothing to be done for 'coccicheck'.\n\nWith this patch, you get:\n\n  $ make coccicheck SPATCH=does-not-exist\n       SPATCH contrib/coccinelle/free.cocci\n  /bin/sh: 4: does-not-exist: not found\n  Makefile:2338: recipe for target 'contrib/coccinelle/free.cocci.patch' failed\n  make: *** [contrib/coccinelle/free.cocci.patch] Error 1\n\nIt also dumps the log on failure, so any errors from spatch\nitself (like syntax errors in our .cocci files) will be seen\nby the user.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nThis shell code is getting a bit unwieldy to stick inside the Makefile,\nwith all the line continuation and $-escaping.  It might be worth moving\nit into a helper script.\n\nIt also doesn't help that shells are awkward at passing status out of a\nfor-loop. I think the most \"make-ish\" way of doing this would actually\nbe to lose the for loop and have a per-cocci-per-source target.\n\nI don't know if that would make the patches harder to apply. The results\naren't full patches, so I assume you usually do some kind of munging on\nthem? I resorted to:\n\n  make coccicheck SPATCH='spatch --in-place'\n\n Makefile | 12 ++++++++++--\n 1 file changed, 10 insertions(+), 2 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 9ec6065cc..d97633892 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -2336,9 +2336,17 @@ check: common-cmds.h\n C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))\n %.cocci.patch: %.cocci $(C_SOURCES)\n \t@echo '    ' SPATCH $<; \\\n+\tret=0; \\\n \tfor f in $(C_SOURCES); do \\\n-\t\t$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \\\n-\tdone >$@ 2>$@.log; \\\n+\t\t$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS) || \\\n+\t\t\t{ ret=$$?; break; }; \\\n+\tdone >$@+ 2>$@.log; \\\n+\tif test $$ret != 0; \\\n+\tthen \\\n+\t\tcat $@.log; \\\n+\t\texit 1; \\\n+\tfi; \\\n+\tmv $@+ $@; \\\n \tif test -s $@; \\\n \tthen \\\n \t\techo '    ' SPATCH result: $@; \\\n-- \n2.12.0.450.gd7e60cc16\n"},{"id":"313759","messageId":"1fd87646-76fb-e67c-e61a-c0ccca20cf0a@web.de","threadId":"45340","inReplyTo":"20170310083117.cbflqx7zbe4s7cqv@sigill.intra.peff.net","subject":"Re: [PATCH] Makefile: detect errors in running spatch","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-03-10T17:03:47Z","receivedAt":"2017-03-10T17:04:11Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 10.03.2017 um 09:31 schrieb Jeff King:\n> The \"make coccicheck\" target runs spatch against each source\n> file. But it does so in a for loop, so \"make\" never sees the\n> exit code of spatch. Worse, it redirects stderr to a log\n> file, so the user has no indication of any failure. And then\n> to top it all off, because we touched the patch file's\n> mtime, make will refuse to repeat the command because it\n> think the target is up-to-date.\n>\n> So for example:\n>\n>   $ make coccicheck SPATCH=does-not-exist\n>       SPATCH contrib/coccinelle/free.cocci\n>       SPATCH contrib/coccinelle/qsort.cocci\n>       SPATCH contrib/coccinelle/xstrdup_or_null.cocci\n>       SPATCH contrib/coccinelle/swap.cocci\n>       SPATCH contrib/coccinelle/strbuf.cocci\n>       SPATCH contrib/coccinelle/object_id.cocci\n>       SPATCH contrib/coccinelle/array.cocci\n>   $ make coccicheck SPATCH=does-not-exist\n>   make: Nothing to be done for 'coccicheck'.\n>\n> With this patch, you get:\n>\n>   $ make coccicheck SPATCH=does-not-exist\n>        SPATCH contrib/coccinelle/free.cocci\n>   /bin/sh: 4: does-not-exist: not found\n>   Makefile:2338: recipe for target 'contrib/coccinelle/free.cocci.patch' failed\n>   make: *** [contrib/coccinelle/free.cocci.patch] Error 1\n\nThat's nice.  The current version is just a contraption that does the \nbare minimum of work.\n\n> It also dumps the log on failure, so any errors from spatch\n> itself (like syntax errors in our .cocci files) will be seen\n> by the user.\n>\n> Signed-off-by: Jeff King <peff@peff.net>\n> ---\n> This shell code is getting a bit unwieldy to stick inside the Makefile,\n> with all the line continuation and $-escaping.  It might be worth moving\n> it into a helper script.\n\nThere is one for the kernel \n(https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/coccicheck). \n  It's quite big, though.\n\n> It also doesn't help that shells are awkward at passing status out of a\n> for-loop. I think the most \"make-ish\" way of doing this would actually\n> be to lose the for loop and have a per-cocci-per-source target.\n>\n> I don't know if that would make the patches harder to apply. The results\n> aren't full patches, so I assume you usually do some kind of munging on\n> them?\n\nThey work with patch -p0.\n\nWe can get rid of the loop by using the spatch options --use-gitgrep and \n--dir.  I can't find the former one in the docs, though, so I'm not sure \nif it only works with certain versions or what exactly it is even doing. \n  It seems to have the side effect of producing git-style patches \n(applicable with patch -p1) at least.\n\n> I resorted to:\n>\n>   make coccicheck SPATCH='spatch --in-place'\n\nUsing SPATCH_FLAGS for adding an option in such case would be a bit simpler.\n\n>  Makefile | 12 ++++++++++--\n>  1 file changed, 10 insertions(+), 2 deletions(-)\n>\n> diff --git a/Makefile b/Makefile\n> index 9ec6065cc..d97633892 100644\n> --- a/Makefile\n> +++ b/Makefile\n> @@ -2336,9 +2336,17 @@ check: common-cmds.h\n>  C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))\n>  %.cocci.patch: %.cocci $(C_SOURCES)\n>  \t@echo '    ' SPATCH $<; \\\n> +\tret=0; \\\n>  \tfor f in $(C_SOURCES); do \\\n> -\t\t$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \\\n> -\tdone >$@ 2>$@.log; \\\n> +\t\t$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS) || \\\n> +\t\t\t{ ret=$$?; break; }; \\\n> +\tdone >$@+ 2>$@.log; \\\n> +\tif test $$ret != 0; \\\n> +\tthen \\\n> +\t\tcat $@.log; \\\n> +\t\texit 1; \\\n> +\tfi; \\\n> +\tmv $@+ $@; \\\n>  \tif test -s $@; \\\n>  \tthen \\\n>  \t\techo '    ' SPATCH result: $@; \\\n>\n"},{"id":"313763","messageId":"20170310181220.mmbjsmhihkd5rajl@sigill.intra.peff.net","threadId":"45340","inReplyTo":"1fd87646-76fb-e67c-e61a-c0ccca20cf0a@web.de","subject":"Re: [PATCH] Makefile: detect errors in running spatch","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-10T18:12:20Z","receivedAt":"2017-03-10T18:12:28Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Mar 10, 2017 at 06:03:47PM +0100, René Scharfe wrote:\n\n> > This shell code is getting a bit unwieldy to stick inside the Makefile,\n> > with all the line continuation and $-escaping.  It might be worth moving\n> > it into a helper script.\n> \n> There is one for the kernel (https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/coccicheck).\n> It's quite big, though.\n\nYeah, there's a lot going on there that I don't think we care about\n(though I am new to coccinelle, so maybe I would grow to appreciate the\nfeatures).\n\nI was thinking of just moving the current Makefile snippet into a\nscript. That lets us avoid the irritating quoting. And we can use things\nlike functions, which would make the $?-handling in the loop less\ntedious (because we can return straight out of the loop).\n\n> > I don't know if that would make the patches harder to apply. The results\n> > aren't full patches, so I assume you usually do some kind of munging on\n> > them?\n> \n> They work with patch -p0.\n\nHrm, you're right. I tried it earlier based on the commit message from\nthe original \"coccicheck\" commit, but I got \"only garbage was found\".\nBut now it works. I must have screwed it up (perhaps tab-completion\nstopped at \"copy.cocci\" instead of \"copy.cocci.patch\").\n\n> >   make coccicheck SPATCH='spatch --in-place'\n> \n> Using SPATCH_FLAGS for adding an option in such case would be a bit simpler.\n\nThat too. :)\n\n> We can get rid of the loop by using the spatch options --use-gitgrep and\n> --dir.  I can't find the former one in the docs, though, so I'm not sure if\n> it only works with certain versions or what exactly it is even doing.  It\n> seems to have the side effect of producing git-style patches (applicable\n> with patch -p1) at least.\n\nI have no objections to pursuing that. But I think with my patch, it's\nworkable for now, so there's no rush.\n\n-Peff\n"},{"id":"313791","messageId":"xmqqbmt8yjii.fsf@gitster.mtv.corp.google.com","threadId":"45340","inReplyTo":"20170310083117.cbflqx7zbe4s7cqv@sigill.intra.peff.net","subject":"Re: [PATCH] Makefile: detect errors in running spatch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-03-10T20:16:53Z","receivedAt":"2017-03-10T20:17:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> It also doesn't help that shells are awkward at passing status out of a\n> for-loop. I think the most \"make-ish\" way of doing this would actually\n> be to lose the for loop and have a per-cocci-per-source target.\n\nAs we assume we can freely use GNUmake facilities, another option,\n(i.e. the most \"gnumake-ish\" way) may be to have it unroll the loop\nwith $(foreach,...) so that the shell just sees a series of\ncommands.\n\n> I don't know if that would make the patches harder to apply. The results\n> aren't full patches, so I assume you usually do some kind of munging on\n> them? I resorted to:\n>\n>   make coccicheck SPATCH='spatch --in-place'\n>\n>  Makefile | 12 ++++++++++--\n>  1 file changed, 10 insertions(+), 2 deletions(-)\n>\n> diff --git a/Makefile b/Makefile\n> index 9ec6065cc..d97633892 100644\n> --- a/Makefile\n> +++ b/Makefile\n> @@ -2336,9 +2336,17 @@ check: common-cmds.h\n>  C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))\n>  %.cocci.patch: %.cocci $(C_SOURCES)\n>  \t@echo '    ' SPATCH $<; \\\n> +\tret=0; \\\n>  \tfor f in $(C_SOURCES); do \\\n> -\t\t$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \\\n> -\tdone >$@ 2>$@.log; \\\n> +\t\t$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS) || \\\n> +\t\t\t{ ret=$$?; break; }; \\\n> +\tdone >$@+ 2>$@.log; \\\n> +\tif test $$ret != 0; \\\n> +\tthen \\\n> +\t\tcat $@.log; \\\n> +\t\texit 1; \\\n> +\tfi; \\\n> +\tmv $@+ $@; \\\n>  \tif test -s $@; \\\n>  \tthen \\\n>  \t\techo '    ' SPATCH result: $@; \\\n"}]}