makefile: How to capture exit status in variable for use elsewhere in makefile

Viewed 38

I understand that MAKE will exit on exit statuses other than 0, but I need to just capture the status and later on in the recipe decide to exit the make script. So I would like to:

  1. capture the exit status of a target command in a variable,
  2. do other tasks like printing a summary of execution progress, etc and then
  3. use the exit status to decide to exit the makefile script.
    I have been through many threads on Stack Overflow, but I don't see any examples that are specific enough to address my issue.

This code works, but MKSTAT is only valid within in the shell where it is.

# The "somecmd" will exit with a status of 0 or greater than 0.
getstatus:
    somecmd; \
    MKSTAT=$$?; \
    echo $$MKSTAT

So, you can see that the getstatus code is all in one shell. I can't use MKSTAT outside of this shell, it has to be used in the shell where it is assigned. I need something more flexible. How do I save MKSTAT as a MAKE variable that the makefile script can use in some other part of the recipe?

1 Answers

What you should do is capture the exit code in a file, have the file be the target of a rule, and use that file as prerequisite for other rules.

Related