Re: [PATCH 08/13] Makefile: introduce sq function for shell-quoting

2014-02-05 Thread Junio C Hamano
Jeff King  writes:

> Since we have recently abolished the prohibition on $(call)
> in our Makefile, we can use it to factor out the repeated
> shell-quoting we do. There are two upsides:
>
>   1. It is much more readable than inline calls to
>  $(subst ','\'').
>
>   2. It is short enough that we can do away with the _SQ
>  variants of many variables (note that we do not do this
>  just yet, as there is a little more cleanup needed
>  first).

Yay.

>  But
> many instances are not really any more readable (e.g., see the first
> hunk below).
> ...
>  .PHONY: install-perl-script install-sh-script install-python-script
>  install-sh-script: $(SCRIPT_SH_INS)
> - $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
> + $(INSTALL) $^ $(call sq,$(DESTDIR)$(gitexec_instdir))

Hmph, I do not see it as bad as the "make-var", which forces you to
say $(eval $(call ...)); this $(call sq, ...) is fairly readable.

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 08/13] Makefile: introduce sq function for shell-quoting

2014-02-05 Thread Jeff King
Since we have recently abolished the prohibition on $(call)
in our Makefile, we can use it to factor out the repeated
shell-quoting we do. There are two upsides:

  1. It is much more readable than inline calls to
 $(subst ','\'').

  2. It is short enough that we can do away with the _SQ
 variants of many variables (note that we do not do this
 just yet, as there is a little more cleanup needed
 first).

Signed-off-by: Jeff King 
---
This is the one I am most on the fence about. I think (2) is nice. And
for (1), we do end up more readable in some instances (e.g., the
horrible inline subst calls in generating BUILD-OPTIONS go away). But
many instances are not really any more readable (e.g., see the first
hunk below).

Two things come to mind:

  1. If we really prefer reading $(FOO_SQ) but don't want to manually
 write each one, we could have a simple script that reads the
 Makefile and produces an _SQ variant of every variable, whether we
 need it or not.

  2. The later parts of the series introduce a new way of getting
 values into programs that does not involve the command-line. So in
 theory many of these quoted uses of variables would just go away in
 the long run (if we choose to pursue that direction).

 Makefile | 120 ++-
 1 file changed, 64 insertions(+), 56 deletions(-)

diff --git a/Makefile b/Makefile
index e12039f..868872f 100644
--- a/Makefile
+++ b/Makefile
@@ -506,11 +506,11 @@ build-python-script: $(SCRIPT_PYTHON_GEN)
 
 .PHONY: install-perl-script install-sh-script install-python-script
 install-sh-script: $(SCRIPT_SH_INS)
-   $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
+   $(INSTALL) $^ $(call sq,$(DESTDIR)$(gitexec_instdir))
 install-perl-script: $(SCRIPT_PERL_INS)
-   $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
+   $(INSTALL) $^ $(call sq,$(DESTDIR)$(gitexec_instdir))
 install-python-script: $(SCRIPT_PYTHON_INS)
-   $(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
+   $(INSTALL) $^ $(call sq,$(DESTDIR)$(gitexec_instdir))
 
 .PHONY: clean-perl-script clean-sh-script clean-python-script
 clean-sh-script:
@@ -1040,7 +1040,7 @@ endif
 
 ifdef SANE_TOOL_PATH
 SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH))
-BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix 
$(SANE_TOOL_PATH_SQ)|'
+BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix $(call 
sqi,$(SANE_TOOL_PATH)|'
 PATH := $(SANE_TOOL_PATH):${PATH}
 else
 BROKEN_PATH_FIX = '/^\# @@BROKEN_PATH_FIX@@$$/d'
@@ -1561,6 +1561,13 @@ ifneq ("$(PROFILE)","")
 endif
 endif
 
+# usage: $(call sq,CONTENTS)
+#
+# Quote the value as appropriate for the shell. Use "sqi" if you are
+# already "i"nside single-quotes.
+sqi = $(subst ','\'',$1)
+sq = '$(call sqi,$1)'
+
 # usage: $(eval $(call make-var,FN,DESC,CONTENTS))
 #
 # Create a rule to write $CONTENTS (which should come from a make variable)
@@ -1568,7 +1575,7 @@ endif
 # dependency on a Makefile variable. Prints $DESC to the user.
 define make-var
 MAKE/$1: FORCE
-   @VALUE='$$(subst ','\'',$3)'; \
+   @VALUE=$$(call sq,$3); \
if ! test -e $$@ || test x"VALUE" != x"`cat $$@`"; then \
echo >&2 "* new $2"; \
printf '%s\n' "VALUE" >$$@+ && \
@@ -1603,7 +1610,7 @@ PERLLIB_EXTRA_SQ = $(subst ','\'',$(PERLLIB_EXTRA))
 
 LIBS = $(GITLIBS) $(EXTLIBS)
 
-BASIC_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' \
+BASIC_CFLAGS += -DSHA1_HEADER=$(call sq,$(SHA1_HEADER)) \
$(COMPAT_CFLAGS)
 LIB_OBJS += $(COMPAT_OBJS)
 
@@ -1665,13 +1672,13 @@ endif
 
 all::
 ifndef NO_TCLTK
-   $(QUIET_SUBDIR0)git-gui $(QUIET_SUBDIR1) 
gitexecdir='$(gitexec_instdir_SQ)' all
+   $(QUIET_SUBDIR0)git-gui $(QUIET_SUBDIR1) gitexecdir=$(call 
sq,$(gitexec_instdir)) all
$(QUIET_SUBDIR0)gitk-git $(QUIET_SUBDIR1) all
 endif
 ifndef NO_PERL
-   $(QUIET_SUBDIR0)perl $(QUIET_SUBDIR1) PERL_PATH='$(PERL_PATH_SQ)' 
prefix='$(prefix_SQ)' localedir='$(localedir_SQ)' all
+   $(QUIET_SUBDIR0)perl $(QUIET_SUBDIR1) PERL_PATH=$(call sq,$(PERL_PATH)) 
prefix=$(call sq,$(prefix)) localedir=$(call sq,$(localedir)) all
 endif
-   $(QUIET_SUBDIR0)templates $(QUIET_SUBDIR1) 
SHELL_PATH='$(SHELL_PATH_SQ)' PERL_PATH='$(PERL_PATH_SQ)'
+   $(QUIET_SUBDIR0)templates $(QUIET_SUBDIR1) SHELL_PATH=$(call 
sq,$(SHELL_PATH)) PERL_PATH=$(call sq,$(PERL_PATH))
 
 please_set_SHELL_PATH_to_a_more_modern_shell:
@$$(:)
@@ -1761,15 +1768,15 @@ $(eval $(call make-var,SCRIPT-DEFINES,script 
parameters,\
 ))
 define cmd_munge_script
 $(RM) $@ $@+ && \
-sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
--e 's|@SHELL_PATH@|$(SHELL_PATH_SQ)|' \
--e 's|@@DIFF@@|$(DIFF_SQ)|' \
--e 's|@@LOCALEDIR@@|$(localedir_SQ)|g' \
+sed -e '1s|#!.*/sh|#!$(call sqi,$(SHELL_PATH))|' \
+-e 's|@SHELL_PATH@|$(call sqi,$(SHELL_PATH))|' \
+-e 's|@@DIFF@@|$(call sqi,$(DIFF))|' \
+-e 's|@@LOCALEDIR@@|$(call sqi,$(lo