help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: assign the result of my python script to a variable of my makefile


From: Britton Kerin
Subject: Re: assign the result of my python script to a variable of my makefile
Date: Wed, 25 Mar 2015 08:39:54 -0800

One other small thing that may or may not be obvious: if your
estimationkmer.py script fails (returns non-zero), you won't be
notified (and ESTK will likely end up empty).

Paul, is there any chance of adding a different version of $(shell)
that fails noisily if the given shell command returns non-zero?

I often use this goofy function:

# This function works almost exactly like the built-in shell command, except it
# stops everything with an error if the shell command given as its argument
# returns non-zero when executed.  The other difference is that the output
# is passed through the strip make function (the shell function strips
# only the last trailing newline).  In practice this doesn't matter much
# since the output is usually collapsed by the surrounding make context
# to the same result produced by strip.  WARNING: don't try to nest calls
# to this function.
SHELL_CHECKED = \
  $(strip \
    $(if $(shell (($1) 1>/tmp/SC_so) || echo 'non-empty'), \
      $(error shell command '$1' failed.  Its stderr should be above \
              somewhere.  Its stdout is available for review in '/tmp/SC_so'), \
      $(shell cat /tmp/SC_so)))

Britton

On Wed, Mar 25, 2015 at 3:55 AM, Paul Smith <address@hidden> wrote:
> On Wed, 2015-03-25 at 02:54 -0700, guylobster wrote:
>> this is the first time I do a makefile.
>> But I block to assign the result of my python script to a variable of my
>> makefile.
>> The function works. Here the function and the result.
>>
>> *My makefile*
>> G_SIZE=10
>> Quake:
>>         python estimationkmer.py $(G_SIZE)
>>         ESTK=$(python estimationkmer.py $(G_SIZE))
>>         echo $(ESTK)
>
> You have three problems here:
>
> First you have to escape dollar signs which you want to be passed to the
> shell:
>
>    ESTK=$$(python estimationkmer.py $(G_SIZE))
>
> (note the double "$$" here) to escape the "$" so make passes it to your
> shell.
>
> The second problem is that make invokes every line of a recipe as a
> separate shell script, so variables assigned in one line are lost before
> the next line.  If you want to set a shell variable and use it again you
> have to put both commands in the same shell:
>
>    ESTK=$$(python estimationkmer.py $(G_SIZE)) ; \
>    echo $$ESTK
>
> Finally, note this is setting a SHELL variable.
>
> You can't set a MAKE variable from within a recipe, because recipes are
> passed to the shell and run there.  If you want to set a make variable
> you have to do it outside of a recipe, using make's shell function:
>
>   ESTK := $(shell python estimationkmer.py $(G_SIZE))
>
>   Quake:
>           echo $(ESTK)
>
>
> _______________________________________________
> Help-make mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-make



reply via email to

[Prev in Thread] Current Thread [Next in Thread]