#******************************************************************************
#
#       Copyright:      2006-2016 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 -> tcl3dOSG
#       Filename:       CMakeLists.txt
#
#       Author:         Paul Obermeier
#
#       Description:    Makefile for the tcl3dOSG subpackage.
#
#******************************************************************************

IF( TCL3D_BUILD_OSG )

    SET( TARGET_OSG            tcl3dOsg )
    SET( TARGET_OSGANIMATION   tcl3dOsgAnimation )
    SET( TARGET_OSGDB          tcl3dOsgDB )
    SET( TARGET_OSGFX          tcl3dOsgFX )
    SET( TARGET_OSGGA          tcl3dOsgGA )
    SET( TARGET_OSGMANIPULATOR tcl3dOsgManipulator )
    SET( TARGET_OSGOPENTHREADS tcl3dOsgOpenThreads )
    SET( TARGET_OSGPARTICLE    tcl3dOsgParticle )
    SET( TARGET_OSGSIM         tcl3dOsgSim )
    SET( TARGET_OSGSHADOW      tcl3dOsgShadow )
    SET( TARGET_OSGTERRAIN     tcl3dOsgTerrain )
    SET( TARGET_OSGTEXT        tcl3dOsgText )
    SET( TARGET_OSGUTIL        tcl3dOsgUtil )
    SET( TARGET_OSGVIEWER      tcl3dOsgViewer )

    SET( TARGET_DEF "-DUSE_OSG_IF -D_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED" )

    ADD_DEFINITIONS( ${TARGET_DEF} )
    INCLUDE_DIRECTORIES( 
        ${OPENTHREADS_INCLUDE_DIR}
        ${OPENSCENEGRAPH_INCLUDE_DIRS} 
    )

    FIND_PACKAGE( OpenSceneGraph REQUIRED
        osgAnimation
        osgDB
        osgFX
        osgGA
        osgManipulator
        osgParticle
        osgShadow
        osgSim
        osgTerrain
        osgText
        osgUtil 
        osgViewer
        osgVolume
    )

    INCLUDE( ${SWIG_USE_FILE} )
  
    SET( OSG_SWIG_FLAGS_COMMON
        -namespace
        ${TARGET_DEF}
        ${COMMON_SWIG_FLAGS}
        -I${CMAKE_CURRENT_SOURCE_DIR}/../tcl3dOgl/swigfiles
        -I${CMAKE_CURRENT_SOURCE_DIR}
        -I${CMAKE_CURRENT_SOURCE_DIR}/swigfiles
        -I${OPENTHREADS_INCLUDE_DIR}
        -I${OPENSCENEGRAPH_INCLUDE_DIRS}
    )

    MACRO( BUILD_OSG_MODULE TARGET PREFIX )
        SET( CMAKE_SWIG_FLAGS ${OSG_SWIG_FLAGS_COMMON} -prefix ${PREFIX} )
        SET( INTERFACE_FILE swigfiles/${TARGET}.i )
        SET_SOURCE_FILES_PROPERTIES( ${INTERFACE_FILE} PROPERTIES CPLUSPLUS ON )
        MY_SWIG_ADD_MODULE( ${TARGET} tcl ${INTERFACE_FILE} )
        SWIG_LINK_LIBRARIES( ${TARGET}
            ${OPENSCENEGRAPH_LIBRARIES}
            ${TCL_STUB_LIBRARY}
            ${TK_STUB_LIBRARY}
            ${SYS_LIBS}
        )
        SET_TARGET_PROPERTIES( ${TARGET} PROPERTIES PREFIX "" )
    ENDMACRO()

    # Build wrappers with SWIG.
    BUILD_OSG_MODULE( ${TARGET_OSG}            osg )
    BUILD_OSG_MODULE( ${TARGET_OSGANIMATION}   osgAnimation )
    BUILD_OSG_MODULE( ${TARGET_OSGDB}          osgDB )
    BUILD_OSG_MODULE( ${TARGET_OSGFX}          osgFX )
    BUILD_OSG_MODULE( ${TARGET_OSGGA}          osgGA )
    BUILD_OSG_MODULE( ${TARGET_OSGMANIPULATOR} osgManipulator )
    BUILD_OSG_MODULE( ${TARGET_OSGOPENTHREADS} osgOpenThreads )
    BUILD_OSG_MODULE( ${TARGET_OSGPARTICLE}    osgParticle )
    BUILD_OSG_MODULE( ${TARGET_OSGSIM}         osgSim )
    BUILD_OSG_MODULE( ${TARGET_OSGSHADOW}      osgShadow )
    BUILD_OSG_MODULE( ${TARGET_OSGTERRAIN}     osgTerrain )
    BUILD_OSG_MODULE( ${TARGET_OSGTEXT}        osgText )
    BUILD_OSG_MODULE( ${TARGET_OSGUTIL}        osgUtil )
    BUILD_OSG_MODULE( ${TARGET_OSGVIEWER}      osgViewer )

    # Install the configuration specific Tcl package index file and all other Tcl files.
    INSTALL_TCL_PACKAGE( ${TARGET_OSG} "tclfiles/pkgIndex.tcl.in" "tclfiles/" "tcl3d*.tcl" )

    # Install the shared libraries containing the SWIG generated wrapper.
    INSTALL(
        TARGETS     
            ${TARGET_OSG}
            ${TARGET_OSGANIMATION}
            ${TARGET_OSGDB}
            ${TARGET_OSGFX}
            ${TARGET_OSGGA}
            ${TARGET_OSGMANIPULATOR}
            ${TARGET_OSGOPENTHREADS}
            ${TARGET_OSGPARTICLE}
            ${TARGET_OSGSIM}
            ${TARGET_OSGSHADOW}
            ${TARGET_OSGTERRAIN}
            ${TARGET_OSGTEXT}
            ${TARGET_OSGUTIL}
            ${TARGET_OSGVIEWER}
        DESTINATION ${TCL3D_INST_DIR}/${TARGET_OSG}
    )
ENDIF( TCL3D_BUILD_OSG )
