CMAKE_MINIMUM_REQUIRED( VERSION 3.0 )

PROJECT( tclpy )

SET( TCLPY_VERSION "0.4.1" )

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

FIND_PACKAGE( Python3 REQUIRED COMPONENTS Interpreter Development )
IF( Python3_FOUND )
    ADD_DEFINITIONS( "-DHAVE_PYTHON3" )
ENDIF()

IF( APPLE )
    MESSAGE( FATAL_ERROR "tclpy not yet supported on Apple" )
ENDIF()

SET( TARGET tclpy )
SET( SOURCES generic/tclpy.c )

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

INCLUDE_DIRECTORIES( ${TCL_INCLUDE_PATH} ${TK_INCLUDE_PATH} ${Python3_INCLUDE_DIRS} )
ADD_DEFINITIONS( "-DUSE_TCL_STUBS -DUSE_TK_STUBS" )
ADD_DEFINITIONS( "-DPACKAGE_VERSION=\"${TCLPY_VERSION}\"" )
IF( MSVC )
    ADD_DEFINITIONS( "-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE" )
    ADD_DEFINITIONS( "-D__WIN32__" )
ENDIF()

# Add compiler flag "-I.".
SET( CMAKE_INCLUDE_CURRENT_DIR ON )

SET( TCLPY_INST_DIR tclpy${TCLPY_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}/tclpy CACHE PATH "tclpy installation path" FORCE )
ENDIF()

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

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