Using Make $(dir) or $(notdir) on a path with spaces

孤人 提交于 2019-12-03 16:09:24
Ville Laurikari

The $(notdir) function in GNU Make takes a list of arguments, separated by spaces. Some functions support escaping spaces with \\, but $(notdir) is not one of them.

This should work:

s? = $(subst $(empty) ,?,$1)
?s = $(subst ?, ,$1)
notdirx = $(call ?s,$(notdir $(call s?,$1)))

$(TARGET):
    touch '$(call notdirx,$@)'

This defines a "space-safe" version of notdir called notdirx. It's quite simple: s? first turns all spaces to question marks (hoping that they cannot be present in file names), and ?s converts back. In between we can safely call the original notdir function.

For an excellent summary on GNU Make and spaces in file names, see GNU Make meets file names with spaces in them.

Assuming you have a Unix shell, you could shell out:

notdirx = $(shell basename '$1')
dirx = $(shell dirname '$1')

A similar strategy could apply to other functions. Just remember the quote marks around the variable that contains the whitespace-infected text that you want to treat as a single argument. This will also protect you from other special characters (except for quote marks!). Here's an example to double-backslash escape any spaces in the result of a wildcard glob:

$(shell ls -1 '*.foo' | sed 's/ /\\\\ /g')

Windows is putting parentheses in directory names now, too: C:\Program Files (x86)\ It's not yet clear to me what are the implications of this when using make.

Gary Benade

This is just a shot in the dark:

TOP='"/home/chris/src/tests/make/space test"'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!