2014-12-31 4 views
2

에 주어진 변수가 나는 메이크업과 같은 것을 할 노력하고있어 때 일치 :으로 반복 문

SRC := $(src/*.md) 
DIST := $(subst -,/,$(patsubst src/%.md, dist/%/index.html, $(SRC))) 

all: $(DIST) 

$(DIST): $(SRC) 
    mkdir -p $(@D) && pandoc $< -o [email protected] 

예는 필수 src/2014-04-myfile.mdpandoc

변환과 대상 dist/2014/04/myfile/index.html에 넣고

그러나 $<을 사용하면 $(SRC) 변수의 첫 번째 인수 만 참조합니다.

나는 우리가 같은 것을 할 것입니다 일반적으로 알고

dist/%.html: src/%.md 

을하지만 난 모든 파일에 대해 단지 index.html에 출력에 파일 이름을 변경하고 난 새 경로를 생성하기 위해 원본 파일 이름을 사용하기 때문에 전제 조건을 반복하는 방법에 대해 확신하지 못합니다.

답변

2

여기 한 가지 방법이 있습니다. 이것이 작동하는 방식은 $(SRC)을 반복하여 소스 파일 하나당 하나의 규칙을 만드는 것입니다. MAKE_DEP의 내용을 처음 읽을 때 make이 함수를 해석하지 못하도록하려면 을 MAKE_DEP에 사용해야합니다. calleval에 대한 설명서도 유용합니다. 내가 얻을

src/2000-01-bar.md 
src/2014-04-foo.md 

$ make -n을 실행합니다 :

mkdir -p dist/2000/01/bar && pandoc src/2000-01-bar.md -o dist/2000/01/bar/index.html 
mkdir -p dist/2014/04/foo && pandoc src/2014-04-foo.md -o dist/2014/04/foo/index.html 

이것은 또한 secondary expansion을 사용하여 수행 할 수 있지만 간단 나에게 표시하지 않았거나

SRC := $(wildcard src/*.md) 

# Set the default goal if no goal has been specified... 
.DEFAULT_GOAL:=all 

# 
# This is a macro that we use to create the rules. 
# 
define MAKE_DEP 
# _target is a temporary "internal" variable used to avoid recomputing 
# the current target multiple times. 
_target:=$$(subst -,/,$$(patsubst src/%.md, dist/%/index.html, $1)) 

# Add the current target to the list of targets. 
TARGETS:=$$(TARGETS) $$(_target) 

# Create the rule proper. 
$$(_target):$1 
    mkdir -p $$(@D) && pandoc $$< -o [email protected] 

endef # MAKE_DEP 

# Iterate over $(SRC) to create each rule. 
$(foreach x,$(SRC),$(eval $(call MAKE_DEP,$x))) 

.PHONY: all 
all: $(TARGETS) 

내가 작성하는 경우 좋네.