# Makefile for win32api library for Lua

ifneq "$(shell pkg-config --version)" ""
  # automagic setup (OS X fink, Linux apt-get, ..)
  #
  LUAINC= $(shell pkg-config --cflags lua5.1)
  LUALIB= $(shell pkg-config --libs lua5.1)
else
  # manual setup (change these to reflect your Lua installation)
  #
  BASE= /usr/local
  LUAINC= -I$(BASE)/include
  LUALIB=
#  Windows' LUALIB is the same as the Lua executable's directory...
#  LUALIB= -L$(BASE)/bin -llua51
  #
  POD2HTML= perl -x -S doc/pod2html.pl
endif

LUAEXE= lua5.1

INSTALL= install -p
INSTALLPATH= $(LUAEXE) installpath.lua

TMP=./tmp
DISTDIR=./archive

# OS detection
#
SHFLAGS=-shared
UNAME= $(shell uname)
ifeq "$(UNAME)" "Linux"
  _SO=so
  SHFLAGS=-shared -fPIC
endif
ifneq "" "$(findstring BSD,$(UNAME))"
  _SO=so
endif
ifneq "" "$(findstring MINGW64,$(UNAME))"
  _SO=dll
endif
ifneq "" "$(findstring MINGW32,$(UNAME))"
  _SO=dll
endif
ifeq "$(UNAME)" "Darwin"
  _SO=so
  SHFLAGS=-fPIC -arch i686 -arch x86_64
  SOFLAGS=-dynamiclib -single_module -undefined dynamic_lookup -arch i686 -arch x86_64
endif
ifneq "" "$(findstring msys,$(OSTYPE))"		# 'msys'
  _SO=dll
endif

ifndef _SO
  $(error $(UNAME))
  $(error Unknown OS)
endif

# no need to change anything below here - HAH!
CFLAGS= $(INCS) $(DEFS) $(WARN) -O2 -fomit-frame-pointer $(SHFLAGS)
WARN= -Wall #-ansi -pedantic -Wall
INCS= $(LUAINC)
LIBS= $(LUALIB) -lpsapi -lMpr

MYNAME= winapi
MYLIB= $(MYNAME)

VER=$(shell svnversion -c . | sed 's/.*://')
TARFILE = $(DISTDIR)/$(MYLIB)-$(VER).tar.gz

OBJS= $(MYLIB).o wutils.o
T= $(MYLIB).$(_SO)

all: $(T)

$(T): $(OBJS)
	$(CC) $(SHFLAGS) $(SOFLAGS) -o $@ $(OBJS) $(LIBS)

install: $(T)
	$(INSTALL) $< `$(INSTALLPATH) $(MYLIB)`

clean:
	rm -f $(OBJS) $T core core.* a.out

dist: html
	echo 'Exporting...'
	mkdir -p $(TMP)
	mkdir -p $(DISTDIR)
	svn export . $(TMP)/$(MYLIB)-$(VER)
	mkdir -p $(TMP)/$(MYLIB)-$(VER)/doc
	cp -p doc/lsqlite3.html $(TMP)/$(MYLIB)-$(VER)/doc
	echo 'Compressing...'
	tar -zcf $(TARFILE) -C $(TMP) $(MYLIB)-$(VER)
	rm -fr $(TMP)/$(MYLIB)-$(VER)
	echo 'Done.'

.PHONY: all test clean dist install
