I'm using code similar to the following in a Makefile:
empty:=
space:= $(empty) $(empty)
path_escape = $(subst $(space),\$(space),$(1))
TOP=$(call path_escape,$(abspath .))
TARGET=$(TOP)/foo
$(info TOP='$(TOP)')
$(info TARGET='$(TARGET)')
all: $(TARGET)
$(TARGET):
touch '$(notdir $@)'
.PHONY: $(TARGET)
If I use this in a directory with no spaces, say space-test, it works fine:
$ make
TOP='/tmp/space-test'
TARGET='/tmp/space-test/foo'
touch 'foo'
However, if I use it in a directory with spaces, say space test, then $(notdir) does the wrong thing:
TOP='/tmp/space\ test'
TARGET='/tmp/space\ test/foo'
touch 'space foo'
What's happening here is that $(notdir) interprets /tmp/space test/foo as two paths and returns the "file part" of both (i.e., space and foo). The weird part of this is that TARGET is properly escaped; somehow, inside the rule or inside $(notdir), the backslash escapes are being ignored.
What am I doing wrong here?