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: Nicholas Clark
Subject: Re: assign the result of my python script to a variable of my makefile
Date: Wed, 25 Mar 2015 08:32:43 -0700

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 <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
>



-- 
Nicholas Clark
Email: address@hidden


reply via email to

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