# Shared libraries
#

# (1) nomadUtils
#

# Utils
add_library (
    nomadUtils 
    SHARED 
    ${MATH_SOURCES}
    ${OUTPUT_SOURCES}
    ${PARAM_SOURCES}
    ${TYPE_SOURCES}
    ${UTIL_SOURCES}
    ${ATTRIBUTE_HEADERS})

target_include_directories(
    nomadUtils 
    PUBLIC
    $<BUILD_INTERFACE: 
     ${CMAKE_SOURCE_DIR}/src/Attribute
     ${CMAKE_SOURCE_DIR}/src/Math
     ${CMAKE_SOURCE_DIR}/src/Output
     ${CMAKE_SOURCE_DIR}/src/Param
     ${CMAKE_SOURCE_DIR}/src/Type
     ${CMAKE_SOURCE_DIR}/src/Util
    >
    $<INSTALL_INTERFACE: 
     ${CMAKE_INSTALL_INCLUDEDIR}
    >
)

if(OpenMP_CXX_FOUND)
    target_link_libraries(
      nomadUtils 
      PUBLIC
        OpenMP::OpenMP_CXX
    )
endif()

set_target_properties(
    nomadUtils 
    PROPERTIES
       VERSION "${NOMAD_VERSION}"
       SOVERSION "${NOMAD_VERSION_MAJOR}.${NOMAD_VERSION_MINOR}"
       NO_SONAME ${NO_SONAME}
       OUTPUT_NAME nomadUtils
)

if (WIN32)
    target_compile_definitions(
      nomadUtils
      PRIVATE 
        -DDLL_UTIL_EXPORTS
    )
endif()


# (1) nomadUtils
#


# Evals
add_library(
    nomadEval 
    SHARED 
      ${CACHE_SOURCES} 
      ${EVAL_SOURCES}
)

target_include_directories(
    nomadEval 
    PUBLIC
      $<BUILD_INTERFACE: 
        ${CMAKE_CURRENT_SOURCE_DIR}/Cache
        ${CMAKE_CURRENT_SOURCE_DIR}/Eval
      >
      $<INSTALL_INTERFACE:
        ${CMAKE_INSTALL_INCLUDEDIR}
      >
)

target_link_libraries(
    nomadEval 
    PUBLIC
    nomadUtils
)

if(OpenMP_CXX_FOUND)
    target_link_libraries(
      nomadEval 
      PUBLIC
        OpenMP::OpenMP_CXX
    )
endif()

set_target_properties(
    nomadEval
    PROPERTIES
       VERSION "${NOMAD_VERSION}"
       SOVERSION "${NOMAD_VERSION_MAJOR}.${NOMAD_VERSION_MINOR}"
       NO_SONAME ${NO_SONAME}
)

if (WIN32)
    target_compile_definitions(
      nomadEval
      PRIVATE 
        -DDLL_EVAL_EXPORTS
    )
endif()



# (3) nomadAlgos
#


# Algos
add_library(
  nomadAlgos 
    SHARED 
      ${ALGOS_SOURCES} ${CS_SOURCES} 
      ${DMULTIMADS_SOURCES} ${DISCOMADS_SOURCES} 
      ${MADS_SOURCES} ${MADSLOGBARRIER_SOURCES} ${SIMPLE_MADS_SOURCES} ${LH_SOURCES}
      ${NM_SOURCES} ${PHASEONE_SOURCES} 
      ${PSDMADS_SOURCES} ${COOPMADS_SOURCES} 
      ${QPSOLVER_SOURCES} ${SSDMADS_SOURCES} 
      ${TEMPLATE_ALGO_SOURCES} ${SIMPLELINESEARCH_ALGO_SOURCES} 
      ${VNSMADS_SOURCES}
)
   
target_include_directories(
  nomadAlgos
    PUBLIC
      $<BUILD_INTERFACE: 
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/CoordinateSearch
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/DMultiMads
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/DiscoMads
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/LatinHypercubeSampling
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/Mads
	${CMAKE_CURRENT_SOURCE_DIR}/Algos/MadsMixedPenaltyLogBarrier
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/SimpleMads
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/NelderMead
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/PhaseOne
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/PSDMads
	${CMAKE_CURRENT_SOURCE_DIR}/Algos/COOPMads
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/QPSolverAlgo
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/SSDMads
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/TemplateAlgo
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/SimpleLineSearch
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/VNSMads
       >
       $<INSTALL_INTERFACE: 
          ${CMAKE_INSTALL_INCLUDEDIR}
       >
)


if(USE_SGTELIB MATCHES ON)
  target_sources(
    nomadAlgos
    PRIVATE
      ${QUADMODEL_SOURCES}
      ${QUADMODELSLD_SOURCES} 
      ${SGTELIBMODEL_SOURCES} 
  )

  target_include_directories(
    nomadAlgos
    PUBLIC
      $<BUILD_INTERFACE: 
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/QuadModel
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/QuadModelSLD
        ${CMAKE_CURRENT_SOURCE_DIR}/Algos/SgtelibModel
      >
  )

  target_link_libraries(
    nomadAlgos
    PUBLIC 
      sgtelib
  )
else()

  target_include_directories(
    nomadAlgos
    PUBLIC
      $<BUILD_INTERFACE: 
        $ENV{IBEX_ROOT}/src
      >
  )
endif()

target_link_libraries(
  nomadAlgos
  PUBLIC
    nomadUtils
    nomadEval
)

if(OpenMP_CXX_FOUND)
   target_link_libraries(
     nomadAlgos
       PUBLIC
         OpenMP::OpenMP_CXX
   )
endif()

if(USE_IBEX MATCHES ON)
   target_link_libraries(nomadAlgos PUBLIC ibex)

   add_library(ibex SHARED IMPORTED)

   set_target_properties(
     ibex
     PROPERTIES
       IMPORTED_LOCATION
         $ENV{IBEX_ROOT}/lib/libibex.so
   )

   target_include_directories(
     ibex
     INTERFACE
       "$ENV{IBEX_ROOT}/__build__"
	 "$ENV{IBEX_ROOT}/include/ibex"
       "$ENV{IBEX_ROOT}/include/ibex/3rd"
   )

   message(CHECK_START " Ready to use IBEX")
endif()

set_target_properties(
   nomadAlgos
     PROPERTIES
       VERSION "${NOMAD_VERSION}"
       SOVERSION "${NOMAD_VERSION_MAJOR}.${NOMAD_VERSION_MINOR}"
       NO_SONAME ${NO_SONAME}
       OUTPUTNAME nomadAlgos
)


if (WIN32)
    target_compile_definitions(
      nomadAlgos
        PRIVATE -DDLL_ALGO_EXPORTS
    )
endif()



#
# Install libraries
#

# installing executables and libraries
install(
  TARGETS
    nomadUtils nomadEval nomadAlgos
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/
)

