0

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?

MarkS
  • 13
  • 2
  • Are you using GNU Make? With GNU Make you can declare `.ONESHELL:` to have all recipes in the Makefile executed as one shell script, instead of one command invocation per recipe line. You don't have to use backslash continuations and semicolons to simulate it any more. – Kaz Sep 10 '22 at 01:39
  • This looks like an xy-problem. Can you provide a short makefile that shows a real use case for this? – G.M. Sep 10 '22 at 08:15
  • In general, you can't. You've assigned a *shell* variable `MKSTAT`. The shell is an entirely different process than make, and nothing that happens in the shell is or can be visible to make (except its output). It's fundamental to the operating system that no process can modify the contents of memory of any other process: this has nothing to do with make. As G.M. says you'll have to do something different and that depends on the real details of your problem. – MadScientist Sep 10 '22 at 14:17
  • @Kaz, Yes, GNU Make. I'm only at version 3.81, so I don't think I have use of .ONESHELL. – MarkS Sep 12 '22 at 12:50
  • @Kaz, just to be clear, I have tried .ONESHELL and doesn't work. Using .ONESHELL would help the readability of the code, but does it help my issue? – MarkS Sep 12 '22 at 12:57
  • @MadScientist, your insight is good, thanks. I know enough about shell processes and Make to be dangerous, definitely not proficient. – MarkS Sep 12 '22 at 13:01
  • `.ONESHELL` won't help: see my comment above. – MadScientist Sep 12 '22 at 13:53

1 Answers1

0

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.

Andreas
  • 5,086
  • 3
  • 16
  • 36