Search code examples
makefiledependencieschaintargets

make: Execution of multiple targets dependant on first one stops after making the first target


I want to do following "stuff" using make:

  1. Convert .pdf files within a folder to .txt files but rename their extension from .txt to .textfile.
  2. Delete their page numbers and save them as .nopage files.
  3. Save all .nopage files into an archive. The archive's name should be archive.tgz.

My targets are: textfile, nopage and archive. all is supposed to do the same as archive.

Here is my code:

SRC=$(wildcard *.pdf)
OBJ=$(SRC:.pdf=.converted)

.PHONY: all archive trimmed converted search-% help clean

.SECONDARY:

help:
    @echo "$$helptext"

textfile: $(OBJ)                        
%.textfile: %.pdf
    pdftotext $< $@

nopage: textfile
%.nopage: %.textfile
    @cat $< | sed 's/Page [0-9]*//g' > $@

archive: nopage
archive.tgz: %nopage
    tar -c --gzip $@ $<

all: archive

Using the variables SRC and OBJ, I collect all the .pdf filenames so that textfile can start with the "first operation", on which the following targets are all depending on. However, my Makefile stops after doing textfile. So if I'd call "make nopage", it would only create the .textfile files and stops there. Same for the targets that follow.

Any ideas on how I can solve that?


Solution

  • Look at these two rules:

    nopage: textfile
    %.nopage: %.textfile
        @cat $< | sed 's/Page [0-9]*//g' > $@
    

    Yes, those are two rules. The first is the one you call; its target is nopagee, it has textfile as a prerequisite, and it has no commands. So after make executes the textfile rule, is does no more. The pattern rule (%.nopage: ...) is never executed at all.

    Here's a good way to do it:

    NOPAGES := $(patsubst %.pdf, %.nopage, $(SRC))
    # or you could use NOPAGES := $(SRC:.pdf=.nopage)
    
    .PHONY: nopages
    nopages: $(NOPAGES)
    %.nopage: %.textfile
        @sed 's/Page [0-9]*//g' $< > $@
    

    And be sure to change the archive rule as well:

    archive.tgz: $(NOPAGES)
        tar -c --gzip $@ $^
    

    Note the use of $^ rather than $< in that last command. Also note that I copied the tar syntax from your makefile; my version of tar won't accept that command, it requires "-f" before "$@".