Ricardo Altamirano
Ricardo Altamirano

Reputation: 15208

How do I write a makefile to run a shell command after all targets in the file have been processed?

I have a single makefile that runs a group of tests involving various files and writes the results to log files. Is it possible to write the makefile so that it will run the same set of shell commands after all other targets in the file have been processed, regardless of errors that occurred.

I know I could use a shell script that calls the makefile and then the shell commands, but I'm wondering if it's possible to do this from within the makefile itself, without using some phony target like make print (for example).

Upvotes: 3

Views: 6997

Answers (1)

Jonathan Ben-Avraham
Jonathan Ben-Avraham

Reputation: 4831

You could try something like this:

   .PHONY: test1 test2 test3 finale

    all: finale

    finale: test1 test2 test3

    test1:
        - exit 1

    test2:
        - exit 2

    test3:
        - exit 3

    finale:
            echo "And the winner is..."

You make your script, the target "finale", dependent on the other targets, and you use the "-" operator to ignore the non-zero return codes from the tests.

Upvotes: 2

Related Questions