#
#    Copyright 2004-2006 Intel Corporation
# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#

#
# Makefile for DTN2/applib
#

# Source files generated by rpcgen
XDRSRCS        := dtn_types_xdr.c

# Sources for the simple client side of the library
CLIENTLIB_SRCS := 			\
		$(XDRSRCS)		\
		dtn_api.c		\
		dtn_errno.c		\
		dtn_ipc.c		\
		sdnv-c.c		\

# Sources for the client-side library that depend on liboasys
CLIENT_EXTRA_SRCS :=			\
		APIEndpointIDOpt.cc	\

# Sources for the server side of the library
SERVERLIB_SRCS := 			\
		$(XDRSRCS)		\
		APIServer.cc		\
		dtn_errno.c		\
		dtn_ipc.c		\

CLIENTLIB_OBJS := $(CLIENTLIB_SRCS:.c=.o)
CLIENTLIB_OBJS := $(CLIENTLIB_OBJS:.cc=.o)
CLIENTLIB_OBJS := $(CLIENTLIB_OBJS) ../dtn-version.o

CLIENT_EXTRA_OBJS := $(CLIENT_EXTRA_SRCS:.c=.o)
CLIENT_EXTRA_OBJS := $(CLIENT_EXTRA_OBJS:.cc=.o)

SERVERLIB_OBJS := $(SERVERLIB_SRCS:.c=.o)
SERVERLIB_OBJS := $(SERVERLIB_OBJS:.cc=.o)

ALLSRCS := $(CLIENTLIB_SRCS) $(CLIENT_EXTRA_SRCS) $(SERVERLIB_SRCS)

#
# Default target is to build the three libraries
#
all: libs

#
# Shorthand rule for versioned library extensions
#
VLIB   = $(DTN_VERSION).a
VSHLIB = $(DTN_VERSION).$(SHLIB_EXT)

#
# rpcgen emits code that has unused variables, so we can't use our
# normally stringent warnings
#
%_xdr.o: %_xdr.c
	@rm -f $@; mkdir -p $(@D)
	$(CC) $(CPPFLAGS) $(CFLAGS_NOWARN) -c $< -o $@

#
# Make sure SRCDIR is set (.. by default)
#
ifeq ($(SRCDIR),)
SRCDIR   := ..
BUILDDIR := ..
endif

#
# Include the common rules and the version rules
#
CLEAN_OTHER := cleanother
.PHONY: cleanother
cleanother:
	@echo "cleaning up miscellaneous generated files/directories... $(OTHER_FILES)" 
	@rm -rf $(OTHER_FILES)

include ../Rules.make
include $(SRCDIR)/dtn-version.mk

#
# When dtn_types.x changes, we now manually rebuild dtn_types.h and
# dtn_types_xdr.c
#
.PHONY: xdrs
xdrs:
	-$(RM) -f dtn_types.h dtn_types_xdr.c
	rpcgen -h dtn_types.x -o dtn_types.h
	rpcgen -c dtn_types.x -o dtn_types_xdr.c

#
# The python and perl scripts need some environment variables to
# properly set up the include and lib paths.
#
ifeq ($(SRCDIR),..)
SWIG_BUILD_ENV := INCDIR=../.. LIBDIR=.. VERSION=$(DTN_VERSION)
else
SWIG_BUILD_ENV := INCDIR=$(SRCDIR) LIBDIR=.. VERSION=$(DTN_VERSION)
endif

#
# For some install rules we need the prefix
#
ifneq ($(DESTDIR),)
INSTALL_PREFIX := --prefix=$(DESTDIR)usr
else
INSTALL_PREFIX := 
endif

#
# SWIG rules to generate the language-specific bindings
#
.PHONY: swig
swig: 
	swig -c++ -tcl    -w302 -o tcl/dtn_api_wrap_tcl.cc    dtn_api.i
	swig -c++ -perl   -w302 -shadow -o perl/dtn_api_wrap_perl.cc  dtn_api.i
	swig -c++ -python -w302 -o python/dtn_api_wrap_python.cc -classic dtn_api.i

#
# Swig rules for Tcl
#
.PHONY: tclapi
tclapi: tcl/libdtntcl-$(VSHLIB)
tcl/libdtntcl-$(VSHLIB): tcl/dtn_api_wrap_tcl.o libdtnapi-$(VSHLIB)
	$(CXX) $(CXXFLAGS) $< $(LDFLAGS_SHLIB) $(LDFLAGS) \
		-ldtnapi-$(DTN_VERSION) $(TCL_LDFLAGS) -o $@

