Makefiles: add and use wildcard "mkdir -p" template

Add a template to do the "mkdir -p" of $(@D) (the parent dir of $@) for us, and use it for the "make lint-docs" targets I added in 8650c6298c1 (doc lint: make "lint-docs" non-.PHONY, 2021-10-15). As seen in 4c64fb5aad9 (Documentation/Makefile: fix lint-docs mkdir dependency, 2021-10-26) maintaining these manual lists of parent directory dependencies is fragile, in addition to being obviously verbose. I used this pattern at the time because I couldn't find another method than "order-only" prerequisites to avoid doing a "mkdir -p $(@D)" for every file being created, which as noted in [1] would be significantly slower. But as it turns out we can use this neat trick of only doing a "mkdir -p" if the $(wildcard) macro tells us the path doesn't exist. A re-run of a performance test similar to that noted downthread of [1] in [2] shows that this is faster, in addition to being less verbose and more reliable (this uses my "git-hyperfine" thin wrapper for "hyperfine"[3]): $ git -c hyperfine.hook.setup= hyperfine -L rev HEAD~1,HEAD~0 -s 'make -C Documentation lint-docs' -p 'rm -rf Documentation/.build' 'make -C Documentation -j1 lint-docs' Benchmark 1: make -C Documentation -j1 lint-docs' in 'HEAD~1 Time (mean ± σ): 2.914 s ± 0.062 s [User: 2.449 s, System: 0.489 s] Range (min … max): 2.834 s … 3.020 s 10 runs Benchmark 2: make -C Documentation -j1 lint-docs' in 'HEAD~0 Time (mean ± σ): 2.315 s ± 0.062 s [User: 1.950 s, System: 0.386 s] Range (min … max): 2.229 s … 2.397 s 10 runs Summary 'make -C Documentation -j1 lint-docs' in 'HEAD~0' ran 1.26 ± 0.04 times faster than 'make -C Documentation -j1 lint-docs' in 'HEAD~1' So let's use that pattern both for the "lint-docs" target, and a few miscellaneous other targets. This method of creating parent directories is explicitly racy in that we don't know if we're going to say always create a "foo" followed by a "foo/bar" under parallelism, or skip the "foo" because we created "foo/bar" first. In this case it doesn't matter for anything except that we aren't guaranteed to get the same number of rules firing when running make in parallel. 1. https://lore.kernel.org/git/211028.861r45y3pt.gmgdl@evledraar.gmail.com/ 2. https://lore.kernel.org/git/211028.86o879vvtp.gmgdl@evledraar.gmail.com/ 3. https://gitlab.com/avar/git-hyperfine/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 3, 2022 at 17:04 UTC 0b6d0bc9246b006ed3c241d4faace132998162d9
3 files changed +27 -27
Documentation/Makefile
+3 -22
@@ -434,25 +434,11 @@ quick-install-html: require-htmlrepo
434 print-man1:
435 @for i in $(MAN1_TXT); do echo $$i; done
436
437 -## Lint: Common
438 -.build:
439 - $(QUIET)mkdir $@
440 -.build/lint-docs: | .build
441 - $(QUIET)mkdir $@
442 -
437 ## Lint: gitlink
444 -.build/lint-docs/gitlink: | .build/lint-docs
445 - $(QUIET)mkdir $@
446 -.build/lint-docs/gitlink/howto: | .build/lint-docs/gitlink
447 - $(QUIET)mkdir $@
448 -.build/lint-docs/gitlink/config: | .build/lint-docs/gitlink
449 - $(QUIET)mkdir $@
438 LINT_DOCS_GITLINK = $(patsubst %.txt,.build/lint-docs/gitlink/%.ok,$(HOWTO_TXT) $(DOC_DEP_TXT))
451 -$(LINT_DOCS_GITLINK): | .build/lint-docs/gitlink
452 -$(LINT_DOCS_GITLINK): | .build/lint-docs/gitlink/howto
453 -$(LINT_DOCS_GITLINK): | .build/lint-docs/gitlink/config
439 $(LINT_DOCS_GITLINK): lint-gitlink.perl
440 $(LINT_DOCS_GITLINK): .build/lint-docs/gitlink/%.ok: %.txt
441 + $(call mkdir_p_parent_template)
442 $(QUIET_LINT_GITLINK)$(PERL_PATH) lint-gitlink.perl \
443 $< \
444 $(HOWTO_TXT) $(DOC_DEP_TXT) \
@@ -463,23 +449,18 @@ $(LINT_DOCS_GITLINK): .build/lint-docs/gitlink/%.ok: %.txt
449 lint-docs-gitlink: $(LINT_DOCS_GITLINK)
450
451 ## Lint: man-end-blurb
466 -.build/lint-docs/man-end-blurb: | .build/lint-docs
467 - $(QUIET)mkdir $@
452 LINT_DOCS_MAN_END_BLURB = $(patsubst %.txt,.build/lint-docs/man-end-blurb/%.ok,$(MAN_TXT))
469 -$(LINT_DOCS_MAN_END_BLURB): | .build/lint-docs/man-end-blurb
453 $(LINT_DOCS_MAN_END_BLURB): lint-man-end-blurb.perl
454 $(LINT_DOCS_MAN_END_BLURB): .build/lint-docs/man-end-blurb/%.ok: %.txt
455 + $(call mkdir_p_parent_template)
456 $(QUIET_LINT_MANEND)$(PERL_PATH) lint-man-end-blurb.perl $< >$@
457 .PHONY: lint-docs-man-end-blurb
474 -lint-docs-man-end-blurb: $(LINT_DOCS_MAN_END_BLURB)
458
459 ## Lint: man-section-order
477 -.build/lint-docs/man-section-order: | .build/lint-docs
478 - $(QUIET)mkdir $@
460 LINT_DOCS_MAN_SECTION_ORDER = $(patsubst %.txt,.build/lint-docs/man-section-order/%.ok,$(MAN_TXT))
480 -$(LINT_DOCS_MAN_SECTION_ORDER): | .build/lint-docs/man-section-order
461 $(LINT_DOCS_MAN_SECTION_ORDER): lint-man-section-order.perl
462 $(LINT_DOCS_MAN_SECTION_ORDER): .build/lint-docs/man-section-order/%.ok: %.txt
463 + $(call mkdir_p_parent_template)
464 $(QUIET_LINT_MANSEC)$(PERL_PATH) lint-man-section-order.perl $< >$@
465 .PHONY: lint-docs-man-section-order
466 lint-docs-man-section-order: $(LINT_DOCS_MAN_SECTION_ORDER)
Makefile
+7 -5
@@ -2745,7 +2745,8 @@ all:: $(MOFILES)
2745 endif
2746
2747 po/build/locale/%/LC_MESSAGES/git.mo: po/%.po
2748 - $(QUIET_MSGFMT)mkdir -p $(dir $@) && $(MSGFMT) -o $@ $<
2748 + $(call mkdir_p_parent_template)
2749 + $(QUIET_MSGFMT)$(MSGFMT) -o $@ $<
2750
2751 LIB_PERL := $(wildcard perl/Git.pm perl/Git/*.pm perl/Git/*/*.pm perl/Git/*/*/*.pm)
2752 LIB_PERL_GEN := $(patsubst perl/%.pm,perl/build/lib/%.pm,$(LIB_PERL))
@@ -2761,15 +2762,16 @@ NO_PERL_CPAN_FALLBACKS_SQ = $(subst ','\'',$(NO_PERL_CPAN_FALLBACKS))
2762 endif
2763
2764 perl/build/lib/%.pm: perl/%.pm GIT-PERL-DEFINES
2764 - $(QUIET_GEN)mkdir -p $(dir $@) && \
2765 + $(call mkdir_p_parent_template)
2766 + $(QUIET_GEN) \
2767 sed -e 's|@@LOCALEDIR@@|$(perl_localedir_SQ)|g' \
2768 -e 's|@@NO_GETTEXT@@|$(NO_GETTEXT_SQ)|g' \
2769 -e 's|@@NO_PERL_CPAN_FALLBACKS@@|$(NO_PERL_CPAN_FALLBACKS_SQ)|g' \
2770 < $< > $@
2771
2772 perl/build/man/man3/Git.3pm: perl/Git.pm
2771 - $(QUIET_GEN)mkdir -p $(dir $@) && \
2772 - pod2man $< $@
2773 + $(call mkdir_p_parent_template)
2774 + $(QUIET_GEN)pod2man $< $@
2775
2776 $(ETAGS_TARGET): $(FOUND_SOURCE_FILES)
2777 $(QUIET_GEN)$(RM) $@+ && \
@@ -2903,7 +2905,7 @@ test_bindir_programs := $(patsubst %,bin-wrappers/%,$(BINDIR_PROGRAMS_NEED_X) $(
2905 all:: $(TEST_PROGRAMS) $(test_bindir_programs)
2906
2907 bin-wrappers/%: wrap-for-bin.sh
2906 - @mkdir -p bin-wrappers
2908 + $(call mkdir_p_parent_template)
2909 $(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
2910 -e 's|@@BUILD_DIR@@|$(shell pwd)|' \
2911 -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%$(X),$(@F))$(patsubst git%,$(X),$(filter $(@F),$(BINDIR_PROGRAMS_NEED_X)))|' < $< > $@ && \
shared.mak
+17
@@ -53,6 +53,8 @@ ifndef V
53 QUIET = @
54 QUIET_GEN = @echo ' ' GEN $@;
55
56 + QUIET_MKDIR_P_PARENT = @echo $(wspfx_SQ) MKDIR -p $(@D);
57 +
58 ## Used in "Makefile"
59 QUIET_CC = @echo ' ' CC $@;
60 QUIET_AR = @echo ' ' AR $@;
@@ -84,3 +86,18 @@ ifndef V
86 export V
87 endif
88 endif
89 +
90 +### Templates
91 +
92 +## mkdir_p_parent: lazily "mkdir -p" the path needed for a $@
93 +## file. Uses $(wildcard) to avoid the "mkdir -p" if it's not
94 +## needed.
95 +##
96 +## Is racy, but in a good way; we might redundantly (and safely)
97 +## "mkdir -p" when running in parallel, but won't need to exhaustively create
98 +## individual rules for "a" -> "prefix" -> "dir" -> "file" if given a
99 +## "a/prefix/dir/file". This can instead be inserted at the start of
100 +## the "a/prefix/dir/file" rule.
101 +define mkdir_p_parent_template
102 +$(if $(wildcard $(@D)),,$(QUIET_MKDIR_P_PARENT)$(shell mkdir -p $(@D)))
103 +endef