#******************************************************************************
#
# Copyright:   2018-2019 Paul Obermeier (obermeier@poSoft.de)
#
#              See the file "Mawt_License.txt" for information on
#              usage and redistribution of this file, and for a
#              DISCLAIMER OF ALL WARRANTIES.
#
# Description: Makefile for the Movie Automation With Tcl (MAWT) package.
#
#******************************************************************************

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( Mawt )

IF( APPLE )
    SET( CMAKE_SHARED_MODULE_SUFFIX ".dylib" )
ENDIF()

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

# Define MAWT version number.
SET( MAWT_VERSION_MAJOR 0 )
SET( MAWT_VERSION_MINOR 3 )
SET( MAWT_VERSION_PATCH 0 )
SET( MAWT_VERSION ${MAWT_VERSION_MAJOR}.${MAWT_VERSION_MINOR}.${MAWT_VERSION_PATCH} )

FILE( WRITE "Mawt_Version.txt" ${MAWT_VERSION} )

SET( TARGET    mawt )
SET( TARGET_IF mawtLib )

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

# Add MAWT specific definitions.
INCLUDE_DIRECTORIES( ${TCL_INCLUDE_PATH} ${TK_INCLUDE_PATH} ${FFMPEG_INCLUDE_DIR} )
ADD_DEFINITIONS( "-DUSE_TCL_STUBS -DUSE_TK_STUBS" )
IF( UNIX )
    ADD_DEFINITIONS( "-fPIC" )
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( "-D_WIN32" )

    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 ${MAWT_VERSION}
    -namespace
    -I${CMAKE_SOURCE_DIR}/swigfiles
)

SET( MAWT_INST_DIR lib/mawt${MAWT_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}/mawt${MAWT_VERSION} CACHE PATH "MAWT installation path" FORCE )
ENDIF()

# Build base files of this module.
FIND_SOURCES( SOURCES_IF . )
ADD_LIBRARY( ${TARGET_IF} STATIC ${SOURCES_IF} )
SET_TARGET_PROPERTIES( ${TARGET_IF} PROPERTIES PREFIX "" )

# 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${FFMPEG_INCLUDE_DIR}
)

MY_SWIG_ADD_MODULE( ${TARGET} tcl ${INTERFACE_FILE} )
SWIG_LINK_LIBRARIES( ${TARGET} ${TARGET_IF} ${FFMPEG_LIBRARIES} ${TCL_STUB_LIBRARY} ${TK_STUB_LIBRARY} ${SYS_LIBS} )
SET_TARGET_PROPERTIES( ${TARGET} PROPERTIES PREFIX "" )
IF( MSVC )
    SET_TARGET_PROPERTIES( ${TARGET} PROPERTIES LINK_FLAGS "-SAFESEH:NO")
ENDIF()

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

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