include(ExternalProject)

# See the same block in tolbase/ThirdParty/CMakeLists.txt: on Windows the TEA
# configure script cannot be executed directly, because ExternalProject spawns its
# commands through cmd.exe and cmd.exe knows nothing about a #! line. Empty on Unix.
if( WIN32 )
  set( TEA_SHELL "sh" CACHE FILEPATH
       "POSIX shell used to run the TEA configure scripts of the third party extensions" )
  set( TEA_PATCH ${CMAKE_COMMAND} -P
       "${CMAKE_CURRENT_SOURCE_DIR}/tea-patch-third-party.cmake" )
  # TclCurl calls select(), which on Windows lives in Winsock and not in libc, and its
  # Makefile links neither: "undefined reference to `__imp_select'".
  set( TEA_LIBS "-lws2_32" )
else( WIN32 )
  set( TEA_SHELL "" )
  set( TEA_PATCH "" )
  set( TEA_LIBS "" )
endif( WIN32 )

# Same reason as in tolbase/ThirdParty/CMakeLists.txt: these sources predate C99
# prototypes and GCC 15 defaults to -std=gnu23, where the old declarations are errors.
include(CheckCCompilerFlag)

# Each flag is offered to the compiler before being used, because a -Wno-error= whose
# warning the compiler does not know is NOT ignored: it is a hard error, and one that
# arrives disguised. GCC 14 named -Wreturn-mismatch and GCC 13.3 answers
#   cc1: error: '-Wno-error=return-mismatch': no option '-Wreturn-mismatch'
# to every compilation, so the TEA configure of rbc and Tktable concludes "C compiler
# cannot create executables" and the whole build of tolbase stops without ever
# mentioning a flag. Measured on Ubuntu 24.04 with GCC 13.3 the 2026-09-16.
set( TEA_CFLAGS_CANDIDATES
     -std=gnu17
     -Wno-error=incompatible-pointer-types
     -Wno-error=implicit-function-declaration
     -Wno-error=int-conversion
     -Wno-error=implicit-int
     -Wno-error=return-mismatch )
set( TEA_CFLAGS_SUPPORTED "" )
foreach( tea_flag IN LISTS TEA_CFLAGS_CANDIDATES )
  string( MAKE_C_IDENTIFIER "TEA_CFLAG_${tea_flag}" tea_flag_var )
  check_c_compiler_flag( "${tea_flag}" ${tea_flag_var} )
  if( ${tea_flag_var} )
    string( APPEND TEA_CFLAGS_SUPPORTED " ${tea_flag}" )
  endif( ${tea_flag_var} )
endforeach( tea_flag )
string( STRIP "${TEA_CFLAGS_SUPPORTED}" TEA_CFLAGS_SUPPORTED )

set( TEA_CFLAGS
     "${TEA_CFLAGS_SUPPORTED}"
     CACHE STRING
     "C dialect for the third party Tcl extensions, whose sources predate C99 prototypes" )

get_filename_component( TCL_LIB_DIR ${TCL_LIBRARY} DIRECTORY )
# TK_LIBRARY is only set when the enclosing project went looking for Tk, and toltcl
# does not: it asks for TclStub alone. TclCurl has no use for Tk either, so the
# variable is left empty instead of failing the configure with "get_filename_component
# called with incorrect number of arguments". This never showed before because the
# whole directory was inside if( UNIX ) and never configured on Windows.
if( TK_LIBRARY )
  get_filename_component( TK_LIB_DIR ${TK_LIBRARY} DIRECTORY )
endif( TK_LIBRARY )

if( NOT TCLCURL_VERSION )
  set( TCLCURL_VERSION 7.22.0 )
endif( )

if( BUILD_TCLCURL )
  message( STATUS "Third party TclCurl${TCLCURL_VERSION} will be built." )
  set( tclcurl_tgz "TclCurl-${TCLCURL_VERSION}.tar.gz" )
  ExternalProject_Add( tclcurl
    URL ${CMAKE_CURRENT_SOURCE_DIR}/${tclcurl_tgz}
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}
    PATCH_COMMAND ${TEA_PATCH}
    CONFIGURE_COMMAND ${TEA_SHELL} ${CMAKE_CURRENT_BINARY_DIR}/src/tclcurl/configure
    CFLAGS=${TEA_CFLAGS}
    LIBS=${TEA_LIBS}
    --prefix=${CMAKE_CURRENT_BINARY_DIR}
    --exec-prefix=${CMAKE_CURRENT_BINARY_DIR}
    --with-tcl=${TCL_LIB_DIR}
    BUILD_IN_SOURCE 1
    )
  install( DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib/TclCurl${TCLCURL_VERSION} COMPONENT RUNTIME DESTINATION lib )
endif( BUILD_TCLCURL )

