damgad
damgad

Reputation: 1446

Getting exit code of shell execution inside define

I have something like this in my makefile:

exit_code := $(shell some_script.py; echo $$?)
ifneq ($(exit_code),0)
    $(error Error occured)
endif

and it works properly - the echo $$? returns exit code of python script

I need to put that code into define like that:

define run-python-script
    exit_code := $(shell some_script.py; echo $$?)
    ifneq ($(exit_code),0)
        $(error Error occured)
    endif
endef
$(call run-python-script)

but then exit_code does not contain the exit code. And $(error Error occured) is always executed.

How to make work the version with define?

Upvotes: 1

Views: 124

Answers (1)

Dummy00001
Dummy00001

Reputation: 17420

I need to put that code into define like that:

  1. The if()/endif are processed and evaluated when the Makefile is parsed. You can't use them inside a variable definition.

  2. The exit_code := ... in first snippet is a definition of a make variable, while in the second it is just a string, part of the make's variable called run-python-script.

You can try this (your script is replaced with false for test purposes):

eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
the-command = false; echo $$?
run-python-script = $(if $(call eq,0,$(shell $(the-command))),,$(error Error occured))
$(run-python-script)

(The $(call) is redundant in that case.)

Upvotes: 2

Related Questions