## Copyright 2014-2016,2021,2022 IPB, Universite de Bordeaux, INRIA & CNRS
##
## This file is part of the Scotch software package for static mapping,
## graph partitioning and sparse matrix ordering.
##
## This software is governed by the CeCILL-C license under French law
## and abiding by the rules of distribution of free software. You can
## use, modify and/or redistribute the software under the terms of the
## CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
## URL: "http://www.cecill.info".
##
## As a counterpart to the access to the source code and rights to copy,
## modify and redistribute granted by the license, users are provided
## only with a limited warranty and the software's author, the holder of
## the economic rights, and the successive licensors have only limited
## liability.
##
## In this respect, the user's attention is drawn to the risks associated
## with loading, using, modifying and/or developing or reproducing the
## software by the user in light of its specific status of free software,
## that may mean that it is complicated to manipulate, and that also
## therefore means that it is reserved for developers and experienced
## professionals having in-depth computer knowledge. Users are therefore
## encouraged to load and test the software's suitability as regards
## their requirements in conditions enabling the security of their
## systems and/or data to be ensured and, more generally, to use and
## operate it in the same conditions as regards security.
##
## The fact that you are presently reading this means that you have had
## knowledge of the CeCILL-C license and that you accept its terms.
##
############################################################
##                                                        ##
##   AUTHORS    : Cedric LACHAT                           ##
##                Amaury JACQUES                          ##
##                Florent PRUVOST                         ##
##                Marc FUENTES                            ##
##                                                        ##
##   FUNCTION   : Secondary configuration file for CMake  ##
##                                                        ##
##   DATES      : # Version 6.0  : from : 01 sep 2014     ##
##                                 to     01 sep 2021     ##
##                # Version 7.0  : from : 01 sep 2021     ##
##                                 to     08 jan 2022     ##
##                                                        ##
############################################################

####################
#  libScotchMeTiS  #
####################

set_source_files_properties(${GENERATED_INCLUDE_DIR}/scotch.h PROPERTIES GENERATED 1)
set_source_files_properties(${GENERATED_INCLUDE_DIR}/metis.h PROPERTIES GENERATED 1)

# metis.h
add_custom_command(OUTPUT ${GENERATED_INCLUDE_DIR}/metis.h
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/library_metis.h ${CMAKE_CURRENT_BINARY_DIR}
  COMMAND $<TARGET_FILE:dummysizes> ${CMAKE_CURRENT_BINARY_DIR}/library_metis.h ${GENERATED_INCLUDE_DIR}/metis.h DEPENDS dummysizes
  DEPENDS library_metis.h
  COMMENT "Generating metis.h")

add_custom_target(metis_h
  DEPENDS "${GENERATED_INCLUDE_DIR}/metis.h")

# metisf.h
add_custom_command(OUTPUT ${GENERATED_INCLUDE_DIR}/metisf.h
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/library_metis_f.h ${CMAKE_CURRENT_BINARY_DIR}
  COMMAND $<TARGET_FILE:dummysizes> ${CMAKE_CURRENT_BINARY_DIR}/library_metis_f.h ${GENERATED_INCLUDE_DIR}/metisf.h DEPENDS dummysizes
  DEPENDS library_metis_f.h
  COMMENT "Generating metisf.h")

add_custom_target(metisf_h
  DEPENDS "${GENERATED_INCLUDE_DIR}/metisf.h")

