Makefile: add and use the ".DELETE_ON_ERROR" flag

Use the GNU make ".DELETE_ON_ERROR" flag in our main Makefile, as we already do in the Documentation/Makefile since db10fc6c09f (doc: simplify Makefile using .DELETE_ON_ERROR, 2021-05-21). Now if a command to make X fails X will be removed, the default behavior of GNU make is to only do so if "make" itself is interrupted with a signal. E.g. if we now intentionally break one of the rules with: - mv $@+ $@ + mv $@+ $@ && \ + false We'll get output like: $ make git CC git.o LINK git make: *** [Makefile:2179: git] Error 1 make: *** Deleting file 'git' $ file git git: cannot open `git' (No such file or directory) Before this change we'd leave the file in place in under this scenario. As in db10fc6c09f this allows us to remove patterns of removing leftover $@ files at the start of rules, since previous failing runs of the Makefile won't have left those littered around anymore. I'm not as confident that we should be replacing the "mv $@+ $@" pattern entirely, since that means that external programs or one of our other Makefiles might race and get partial content. I'm not changing $(REMOTE_CURL_ALIASES) since that uses a ln/ln -s/cp dance, and would require the addition of "-f" flags if the "rm" at the start was removed. I've also got plans to fix that ln/ln -s/cp pattern in another series. For $(LIB_FILE) and $(XDIFF_LIB) we can rely on the "c" (create) being present in ARFLAGS. I'm not changing "$(ETAGS_TARGET)", "tags" and "cscope" because they've got a messy combination of removing "$@+" not "$@" at the beginning, or "$@*". I'm also addressing those in another series. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 29, 2021 at 10:44 UTC 7b76d6bf221071d733a63cb65a616ec9f8fc4b92
1 file changed +18 -11
Makefile
+18 -11
@@ -2160,6 +2160,16 @@ shell_compatibility_test: please_set_SHELL_PATH_to_a_more_modern_shell
2160 strip: $(PROGRAMS) git$X
2161 $(STRIP) $(STRIP_OPTS) $^
2162
2163 +### Flags affecting all rules
2164 +
2165 +# A GNU make extension since gmake 3.72 (released in late 1994) to
2166 +# remove the target of rules if commands in those rules fail. The
2167 +# default is to only do that if make itself receives a signal. Affects
2168 +# all targets, see:
2169 +#
2170 +# info make --index-search=.DELETE_ON_ERROR
2171 +.DELETE_ON_ERROR:
2172 +
2173 ### Target-specific flags and dependencies
2174
2175 # The generic compilation pattern rule and automatically
@@ -2243,7 +2253,6 @@ SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\
2253 $(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV):\
2254 $(perllibdir_SQ)
2255 define cmd_munge_script
2246 -$(RM) $@ $@+ && \
2256 sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
2257 -e 's|@SHELL_PATH@|$(SHELL_PATH_SQ)|' \
2258 -e 's|@@DIFF@@|$(DIFF_SQ)|' \
@@ -2313,7 +2322,7 @@ endif
2322 PERL_DEFINES += $(gitexecdir) $(perllibdir) $(localedir)
2323
2324 $(SCRIPT_PERL_GEN): % : %.perl GIT-PERL-DEFINES GIT-PERL-HEADER GIT-VERSION-FILE
2316 - $(QUIET_GEN)$(RM) $@ $@+ && \
2325 + $(QUIET_GEN) \
2326 sed -e '1{' \
2327 -e ' s|#!.*perl|#!$(PERL_PATH_SQ)|' \
2328 -e ' r GIT-PERL-HEADER' \
@@ -2333,7 +2342,7 @@ GIT-PERL-DEFINES: FORCE
2342 fi
2343
2344 GIT-PERL-HEADER: $(PERL_HEADER_TEMPLATE) GIT-PERL-DEFINES Makefile
2336 - $(QUIET_GEN)$(RM) $@ && \
2345 + $(QUIET_GEN) \
2346 INSTLIBDIR='$(perllibdir_SQ)' && \
2347 INSTLIBDIR_EXTRA='$(PERLLIB_EXTRA_SQ)' && \
2348 INSTLIBDIR="$$INSTLIBDIR$${INSTLIBDIR_EXTRA:+:$$INSTLIBDIR_EXTRA}" && \
@@ -2359,7 +2368,7 @@ git-instaweb: git-instaweb.sh GIT-SCRIPT-DEFINES
2368 mv $@+ $@
2369 else # NO_PERL
2370 $(SCRIPT_PERL_GEN) git-instaweb: % : unimplemented.sh
2362 - $(QUIET_GEN)$(RM) $@ $@+ && \
2371 + $(QUIET_GEN) \
2372 sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
2373 -e 's|@@REASON@@|NO_PERL=$(NO_PERL)|g' \
2374 unimplemented.sh >$@+ && \
@@ -2373,14 +2382,14 @@ $(SCRIPT_PYTHON_GEN): GIT-BUILD-OPTIONS
2382 ifndef NO_PYTHON
2383 $(SCRIPT_PYTHON_GEN): GIT-CFLAGS GIT-PREFIX GIT-PYTHON-VARS
2384 $(SCRIPT_PYTHON_GEN): % : %.py
2376 - $(QUIET_GEN)$(RM) $@ $@+ && \
2385 + $(QUIET_GEN) \
2386 sed -e '1s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
2387 $< >$@+ && \
2388 chmod +x $@+ && \
2389 mv $@+ $@
2390 else # NO_PYTHON
2391 $(SCRIPT_PYTHON_GEN): % : unimplemented.sh
2383 - $(QUIET_GEN)$(RM) $@ $@+ && \
2392 + $(QUIET_GEN) \
2393 sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
2394 -e 's|@@REASON@@|NO_PYTHON=$(NO_PYTHON)|g' \
2395 unimplemented.sh >$@+ && \
@@ -2388,8 +2397,7 @@ $(SCRIPT_PYTHON_GEN): % : unimplemented.sh
2397 mv $@+ $@
2398 endif # NO_PYTHON
2399
2391 -CONFIGURE_RECIPE = $(RM) configure configure.ac+ && \
2392 - sed -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
2400 +CONFIGURE_RECIPE = sed -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
2401 configure.ac >configure.ac+ && \
2402 autoconf -o configure configure.ac+ && \
2403 $(RM) configure.ac+
@@ -2514,7 +2522,6 @@ endif
2522 ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
2523 all:: compile_commands.json
2524 compile_commands.json:
2517 - @$(RM) $@
2525 $(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)/*.o.json > $@+
2526 @if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
2527 endif
@@ -2587,10 +2594,10 @@ $(REMOTE_CURL_PRIMARY): remote-curl.o http.o http-walker.o GIT-LDFLAGS $(GITLIBS
2594 $(CURL_LIBCURL) $(EXPAT_LIBEXPAT) $(LIBS)
2595
2596 $(LIB_FILE): $(LIB_OBJS)
2590 - $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2597 + $(QUIET_AR)$(AR) $(ARFLAGS) $@ $^
2598
2599 $(XDIFF_LIB): $(XDIFF_OBJS)
2593 - $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2600 + $(QUIET_AR)$(AR) $(ARFLAGS) $@ $^
2601
2602 export DEFAULT_EDITOR DEFAULT_PAGER
2603