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

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

guylobster
This post was updated on .
hello,
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)


results
python estimationkmer.py 10
5.48289214233
ESTK=
echo

The problem is the line "ESTK=$(python estimationkmer.py $(G_SIZE))"

and the script in python is:

from math import log10
import sys
def estimationkmer():
        n1 = int(sys.argv[1])
        n2 = log10(200 * n1) / log10(4)
        print(n2)
estimationkmer()


thank you for your help
Reply | Threaded
Open this post in threaded view
|

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

Paul Smith-20
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
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

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

guylobster
thank you for your help.
it works now. With your explanation, I understood how to fix the three problems of the line.

Just a clarification. The second problem means that I could not reuse the variable ESTK in another rule (not in the rule Quake where it is defined)? because I can use "; \" only for consecutive lignes.

Reply | Threaded
Open this post in threaded view
|

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

Nicholas Clark
In reply to this post by Paul Smith-20
What about doing it with an EVAL statement?

Like this:
---------------------------- Begin to_upper.py
---------------------------------

$ cat to_upper.py
#!/usr/bin/env python

import sys

def main():
    sys.stdout.write(sys.argv[1].upper().strip()+"\n")

if __name__ == "__main__":
    main()

------------------------------- Begin Makefile
-----------------------------------
$ cat Makefile

INPUT := "test string"
OUTPUT := "UNDEFINED"


test:
    @echo OUTPUT is: [$(OUTPUT)]
    $(eval OUTPUT := $(shell python to_upper.py $(INPUT)))
    @echo OUTPUT is: [$(OUTPUT)]

--------------------------------Results of Execution
-----------------------------

$ make test
OUTPUT is: [UNDEFINED]
OUTPUT is: [TEST STRING]


-Nick


On Wed, Mar 25, 2015 at 4:55 AM, Paul Smith <[hidden email]> 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
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-make
>



--
Nicholas Clark
Email: [hidden email]
_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

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

Britton Kerin
In reply to this post by Paul Smith-20
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 <[hidden email]> 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
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-make

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

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

guylobster
In reply to this post by Nicholas Clark
hello Nicholas Clark
thank you for your advice
You are right, I was not even asked the question but it is important that I can see if my command fails.
I tried your method. this may be a good idea.
thank you
Reply | Threaded
Open this post in threaded view
|

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

guylobster
In reply to this post by Britton Kerin
hello Britton
thank you for your advice
You are right, I was not even asked the question but it is important that I can see if my command fails. I'll think about that. thank you