# -*- makefile -*- Automake include file
# $Id: pluglib-bindings.ami 27327 2006-01-25 14:12:13Z mvidner $

# Wraps a pluglib for use by YaST. Done by using Swig for Perl, adding
# type information. Compiles, installs.

# expected input:
#  PLUGLIB_MODULE = the name after %module in .i
#  PLUGLIB_LIBRARY = the object file/library that is being wrapped
#  PLUGLIB_DEPEND = dependencies
# example:
#  PLUGLIB_MODULE = Storage
#  PLUGLIB_LIBRARY = .libs/liby2storage.so
#  PLUGLIB_DEPEND = ../../src/StorageInterface.h
#  include $(top_srcdir)/pluglib-bindings.ami

TARGET_PM = $(PLUGLIB_MODULE).pm
TARGET_SO = $(PLUGLIB_MODULE).so

# ydatadir or `pkg-config --variable=yast2dir yast2-devtools`/data ?
# putting files to a directory owned by another package?
# ok but then they should share prefixes (maybe not really)
PLGB_BASEDIR = $(ydatadir)/devtools/pluglib-bindings

# TODO configure
SWIG = swig
#SWIG = $(HOME)/pfx-swig-1.3.24/bin/swig
SWIGFLAGS = -I$(PLGB_BASEDIR)/swig -c++

# Process the .i file, creating a wrapper for Perl and a XML parse.
%_wrap.cc %_wrap.xml: %.i $(PLUGLIB_DEPEND)
	$(SWIG) -perl $(SWIGFLAGS) -o $*_wrap.cc -xmlout $*_wrap.xml $<

PLGB_TYPEINFO_XSL = $(PLGB_BASEDIR)/stylesheets/swigxml2typeinfo.xsl
PLGB_TYPEINFO_FILTER = $(PLGB_BASEDIR)/stylesheets/filter.pl

# Add typeinfo
# (sablotron indeed has fewer build dependencies than xsltproc)
# PLGB_TYPEINFO_FILTER opens all files in append mode
# which is strange but we can live with it by deleting the
# appropriate files beforehand.
$(TARGET_PM): %.pm: %_wrap.xml
	if test -d $(PLUGLIB_MODULE); then \
	  rm -f $(PLUGLIB_MODULE)/*.pm; \
	fi
	sabcmd $(PLGB_TYPEINFO_XSL) $< | perl $(PLGB_TYPEINFO_FILTER)

# Install the Perl modules
PLGB_PMdir = $(moduledir)
PLGB_PM_DATA = $(TARGET_PM)
# Copy the directory with additional pm modules
install-data-local:
	if test -d $(PLUGLIB_MODULE); then \
	  cp --recursive $(PLUGLIB_MODULE) $(DESTDIR)$(PLGB_PMdir); \
	fi
# This is to ensure that automake generates the LTCXXCOMPILE variable.
# We need to use LTCXXCOMPILE because libtool puts in -fPIC on some
# architectures and also we get RPM_OPT_FLAGS via CXXCLAGS that way.
noinst_PROGRAMS = dummy_to_have_cxxcompile
dummy_to_have_cxxcompile_SOURCES = dummy_to_have_cxxcompile.cc
dummy_to_have_cxxcompile.cc:
	echo 'int main() {return 0;}' > $@

# Compile the wrapper
.libs/%_wrap.o: %_wrap.cc
	$(LTCXXCOMPILE) $(PERL_CFLAGS) -c -o $*_wrap.lo $<

# Link the wrapper and the actual library
$(TARGET_SO): %.so: .libs/%_wrap.o $(PLUGLIB_LIBRARY)
	$(CXXCOMPILE) -shared -o $@ $^

# Install the library
PLGB_SOdir = $(PERL_VENDORARCH)
PLGB_SO_DATA = $(TARGET_SO)

CLEANFILES = $(PLUGLIB_MODULE)_wrap.*