Post-build step for multiple targets

2020-07-18 04:55发布

问题:

I have a makefile that has multiple targets for outputting data in different formats, e.g. make html, make pdf, make txt etc. and I would like to have pre-build and post-build steps that run when any of these options are used. I have the pre-build step sorted, but not sure how I can get the post-build step working properly.

.PHONY: html pdf txt pre-build post-build

pre-build:
    do-pre-build-stuff

post-build:
    do-post-build-stuff

html: data.dat
    generate-html data.dat

pdf: data.dat
    generate-pdf data.dat

txt: data.dat
    generate-txt data.dat

data.dat: pre-build
    generate-some-data > data.dat

How can I get the post-build step to run after every target?

回答1:

You have to write a different rule for each one, unfortunately. But you can make it simpler with a static pattern rule:

html pdf txt: %: real-%
        do-post-build-stuff

real-html: data.dat
        generate-html data.dat

real-pdf: data.dat
        generate-pdf data.dat

real-txt: data.dat
        generate-txt data.dat

This creates targets html, pdf, and txt which depend on the real- versions. The real- versions do the actual work, then after they're done the post-build stuff is done as a recipe in the base target (html, pdf, and txt).

That rule is just a shorthand so you don't have to write it all out; the result is identical:

html: real-html
        do-post-build-stuff

pdf: real-pdf
        do-post-build-stuff

txt: real-txt
        do-post-build-stuff