#########################################################################
## Sundials
message(STATUS "##### 3rdParty Sundials")
if(NOT OPENMODELICA_NEW_CMAKE_BUILD)
  option(SUNDIALS_BUILD_SHARED_LIBS "Build shared libraries" OFF)
  option(SUNDIALS_EXAMPLES_ENABLE_C "Build SUNDIALS C examples" OFF)
  add_subdirectory(sundials-5.4.0 EXCLUDE_FROM_ALL)

  ## Sundials thoughtfully has organized its headers cleanly in one include/ directory
  ## Take advantage of that to transitively provide the headers when an external target links to
  ## any one of the sundials' libs.
  add_library(sundials_interface INTERFACE)
  target_include_directories(sundials_interface INTERFACE ${sundials_SOURCE_DIR}/include/)
  ## The sundials_config.h files are generated in the build directory. Add it as an include dir.
  target_include_directories(sundials_interface INTERFACE ${sundials_BINARY_DIR}/include/)

  ## Add an interface lib for linking ot the static libs. This will transitively add
  ## DLINK_SUNDIALS_STATIC to anything that links to the static sundials libs.
  add_library(sundials_interface_static INTERFACE)
  target_link_libraries(sundials_interface_static INTERFACE sundials_interface)
  target_compile_definitions(sundials_interface_static INTERFACE LINK_SUNDIALS_STATIC)

  ## Now that the includes and defines are attached to a utility interface library (sundials_interface_static) link it
  ## to the sundials static libs so they can be found when the sundials lib is linked-to from an external lib.
  ### Note! It should have been enough for the scope here to be INTERFACE instead of PUBLIC. However,
  ### that does not seem to work. This should be fine anyway.
  target_link_libraries(sundials_cvode_static PUBLIC sundials_interface_static)
  target_link_libraries(sundials_kinsol_static PUBLIC sundials_interface_static)
endif()

add_library(oms::3rd::cvode ALIAS sundials_cvode_static)
add_library(oms::3rd::kinsol ALIAS sundials_kinsol_static)

#########################################################################
## zlib.
message(STATUS "##### 3rdParty zlib")
set(ZLIB_BUILD_SHARED OFF CACHE BOOL "Disable zlib shared library")
set(ZLIB_BUILD_TESTING OFF CACHE BOOL "Disable zlib's own test suite; it registers global ctest targets (zlib_install, zlib_find_package_*, ...) that don't apply when zlib is only vendored as a static-lib dependency")
add_subdirectory(zlib EXCLUDE_FROM_ALL)
add_library(oms::3rd::zlib ALIAS zlibstatic)
# Expose zlibstatic as ZLIB::ZLIB so libzip's find_package(ZLIB) resolves to our
# in-tree build without requiring a system zlib (e.g. Alpine, Windows).
add_library(ZLIB::ZLIB ALIAS zlibstatic)
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zlib" CACHE PATH "" FORCE)
set(ZLIB_LIBRARY "zlibstatic" CACHE STRING "" FORCE)

#########################################################################
## minizip.
message(STATUS "##### 3rdParty minizip")
add_subdirectory(minizip EXCLUDE_FROM_ALL)
add_library(oms::3rd::minizip ALIAS oms_minizip)

#########################################################################
## fmi4c.
message(STATUS "##### 3rdParty fmi4c")
set(FMI4C_BUILD_SHARED OFF)
set(FMI4C_USE_SYSTEM_ZIP OFF)
set(FMI4C_USE_EXTERNAL_MINIZIP ON)
set(FMI4C_EXTERNAL_MINIZIP "oms::3rd::minizip" CACHE STRING "Defines an external target for minizip.")
add_subdirectory(fmi4c EXCLUDE_FROM_ALL)
add_library(oms::3rd::fmi4c ALIAS fmi4c)

#########################################################################
## Lua.
message(STATUS "##### 3rdParty Lua")
option(LUA_ENABLE_SHARED OFF)
add_subdirectory(Lua EXCLUDE_FROM_ALL)
add_library(oms::3rd::lua ALIAS lua_static)

#########################################################################
## PugiXml.
message(STATUS "##### 3rdParty PugiXml")
add_subdirectory(PugiXml EXCLUDE_FROM_ALL)
add_library(oms::3rd::pugixml::header ALIAS pugixml_header_only)

#########################################################################
## xerces
message(STATUS "##### 3rdParty xerces")
option(XERCES_BUILD_SHARED_LIBS OFF)
add_subdirectory(xerces EXCLUDE_FROM_ALL)
### xerces does not include the target directories by default, so we have to externally add the include directories
target_include_directories(xerces-c INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/xerces/src)
### Xerces_autoconf_config.hpp is generated at build directory and we have to include it
target_include_directories(xerces-c INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/xerces/src)
add_library(oms::3rd::xerces ALIAS xerces-c)

