cmake_minimum_required(VERSION 2.4)

IF(NOT MULTI_THREADED)
  OPTION(MULTI_THREADED "Build multi-threaded httpd deamon (if possible)" ON)
ENDIF(NOT MULTI_THREADED)

INCLUDE( ${WT_SOURCE_DIR}/cmake/WtFindBoost.txt )
INCLUDE(FindThreads)

#IF(NOT BOOST_WT_FOUND)
#  MESSAGE("** Error finding Wt required library: ")
#  MESSAGE("** Could not find a boost installation in " ${BOOST_DIR} ".")
#  MESSAGE("** It may be necessary to set appropriate values for the")
#  MESSAGE("   variables BOOST_DIR, BOOST_COMPILER, and BOOST_VERSION")
#  MESSAGE(FATAL_ERROR "** Wt requires the following C++ boost libraries:
#  boost_date_time, boost_regex, and boost_program_options, boost_signals,
#  and optionally boost_thread")
#ENDIF(NOT BOOST_WT_FOUND)

IF(BOOST_WT_MT_FOUND)
  IF(MULTI_THREADED)
    ADD_DEFINITIONS(-DTHREADED -D_REENTRANT -DBOOST_SPIRIT_THREADSAFE)
    MESSAGE("** Enabling multi threading.")
  ELSE(MULTI_THREADED)
    MESSAGE("** Disabling multi threading.")
    ADD_DEFINITIONS(-DBOOST_DISABLE_THREADS)
  ENDIF(MULTI_THREADED)
ELSE(BOOST_WT_MT_FOUND)
  MESSAGE("** Disabling multi threading.")
  ADD_DEFINITIONS(-DBOOST_DISABLE_THREADS)
ENDIF(BOOST_WT_MT_FOUND)

# Boost is used nearly everywhere, so we can put these here
INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIRS})
LINK_DIRECTORIES(${BOOST_LIB_DIRS})
IF(WIN32)
  IF(BOOST_DYNAMIC)
    ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK)
  ENDIF(BOOST_DYNAMIC)
ENDIF(WIN32)

IF(BOOST_WT_MT_FOUND)
  IF(MULTI_THREADED)
    ADD_DEFINITIONS(-DWT_THREADED -D_REENTRANT -DBOOST_SPIRIT_THREADSAFE)
  ELSE(MULTI_THREADED)
    ADD_DEFINITIONS(-DBOOST_DISABLE_THREADS)
  ENDIF(MULTI_THREADED)
ELSE(BOOST_WT_MT_FOUND)
  ADD_DEFINITIONS(-DBOOST_DISABLE_THREADS)
ENDIF(BOOST_WT_MT_FOUND)

ADD_DEFINITIONS(-DWT_DEPRECATED_3_0_0)

