Yaegashi's approach will work, but it won't be easy to give it on-the-fly values. The 'export' command is evaluated while the Makefile is being parsed, and sets a shell environment variable to the recipe. Then the environment variable is evaluated during execution of the 'docs' recipe.
If your snippet needs to have any target-dependent variables filled in, I'd recommend an approach like this:
Quick Approach
If you just need to run a couple of one-liners, this pattern will work pretty well.
run_script = python -c \
"import time ;\
print 'Hello world!' ;\
print '%d + %d = %d' %($1,$2,$1+$2) ;\
print 'Running target \'%s\' at time %s' %('$3', time.ctime())"
test:
$(call run_script,4,3,$@)
Fancy Approach
If you want to use weird characters and functions, for-loops, or other multi-line constructs, here's a fancy pattern that will work beautifully.
#--------------------------- Python Script Runner ----------------------------#
define \n
endef
escape_shellstring = \
$(subst `,\`,\
$(subst ",\",\
$(subst $$,\$$,\
$(subst \,\\,\
$1))))
escape_printf = \
$(subst \,\\,\
$(subst %,%%,\
$1))
create_string = \
$(subst $(\n),\n,\
$(call escape_shellstring,\
$(call escape_printf,\
$1)))
python_script = printf "$(call create_string,$($(1)))" | python
#------------------------------- User Scripts --------------------------------#
define my_script
def good_times():
print "good times!"
import time
print 'Hello world!'
print '%d + %d = %d' %($2,$3,$2+$3)
print 'Runni`ng $$BEEF \ttarget \t\n\n"%s" at time %s' %('$4', time.ctime())
good_times()
endef
#--------------------------------- Recipes -----------------------------------#
test:
$(call python_script,my_script,1,2,$@)