diff mbox series

[1/3] Makefile: make the "sparse" target non-.PHONY

Message ID patch-1.3-e03fde1b642-20210921T224944Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series Makefile: make "sparse" and "hdr-check" non-.PHONY | expand

Commit Message

Ævar Arnfjörð Bjarmason Sept. 21, 2021, 10:55 p.m. UTC
Change the "sparse" target and its *.sp dependencies to be
non-.PHONY. It's now viable to run it as part of a normal compilation
target, as we'll only re-generate these checks if the source *.c file
has changed.

On my box with -j8 it was fast before, or around 5 seconds, now it
only takes that long the first time, and the common case is <100ms, or
however long it takes GNU make to stat the *.sp file and see that all
the corresponding *.c files are older.

See 0bcd9ae85d7 (sparse: Fix errors due to missing target-specific
variables, 2011-04-21) for the modern implementation of the sparse
target being changed here.

It is critical that we use -Wsparse-error here, otherwise the error
would only show up once, but we'd successfully create the empty *.sp
file, and running a second time wouldn't show the error. I'm therefore
not putting it into SPARSE_FLAGS or SP_EXTRA_FLAGS, it's not optional,
the Makefile logic won't behave properly without it.

Appending to $@ without a move is OK here because we're using the
.DELETE_ON_ERROR Makefile feature. See 7b76d6bf221 (Makefile: add and
use the ".DELETE_ON_ERROR" flag, 2021-06-29). GNU make ensures that on
error this file will be removed.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 .gitignore | 1 +
 Makefile   | 8 +++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

Comments

Jeff King Sept. 22, 2021, 2:24 a.m. UTC | #1
On Wed, Sep 22, 2021 at 12:55:13AM +0200, Ævar Arnfjörð Bjarmason wrote:

> On my box with -j8 it was fast before, or around 5 seconds, now it
> only takes that long the first time, and the common case is <100ms, or
> however long it takes GNU make to stat the *.sp file and see that all
> the corresponding *.c files are older.

Sort of side note, but I think the main culprit here is all of the
$(shell) invocations, etc, we run. Try:

  make SHELL_PATH='sh -x'

to get a taste (do it twice, because the first one will actually rebuild
everything due to the changed shell; the second one _should_ be a noop
but still runs a bunch of stuff).

Though even that doesn't tell the whole story, as it doesn't make clear
which shells are invoked by make directly. Try this:

	cat >/tmp/foo <<-\EOF
	#!/bin/sh
	echo >&2 "==> shell $*"
	exec sh "$@"
	EOF

	make SHELL_PATH=/tmp/foo

There's lots of low-hanging fruit like:

==> shell -c echo TEST_SHELL_PATH=\''/tmp/foo'\' >>GIT-BUILD-OPTIONS+
==> shell -c echo PERL_PATH=\''/usr/bin/perl'\' >>GIT-BUILD-OPTIONS+
==> shell -c echo DIFF=\''diff'\' >>GIT-BUILD-OPTIONS+
==> shell -c echo PYTHON_PATH=\''/usr/bin/python'\' >>GIT-BUILD-OPTIONS+
[...and over a dozen more...]

Those could easily be a single shell invocation, rather than one per
echo. Another culprit is GIT-VERSION-GEN, which we run whether it's
needed or not, and takes something like 25ms.

It's probably not worth spending too much time micro-optimizing here
(and certainly it's orthogonal to your series), but there may be some
low-hanging fruit (although a hacky attempt at minimizing the shell
calls for GIT-BUILD-OPTIONS didn't seem to show any speedup, so maybe
it's not so low-hanging after all).

-Peff
diff mbox series

Patch

diff --git a/.gitignore b/.gitignore
index 311841f9bed..b02250a50c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -224,6 +224,7 @@ 
 *.lib
 *.res
 *.sln
+*.sp
 *.suo
 *.ncb
 *.vcproj
diff --git a/Makefile b/Makefile
index a9f9b689f0c..5b09f67aab0 100644
--- a/Makefile
+++ b/Makefile
@@ -2896,11 +2896,13 @@  check-sha1:: t/helper/test-tool$X
 
 SP_OBJ = $(patsubst %.o,%.sp,$(C_OBJ))
 
-$(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE
+$(SP_OBJ): %.sp: %.c GIT-CFLAGS
 	$(QUIET_SP)cgcc -no-compile $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) \
-		$(SPARSE_FLAGS) $(SP_EXTRA_FLAGS) $<
+		-Wsparse-error \
+		$(SPARSE_FLAGS) $(SP_EXTRA_FLAGS) $< && \
+	>$@
 
-.PHONY: sparse $(SP_OBJ)
+.PHONY: sparse
 sparse: $(SP_OBJ)
 
 EXCEPT_HDRS := command-list.h config-list.h unicode-width.h compat/% xdiff/%