#########################################################################
## CTPL.
message(STATUS "##### 3rdParty CTPL")
add_subdirectory(CTPL EXCLUDE_FROM_ALL)
add_library(oms::3rd::ctpl::header ALIAS ctpl_header_only)

#########################################################################
## Dear ImGui.
if (OMS_ENABLE_OMSimulatorGui)
  message(STATUS "##### 3rdParty Dear ImGui")
  add_subdirectory(imgui EXCLUDE_FROM_ALL)
  add_library(oms::3rd::imgui ALIAS imgui)
endif ()

#########################################################################
## GLFW.
if (OMS_ENABLE_OMSimulatorGui)
  message(STATUS "##### 3rdParty GLFW")
  set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
  set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
  set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
  set(GLFW_BUILD_WAYLAND OFF CACHE BOOL "" FORCE)

  add_subdirectory(glfw EXCLUDE_FROM_ALL)
  add_library(oms::3rd::GLFW ALIAS glfw)
endif ()

#########################################################################
## tinyfiledialogs.
if (OMS_ENABLE_OMSimulatorGui)
  message(STATUS "##### 3rdParty tinyfiledialogs")
  add_subdirectory(tinyfd EXCLUDE_FROM_ALL)
  add_library(oms::3rd::tinyfd ALIAS tinyfd)
endif ()

#########################################################################
## nlohmann/json
message(STATUS "##### 3rdParty nlohmann/json")
add_subdirectory(json EXCLUDE_FROM_ALL)
add_library(oms::3rd::json::header ALIAS json_header_only)

#########################################################################
## libzip.
message(STATUS "##### 3rdParty libzip")
set(BUILD_REGRESS OFF CACHE BOOL "Disable regression tests")
set(BUILD_OSSFUZZ OFF CACHE BOOL "Disable fuzzers for ossfuzz")
set(BUILD_EXAMPLES OFF CACHE BOOL "Disable examples")
set(BUILD_DOC OFF CACHE BOOL "Disable documentation")
set(BUILD_TOOLS OFF CACHE BOOL "Disable zipcmp, zipmerge, ziptool binaries")
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build zip as a static library")
set(SHARED_LIB_VERSIONNING OFF CACHE BOOL "Disable SO versioning")
set(LIBZIP_DO_INSTALL OFF CACHE BOOL "Install libzip and the related files" FORCE)
set(ENABLE_OPENSSL OFF CACHE BOOL "Disable OpenSSL (not needed for FMUs)")
set(ENABLE_COMMONCRYPTO OFF CACHE BOOL "Disable CommonCrypto (not needed for FMUs)")
set(ENABLE_GNUTLS OFF CACHE BOOL "Disable GnuTLS (not needed for FMUs)")
set(ENABLE_MBEDTLS OFF CACHE BOOL "Disable mbed TLS (not needed for FMUs)")
set(ENABLE_WINDOWS_CRYPTO OFF CACHE BOOL "Disable Windows Crypto (not needed for FMUs)")
set(ENABLE_BZIP2 OFF CACHE BOOL "Disable BZip2 (not used in FMUs)")
set(ENABLE_LZMA OFF CACHE BOOL "Disable LZMA (not used in FMUs)")
set(ENABLE_ZSTD OFF CACHE BOOL "Disable Zstandard (not used in FMUs)")
add_subdirectory(libzip EXCLUDE_FROM_ALL)
add_library(oms::3rd::libzip ALIAS zip)

#########################################################################
## DCPLib.
message(STATUS "##### 3rdParty DCPLib")
set(DCPLIB_USE_EXTERNAL_XERCES ON CACHE BOOL "")
set(DCPLIB_USE_EXTERNAL_ZIP ON CACHE BOOL "")
set(DCPLIB_EXTERNAL_XERCES "oms::3rd::xerces" CACHE STRING "Defines an external target for xerces.")
set(DCPLIB_EXTERNAL_ZIP "oms::3rd::libzip" CACHE STRING "Defines an external target for libzip.")
add_subdirectory(DCPLib EXCLUDE_FROM_ALL)
add_library(oms::3rd::dcplib::core ALIAS Core)
add_library(oms::3rd::dcplib::master ALIAS Master)
add_library(oms::3rd::dcplib::slave ALIAS Slave)
add_library(oms::3rd::dcplib::ethernet ALIAS Ethernet)
add_library(oms::3rd::dcplib::zip ALIAS Zip)
add_library(oms::3rd::dcplib::xml ALIAS Xml)
