cmake_minimum_required(VERSION 3.16)
project(amdis LANGUAGES CXX VERSION 2.11)

# find dune-common and set the module path
find_package(dune-common REQUIRED)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules"
  ${dune-common_MODULE_PATH})

# include the dune macros
include(DuneMacros)

# some options to control the cmake behavior
option(ENABLE_ALL_WARNINGS "enable all meaningful warnings" OFF)
option(ENABLE_CCACHE "enable compiler cache" ON)

if(dune-common_VERSION VERSION_GREATER_EQUAL 2.11)
  # deactivate global include-directories
  dune_policy(SET DP_DEFAULT_INCLUDE_DIRS NEW)
  # do not set all flags to tests automatically
  dune_policy(SET DP_TEST_ADD_ALL_FLAGS NEW)
  # do not making all suggested dependencies required automatically
  dune_policy(SET DP_SUGGESTED_MODULE_DEPENDENCIES_REQUIRED_DOWNSTREAM OLD)
endif()

# start a dune project with information from dune.module
dune_project()

# first find or create all external dependencies
find_package(fmt 9.0 CONFIG)
find_package(muparser CONFIG)
add_subdirectory("libs")

dune_add_library(amdis
  LINK_LIBRARIES ${DUNE_LIBS} fmt::fmt muparser::muparser
  EXPORT_NAME AMDiS NAMESPACE AMDiS::)

if(dune-common_VERSION VERSION_GREATER_EQUAL 2.11)
  # set include directories for amdis library
  dune_default_include_directories(amdis PUBLIC)
endif()

add_subdirectory("amdis")
add_subdirectory("bin")
add_subdirectory("cmake/modules")
add_subdirectory("examples" EXCLUDE_FROM_ALL)
add_subdirectory("test")

set(AMDIS_PACKAGE_DEPENDENCIES)
if(fmt_FOUND)
  list(APPEND AMDIS_PACKAGE_DEPENDENCIES "find_dependency(fmt)")
endif()

if(muparser_FOUND)
  list(APPEND AMDIS_PACKAGE_DEPENDENCIES "find_dependency(muparser)")
endif()

if (MTL_FOUND)
  target_link_libraries(amdis PUBLIC MTL::MTL)
  list(APPEND AMDIS_PACKAGE_DEPENDENCIES "find_dependency(MTL)")
endif (MTL_FOUND)

if (HYPRE_FOUND)
  target_link_libraries(amdis PUBLIC HYPRE::HYPRE)
  target_compile_definitions(amdis PUBLIC ENABLE_HYPRE)
  list(APPEND AMDIS_PACKAGE_DEPENDENCIES "find_dependency(HYPRE)")
endif(HYPRE_FOUND)

if (Eigen3_FOUND)
  target_link_libraries(amdis PUBLIC Eigen3::Eigen)
  list(APPEND AMDIS_PACKAGE_DEPENDENCIES "find_dependency(Eigen3)")
endif (Eigen3_FOUND)

if (PETSc_FOUND)
  target_link_libraries(amdis PUBLIC PETSc::PETSc)
  list(APPEND AMDIS_PACKAGE_DEPENDENCIES "find_dependency(PETSc)")
endif (PETSc_FOUND)


if (ENABLE_ALL_WARNINGS)
  target_compile_options(amdis PUBLIC "-Wall" "-Wpedantic"
                                      "-Wextra" "-Wnon-virtual-dtor"
                                      "-Wold-style-cast" "-Wcast-align"
                                      "-Woverloaded-virtual" "-Wconversion")
endif (ENABLE_ALL_WARNINGS)

# explicitly set that dune-typetree is available. It is a required dependency.
target_compile_definitions(amdis PUBLIC HAVE_DUNE_TYPETREE)

# export the backend and package dependencies
string(JOIN "\n" DUNE_CUSTOM_PKG_CONFIG_SECTION
  [[list(APPEND CMAKE_MODULE_PATH "${amdis_MODULE_PATH}")]]
  [[set(BACKEND @BACKEND@)]]
  ${AMDIS_PACKAGE_DEPENDENCIES}
)

# finalize the dune project, e.g. generating config.h etc.
finalize_dune_project()