# libScotchMeTiS
foreach(version 3 5)
  add_library(scotchmetisv${version}
    metis_graph_order.c
    metis_graph_order_f.c
    metis_graph_part.c
    metis_graph_part_f.c
    metis_graph_dual.c
    metis_graph_dual_f.c
    metis_graph_part_dual.c
    metis_graph_part_dual_f.c
    metis_options.c
    metis_options_f.c
    ${LIBSCOTCH_INCLUDE_DIR}/module.h
    ${LIBSCOTCH_INCLUDE_DIR}/common.h
    ${GENERATED_INCLUDE_DIR}/scotch.h
    ${GENERATED_INCLUDE_DIR}/metis.h
    ${GENERATED_INCLUDE_DIR}/metisf.h)

  target_compile_definitions(scotchmetisv${version} PUBLIC SCOTCH_METIS_VERSION=${version})

  target_include_directories(scotchmetisv${version} PRIVATE
    $<BUILD_INTERFACE:${LIBSCOTCH_INCLUDE_DIR}>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    $<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>
    $<INSTALL_INTERFACE:include>)

  target_link_libraries(scotchmetisv${version} PRIVATE scotch)

  add_dependencies(scotchmetisv${version} scotch_h metisf_h)
  if(INSTALL_METIS_HEADERS)
    set_target_properties(scotchmetisv${version} PROPERTIES PUBLIC_HEADER "${GENERATED_INCLUDE_DIR}/metis.h;${GENERATED_INCLUDE_DIR}/metisf.h")
  endif()

  if(APPLE)
    set_target_properties(scotchmetisv${version} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
  endif(APPLE)

  # libScotchMeTiS targets install
  install(EXPORT scotchmetisTargets
    FILE scotchmetisTargets.cmake
    NAMESPACE SCOTCH::
    DESTINATION lib/cmake/scotch)
  if(INSTALL_METIS_HEADERS)
    install(TARGETS scotchmetisv${version}
      EXPORT scotchmetisTargets
      ARCHIVE DESTINATION lib
      LIBRARY DESTINATION lib
      PUBLIC_HEADER DESTINATION include)
  else()
    install(TARGETS scotchmetisv${version}
      EXPORT scotchmetisTargets
      ARCHIVE DESTINATION lib
      LIBRARY DESTINATION lib)
  endif()
endforeach(version 3 5)

#########################
#  libPTScotchParMeTiS  #
#########################

if(BUILD_PTSCOTCH)
  set_source_files_properties(${GENERATED_INCLUDE_DIR}/ptscotch.h PROPERTIES GENERATED 1)

  # parmetis.h
  add_custom_command(OUTPUT ${GENERATED_INCLUDE_DIR}/parmetis.h
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/library_parmetis.h ${CMAKE_CURRENT_BINARY_DIR}
    COMMAND $<TARGET_FILE:dummysizes> ${CMAKE_CURRENT_BINARY_DIR}/library_parmetis.h ${GENERATED_INCLUDE_DIR}/parmetis.h DEPENDS dummysizes
    DEPENDS library_parmetis.h
    COMMENT "Generating parmetis.h")

  add_custom_target(parmetis_h
    DEPENDS "${GENERATED_INCLUDE_DIR}/parmetis.h")

  # libPTScotchParMeTiS
  foreach(version 3)
    add_library(ptscotchparmetisv${version}
      parmetis_dgraph_order.c
      parmetis_dgraph_order_f.c
      parmetis_dgraph_part.c
      parmetis_dgraph_part_f.c
      ${LIBSCOTCH_INCLUDE_DIR}/module.h
      ${LIBSCOTCH_INCLUDE_DIR}/common.h
      ${GENERATED_INCLUDE_DIR}/ptscotch.h
      ${GENERATED_INCLUDE_DIR}/parmetis.h)

    target_compile_definitions(ptscotchparmetisv${version} PUBLIC SCOTCH_PTSCOTCH SCOTCH_METIS_VERSION=${version})

    target_include_directories(ptscotchparmetisv${version} PRIVATE
      $<BUILD_INTERFACE:${LIBSCOTCH_INCLUDE_DIR}>
      $<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
      $<INSTALL_INTERFACE:include>)

    target_link_libraries(ptscotchparmetisv${version} PRIVATE scotch)
    target_link_libraries(ptscotchparmetisv${version} PRIVATE ptscotch)
    target_link_libraries(ptscotchparmetisv${version} PUBLIC MPI::MPI_C)

    add_dependencies(ptscotchparmetisv${version} ptscotch_h)
    if(INSTALL_METIS_HEADERS)
      set_target_properties(ptscotchparmetisv${version} PROPERTIES PUBLIC_HEADER "${GENERATED_INCLUDE_DIR}/parmetis.h")
    endif()

    if(APPLE)
      set_target_properties(ptscotchparmetisv${version} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
    endif(APPLE)

    # libPTScotchParMeTiS targets install
    install(EXPORT ptscotchparmetisTargets
      FILE ptscotchparmetisTargets.cmake
      NAMESPACE SCOTCH::
      DESTINATION lib/cmake/scotch)

    if(INSTALL_METIS_HEADERS)
      install(TARGETS ptscotchparmetisv${version}
        EXPORT ptscotchparmetisTargets
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib
        PUBLIC_HEADER DESTINATION include)
    else()
      install(TARGETS ptscotchparmetisv${version}
        EXPORT ptscotchparmetisTargets
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib)
    endif()
  endforeach(version 3)
endif(BUILD_PTSCOTCH)
