CMAKE_MINIMUM_REQUIRED( VERSION 3.0 )

PROJECT( printer )

SET( PRINTER_VERSION "0.9.6.16" )

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

IF( APPLE )
    MESSAGE( FATAL_ERROR "printer not supported on Apple" )
ELSEIF( UNIX )
    MESSAGE( FATAL_ERROR "printer not supported on Linux" )
ENDIF()

SET( TARGET printer )
SET( SOURCES printer.c )

# Check whether we have to build 32- or 64-bit targets.
IF( CMAKE_SIZEOF_VOID_P EQUAL 8 )
  SET( PLATFORM_NUM_BITS 64 )
ELSE()
  SET( PLATFORM_NUM_BITS 32 )
ENDIF()

ADD_LIBRARY( ${TARGET} SHARED ${SOURCES} )
SET_TARGET_PROPERTIES( ${TARGET} PROPERTIES PREFIX "" )
TARGET_LINK_LIBRARIES( ${TARGET} ${TCL_STUB_LIBRARY} ${TK_STUB_LIBRARY} )

INCLUDE_DIRECTORIES( ${TCL_INCLUDE_PATH} ${TK_INCLUDE_PATH} )
ADD_DEFINITIONS( "-DUSE_TCL_STUBS -DUSE_TK_STUBS" )
ADD_DEFINITIONS( "-DPLATFORM_NUM_BITS=${PLATFORM_NUM_BITS}" )
IF( MSVC )
    ADD_DEFINITIONS( "-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE" )
    ADD_DEFINITIONS( "-D__WIN32__" )
ENDIF()

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

SET( PRINTER_INST_DIR printer${PRINTER_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}/printer CACHE PATH "printer installation path" FORCE )
ENDIF()

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

# Install the shared library file.
INSTALL(
    TARGETS     ${TARGET}
    DESTINATION ${PRINTER_INST_DIR}
)
