properly search for lua includes and the library directory
[project/uci.git] / lua / Makefile
1 include ../Makefile.inc
2 LUA_VERSION=5.1
3 PREFIX_SEARCH=/usr /usr/local /opt/local
4 LUA_PLUGINDIR=$(firstword \
5         $(foreach ldir,$(subst ;, ,$(shell lua -e 'print(package.cpath)')), \
6                 $(if $(findstring lib/lua/,$(ldir)),$(patsubst %/?.so,%,$(ldir))) \
7         ) \
8 )
9
10 # find lua prefix
11 LUA_PREFIX=$(firstword \
12         $(foreach prefix,$(PREFIX_SEARCH),\
13                 $(if $(wildcard $(prefix)/include/lua.h),$(prefix)) \
14         ) \
15 )
16
17 libdir=$(prefix)/libs
18 luadir=$(if $(LUA_PLUGINDIR),$(LUA_PLUGINDIR),$(libdir)/lua/$(LUA_VERSION))
19 luainc=$(shell pkg-config --silence-errors --cflags lua$(LUA_VERSION))
20
21 CPPFLAGS=-I.. -I$(if $(luainc),$(luainc),$(LUA_PREFIX)/include)
22 LIBS=-L.. -luci $(shell pkg-config --silence-errors --libs lua$(LUA_VERSION))
23
24 PLUGIN_LD=$(CC)
25 ifeq ($(OS),Darwin)
26   PLUGIN_LDFLAGS=-bundle -undefined dynamic_lookup
27 else
28   PLUGIN_LDFLAGS=-shared -Wl,-soname,$(SHLIB_FILE)
29 endif
30
31 all: uci.so
32
33 uci.so: uci.o
34         $(PLUGIN_LD) $(PLUGIN_LDFLAGS) -o $@ $^ $(LIBS)
35
36 %.o: %.c
37         $(CC) $(CPPFLAGS) $(CFLAGS) $(FPIC) -c -o $@ $<
38
39 install:
40         mkdir -p $(DESTDIR)$(luadir)
41         $(INSTALL) -m0644 uci.so $(DESTDIR)$(luadir)/
42
43 clean:
44         rm -f *.so *.o uci.so