cmake_minimum_required( VERSION 3.10 )
project( TOLTCL )

set( INSTALL_WITH_TOL 1 )
# -fno-strict-aliasing, por el mismo motivo que en tol/CMakeLists.txt y con la
# misma autorizacion del 2026-08-24. Alli el razonamiento esta entero; aqui basta
# decir por que este proyecto tambien lo necesita, que es que el idioma cruzo la
# frontera y la bandera de alli no llega hasta aqui: toltcl es un proyecto CMake
# aparte.
#
# Los dos sitios son generic/tolaccess.cpp y generic/toltableset.cpp, que hacen
#   theMat = ((DMat&)Mat((BSyntaxObject *)tol_obj));
# o sea exactamente la misma reinterpretacion de una BMatrix<BDat>& como una
# BMatrix<double>& que motivo la bandera en el motor. Sin ella estos dos ficheros
# se compilan con -fstrict-aliasing, que esta activo desde -O2, y quedan con el
# mismo permiso concedido al optimizador que se quiso quitar. Encontrado el
# 2026-08-25 al recompilar en Windows tras el cambio del motor.
if( CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" )
  add_compile_options( -fno-strict-aliasing )
endif( )


set( CMAKE_MODULE_PATH
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
  ${CMAKE_MODULE_PATH} )

include( LibFindMacros )

# The version number.
set (TOLTCL_VERSION_MAJOR 4)
set (TOLTCL_VERSION_MINOR 1)

# look for tol
find_package( TOL REQUIRED )
if( TOL_FOUND )
  message( STATUS "TOL_INCLUDE_DIR = ${TOL_INCLUDE_DIR}" )
  message( STATUS "TOL_LIBRARIES = ${TOL_LIBRARIES}" )
else( TOL_FOUND )
endif( TOL_FOUND )

# look for tcl
if ( WIN32 AND TOL_PREFIX_PATH )
  if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
    set( g "g" )
  endif( CMAKE_BUILD_TYPE STREQUAL "Debug" )
  file( TO_CMAKE_PATH "${TOL_PREFIX_PATH}" TCL_LIBRARY_PATH )
  set( TCL_LIBRARY_PATH "${TCL_LIBRARY_PATH}/lib" )
  message( STATUS "buscando tcl libs en ${TCL_LIBRARY_PATH}" )
  find_library( TCL_STUB_LIBRARY
      NAMES "libtclstub84" "libtclstub85" "libtclstub86"
      PATHS ${TCL_LIBRARY_PATH}
      NO_DEFAULT_PATH
    )
  find_library( TCL_LIBRARY
      NAMES "libtcl84" "libtcl85" "libtcl86"
      PATHS "${TCL_LIBRARY_PATH}"
      NO_DEFAULT_PATH
    )
  find_path(
    TCL_INCLUDE_PATH
    NAMES "tcl.h"
    PATHS "${TOL_PREFIX_PATH}/include"
    NO_DEFAULT_PATH
    )
  set( TCL_PROCESS_INCLUDES TCL_INCLUDE_PATH )
  set( TCL_PROCESS_LIBS TCL_STUB_LIBRARY )
  libfind_process( TCL )
else( WIN32 AND TOL_PREFIX_PATH )
  find_package( TclStub REQUIRED )
endif( WIN32 AND TOL_PREFIX_PATH )

if( TCL_FOUND )
  string(REGEX REPLACE
    "^.*tclstub([0-9])\\.([0-9]).*$" "\\1;\\2" 
    TCL_STUB_VERSION "${TCL_STUB_LIBRARY}")  
  message( STATUS "TCL_STUB_VERSION = ${TCL_STUB_VERSION}" )
  # list( GET TCL_STUB_VERSION 0 TCL_MAJOR_VERSION )
  # list( GET TCL_STUB_VERSION 1 TCL_MINOR_VERSION )
  # message( STATUS "TCL_STUB_VERSION = ${TCL_MAJOR_VERSION}.${TCL_MINOR_VERSION}" )
  message( STATUS ${TCL_STUB_LIBRARY} )
  message( STATUS ${TCL_INCLUDE_PATH} )
  message( STATUS ${TCL_LIBRARY_PATH} )
  if( ${INSTALL_WITH_TOL} )
    set( CMAKE_INSTALL_PREFIX ${TOL_PREFIX_PATH} )
  else( ${INSTALL_WITH_TOL} )
    get_filename_component( CMAKE_INSTALL_PREFIX ${TCL_LIBRARY_PATH} PATH )
  endif( )
  if( WIN32 )
    string( REPLACE "\\" "/" CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} ) 
  endif( WIN32 )
  message( STATUS "Using CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
else( TCL_FOUND )
  message( SEND_ERROR "TCL extensions need Tcl Stubs library" )
endif( TCL_FOUND )
  
set( TOLTCL_DEST_DIR "lib/toltcl${TOLTCL_VERSION_MAJOR}.${TOLTCL_VERSION_MINOR}"
  CACHE INTERNAL "" )

# ThirdParty used to be Unix-only, and the reason is worth recording: TclCurl never
# built for Windows, so the TOL 3.4 installer shipped a TclCurl borrowed from 2017 and
# the guard made that permanent. The cause was never the platform -- it was the TEA
# configure not recognising 64-bit MinGW, which tea-patch-third-party.cmake now fixes,
# and with that TclCurl builds natively. TOLBase needs it: init_toltk.tcl does an
# unguarded "package require TclCurl" and will not start without it.
add_subdirectory( ThirdParty )

add_subdirectory( generic )
add_subdirectory( library )

