title: Using Make $(dir) or $(notdir) on a path with spaces
body: <p>I'm using code similar to the following in a Makefile:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>If I use this in a directory with no spaces, say <code>space-test</code>, it works fine:</p>
  
  <pre><code>$ make
  TOP='/tmp/space-test'
  TARGET='/tmp/space-test/foo'
  touch 'foo'
  </code></pre>
  
  <p>However, if I use it in a directory with spaces, say <code>space test</code>, then <code>$(notdir)</code> does the wrong thing:</p>
  
  <pre><code>TOP='/tmp/space\ test'
  TARGET='/tmp/space\ test/foo'
  touch 'space foo'
  </code></pre>
  
  <p>What's happening here is that <code>$(notdir)</code> interprets <code>/tmp/space test/foo</code> as <em>two</em> paths and returns the "file part" of <em>both</em> (i.e., <code>space</code> and <code>foo</code>). The weird part of this is that <code>TARGET</code> is properly escaped; somehow, inside the rule or inside <code>$(notdir)</code>, the backslash escapes are being ignored.</p>
  
  <p>What am I doing wrong here? </p>