variable和function是Makefile在被讀取時即展開。

wildcard可用來檢測檔案是否存在,但如果沒注意到變數展開的時間,就會出現結果與預期不符的情況。

 

現有Makefile如下

FILE=/path/to/some/file

EXISTED=$(wildcard $(FILE)

target:

    echo $(EXISTED)

正常情況下,EXISTED變數會正確的反應檔案是否存在。但在以下情況:

FILE=/path/to/some/file

EXISTED=$(wildcard $(FILE)

target: target1

    echo $(EXISTED)

 其中,/path/to/some/file是在make target1時才產生。當Makefile被讀取的時候,EXISTED變數已經被assigned,因此雖然target dependency會先去make target1,產生/path/to/some/file,但時間點比Makefile讀取的時間晚,因此在maket target時,即使/path/to/some/file已經存在,$(EXISTED)仍然return 空字串。這就是展開時間的問題。
 
在這種情況下,可使用shell command如下

FILE=/path/to/some/file

EXISTED=$(wildcard $(FILE)

target: target1

    if [ -f "$(FILE)" ]; then \

        echo "Existing"; \

    else \

        echo "Not Existing"; \

  fi;

 
arrow
arrow
    全站熱搜

    kezeodsnx 發表在 痞客邦 留言(0) 人氣()