add_definitions( -DTCL_THREADS 
  -D__TOLTCL_MAJOR_VERSION__=${TOLTCL_VERSION_MAJOR}
  -D__TOLTCL_MINOR_VERSION__=${TOLTCL_VERSION_MINOR} )

include_directories( ${TCL_INCLUDE_PATH} ${TOL_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
 
message( STATUS "Using TCL_INCLUDE_PATH = ${TCL_INCLUDE_PATH}" )
message( STATUS "Using TCL_STUB_LIBRARY = ${TCL_STUB_LIBRARY}" )
message( STATUS "Using TCL_LIBRARY = ${TCL_LIBRARY}" )

if( UNIX )
  SET(CMAKE_INSTALL_RPATH "$ORIGIN/..") 
endif( UNIX )
add_library( toltcl SHARED
  crash.c toltcl.c pool.cpp tclutils.cpp tolaccess.cpp tolsergrp.cpp 
  toltableset.cpp toltime.cpp
)

set_target_properties( toltcl PROPERTIES OUTPUT_NAME 
  toltcl${TOLTCL_VERSION_MAJOR}.${TOLTCL_VERSION_MINOR} )

set_target_properties( toltcl PROPERTIES COMPILE_FLAGS "-DBUILD_TOLTCL -DUSE_TCL_STUBS")

# libtol on both platforms, and the asymmetry that was here is worth explaining
# because it looked deliberate. On Windows a DLL cannot have undefined symbols,
# so toltcl had to link libtol. On Unix a shared object can, so it did not, and
# it worked: the only thing that ever loaded it was tolsh, which already has
# libtol in the process.
#
# It stops working the moment anything else loads it. A plain tclsh doing
# "package require Toltcl" gets
#   couldn't load file ".../libtoltcl4.1.so": undefined symbol: _ZTI13BSyntaxObject
# and that is exactly what the nine tests of the toltcl battery hit, on Linux,
# after the three other obstacles in front of it were cleared. There is no
# interpreter that can run the tester: tolsh carries Toltcl but only accepts TOL
# files as arguments, not Tcl scripts.
#
# Declaring the dependency costs nothing and there is no second copy of anything:
# libtol is a shared library, so tolsh and toltcl resolve to the same one. The
# RPATH just above already says $ORIGIN/.., which is where libtol lives relative
# to lib/toltcl<version>/ -- somebody foresaw the dependency and never declared it.
target_link_libraries( toltcl
  ${TCL_STUB_LIBRARY} ${TOL_LIBRARY} )

if( UNIX )
  SET(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") 
endif( UNIX )

# Paso 3.3: la pagina ANSI del proceso en UTF-8, por manifiesto. Los porques y
# las medidas estan en tol/tol-utf8.manifest.
if( MINGW )
  set( CMAKE_RC_COMPILER_INIT windres )
  enable_language( RC )
  # La regla por defecto le pasa a windres <FLAGS>, que son las banderas del
  # compilador de C, y windres muere con "invalid option -- W". Se le pasan los
  # <DEFINES> --que son -D y si los entiende-- y nada mas.
  if( CMAKE_SIZEOF_VOID_P EQUAL 4 )
    set( CMAKE_RC_COMPILE_OBJECT
         "<CMAKE_RC_COMPILER> -O coff <DEFINES> -i <SOURCE> -o <OBJECT> -F pe-i386" )
  else( CMAKE_SIZEOF_VOID_P EQUAL 4 )
    set( CMAKE_RC_COMPILE_OBJECT
         "<CMAKE_RC_COMPILER> -O coff <DEFINES> -i <SOURCE> -o <OBJECT> -F pe-x86-64" )
  endif( CMAKE_SIZEOF_VOID_P EQUAL 4 )
  set( TOL_UTF8_MANIFEST "${CMAKE_CURRENT_SOURCE_DIR}/../../tol/tol-utf8.manifest" )
  configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/../../tol/tol-utf8.rc.in"
                  "${CMAKE_CURRENT_BINARY_DIR}/tol-utf8.rc" @ONLY )
  set( TOL_UTF8_RC "${CMAKE_CURRENT_BINARY_DIR}/tol-utf8.rc" )
  # El .rc generado solo NOMBRA al manifiesto, asi que sin esto cambiar el
  # manifiesto no recompila nada y se mide un binario viejo creyendolo nuevo.
  set_source_files_properties( ${TOL_UTF8_RC} PROPERTIES
                               OBJECT_DEPENDS "${TOL_UTF8_MANIFEST}" )
else( MINGW )
  set( TOL_UTF8_RC "" )
endif( MINGW )
add_executable( tolsh tolsh.c ${TOL_UTF8_RC} )
if( WIN32 )
  target_link_libraries( tolsh ${TCL_LIBRARY} )
else( WIN32 )
  target_link_libraries( tolsh ${TCL_LIBRARY} ${TOL_LIBRARY} )
endif( WIN32 )

message( STATUS "TOLTCL_DEST_DIR = ${TOLTCL_DEST_DIR}" )
install( TARGETS tolsh RUNTIME DESTINATION "bin" )
if( WIN32 )
  install( TARGETS toltcl RUNTIME DESTINATION "${TOLTCL_DEST_DIR}" )
else( WIN32 )
  install( TARGETS toltcl RUNTIME LIBRARY DESTINATION "${TOLTCL_DEST_DIR}" )
endif( WIN32 )
