Makefile 变量中的尾随空格

发布于 2025-01-02 01:32:17 字数 672 浏览 1 评论 0原文

Makefile:

#there is a whitespace after "/my/path/to"
FOO = "/my/path/to"
BAR = "dir"

INCLUDE_DIRS = $(FOO)/$(BAR) "/another/path"

INCLUDES = $(foreach dir,$(INCLUDE_DIRS),-I$(dir))

all:
     @echo $(INCLUDES)

对于 Gnu make,我期望我的 $(INCLUDES) 为:

-I/my/path/to/dir -I/another/path

但是,如果该行

FOO = "/my/path/to"

以空格结尾(这是一个常见的“错误”),则变量 FOO 将包含空格,并且生成的 INCLUDES 将包含三个目录(两个第一被第一个分开):

-I/my/path/to -I/dir -I/another/path

我发现的唯一解决方案是使用 strip 函数:

FOO = $(strip "/my/path/to" )

但是没有更自然的语法,或者有任何方法可以避免这个陷阱吗?

The Makefile:

#there is a whitespace after "/my/path/to"
FOO = "/my/path/to"
BAR = "dir"

INCLUDE_DIRS = $(FOO)/$(BAR) "/another/path"

INCLUDES = $(foreach dir,$(INCLUDE_DIRS),-I$(dir))

all:
     @echo $(INCLUDES)

With Gnu make I expect my $(INCLUDES) to be:

-I/my/path/to/dir -I/another/path

However, if the line

FOO = "/my/path/to"

ends with a whitespace (which is a common "mistake"), variable FOO will contains the whitespace, and the resulting INCLUDES will contains three directories (the two firsts beeing the first one splitted):

-I/my/path/to -I/dir -I/another/path

The only solution I found is to use the strip function:

FOO = $(strip "/my/path/to" )

But isn't there a more natural syntax, or any way to avoid this pitfall?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2025-01-09 01:32:17

首先,请注意路径周围可能不应该有双引号。在您的示例中,我猜 $(FOO)/$(BAR) 的值将是 "/my/path/to"/"dir" 而不是预期的 /my/path/to/dir

回答你的问题,一般来说,不会。连接两个值会保留空格,因此如果您想编写 $(FOO)/$(BAR) ,您可以保证 $(FOO)>$(BAR) 是没有前导或尾随空格的单个单词。 strip 函数足以删除后者(如果有)。

但是,您可以将这些变量之一视为列表并编写类似 $(FOO:%=%/$(BAR)) 的内容,这样就可以正常工作。但就我个人而言,我更愿意检查 FOO 的值(如果不好则修复它或因错误而失败),然后照常使用它,例如:

FOO = /my/path/to # <- a space!
BAR = dir

...

ifneq ($(word 2,[$(FOO)]),)
  $(error There is a whitespace inside the value of 'FOO')
endif

First of all, note that there probably shouldn't be double quotes around the paths. In your example I guess the value of $(FOO)/$(BAR) would be "/my/path/to"/"dir" instead of expected /my/path/to/dir.

Answering to your question, generally speaking, no. Concatenating two values preserves whitespaces, so if you want to write $(FOO)/$(BAR) is up to you to guarantee that both $(FOO) and $(BAR) are single words with no leading or trailing whitespaces. strip function is good enough to remove the latter (if any).

However you can treat one of these variables as a list and write something like $(FOO:%=%/$(BAR)) and this will work fine. But personally I would prefer to check the value of FOO (either fixing it or failing with an error if it is bad) and then use it as usual, for example:

FOO = /my/path/to # <- a space!
BAR = dir

...

ifneq ($(word 2,[$(FOO)]),)
  $(error There is a whitespace inside the value of 'FOO')
endif
喜爱纠缠 2025-01-09 01:32:17

基于 Eldar Abusalimov 解决方案,这里有一个可以在循环中使用的函数
检查多个目录中是否有空格:

FOO = /my/path
BAR = to # <- a space!
BAZ = dir

# $(call assert-no-whitespace,DIRECTORY)
define assert-no-whitespace
  $(if $(word 2,[$($1)]),$(error There is a whitesapce inside variable '$(1)', please correct it),)
endef

CHECK_FOR_WHITESPACE = \
  FOO \
  BAR 

$(foreach dir,$(CHECK_FOR_WHITESPACE),$(call assert-no-whitespace,$(dir)))

all:
  @echo $(FOO)/$(BAR)/$(BAZ)

Based on Eldar Abusalimov solution, here is a function that can be used in a loop to
check multilple directories for whitespace:

FOO = /my/path
BAR = to # <- a space!
BAZ = dir

# $(call assert-no-whitespace,DIRECTORY)
define assert-no-whitespace
  $(if $(word 2,[$($1)]),$(error There is a whitesapce inside variable '$(1)', please correct it),)
endef

CHECK_FOR_WHITESPACE = \
  FOO \
  BAR 

$(foreach dir,$(CHECK_FOR_WHITESPACE),$(call assert-no-whitespace,$(dir)))

all:
  @echo $(FOO)/$(BAR)/$(BAZ)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文