2012-02-02 56 views
2

的Makefile文件: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使我想到我的$(含)爲:

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

然而,如果線路

FOO = "/my/path/to" 

以空白結尾(這是一個常見的「錯誤」),變量FOO將包含空格,並且生成的INCLUDES將包含三個目錄(所述兩個第一beeing第一個分裂):

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

我發現的唯一的解決方案是使用帶功能:

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

但是不是有更自然的語法,或以任何方式避免這個陷阱?

回答

0

基於埃爾達爾Abusalimov解決方案,這裏是可以在循環中使用 檢查multilple目錄的空白函數:

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) 
2

首先,請注意,可能不應該在路徑周圍使用雙引號。在你的例子中,我猜$(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 
+0

我喜歡這個解決方案,將使用它,謝謝! – user744629 2012-02-03 14:17:21