2015-03-19 27 views
0

我試圖編譯一個Makefile來編譯一些文檔。Makefile中有多個內部版本

在我的Makefile中有三個獨立的'作業',我的問題是隻處理第一個作業。每個作業都是獨立工作的,或者它們是文件中的第一份工作。

運行整個構建非常重要,就像make一樣簡單,因此貢獻文檔的人員不必擺弄東西。

如何讓所有三個工作都運行?

# Generate index 
index.html: index.md 
    pandoc \ 
     index.md \ 
     -f markdown_github \ 
     -t html5 \ 
     -s \ 
     -o index.html 


# Generate background-writing documents 
source := background-writing 
output := dist 
sources := $(wildcard $(source)/*.md) 
objects := $(patsubst %.md,%.pdf,$(subst $(source),$(output),$(sources))) 
all: $(objects) 

$(output)/%.pdf: $(source)/%.md 
    pandoc \ 
     --variable geometry:a4paper \ 
     --number-sections \ 
     -f markdown $< \ 
     -s \ 
     -o [email protected] 


# Compile report 
source := draft 
output := dist 
sources := $(wildcard $(source)/*.md) 
all: $(output)/report-print.pdf 

$(output)/report-print.pdf: $(sources) 
    cat $^ | pandoc \ 
     --variable geometry:a4paper \ 
     --number-sections \ 
     --toc \ 
     --from markdown \ 
     -s \ 
     -o [email protected] 


.PHONY : clean 

clean: 
    rm -f $(output)/*.pdf 

- UPDATE

基於下面的答案,全功能的Makefile看起來是這樣的:

all: index.html docs report 

# Generate index 
index.html: index.md 
    pandoc \ 
     index.md \ 
     -f markdown_github \ 
     -t html5 \ 
     -s \ 
     -o index.html 


output := dist 


# Generate background-writing documents 
docsource := background-writing 
docsources := $(wildcard $(docsource)/*.md) 
objects := $(patsubst %.md,%.pdf,$(subst $(docsource),$(output),$(docsources))) 
docs: $(objects) 

$(output)/%.pdf: $(docsource)/%.md 
    pandoc \ 
     --variable geometry:a4paper \ 
     --number-sections \ 
     -f markdown $< \ 
     -s \ 
     -o [email protected] 


# Compile report 
reportsource := draft 
reportsources := $(wildcard $(reportsource)/*.md) 
report: $(output)/report-print.pdf 

$(output)/report-print.pdf: $(reportsources) 
    cat $^ | pandoc \ 
     --variable geometry:a4paper \ 
     --number-sections \ 
     --toc \ 
     --from markdown \ 
     -s \ 
     -o [email protected] 


.PHONY : clean all docs report 

clean: 
    rm -f $(output)/*.pdf 

回答

1

除非你指定一個目標,只有在文件中的第一個目標將是建成。通常情況下,您會將目標all作爲第一個目標,並將所有其他目標作爲依存關係。這樣所有目標都將默認生成。所以一般第一目標應該是:

all : job1 job2 job3 

我建議你的情況,這兩個all目標應該改名。重複宏source,outputsources也需要是唯一的或刪除重複。

+2

這通常是正確的,但有一些問題。首先也是最關鍵的,你從不在先決條件之間使用逗號;這會給你一個錯誤(例如,無法構建目標'index.html')。它應該是'all:index.html文檔報告'。其次,如果你有多個目標,比如'all',在makefile中有不同的先決條件;只要他們只有一個配方(在這種情況下,「全部」根本沒有任何配方,所以沒關係)。第三,因爲'all','docs'和'report'不是真正的目標,所以作者應該聲明它們爲'.PHONY'。 – MadScientist 2015-03-19 11:47:18

+2

最後,您應該明確爲什麼作者的嘗試失敗:如果您沒有在命令行上提供任何目標,make的默認行爲是確保它在makefile中找到的_first_目​​標是最新的。在這個問題中,第一個目標是'index.html',所以這就是make的構建方式,除非你用'make report'來請求另外一個。所以,答案是創建一個像'all'這樣的目標,這取決於所有你想要默認建立的東西,並確保_that_是makefile中的第一個目標。然後'make'和'make all'是等價的。 – MadScientist 2015-03-19 11:51:03

+0

我把你的建議寫入了Makefile,現在它正在工作。我在問題中包含了功能文件,以便其他人也可以從中學習。多謝你們。 – henrikstroem 2015-03-19 12:51:50