From af8c0d17c7627c4c5f83aecace3f5778a6b7293b Mon Sep 17 00:00:00 2001 From: Stefano Lattarini Date: Wed, 8 Aug 2012 23:09:06 +0200 Subject: [PATCH] [ng] uninstall: refactor, more processing delegated to GNU make * lib/am/header-vars.mk (am.chars.space, am.chars.tab): New variables, defined respectively to a literal white space characters and a literal tabulation character. (am.util.whitespace-escape): New function, backslash-escape whitespace characters in the given string. (am.util.file-exists): New function, tell whether the given argument is an existing file; it does so coping correctly with whitespaces and wildcard metacharacters as well, to be usable with paths containing references to $(DESTDIR). (am.uninst.cmd.aux): Re-implement using the new $(am.util.file-exists) function. Signed-off-by: Stefano Lattarini --- lib/am/header-vars.mk | 57 +++++++++++++++++++++++++++---------------- t/internals.tap | 29 +++++++++++++++++++++- 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/lib/am/header-vars.mk b/lib/am/header-vars.mk index 4ae77e95e..50519a118 100644 --- a/lib/am/header-vars.mk +++ b/lib/am/header-vars.mk @@ -90,6 +90,12 @@ am.chars.squote := ' # definition of $(am.chars.newline) just below for a significant example. am.chars.empty := +# A single whitespace. +am.chars.space := $(am.chars.empty) $(am.chars.empty) + +# A single tabulation character. +am.chars.tab := $(am.chars.empty) $(am.chars.empty) + # A literal newline character, that does not get stripped if used # at the end of the expansion of another macro. define am.chars.newline @@ -381,6 +387,26 @@ am__base_list = \ sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \ sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g' +# New function, backslash-escape whitespace in the given string. +define am.util.whitespace-escape +$(subst $(am.chars.space),\$(am.chars.space),$(subst $(am.chars.tab),\$(am.chars.tab),$1)) +endef + +# Determine whether the given file exists. Since this function is +# expected to be used on paths referencing $(DESTDIR), it must be +# ready to cope with whitespaces and shell metacharacters. +# FIXME: here we assume that the shell found by Autoconf-generated +# configure supports "test -e"; that is not completely correct ATM, but +# future versions of Autoconf will reject non-POSIX shells, so we should +# be safe in the long term. +define am.util.file-exists +$(strip \ + $(if $(strip $(findstring *,$1) $(findstring ?,$1) \ + $(findstring [,$1) $(findstring ],$1)), \ + $(shell test -e '$(subst $(am.chars.squote),'\'',$1)' && echo yes), \ + $(if $(wildcard $(call am.util.whitespace-escape,$1)),yes))) +endef + # $(call am.uninst.cmd,DIR,FILES,[RM-OPTS]) # ----------------------------------------- # Uninstall the given files from the given directory, avoiding to hit @@ -390,25 +416,14 @@ am__base_list = \ # passed to the rm invocation removing the files (this ca be useful in # case such files are actually directories (as happens with the HTML # documentation), in which case rm should be passed the '-r' option. -# At least Solaris /bin/sh still lacks 'test -e', so we use the multiple -# "test ! -[fdr]" below instead (FIXME: this should become obsolete when -# we can assume the $SHELL set by Autoconf-generated configure scripts is -# a truly POSIX shell; see: -# ). -# We expect $dir to be either non-existent or a directory, so the -# failure we'll experience if it is a regular file is indeed desired -# and welcome (better to fail loudly than silently). -# Similarly to the 'am.clean-cmd.f' above, this function is only meant to -# be used in a "sub-recipe" by its own. -am.uninst.cmd.aux = \ - $(if $(and $2,$1), \ - { test ! -d '$(DESTDIR)'$2 \ - && test ! -f '$(DESTDIR)'$2 \ - && test ! -r '$(DESTDIR)'$2; } \ - || { \ - echo " cd '$(DESTDIR)$2' && rm -f $1" \ - && cd '$(DESTDIR)$2' \ - && rm -f$(if $3, $3) $1; }$(am.chars.newline)) -am.uninst.cmd = \ - @$(call am.xargs-map,$0.aux,$(strip $2),$(strip $1),$(strip $3)) +# Similarly to the 'am.clean-cmd.f' above, this function is only meant +# to be used in a "sub-recipe" by its own. + +define am.uninst.cmd.aux +$(if $(and $2,$1),$(if $(call am.util.file-exists,$(DESTDIR)$2),$(strip \ +)cd '$(DESTDIR)$2' && rm -f$(if $3, $3) $1$(am.chars.newline))) +endef +define am.uninst.cmd +$(call am.xargs-map,$0.aux,$(strip $2),$(strip $1),$(strip $3)) +endef diff --git a/t/internals.tap b/t/internals.tap index 31b939c83..f4c8a18f6 100755 --- a/t/internals.tap +++ b/t/internals.tap @@ -19,7 +19,7 @@ am_create_testdir=empty . ./defs || exit 1 -plan_ 12 +plan_ 13 # Filter out Automake comments. grep -v '^##' "$am_amdir"/header-vars.mk > defn.mk \ @@ -252,4 +252,31 @@ test: test -f n3 END +T 'am.util.file-exists' <<'END' +prepare: + : > 'a' + test -f 'a' + : > '?' + test -f '?' + : > 'foo bar' + test -f 'foo bar' + mkdir 'a b' + test -d 'a b' + : > 'a b'/'c d' + test -f 'a b'/'c d' +test: prepare + test x'$(call am.util.file-exists,.)' = x'yes' + test x'$(call am.util.file-exists,Makefile)' = x'yes' + test x'$(call am.util.file-exists,.././defn.mk)' = x'yes' + test x'$(call am.util.file-exists,none)' = x + test x'$(call am.util.file-exists,Makefile oops)' = x + test x'$(call am.util.file-exists,foo bar)' = x'yes' + test x'$(call am.util.file-exists,a b)' = x'yes' + test x'$(call am.util.file-exists,a b/c d)' = x'yes' + test x'$(call am.util.file-exists,*)' = x + test x'$(call am.util.file-exists,a)' = x'yes' + test x'$(call am.util.file-exists,[ab])' = x + test x'$(call am.util.file-exists,?)' = x'yes' +END + : -- 2.47.2