#******************************************************************************
#
#       Copyright:      2005-2010 Paul Obermeier (obermeier@tcl3d.org)
#
#                       See the file "Tcl3D_License.txt" for information on
#                       usage and redistribution of this file, and for a
#                       DISCLAIMER OF ALL WARRANTIES.
#
#       Module:         Tcl3D -> tcl3dCg
#       Filename:       Makefile
#
#       Author:         Paul Obermeier
#
#       Description:    Makefile for the tcl3dCg subpackage.
#                       The syntax is based on GNU make.
#
#******************************************************************************

.PHONY: all install clean distclean

include ../make.oscheck
include ../make.macros
include ../make.wrap

# The key file name to detect whether Tcl3D could be wrapped
# with Cg support. If this file does not exist, we can not wrap
# the Cg library, even if WRAP_CG has been defined in make.wrap.
KEY_CG  = Cg/cg.h

HAVE_CG  =
ifdef WRAP_CG
HAVE_CG  = $(strip $(wildcard $(KEY_CG)))
endif

CGLIBS =
ifneq (,$(HAVE_CG))
PPDEFS += -DUSE_CG_IF
ifeq (win32,$(KERNEL))
CGLIBS = lib$(DSEP)cg$(LIBEXT) lib$(DSEP)cgGL$(LIBEXT) 
THREADLIB =
else
ifeq (Darwin,$(KERNEL))
CGLIBS = -framework Cg
THREADLIB =
else
ifeq (mingw,$(KERNEL))
CGLIBS = -L../extlibs/$(OS_TYPE) -lCg -lCgGL
THREADLIB =
else
CGLIBS = -L../extlibs/$(OS_TYPE) -lCg -lCgGL
THREADLIB = -lpthread
endif
endif
endif
endif

WRAPOBJS = \
        tcl3dCg_wrap$(OBJEXT)

SWIG_FILES = \
        $(COMMON_SWIGFILES) \
        swigfiles/cg.i \
        swigfiles/tcl3dCg.i

CGEXPANDED = \
        cgExpanded.h

WRAPSRCS = $(WRAPOBJS:$(OBJEXT)=.c)
ALL_SRCS = $(WRAPSRCS)

PKG_NAME = tcl3dCg
PKG_LIB  = $(PKG_NAME)$(SHLIBEXT)

CFLAGS = $(OPT) $(PPDEFS) -I$(GL_INCDIR) -I$(GLU_INCDIR) -ICg $(INCL)

.SUFFIXES:                  # Delete the default suffixes.
.SUFFIXES: .c $(OBJEXT) .h  # Install our own suffixes.

.c$(OBJEXT):
	$(CC) $(CFLAGS) $<

all:    $(PKG_LIB)

install: all
	-$(MKDIR) $(TCL3D_INSTLIB)
	-$(MKDIR) $(TCL3D_INSTLIB)$(DSEP)$(PKG_NAME)
	$(CP) $(PKG_LIB)                      $(TCL3D_INSTLIB)$(DSEP)$(PKG_NAME)
	$(CP) tclfiles$(DSEP)tcl3dCgQuery.tcl $(TCL3D_INSTLIB)$(DSEP)$(PKG_NAME)
	$(CP) tclfiles$(DSEP)tcl3dCgUtil.tcl  $(TCL3D_INSTLIB)$(DSEP)$(PKG_NAME)
	$(CP) tclfiles$(DSEP)pkgIndex.tcl     $(TCL3D_INSTLIB)$(DSEP)$(PKG_NAME)
	$(CP) ..$(DSEP)pkgIndex.tcl           $(TCL3D_INSTLIB)

clean:
	-$(RM) $(WRAPOBJS)
	-$(RM) $(WRAPSRCS)
	-$(RM) $(VC_CLEANUP_FILES)
	-$(RM) $(CGEXPANDED)

distclean: clean
	-$(RM) $(PKG_LIB)

tcl3dCg_wrap.c: $(SWIG_FILES) $(CGEXPANDED)
	swig -tcl -pkgversion $(VERSION) $(PPDEFS) \
             -I../swigfiles -Iswigfiles -ICg -I$(GL_INCDIR) -I$(GLU_INCDIR) $(INCL) \
             -o $@ swigfiles/tcl3dCg.i

$(PKG_LIB): $(WRAPOBJS)
	$(SHLIB_LD) $(SHLIB_CFLAGS) $(LDOUT)$@ \
                    $(WRAPOBJS) \
                    $(TCLLIBS) $(CGLIBS) $(GLLIBS) $(SYSLIBS) $(THREADLIB)
	$(MT32) -manifest $(PKG_LIB).manifest -outputresource:"$(PKG_LIB);2"
ifneq (,$(HAVE_CG))
	@echo Tcl3D built with Cg support
else
	@echo Tcl3D built without Cg support
endif

ifneq (,$(HAVE_CG))
$(CGEXPANDED): Cg/cg.h
	$(CC) $(CFLAGS) -E $< > $@
else
$(CGEXPANDED):
	echo "/* Dummy Cg extensions file */" > $@
endif
