CMAKE_MINIMUM_REQUIRED( VERSION 3.0 )

# Prefer find scripts defined by CMAKE_MODULE_PATH over those in the
# CMake module directory.
CMAKE_POLICY( SET CMP0017 OLD )

PROJECT( Tcl3D )

# Include some general purpuse macros and definitions.
SET( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMakeModules )
INCLUDE( Macros )
INCLUDE( Common )

# Define Tcl3D version number.
SET( TCL3D_VERSION_MAJOR 0 )
SET( TCL3D_VERSION_MINOR 9 )
SET( TCL3D_VERSION_PATCH 2 )
SET( TCL3D_VERSION ${TCL3D_VERSION_MAJOR}.${TCL3D_VERSION_MINOR}.${TCL3D_VERSION_PATCH} )

FILE( WRITE "Tcl3D_Version.txt" ${TCL3D_VERSION} )

# Find packages needed for all modules.
FIND_PACKAGE( TCL     REQUIRED )
FIND_PACKAGE( Tclsh   REQUIRED )
FIND_PACKAGE( TclStub REQUIRED )
FIND_PACKAGE( SWIG    REQUIRED )

IF( APPLE )
    SET( MACHINE Darwin )
ELSEIF( CYGWIN )
    SET( MACHINE win32 )
ELSEIF( MINGW )
    SET( MACHINE mingw )
ELSEIF( UNIX )
    SET( MACHINE Linux )
ELSE()
    SET( MACHINE win32 )
ENDIF()

# Add Tcl3D specific definitions.
INCLUDE_DIRECTORIES( ${TCL_INCLUDE_PATH} ${TK_INCLUDE_PATH} ${CMAKE_SOURCE_DIR}/tcl3dOgl ${CMAKE_SOURCE_DIR}/tcl3dOgl/GL/${MACHINE} )
ADD_DEFINITIONS( "-DUSE_TCL_STUBS -DUSE_TK_STUBS" )
ADD_DEFINITIONS( "-DGLEW_STATIC" )
IF( APPLE )
    ADD_DEFINITIONS( "-DTOGL_AGL" )
ELSEIF( UNIX )
    ADD_DEFINITIONS( "-DTOGL_X11 -fPIC" )
ELSE()
    ADD_DEFINITIONS( "-DTOGL_WGL" )
    ADD_DEFINITIONS( "-D_WIN32" )
ENDIF()

# Disable some often seen, but non-critical warnings:
# Warning 4018: signed/unsigned mismatch
# Warning 4101: unreferenced local variable
# Warning 4244: conversion from 'double' to 'float', possible loss of data
# Warning 4305: 'function' : truncation from 'double' to 'float'
# Warning 4996: POSIX name for this item /strdup) is deprecated.
    
IF( MSVC )
    ADD_DEFINITIONS( "/wd4018" )
    ADD_DEFINITIONS( "/wd4101" )
    ADD_DEFINITIONS( "/wd4244" )
    ADD_DEFINITIONS( "/wd4305" )
    ADD_DEFINITIONS( "/wd4996" )
ENDIF()

# Add compiler flags "-I." and "-I<SourceRoot>".
SET( CMAKE_INCLUDE_CURRENT_DIR ON )
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR} )

# Add common SWIG options.
# Disable warning 302: Identifier 'XYZ' redefined (ignored)
SET( COMMON_SWIG_FLAGS
    -w302
    -pkgversion ${TCL3D_VERSION}
    -I${CMAKE_SOURCE_DIR}/swigfiles
)

SET( TCL3D_INST_DIR lib/tcl3d${TCL3D_VERSION} )

# Set option "CMAKE_INSTALL_PREFIX", if option was not explicitly set by user.
IF( CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT )
    SET( CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/tcl3d${TCL3D_VERSION} CACHE PATH "Tcl3D installation path" FORCE )
ENDIF()

#
# Add cache entries for the different Tcl3D modules.
#

# OpenGL module must always be compiled.
SET( TCL3D_BUILD_OGL TRUE CACHE BOOL "Compile base module tcl3dOgl." )

CMAKE_DEPENDENT_OPTION( TCL3D_BUILD_GAUGES "Compile module tcl3dGauges." ON   "TCL3D_BUILD_OGL" OFF )
CMAKE_DEPENDENT_OPTION( TCL3D_BUILD_GL2PS  "Compile module tcl3dGl2ps."  ON   "TCL3D_BUILD_OGL" OFF )

CMAKE_DEPENDENT_OPTION( TCL3D_BUILD_FTGL   "Compile module tcl3dFTGL."   OFF  "TCL3D_BUILD_OGL" OFF )
CMAKE_DEPENDENT_OPTION( TCL3D_BUILD_SDL    "Compile module tcl3dSDL."    OFF  "TCL3D_BUILD_OGL" OFF )
CMAKE_DEPENDENT_OPTION( TCL3D_BUILD_OSG    "Compile module tcl3dOsg."    OFF  "TCL3D_BUILD_OGL" OFF )

# Recurse into the directories of the Tcl3D modules.
# Note, that the decision to compile a module is taken
# in the CMakeLists.txt file of the module directory.
ADD_SUBDIRECTORY( tcl3dOgl )
ADD_SUBDIRECTORY( tcl3dGauges )
ADD_SUBDIRECTORY( tcl3dGl2ps )

ADD_SUBDIRECTORY( tcl3dFTGL )
ADD_SUBDIRECTORY( tcl3dSDL )
ADD_SUBDIRECTORY( tcl3dOsg )

# Install the main pkgIndex.tcl file.
CONFIGURE_FILE( pkgIndex.tcl.in pkgIndex.tcl @ONLY )
INSTALL(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgIndex.tcl
    DESTINATION ${TCL3D_INST_DIR}
)
