Skip to content

Instantly share code, notes, and snippets.

@deryni
Last active January 1, 2016 11:59
Show Gist options
  • Save deryni/8142074 to your computer and use it in GitHub Desktop.
Save deryni/8142074 to your computer and use it in GitHub Desktop.
Make snippet to construct a lua command.
# Construct LUA_{,C}PATH strings from input modules.
# $1 - list of modules
# $2 - suffix of valid module names
luapath = $(subst $e ,;,$(filter $(addprefix %.,$2),$(join $(foreach m,$1,$(dir $m)),$(foreach m,$1,?$(suffix $m)))))
# Create -l arguments from the input modules.
# $1 - list of modules
luamodules = $(addprefix -l,$(filter-out ?,$(basename $(notdir $1))))
# Create a dummy loaded package for the given names.
# 'lua_dummymod_$modname' variables will be included as the contents of the
# dummy tables created.
luadummypkg = $(foreach m,$1,$m = {$(lua_dummymod_$m)} package.loaded.$m = $m)
# $1 - modules to load (paths optional, will get added to LUA_{,C}PATH as appropriate)
# $2 - lua command string
# $3 - pre-module command string
define luarun
LUA_PATH='$(call luapath,$1,lua);;' LUA_CPATH='$(call luapath,$1,so);;' lua $(and $3,-e '$3') $(luamodules) $(and $2,-e '$2')
endef
# Transforms calls like
#
# $(call luarun,,print("test 1"),)
# into
# LUA_PATH='' LUA_CPATH='' lua -e 'print("test 1")'
#
# and
#
# $(call luarun,foo/mod1.lua bar/mod2.so,print("test 2"))
# into
# LUA_PATH='foo/?.lua;;' LUA_CPATH='bar/?.so;;' lua -lmod1 -lmod2 -e 'print("test 2")'
#
# and
#
# $(call luarun,foo/mod1.lua bar/mod2.so,print("test 3"),print("pre-test 3"))
# into
# LUA_PATH='foo/?.lua;;' LUA_CPATH='bar/?.so;;' lua -e 'print("pre-test 3")' -lmod1 -lmod2 -e 'print("test 3")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment