flashburn
flashburn

Reputation: 4528

execute python from makefile using $(shell ...)

I'd like to be able to execute python from a makefile using $(shell ...) command. The trick however is that I do not want to write a full python script.

Here is what I'm trying to do:

VAR = $(shell python -c '''
import argparse
import sys
def main():
    print(r'Hello world')
if '__main__' == __name__:
    main()
''')

I have tried different variations of this but it doesn't work. There is always something - missing separator, incorrect indentation, unknown symbol etc. Has anybody done anything like this before?

Any help is appreciated.

Upvotes: 2

Views: 3392

Answers (2)

Etan Reisner
Etan Reisner

Reputation: 81032

Your quoting is wrong. You are crossing syntax lines that don't like to be crossed. Python's significant whitespace rules are complicating this matter even further. That being said the following solution should work for you:

define PYSCRIPT
import sys
def main():
    print(r"Hello world")
if "__main__" == __name__:
    main()
endef
VAR =: $(shell python -c '$(PYSCRIPT)')

$(info VAR: $(VAR))

''' ... ''' is not a meaningful quotation mechanism at the shell level. The shell sees it as an empty string followed by an opening quote. That's python long string syntax.

make requires line continuation markers to understand make directions/directives/etc. that span multiple lines. The problem with that in the python script context is that python has strict newline/indent/whitespace rules that do not play well with that.

The only context where make doesn't have that restriction is within a define block.

Upvotes: 2

user2325399
user2325399

Reputation: 49

You can simply add "#! /usr/bin/env python3", to the top of your script. Then make it executable by running "chmod +x nameofscript" then in your bash script call it by using "$(shell ./nameofscript)".

Upvotes: 0

Related Questions