2017-02-09 25 views
5

GNU Make有-B選項,強制make忽略現有目標。它允許重建目標,但它也重建目標的所有依賴樹。我不知道有沒有辦法強制重建目標而不重建它的依賴關係(使用GNU Make選項,Makefile中的設置,兼容make的軟件等)?有沒有辦法強制重建目標(make -B)而不重建它的依賴關係?

插圖的問題:

$ mkdir test; cd test 
$ unexpand -t4 >Makefile <<EOF 
huge: 
    @echo "rebuilding huge"; date >huge 
small: huge 
    @echo "rebuilding small"; sh -c 'cat huge; date' >small 
EOF 
$ make small 
rebuilding huge 
rebuilding small 
$ ls 
huge Makefile small 
$ make small 
make: 'small' is up to date. 
$ make -B small 
rebuilding huge # how to get rid of this line? 
rebuilding small 
$ make --version | head -n3 
GNU Make 4.0 
Built for x86_64-pc-linux-gnu 
Copyright (C) 1988-2013 Free Software Foundation, Inc. 

當然一個可以rm small; make small,但有一個內置的方式?

+0

確實[這個問題](http://stackoverflow.com/questions/12199237/tell-make-to-ignore-dependencies-when-the-top-has-been-created)幫助你? – sycko

+0

謝謝,但不是很多:正如我使用'ls'顯示的那樣,'huge'保存在這裏(沒有'.SECONDARY:'目標),而重建則是由於'-B'。 –

回答

3

一種方法是使用-o選項,你不希望所有的目標,以重拍:

-o FILE, --old-file=FILE, --assume-old=FILE 
     Consider FILE to be very old and don't remake it. 

編輯

我想你誤讀的文檔爲-B;它說:

-B, --always-make 
     Unconditionally make all targets. 

注意,所有目標這裏; huge肯定是一個目標,所以如果你使用-B它將被重拍。

但是,我也誤解了你的問題。我以爲你想重建small而不重建huge即使huge是新的,但你試圖讓small重建,儘管huge沒有改變,對吧?

你絕對不想使用-B。這個選項根本不是你想要的。

通常人們會做,通過刪除small

rm -f small 
make small 

這可能是有用的,迫使一個給定的目標重新創建一個選項,但該選項不存在。

您可以使用-W huge,但這又意味着您需要知道必備條件的名稱,而不僅僅是要構建的目標。在相當惡劣的黑客

+0

非常感謝''-B -o large small'真的有效,但它不是理想的,除非這種依賴可以自動獲得... –

+0

我同意這不是理想的,但你確實要求任何方法:) – MadScientist

+0

是的,這是爲什麼我投了贊成票(但並未準備好將答案標記爲已接受)。實際上,如果選項做到了自己所說的話(將目標視爲非常古老,因此迫使它獨自被重新制作而不是忽略它),答案將是理想的。 –

2

文件:

huge: 
    @echo "rebuilding huge"; date >huge 

small: $(if $(filter just,${MAKECMDGOALS}),huge) 
    @echo "rebuilding small"; sh -c 'cat huge; date' >small 

.PHONY: just 
just: ; 

現在你可以

$ make -B just small 
+0

確實很討厭! –

+0

謝謝,這是目前爲止「最常用的自動調用」,但不幸的是它不適用於規則中的'$ <','$ ^'等。 –

3

這能否幫助:

touch huge; make small 

+0

謝謝,它可以工作('make -W巨大的小'也是如此),但它不是理想的,除非可以自動獲得這種依賴關係。 –

0

好了,到目前爲止我用這部分解決方案:

$ cat >~/bin/remake <<'EOF' 
#!/bin/sh 
# http://stackoverflow.com/questions/42139227 
for f in "[email protected]"; do 
    if [ -f "$f" ]; then 
    # Make the file very old; it's safer than removing. 
    touch [email protected] "$f" 
    fi 
done 
make "[email protected]" 
EOF 
$ chmod u+x ~/bin/remake 
$ # PATH="$HOME/bin:$PATH" in .bashrc 
$ remake --debug=b small 
GNU Make 4.0 
Built for x86_64-pc-linux-gnu 
Copyright (C) 1988-2013 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. 
Reading makefiles... 
Updating goal targets.... 
Prerequisite 'huge' is newer than target 'small'. 
Must remake target 'small'. 
rebuilding small 
相關問題