LIBTOOL = libtool --silent --tag=CC

LUA_CFLAGS := $(shell pkg-config lua5.1 --cflags)
# This retrieves the name of the libtool convenience library for Lua
# (e.g. "/usr/lib/liblua5.1.la").
LUA_LIBTOOL := $(shell pkg-config lua5.1 --variable=libtool_lib)
# this is the path where you'll eventually install the module
RPATH=/usr/local/lib/lua5.1

# Following is a build and use example for a binary Lua module "foo".  The
# implementation is in "lua-foo.c".  To support dynamic loading conventions
# it will need to have an open function named "luaopen_foo" which registers
# the global module "foo".

test: test-dynamic test-static

# compile source to make objects for static and dynamic link
lua-foo.lo: lua-foo.c
	$(LIBTOOL) --mode=compile $(CC) -c -Wall $(LUA_CFLAGS) lua-foo.c

# link objects to make static and dynamic libraries.  The .so will be
# left in "./.libs/".
liblua-foo.la liblua-foo.so: lua-foo.lo
	$(LIBTOOL) --mode=link $(CC) $(LUA_LIBTOOL) \
		-rpath $(RPATH) -o liblua-foo.la lua-foo.lo
	ln -sf ./.libs/liblua-foo.so

# If all went well, we can dynamically load the module into Lua.  (This
# relies on the addition of "liblua-?.so" pattern to upstream's default
# CPATH.)  The following will load the library into the interpreter and
# call a function.
test-dynamic: liblua-foo.so
	lua5.1 -l foo -e 'print(foo.get_greeting())'

app: app.c liblua-foo.la
	$(LIBTOOL) --mode=link $(CC) -Wall $(LUA_CFLAGS) \
		$(LUA_LIBTOOL) -static -o app app.c liblua-foo.la

test-static: app
	./app

# install static and dynamic libraries for module to global location
install: liblua-foo.la
	$(LIBTOOL) --mode=install install liblua-foo.la $(RPATH)/liblua-foo.la

clean:
	$(RM) *.o *.lo *.la *.so app
	$(RM) -r ./.libs/
