henrikstroem
henrikstroem

Reputation: 3068

More than one build in a Makefile

I am trying to put together a Makefile to compile a number of documents.

There a three separate 'jobs' in my Makefile, my problem is that only the first job is processed. Each of the jobs works by themselves, or if they are the first job in the file.

It is important that running the total build is as easy as make, so the people contributing with documents don't have to fiddle around with things.

How do I make all three jobs run?

# 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 $@


# 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 $@


.PHONY : clean

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

-- UPDATE

Based on the answers below, the fully functioning Makefile looks like this:

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 $@


# 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 $@


.PHONY : clean all docs report

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

Upvotes: 0

Views: 113

Answers (1)

Clifford
Clifford

Reputation: 93446

Unless you specify a target, only the first target in the file will be built. Conventionally you would have a target all as the first target with all other targets as dependencies. That way all targets will be built by default. So in general the first target should be:

all : job1 job2 job3

I suggest that in your case, the two all targets should be renamed. The duplicate macros source, output and sources also need to be unique or the duplication removed.

Upvotes: 1

Related Questions