#
# Copyright (c) 2008 Aconex.  All Rights Reserved.
# Copyright (c) 2000,2003,2004 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
#

TOPDIR = ../../..
include $(TOPDIR)/src/include/builddefs
-include ./GNUlocaldefs

CFILES = connect.c context.c desc.c err.c fetch.c freeresult.c \
        help.c instance.c p_desc.c p_error.c p_fetch.c p_instance.c \
	p_profile.c p_result.c p_text.c p_pmns.c p_creds.c pdu.c \
	pdubuf.c pmns.c pool.c profile.c store.c units.c util.c ipc.c \
        sortinst.c logmeta.c logportmap.c logutil.c tz.c interp.c \
	checksum.c rtime.c tv.c spec.c fetchlocal.c optfetch.c AF.c \
        stuffvalue.c endian.c config.c access.c authcookie.c auxconnect.c \
	p_lcontrol.c p_lrequest.c p_lstatus.c logconnect.c logcontrol.c

ifneq "$(TARGET_OS)" "mingw"
CFILES += loop.c
LSRCFILES = win32.c
else
CFILES += win32.c
LSRCFILES = loop.c
LLDLIBS	+= -lpsapi
endif

ifeq "$(LIB_FOR_BASENAME)" "-lpcp"
# don't need to be linked to myself in this case!
LIB_FOR_BASENAME =
endif

LLDLIBS	+= $(LIB_FOR_MATH)

LCFLAGS += -DLIBPCP_INTERNAL '-DEXEC_SUFFIX="$(EXECSUFFIX)"' \
	'-DCPP_SIMPLE="$(CPP_SIMPLE)"' '-DCPP_SIMPLE_ARGS="$(CPP_SIMPLE_ARGS)"'

DSOVERSION = 3
STATICLIBTARGET = libpcp.a
LIBTARGET = libpcp.$(DSOSUFFIX).$(DSOVERSION)
SYMTARGET = libpcp.$(DSOSUFFIX) libpcp.$(DSOSUFFIX).2

ifeq "$(PACKAGE_DISTRIBUTION)" "debian"
SYMTARGET = libpcp.$(DSOSUFFIX)
endif
ifeq "$(TARGET_OS)" "darwin"
LIBTARGET = libpcp.$(DSOVERSION).$(DSOSUFFIX)
SYMTARGET = libpcp.$(DSOSUFFIX)
endif
ifeq "$(TARGET_OS)" "mingw"
STATICLIBTARGET = 
LIBTARGET = libpcp.$(DSOSUFFIX)
SYMTARGET =
endif

LDIRT = $(SYMTARGET) dsotbl.h

base default : $(LIBTARGET) $(SYMTARGET) $(STATICLIBTARGET)

ifneq "$(SYMTARGET)" ""
$(SYMTARGET): 
	$(LN_S) -f $(LIBTARGET) $@
endif

include $(BUILDRULES)

connect.o: dsotbl.h

ifeq "$(TARGET_OS)" "mingw"
kernel_pmda_dso	= windows
else
kernel_pmda_dso	= $(TARGET_OS)
endif

ifeq ($(HAVE_IBDEV),1)
infiniband_pmda_dso = ib
else
infiniband_pmda_dso =
endif

dsotbl.h:	$(TOPDIR)/src/pmns/stdpmid 
	echo '/* This file is automatically generated by build' > $@
	echo ' *' >> $@
	echo ' * It contains list of DSO, supported by the CONTEXT_LOCAL' >> $@
	echo ' */' >> $@
	echo 'static __pmDSO dsotab[] = {' >> $@
	for dso in $(kernel_pmda_dso) $(infiniband_pmda_dso) sample ; do \
		udso=`echo $$dso | tr '[:lower:]' '[:upper:]' | sed -e's/SAMPLE/SAMPLEDSO/'` ; \
		domnum=`$(PCP_AWK_PROG) < $^ '$$1=="#define" && $$2=="'$$udso'" { print $$3 }'` ;\
		echo '#define ' `echo $$dso | tr '[:lower:]' '[:upper:]'`_DSO $$domnum ; \
		echo "	{ $${domnum}, \"$$dso/pmda_$${dso}.$(DSOSUFFIX)\", \"$${dso}_init\" },"; \
	done >> $@
	echo '};' >> $@
	echo 'static int	numdso = (sizeof(dsotab)/sizeof(dsotab[0]));' >> $@

install : default
	$(INSTALL) -m 755 $(LIBTARGET) $(PCP_LIB_DIR)/$(LIBTARGET)
ifneq "$(SYMTARGET)" ""
	for tt in $(SYMTARGET); do \
	    $(INSTALL) -S $(LIBTARGET) $(PCP_LIB_DIR)/$$tt || exit 1; \
	done
endif
ifneq "$(STATICLIBTARGET)" ""
	$(INSTALL) -m 755 $(STATICLIBTARGET) $(PCP_LIB_DIR)/$(STATICLIBTARGET)
endif

default_pcp : default

install_pcp : install

$(TOPDIR)/src/pmns/stdpmid:
	cd $(@D); $(MAKE) $(@F)

