CMAKE_MINIMUM_REQUIRED( VERSION 3.0 )

PROJECT( tclMuPdf )

SET( TCLMUPDF_VERSION "2.3" )

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

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

IF( MSVC OR MINGW )
    SET( TARGET_TCL tclMuPDF )
    SET( TARGET_TK  tkMuPDF  )
ELSE()
    SET( TARGET_TCL libtclMuPDF )
    SET( TARGET_TK  libtkMuPDF  )
ENDIF()
FIND_SOURCES( SOURCES src )

ADD_LIBRARY( ${TARGET_TCL} SHARED ${SOURCES} )
SET_TARGET_PROPERTIES( ${TARGET_TCL} PROPERTIES PREFIX "" )
TARGET_LINK_LIBRARIES( ${TARGET_TCL} ${TCL_STUB_LIBRARY} ${MUPDF_LIBRARIES} )
TARGET_COMPILE_DEFINITIONS( ${TARGET_TCL} PUBLIC USE_TCL_STUBS USE_TCLOO_STUBS )

ADD_LIBRARY( ${TARGET_TK} SHARED ${SOURCES} )
SET_TARGET_PROPERTIES( ${TARGET_TK} PROPERTIES PREFIX "" )
TARGET_LINK_LIBRARIES( ${TARGET_TK} ${TCL_STUB_LIBRARY} ${TK_STUB_LIBRARY} ${MUPDF_LIBRARIES} )
TARGET_COMPILE_DEFINITIONS( ${TARGET_TK} PUBLIC USE_TCL_STUBS USE_TK_STUBS USE_TCLOO_STUBS )

INCLUDE_DIRECTORIES( ${TCL_INCLUDE_PATH} ${TK_INCLUDE_PATH} ${MUPDF_INCLUDE_DIR} )

IF( UNIX )
    ADD_DEFINITIONS( "-std=c99" )
    ADD_DEFINITIONS( "-fPIC" )
ENDIF()
IF( MSVC )
    ADD_DEFINITIONS( "-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE" )
    ADD_DEFINITIONS( "-D__WIN32__" )
ENDIF()

SET( CMAKE_INCLUDE_CURRENT_DIR ON )
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src )

SET( TCLMUPDF_INST_DIR tclmupdf${TCLMUPDF_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}/tkMuPdf CACHE PATH "tkMuPdf installation path" FORCE )
ENDIF()

# Install the pkgIndex.tcl file.
INSTALL(
    FILES
        tclMuPDF-license.terms
        src/class_Doc.tcl
        src/class_Page.tcl
        src/class_TextSearch.tcl
        src/mupdf.tcl
        src/pkgIndex.tcl
    DESTINATION ${TCLMUPDF_INST_DIR}
)

IF( APPLE )
    SET( MACHINE darwin )
ELSEIF( CYGWIN )
    SET( MACHINE win )
ELSEIF( MINGW )
    SET( MACHINE win )
ELSEIF( UNIX )
    SET( MACHINE linux )
ELSE()
    SET( MACHINE win )
ENDIF()

# Check whether we build 32 or 64-bit targets.
IF( CMAKE_SIZEOF_VOID_P EQUAL 8 )
    SET( ARCH x64 )
ELSE()
    SET( ARCH x32 )
ENDIF()

# Install the shared library file.
INSTALL(
    TARGETS     ${TARGET_TCL} ${TARGET_TK}
    DESTINATION ${TCLMUPDF_INST_DIR}/${MACHINE}-${ARCH}
)
