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

IF( TCL3D_BUILD_OGL )

    SET( TARGET      tcl3dOgl )
    SET( TARGET_BASE tcl3dOglBase )

    # Build base files of this module.
    FIND_SOURCES( BASE_SOURCES . Togl )
    ADD_DEFINITIONS( "-DUNICODE -D_UNICODE" )
    ADD_LIBRARY( ${TARGET_BASE} STATIC ${BASE_SOURCES} )
    SET_TARGET_PROPERTIES( ${TARGET_BASE} PROPERTIES PREFIX "" )

    # Dependencies:
    # GLSpec$(DSEP)glExtUrlList.html
    ADD_CUSTOM_COMMAND(
        TARGET ${TARGET_BASE}
        POST_BUILD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        COMMAND ${TCL_TCLSH} ${CMAKE_CURRENT_SOURCE_DIR}/createExtUrlList.tcl
        COMMENT "Generate glExtUrlList.txt ..."
    )

    # Build SWIG include files (glewdefs.i, glewfuncs.i) from OpenGL specification.
    # Dependencies:
    # GL$(DSEP)glew.h GLSpec$(DSEP)gl.spec
    # GLSpec$(DSEP)glFuncUrlList.txt
    # GLSpec$(DSEP)glExtUrlList.txt
    # createSwigAndHelpFile.tcl
    ADD_CUSTOM_COMMAND(
        TARGET ${TARGET_BASE}
        POST_BUILD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        COMMAND ${TCL_TCLSH} ${CMAKE_CURRENT_SOURCE_DIR}/createSwigAndHelpFile.tcl -swig
        COMMENT "Generate glewdefs.i and glewdefs.i ..."
    )

    # Dependencies:
    # GL$(DSEP)glew.h GLSpec$(DSEP)gl.spec
    # GLSpec$(DSEP)glFuncUrlList.txt
    # GLSpec$(DSEP)glExtUrlList.txt
    # GLSpec$(DSEP)tcl3dOglHelp.txt
    # createSwigAndHelpFile.tcl
    ADD_CUSTOM_COMMAND(
        TARGET ${TARGET_BASE}
        POST_BUILD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        COMMAND ${TCL_TCLSH} ${CMAKE_CURRENT_SOURCE_DIR}/createSwigAndHelpFile.tcl -help
        COMMENT "Generate tcl3dOglHelp.tcl ..."
    )

    # Build wrapper with SWIG.
    SET( INTERFACE_FILE swigfiles/${TARGET}.i)

    INCLUDE( ${SWIG_USE_FILE} )
  
    SET( CMAKE_SWIG_FLAGS 
        ${COMMON_SWIG_FLAGS}
        -I${CMAKE_CURRENT_SOURCE_DIR}
        -I${CMAKE_CURRENT_SOURCE_DIR}/swigfiles
        -I${CMAKE_CURRENT_SOURCE_DIR}/GL/${MACHINE}
    )

    MY_SWIG_ADD_MODULE( ${TARGET} tcl ${INTERFACE_FILE} )
    SWIG_LINK_LIBRARIES( ${TARGET} ${TARGET_BASE} ${TCL_STUB_LIBRARY} ${TK_STUB_LIBRARY} ${SYS_LIBS} )
    SET_TARGET_PROPERTIES( ${TARGET} PROPERTIES PREFIX "" )

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

    # Install the shared library containing the SWIG generated wrapper.
    INSTALL(
        TARGETS     ${TARGET}
        DESTINATION ${TCL3D_INST_DIR}/${TARGET}
    )

ENDIF( TCL3D_BUILD_OGL )
