cmake_minimum_required(VERSION 3.10)
project(METIS C)

include(GNUInstallDirs)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Link-time optimization (LTO) is opt-in (lto=1). With GCC the default LTO
# objects are "slim" (GIMPLE only, no machine code), so an installed static
# libmetis.a could be linked only by the same GCC version through its linker
# plugin; clang, ifx, and other GCC versions failed (issue #128). When LTO is
# on, GCC builds emit fat objects (see libmetis/CMakeLists.txt) so the library
# still links without the plugin. A -DCMAKE_INTERPROCEDURAL_OPTIMIZATION given
# on the command line is left untouched when LTO is off.
option(LTO "enable link-time optimization" OFF)
if(LTO)
  include(CheckIPOSupported)
  check_ipo_supported(RESULT METIS_IPO_SUPPORTED OUTPUT METIS_IPO_MSG)
  if(METIS_IPO_SUPPORTED)
    set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
    message(STATUS "METIS: interprocedural optimization (LTO) enabled")
  else()
    message(STATUS "METIS: IPO/LTO not supported: ${METIS_IPO_MSG}")
  endif()
endif()
set(SHARED FALSE CACHE BOOL "build a shared library")

if(MSVC)
  set(METIS_INSTALL FALSE)
else()
  set(METIS_INSTALL TRUE)
endif()

# Configure libmetis library.
if(SHARED)
  set(METIS_LIBRARY_TYPE SHARED)
else()
  set(METIS_LIBRARY_TYPE STATIC)
endif(SHARED)

include(./conf/gkbuild.cmake)

# METIS' custom options
#option(IDX64 "enable 64 bit ints" OFF)
#option(REAL64 "enable 64 bit floats (i.e., double)" OFF)
#if(IDX64)
#  set(METIS_COPTIONS "${METIS_COPTIONS} -DIDXTYPEWIDTH=64")
#else()
#  set(METIS_COPTIONS "${METIS_COPTIONS} -DIDXTYPEWIDTH=32")
#endif(IDX64)
#if(REAL64)
#  set(METIS_COPTIONS "${METIS_COPTIONS} -DREALTYPEWIDTH=64")
#else()
#  set(METIS_COPTIONS "${METIS_COPTIONS} -DREALTYPEWIDTH=32")
#endif(REAL64)
#
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${METIS_COPTIONS}")


# Add include directories.
# i.e., the -I equivalent
include_directories(build/xinclude)
include_directories(${GKLIB_PATH}/include)
include_directories(${CMAKE_INSTALL_PREFIX}/include)

# List of paths that the compiler will search for library files.
# i.e., the -L equivalent
set(METIS_LINK_DIRS)
foreach(prefix ${GKLIB_PATH} ${CMAKE_INSTALL_PREFIX})
  foreach(libdir ${CMAKE_INSTALL_LIBDIR} lib64 lib)
    if(IS_DIRECTORY "${prefix}/${libdir}")
      list(APPEND METIS_LINK_DIRS "${prefix}/${libdir}")
    endif()
  endforeach()
endforeach()
list(REMOVE_DUPLICATES METIS_LINK_DIRS)
link_directories(${METIS_LINK_DIRS})

# Recursively look for CMakeLists.txt in subdirs.
add_subdirectory("build/xinclude")
add_subdirectory("libmetis")
add_subdirectory("programs")
