#
# $Id$
# $HeadURL$
# $Author$
#

# Make ${Qt6_Widgets} optional
if(QT6_GUI_AVAILABLE AND TARGET Qt6::Widgets)
  set(Qt6_Widgets ${Qt6_Widgets})
else()
  set(Qt6_Widgets "")
endif()


FIND_PACKAGE(RTI)

# KTextTemplate is Qt6-compatible replacement for Grantlee5
IF(BUILD_GRANTLEE)
  FIND_PACKAGE(KF6TextTemplate REQUIRED)
ENDIF()

NGL_ADD_LIBRARY2(ALL codegen_base SHARED
  codegen_export.h
  SqlGenerator.cpp
  SqlGenerator.h
  StructBuilder.cpp
  StructBuilder.h
  XmlReader.cpp
  XmlReader.h
)
NGL_TARGET_LINK_LIBRARIES(codegen_base
  PRIVATE
    Qt6::Xml
    NGLLogger
    ngltools
  PUBLIC
    Qt6::Core
)
NGL_TARGET_INCLUDE_DIRECTORIES(codegen_base
  PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../
    ${CMAKE_CURRENT_BINARY_DIR}
)
# If RTI target is available, propagate its include dirs/compile defs
if (TARGET RTI::nddscpp2)
  NGL_TARGET_INCLUDE_DIRECTORIES(codegen_base
    PUBLIC
      $<TARGET_PROPERTY:RTI::nddscpp2,INTERFACE_INCLUDE_DIRECTORIES>
  )
  NGL_TARGET_COMPILE_DEFINITIONS(codegen_base
    PUBLIC
      $<TARGET_PROPERTY:RTI::nddscpp2,INTERFACE_COMPILE_DEFINITIONS>
      RTI_DDS_AVAILABLE
  )
  message(STATUS "RTI found; building codegen_base with RTI support")
else()
  message(STATUS "RTI not found; building codegen_base without RTI support")
endif()
NGL_INSTALL_TARGET(codegen_base)

# KTextTemplate is Qt6-compatible replacement for Grantlee5
IF(BUILD_GRANTLEE AND TARGET KF6::TextTemplate)
  NGL_ADD_LIBRARY(ALL codegen_grantlee STATIC
    CppTopicGeneratorGrantlee.cpp
    CppTopicGeneratorGrantlee.h
    DaoGeneratorGrantlee.cpp
    DaoGeneratorGrantlee.h
    DatabaseGeneratorGrantlee.cpp
    DatabaseGeneratorGrantlee.h
    GeneratorGrantleeBase.cpp
    GeneratorGrantleeBase.h
    GrantleeWrappers.cpp
    GrantleeWrappers.h

    svc.idl.grantlee
    svc_enums.idl.grantlee

    svc_translation.cpp.grantlee
    svc_translation.h.grantlee

    svc_dao.cpp.grantlee
    svc_dao.h.grantlee

    svc_db.h.grantlee
    svc_db_impl.h.grantlee
    svc_db_impl.cpp.grantlee

    svc_dtc.h.grantlee
    svc_dtc_impl.cpp.grantlee
    svc_dtc_impl.h.grantlee

    svc_dts.h.grantlee
    svc_dts_impl.cpp.grantlee
    svc_dts_impl.h.grantlee

    svc_ftc.h.grantlee
    svc_ftc_impl.cpp.grantlee
    svc_ftc_impl.h.grantlee

    svc_fts.h.grantlee
    svc_fts_impl.cpp.grantlee
    svc_fts_impl.h.grantlee

    svc_json.cpp.grantlee
    svc_json.h.grantlee

    svc_topic_type_plugin.cpp.grantlee
    svc_topic_type_plugin.h.grantlee

    svc_record_replay_plugin.cpp.grantlee
    svc_record_replay_plugin.h.grantlee

    codegen_simple.py
  )

  NGL_TARGET_LINK_LIBRARIES(codegen_grantlee
    PRIVATE
      NGLLogger
      ProjectConfig
    PUBLIC
      codegen_base
      KF6::TextTemplate
  )
  NGL_TARGET_INCLUDE_DIRECTORIES(codegen_grantlee
    PUBLIC
      ${CMAKE_CURRENT_SOURCE_DIR}/../
      ${CMAKE_BINARY_DIR}/Z/src/NGLLogger
  )

  # Smoke-test: parse all .grantlee templates in this directory to detect parse/render regressions
  find_package(Qt6 COMPONENTS Core QUIET)
  if (Qt6_FOUND)
    set(QTCORE_TARGET Qt6::Core)
  else()
    find_package(Qt6 COMPONENTS Core REQUIRED)
    set(QTCORE_TARGET Qt6::Core)
  endif()

  add_executable(grantlee_parser_smoke tests/GrantleeParserSmoke.cpp)
  target_link_libraries(grantlee_parser_smoke PRIVATE KF6::TextTemplate ${QTCORE_TARGET})
  add_test(NAME GrantleeParserSmoke COMMAND grantlee_parser_smoke ${CMAKE_CURRENT_SOURCE_DIR})

  NGL_ADD_EXECUTABLE(ALL ngl_codegen
    main_grantlee.cpp
  )
  NGL_TARGET_LINK_LIBRARIES(ngl_codegen
    codegen_grantlee
    LtsCommandLineParser
    nglcoreapplication
    Qt6::Core
    KF6::TextTemplate
  )

  NGL_WRAP_UI(codegen_gui_uics
      main_grantlee_gui.ui
  )
  NGL_ADD_EXECUTABLE(WS AAR SV NO_TRANSLATION codegen_gui
    ${codegen_gui_uics}
    main_grantlee_gui.cpp
    main_grantlee_gui.ui
  )
if(QT6_GUI_AVAILABLE)
    NGL_TARGET_LINK_LIBRARIES(codegen_gui
      codegen_grantlee
      LtsCommandLineParser
      nglapplication
      ${Qt6_Widgets}
      KF6::TextTemplate
    )
endif()
ELSE()
  message(STATUS "BUILD_GRANTLEE disabled or Grantlee not found; skipping codegen_grantlee, ngl_codegen, and codegen_gui targets")
ENDIF()
