2011-08-27 87 views
0

假設我有兩個或兩個以上的子文件夾foobar,等一個項目,我有一個Makefile在項目的根目錄,並在每個子目錄。如何在使用遞歸製作時避免這些重複?

我想某些目標(例如all,clean等)在每個子目錄中遞歸運行。我的頂級Makefile看起來是這樣的:

all: 
    $(MAKE) -C foo all 
    $(MAKE) -C bar all 

clean: 
    $(MAKE) -C foo clean 
    $(MAKE) -C bar clean 

在我看來,有很多重複會在這裏的。 有沒有一種方法可以避免我的Makefiles中這種繁瑣的重複?

+0

是否有不使用automake的理由嗎? – Flexo

+0

@awoodland不一定,會有什麼好處? – lindelof

+0

的好處是,你得到所有種類的爲你寫的東西:) – Flexo

回答

0

如何:

SUBDIRS=foo bar 
all clean: 
     for dir in $(SUBDIRS) ; do \ 
      $(MAKE) -C $$dir [email protected] ; \ 
     done 
+0

遞歸製作是有害的http://miller.emu.id.au/pmiller/books/rmch/ – reprogrammer

+0

@reprogrammer:問題不是遞歸作爲一個概念,而是遞歸的通常實現,比如在GNU make中發現的那樣。相比之下,Electric Make使用[non-blocking遞歸make](http://blog.melski.net/2012/09/04/fixing-recursive-make/)來解決遞歸make的性能問題,[衝突檢測和更正](http://blog.melski.net/2011/07/05/how-electricmake-guarantees-reliable-parallel-builds/)以確保正確性。最終的結果是你獲得了遞歸的便利性,並且非遞歸的性能/正確性。 –

0

有點嚇人:

SUBDIRS=foo bar 
SUBDIR_TARGETS=all clean 

define subdir_rule 
$(2): $(1)-$(2) 
$(1)-$(2): 
    make -C $(1) $(2) 
endef 

$(foreach targ,$(SUBDIR_TARGETS),\ 
    $(foreach dir,$(SUBDIRS),\ 
     $(eval $(call subdir_rule,$(dir),$(targ))))) 
0

以下是我會做:

SUBDIRS=foo bar baz 

TARGETS = clean all whatever 
.PHONY:$(TARGETS) 

# There really should be a way to do this as "$(TARGETS):%:TARG=%" or something... 
all: TARG=all 
clean: TARG=clean 
whatever: TARG=whatever 

$(TARGETS): $(SUBDIRS) 
    @echo [email protected] done 

.PHONY: $(SUBDIRS) 
$(SUBDIRS): 
    @$(MAKE) -s -C [email protected] $(TARG)