IF(ENABLE_LIBWTTEST)
  SET(TEST_SOURCES
    test.C
    auth/BCryptTest.C
    auth/SHA1Test.C
    chart/WChartTest.C
    json/JsonParserTest.C
    json/JsonSerializerTest.C
    http/HttpClientTest.C
    mail/MailClientTest.C
    models/WBatchEditProxyModelTest.C
    models/WStandardItemModelTest.C
    private/HttpTest.C
    private/CExpressionParserTest.C
    private/I18n.C
    render/BlockCssPropertyTest.C
    render/CssParserTest.C
    render/CssSelectorTest.C
    render/SpecificityTest.C
    render/WTextRendererTest.C
    utf8/Utf8Test.C
    utf8/XmlTest.C
    utils/Base64Test.C
    wdatetime/WDateTimeTest.C
    widgets/WSpinBoxTest.C
    length/WLengthTest.C
    color/WColorTest.C
    paintdevice/WSvgTest.C
    payment/MoneyTest.C
    locale/LocaleNumberTest.C
    trampoline/RefEncoder.C
  )

  IF (WT_HAS_WRASTERIMAGE)
     SET(TEST_SOURCES ${TEST_SOURCES}
       paintdevice/WRasterTest.C
     )
  ENDIF(WT_HAS_WRASTERIMAGE)

  ADD_EXECUTABLE(test
    ${TEST_SOURCES}
  )

  TARGET_LINK_LIBRARIES(test wt wttest ${BOOST_FS_LIB})

  IF( UNIX )
    TARGET_LINK_LIBRARIES( test dl )
  ENDIF( UNIX )

  IF(ENABLE_LIBWTDBO)
    # Test all dbo backends
    SET(DBO_TEST_SOURCES
      test.C
      dbo/DboTest.C
      dbo/DboTest2.C
      dbo/DboTest3.C
      dbo/Benchmark.C
      dbo/Benchmark2.C
      dbo/JsonTest.C
      dbo/AuthDboTest.C
      dbo/DboTestCompositeKey.C
      private/DboImplTest.C
    )

    IF(HAVE_POSTGRES)
      ADD_EXECUTABLE(       test.postgres ${DBO_TEST_SOURCES})
      TARGET_LINK_LIBRARIES(test.postgres wt wttest wtdbo wtdbopostgres)
      SET_TARGET_PROPERTIES(test.postgres PROPERTIES COMPILE_FLAGS "-DPOSTGRES")
      MESSAGE("** Testing Wt::Dbo using Postgres backend")
    ENDIF(HAVE_POSTGRES)

    IF(HAVE_MYSQL)
      ADD_EXECUTABLE(       test.mysql ${DBO_TEST_SOURCES})
      TARGET_LINK_LIBRARIES(test.mysql wt wttest wtdbo wtdbomysql)
      SET_TARGET_PROPERTIES(test.mysql PROPERTIES COMPILE_FLAGS "-DMYSQL")
      MESSAGE("** Testing Wt::Dbo using Mysql backend")
    ENDIF(HAVE_MYSQL)

    IF(HAVE_ORACLE)
      ADD_EXECUTABLE(       test.oracle ${DBO_TEST_SOURCES})
      TARGET_LINK_LIBRARIES(test.oracle wt wttest wtdbo wtdbooracle)
      SET_TARGET_PROPERTIES(test.oracle PROPERTIES COMPILE_FLAGS "-DORACLE")
      MESSAGE("** Testing Wt::Dbo using Oracle backend")
    ENDIF(HAVE_ORACLE)

    IF(HAVE_FIREBIRD)
      ADD_EXECUTABLE(       test.firebird ${DBO_TEST_SOURCES})
      TARGET_LINK_LIBRARIES(test.firebird wt wttest wtdbo wtdbofirebird)
      SET_TARGET_PROPERTIES(test.firebird PROPERTIES COMPILE_FLAGS "-DFIREBIRD")
      MESSAGE("** Testing Wt::Dbo using Firebird backend")
    ENDIF(HAVE_FIREBIRD)

    IF(HAVE_SQLITE)
      ADD_EXECUTABLE(       test.sqlite3 ${DBO_TEST_SOURCES})
      TARGET_LINK_LIBRARIES(test.sqlite3 wt wttest wtdbo wtdbosqlite3)
      SET_TARGET_PROPERTIES(test.sqlite3 PROPERTIES COMPILE_FLAGS "-DSQLITE3")
      MESSAGE("** Testing Wt::Dbo using Sqlite3 backend")
    ENDIF(HAVE_SQLITE)
  ENDIF(ENABLE_LIBWTDBO)

  INCLUDE_DIRECTORIES(${WT_SOURCE_DIR}/src)

  IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/interactive)
    SUBDIRS(interactive)
  ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/interactive)

  IF(CONNECTOR_HTTP)
    SET(HTTP_TEST_SOURCES
      test.C
      http/HttpClientServerTest.C
    )

    ADD_EXECUTABLE(test.http ${HTTP_TEST_SOURCES})
    TARGET_LINK_LIBRARIES(test.http wt wthttp)    
  ENDIF(CONNECTOR_HTTP)

ELSE (ENABLE_LIBWTTEST)
  MESSAGE("** libwttest disabled, not building tests")
ENDIF (ENABLE_LIBWTTEST)