#
# Swig rules for Python
#
.PHONY: pythonapi pythonapi_install
pythonapi: python/dtnapi.pyc
python/dtnapi.pyc: python/setup.py python/dtn_api_wrap_python.cc libdtnapi-$(VSHLIB)
	cd python && $(SWIG_BUILD_ENV) python setup.py build
	cd python && python -c "import py_compile; py_compile.compile('dtnapi.py')"

#
# Need to create the python directory and symlink the relevant files
# if we're not building in the source directory
#
python/setup.py:
	mkdir python && cd python && \
	ln -s $(SRCDIR)/applib/python/dtnapi.py && \
	ln -s $(SRCDIR)/applib/python/dtn_api_wrap_python.cc && \
	ln -s $(SRCDIR)/applib/python/setup.py

pythonapi_install:
	cd python && $(SWIG_BUILD_ENV) python setup.py install $(INSTALL_PREFIX)

OTHER_FILES += python/MANIFEST python/dtnapi.pyc python/build python/dist

#
# Swig rules for Perl
#
.PHONY: perlapi perlapi_install
perlapi: perl/dtn_api_wrap_perl.o

perl/dtn_api_wrap_perl.o: perl/Makefile.PL perl/dtn_api_wrap_perl.cc libdtnapi-$(VSHLIB)
	cd perl && \
	  $(SWIG_BUILD_ENV) perl Makefile.PL CC=$(CXX) LD=$(CXX) CCFLAGS=$(CFLAGS) VERSION=$(DTN_VERSION) && make

perlapi_install: perlapi
	cd perl && make install

#
# Need to create the perl directory and symlink the relevant files
# if we're not building in the source directory
#
perl/Makefile.PL:
	mkdir perl && cd perl && \
	ln -s $(SRCDIR)/applib/perl/Makefile.PL && \
	ln -s $(SRCDIR)/applib/perl/dtn_api_wrap_perl.cc

OTHER_FILES += perl/Makefile perl/Makefile.old perl/blib perl/dtnapi.bs perl/pm_to_blib

#
# Linking rules for the libraries.
#
libdtnapi-$(VLIB): $(CLIENTLIB_OBJS)
	rm -f $@
	$(AR) ruc $@ $^
	$(RANLIB) $@ || true

libdtnapi-$(VSHLIB): $(CLIENTLIB_OBJS)
	rm -f $@
	$(CXX) $(CXXFLAGS) $^ $(LDFLAGS_SHLIB) $(LDFLAGS) -o $@

libdtnapi++-$(VLIB): $(CLIENT_EXTRA_OBJS)
	rm -f $@
	$(AR) ruc $@ $^
	$(RANLIB) $@ || true

libdtnapi++-$(VSHLIB): $(CLIENT_EXTRA_OBJS) libdtnapi.a
	rm -f $@
	$(CXX) $(CXXFLAGS) $^ $(LDFLAGS_SHLIB) $(OASYS_LDFLAGS) libdtnapi.a $(LDFLAGS) -o $@

libdtnapisrv.a: $(SERVERLIB_OBJS)
	rm -f $@
	$(AR) ruc $@ $^
	$(RANLIB) $@ || true

#
# Generate symbolic links to the versioned libraries
#
%.a : %-$(VLIB)
	rm -f $@
	cd `dirname $@` && ln -s `basename $<` `basename $@`

%.$(SHLIB_EXT) : %-$(VSHLIB)
	rm -f $@
	cd `dirname $@` && ln -s `basename $<` `basename $@`

#
# Generate the list of files that is built by default
#
LIBFILES =  libdtnapi.a libdtnapi-$(VLIB) 
LIBFILES += libdtnapi++.a libdtnapi++-$(VLIB) 
LIBFILES += libdtnapisrv.a 
ifeq ($(SHLIBS),yes)
LIBFILES += libdtnapi.$(SHLIB_EXT) libdtnapi-$(VSHLIB) 
LIBFILES += libdtnapi++.$(SHLIB_EXT) libdtnapi++-$(VSHLIB)
LIBFILES += tcl/libdtntcl.$(SHLIB_EXT) tcl/libdtntcl-$(VSHLIB)
endif

ifneq ($(PYTHON_BUILD_EXT),)
LIBFILES += python/dtnapi.pyc
endif

libs: $(LIBFILES)
