Skip to content

Commit 0b6d0bc

Browse files
avargitster
authored andcommitted
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 8650c62 (doc lint: make "lint-docs" non-.PHONY, 2021-10-15). As seen in 4c64fb5 (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/[email protected]/ 2. https://lore.kernel.org/git/[email protected]/ 3. https://gitlab.com/avar/git-hyperfine/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a9fda01 commit 0b6d0bc

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

Documentation/Makefile

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -434,25 +434,11 @@ quick-install-html: require-htmlrepo
434434
print-man1:
435435
@for i in $(MAN1_TXT); do echo $$i; done
436436

437-
## Lint: Common
438-
.build:
439-
$(QUIET)mkdir $@
440-
.build/lint-docs: | .build
441-
$(QUIET)mkdir $@
442-
443437
## 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 $@
450438
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
454439
$(LINT_DOCS_GITLINK): lint-gitlink.perl
455440
$(LINT_DOCS_GITLINK): .build/lint-docs/gitlink/%.ok: %.txt
441+
$(call mkdir_p_parent_template)
456442
$(QUIET_LINT_GITLINK)$(PERL_PATH) lint-gitlink.perl \
457443
$< \
458444
$(HOWTO_TXT) $(DOC_DEP_TXT) \
@@ -463,23 +449,18 @@ $(LINT_DOCS_GITLINK): .build/lint-docs/gitlink/%.ok: %.txt
463449
lint-docs-gitlink: $(LINT_DOCS_GITLINK)
464450

465451
## Lint: man-end-blurb
466-
.build/lint-docs/man-end-blurb: | .build/lint-docs
467-
$(QUIET)mkdir $@
468452
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
470453
$(LINT_DOCS_MAN_END_BLURB): lint-man-end-blurb.perl
471454
$(LINT_DOCS_MAN_END_BLURB): .build/lint-docs/man-end-blurb/%.ok: %.txt
455+
$(call mkdir_p_parent_template)
472456
$(QUIET_LINT_MANEND)$(PERL_PATH) lint-man-end-blurb.perl $< >$@
473457
.PHONY: lint-docs-man-end-blurb
474-
lint-docs-man-end-blurb: $(LINT_DOCS_MAN_END_BLURB)
475458

476459
## Lint: man-section-order
477-
.build/lint-docs/man-section-order: | .build/lint-docs
478-
$(QUIET)mkdir $@
479460
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
481461
$(LINT_DOCS_MAN_SECTION_ORDER): lint-man-section-order.perl
482462
$(LINT_DOCS_MAN_SECTION_ORDER): .build/lint-docs/man-section-order/%.ok: %.txt
463+
$(call mkdir_p_parent_template)
483464
$(QUIET_LINT_MANSEC)$(PERL_PATH) lint-man-section-order.perl $< >$@
484465
.PHONY: lint-docs-man-section-order
485466
lint-docs-man-section-order: $(LINT_DOCS_MAN_SECTION_ORDER)

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,7 +2745,8 @@ all:: $(MOFILES)
27452745
endif
27462746

27472747
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 $@ $<
27492750

27502751
LIB_PERL := $(wildcard perl/Git.pm perl/Git/*.pm perl/Git/*/*.pm perl/Git/*/*/*.pm)
27512752
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))
27612762
endif
27622763

27632764
perl/build/lib/%.pm: perl/%.pm GIT-PERL-DEFINES
2764-
$(QUIET_GEN)mkdir -p $(dir $@) && \
2765+
$(call mkdir_p_parent_template)
2766+
$(QUIET_GEN) \
27652767
sed -e 's|@@LOCALEDIR@@|$(perl_localedir_SQ)|g' \
27662768
-e 's|@@NO_GETTEXT@@|$(NO_GETTEXT_SQ)|g' \
27672769
-e 's|@@NO_PERL_CPAN_FALLBACKS@@|$(NO_PERL_CPAN_FALLBACKS_SQ)|g' \
27682770
< $< > $@
27692771

27702772
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 $< $@
27732775

27742776
$(ETAGS_TARGET): $(FOUND_SOURCE_FILES)
27752777
$(QUIET_GEN)$(RM) $@+ && \
@@ -2903,7 +2905,7 @@ test_bindir_programs := $(patsubst %,bin-wrappers/%,$(BINDIR_PROGRAMS_NEED_X) $(
29032905
all:: $(TEST_PROGRAMS) $(test_bindir_programs)
29042906

29052907
bin-wrappers/%: wrap-for-bin.sh
2906-
@mkdir -p bin-wrappers
2908+
$(call mkdir_p_parent_template)
29072909
$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
29082910
-e 's|@@BUILD_DIR@@|$(shell pwd)|' \
29092911
-e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%$(X),$(@F))$(patsubst git%,$(X),$(filter $(@F),$(BINDIR_PROGRAMS_NEED_X)))|' < $< > $@ && \

shared.mak

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ ifndef V
5353
QUIET = @
5454
QUIET_GEN = @echo ' ' GEN $@;
5555

56+
QUIET_MKDIR_P_PARENT = @echo $(wspfx_SQ) MKDIR -p $(@D);
57+
5658
## Used in "Makefile"
5759
QUIET_CC = @echo ' ' CC $@;
5860
QUIET_AR = @echo ' ' AR $@;
@@ -84,3 +86,18 @@ ifndef V
8486
export V
8587
endif
8688
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

0 commit comments

Comments
 (0)