PACKAGE=lua-oocairo
VERSION=$(shell head -1 Changes | sed 's/ .*//')
RELEASEDATE=$(shell head -1 Changes | sed 's/.* //')
PREFIX=/usr/local
DISTNAME=$(PACKAGE)-$(VERSION)

# The path to where the module's source files should be installed.
LUA_CPATH:=$(shell pkg-config lua5.1 --define-variable=prefix=$(PREFIX) \
                              --variable=INSTALL_CMOD)

LIBDIR = $(PREFIX)/lib

# Uncomment this to run the regression tests with valgrind.
#VALGRIND = valgrind -q --leak-check=yes --show-reachable=yes --num-callers=10

OBJECTS = oocairo.lo
SOURCES := $(OBJECTS:.lo=.c)
MANPAGES = doc/lua-oocairo-context.3 doc/lua-oocairo-fontface.3 doc/lua-oocairo-fontopt.3 doc/lua-oocairo-matrix.3 doc/lua-oocairo-path.3 doc/lua-oocairo-pattern.3 doc/lua-oocairo-scaledfont.3 doc/lua-oocairo-surface.3 doc/lua-oocairo-userfont.3 doc/lua-oocairo.3
GTKBUILDERFILES = examples/gtk-drawing.ui examples/gtk-image.ui

LIBTOOL := libtool --quiet

CFLAGS := -ansi -pedantic -Wall -W -Wshadow -Wpointer-arith \
          -Wcast-align -Wwrite-strings -Wstrict-prototypes \
          -Wmissing-prototypes -Wnested-externs -Wno-long-long \
          $(shell pkg-config --cflags lua5.1 cairo) \
          -DVERSION=\"$(VERSION)\"
LDFLAGS := $(shell pkg-config --libs lua5.1 cairo)

# Uncomment this line to enable optimization.
#CFLAGS := $(CFLAGS) -O3 -fomit-frame-pointer

# Uncomment this line to enable debugging.
DEBUG := -g

# Uncomment this line to prevent the module from being unloaded when Lua exits,
# so that Valgrind can still access the debugging symbols.
#DEBUG := $(DEBUG) -DVALGRIND_LUA_MODULE_HACK

# Uncomment one of these lines to enable profiling and/or gcov coverage testing.
#DEBUG := $(DEBUG) -pg
#DEBUG := $(DEBUG) -fprofile-arcs -ftest-coverage

all: liblua-oocairo.la $(MANPAGES) $(GTKBUILDERFILES)

test: all
	echo 'lunit.main({...})' | $(VALGRIND) lua -llunit - test/*.lua
	lua test-loading.lua
testexamples: all
	for f in examples/*.lua; do \
	    $(VALGRIND) LUA_CPATH='.libs/liblua-?.so;;' lua $$f; \
	done

# Dependencies.
SOURCE_FILES := oocairo.c oocairo.h \
	        obj_context.c obj_font_face.c obj_font_opt.c obj_matrix.c \
	        obj_path.c obj_pattern.c obj_scaled_font.c obj_surface.c
oocairo.lo: $(SOURCE_FILES)

# Compiling the module into a library
%.lo: %.c
	@echo 'CC>' $@
	@$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(DEBUG) -c -o $@ $<
liblua-oocairo.la: oocairo.lo
	@echo 'LD>' $@
	@$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(DEBUG) -o $@ $< -rpath $(LIBDIR)

# This is for building the Windows DLL for the module.  You might have to
# tweak the location of the MingW32 compiler and the Lua library and include
# files to get it to work.  The defaults here are set up for the Lua libraries
# to be unpacked in the current directory, and to compile on Debian Linux
# with the Windows cross compiler from the 'mingw32' package.
WIN32CC = /usr/bin/i586-mingw32msvc-cc
WIN32CFLAGS := -O2 -I/usr/i586-mingw32msvc/include -Iinclude \
               -DVERSION=\"$(VERSION)\"
WIN32LDFLAGS := -L. -llua5.1 -L/usr/i586-mingw32msvc/lib \
                --no-undefined --enable-runtime-pseudo-reloc
win32bin: oocairo.dll
oocairo.win32.o: $(SOURCE_FILES)
	$(WIN32CC) $(DEBUG) $(WIN32CFLAGS) -c -o $@ $<
oocairo.dll: oocairo.win32.o
	$(WIN32CC) $(DEBUG) -O -Wl,-S -shared -o $@ $< $(WIN32LDFLAGS)

# Compiling the POD documentation into man pages.
doc/lua-%.3: doc/lua-%.pod Changes
	sed 's/E<copy>/(c)/g' <$< | sed 's/E<trade>/(tm)/g' <$< | sed 's/E<ndash>/-/g' | sed 's/E<pi>/pi/g' | \
	    pod2man --center="Lua OO Cairo binding" \
	            --name="$(shell echo $< | sed 's/^doc\///' | sed 's/\.pod$$//' | tr a-z A-Z)" --section=3 \
	            --release="$(VERSION)" --date="$(RELEASEDATE)" >$@

# Compile Glade format files generated by Glade into the .ui files, which
# are the ones actually used by the code.
%.ui: %.glade
	gtk-builder-convert $< $@

install: all
	mkdir -p $(LUA_CPATH)
	install --mode=644 .libs/liblua-oocairo.so.0.0.0 $(LUA_CPATH)/oocairo.so
	mkdir -p $(PREFIX)/share/man/man3
	for manpage in $(MANPAGES); do \
	    gzip -c $$manpage >$(PREFIX)/share/man/man3/$$(echo $$manpage | sed -e 's/^doc\///').gz; \
	done

checktmp:
	@if [ -e tmp ]; then \
	    echo "Can't proceed if file 'tmp' exists"; \
	    false; \
	fi
dist: all checktmp
	mkdir -p tmp/$(DISTNAME)
	tar cf - --files-from MANIFEST | (cd tmp/$(DISTNAME) && tar xf -)
	cd tmp && tar cf - $(DISTNAME) | gzip -9 >../$(DISTNAME).tar.gz
	cd tmp && tar cf - $(DISTNAME) | bzip2 -9 >../$(DISTNAME).tar.bz2
	rm -rf tmp

clean:
	rm -f *.o *.lo core
	rm -rf liblua-oocairo.la .libs
	rm -f gmon.out *.bb *.bbg *.da *.gcov
realclean: clean
	rm -f $(MANPAGES)
	rm -f $(GTKBUILDERFILES)

.PHONY: all test testexamples win32bin install checktmp dist clean realclean
