Skip to content

Commit

Permalink
CMake project modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
LecrisUT committed Oct 26, 2023
1 parent e7af6aa commit 339f94c
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 98 deletions.
186 changes: 129 additions & 57 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ cmake_minimum_required(VERSION 3.15)

list(APPEND CMAKE_MESSAGE_CONTEXT METIS)
project(METIS
VERSION 5.3.0
DESCRIPTION "Serial Graph Partitioning and Fill-reducing Matrix Ordering"
HOMEPAGE_URL https://github.com/KarypisLab/METIS
LANGUAGES C
VERSION 5.3.0
DESCRIPTION "Serial Graph Partitioning and Fill-reducing Matrix Ordering"
HOMEPAGE_URL https://github.com/KarypisLab/METIS
LANGUAGES C
)

# Back-porting to PROJECT_IS_TOP_LEVEL to older cmake
# TODO: Remove when requiring cmake >= 3.21
if (NOT DEFINED Spglib_IS_TOP_LEVEL)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(PROJECT_IS_TOP_LEVEL ON)
else ()
set(PROJECT_IS_TOP_LEVEL OFF)
endif ()
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(PROJECT_IS_TOP_LEVEL ON)
else ()
set(PROJECT_IS_TOP_LEVEL OFF)
endif ()
endif ()

# Specify C standard
Expand All @@ -28,80 +28,152 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Release)
endif ()

#[==============================================================================================[
# Options #
]==============================================================================================]

set(METIS_SHARED FALSE CACHE BOOL "build a shared library")
set(METIS_BUILD_PROGRAMS TRUE CACHE BOOL "build metis programs")

if(MSVC)
set(METIS_INSTALL FALSE)
else()
set(METIS_INSTALL TRUE)
endif()
include(CMakeDependentOption)
include(FeatureSummary)
option(METIS_TESTS "METIS: Build unit tests" ${PROJECT_IS_TOP_LEVEL})
option(METIS_SHARED_LIBS "METIS: Build as a shared library" ${PROJECT_IS_TOP_LEVEL})
option(METIS_INSTALL "METIS: Install project" ${PROJECT_IS_TOP_LEVEL})
option(METIS_PROGRAMS "METIS: Build executable programs" OFF)
add_feature_info(METIS_PROGRAMS METIS_PROGRAMS "Build executable programs")
option(METIS_IDX64 "METIS: Build with 64 bit ints" OFF)
add_feature_info(METIS_IDX64 METIS_IDX64 "Build with 64 bit ints")
option(METIS_REAL64 "METIS: Build with 64 bit floats" OFF)
add_feature_info(METIS_REAL64 METIS_REAL64 "Build with 64 bit floats")

#[==============================================================================================[
# Project configuration #
]==============================================================================================]

# Configure libmetis library.
if(METIS_SHARED)
set(METIS_LIBRARY_TYPE SHARED)
else()
set(METIS_LIBRARY_TYPE STATIC)
endif(METIS_SHARED)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# Include basic tools
include(FetchContent)
if (METIS_INSTALL)
include(CMakePackageConfigHelpers)
if (UNIX)
include(GNUInstallDirs)
endif ()
endif ()

# Define basic parameters
set(BUILD_SHARED_LIBS ${METIS_SHARED_LIBS})

include(conf/gkbuild.cmake)

# METIS' custom options
option(METIS_IDX64 "enable 64 bit ints" OFF)
option(METIS_REAL64 "enable 64 bit floats (i.e., double)" OFF)
if(METIS_IDX64)
set(IDXTYPEWIDTH 64)
else()
set(IDXTYPEWIDTH 32)
endif(METIS_IDX64)
if(METIS_REAL64)
set(REALTYPEWIDTH 64)
else()
set(REALTYPEWIDTH 32)
endif(METIS_REAL64)
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/include/metis.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/include/metis.h"
@ONLY
)

#[==============================================================================================[
# External packages #
]==============================================================================================]

find_package(GKlib CONFIG)
include(METIS_PackagesInfo)

feature_summary(
FILENAME ${CMAKE_CURRENT_BINARY_DIR}/METIS.info
VAR METIS_Info
DESCRIPTION "METIS supported libraries"
FATAL_ON_MISSING_REQUIRED_PACKAGES
WHAT ALL
)
message(STATUS ${METIS_Info})

#[==============================================================================================[
# Main definition #
]==============================================================================================]

# Add include directories.
# i.e., the -I equivalent
#include_directories(build/xinclude)
include_directories(${GKLIB_PATH}/include)
include_directories(${CMAKE_INSTALL_PREFIX}/include)
# Main project
add_library(METIS_METIS)
set_target_properties(METIS_METIS PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
EXPORT_NAME METIS
OUTPUT_NAME metis
)
add_library(METIS::METIS ALIAS METIS_METIS)

# Main definitions inside libmetis
add_subdirectory(include)
add_subdirectory(libmetis)

# List of paths that the compiler will search for library files.
# i.e., the -L equivalent
link_directories(${GKLIB_PATH}/lib)
link_directories(${CMAKE_INSTALL_PREFIX}/lib)
if (METIS_PROGRAMS)
add_subdirectory(programs)
endif ()

# Recursively look for CMakeLists.txt in subdirs.
#add_subdirectory("build/xinclude")
add_subdirectory("libmetis")
if(METIS_BUILD_PROGRAMS)
add_subdirectory("programs")
endif(METIS_BUILD_PROGRAMS)
# Testing
if (METIS_TESTS)
enable_testing()
add_subdirectory(test)
endif ()

#[==============================================================================================[
# Install or Export #
]==============================================================================================]

# Installation
if (METIS_INSTALL)
# pkg-config files
configure_file(cmake/metis.pc.in metis.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/metis.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
COMPONENT METIS_Development
)

# cmake export files
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/METISConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
cmake/METISConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/METISConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/METIS
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/METISConfigVersion.cmake
${CMAKE_CURRENT_BINARY_DIR}/METISConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/METIS
COMPONENT METIS_Development
)
export(EXPORT METISTargets
FILE METISTargets.cmake
NAMESPACE METIS::
)
install(EXPORT METISTargets
FILE METISTargets.cmake
NAMESPACE METIS::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/METIS
COMPONENT METIS_Development
)
endif ()

# Make project available for FetchContent
if (NOT PROJECT_IS_TOP_LEVEL)
# Propagate variables
if (CMAKE_VERSION VERSION_LESS 3.25)
# TODO: Remove when cmake 3.25 is commonly distributed
set(METIS_VERSION ${METIS_VERSION} PARENT_SCOPE)
set(METIS_VERSION_MAJOR ${METIS_VERSION_MAJOR} PARENT_SCOPE)
set(METIS_VERSION_MINOR ${METIS_VERSION_MINOR} PARENT_SCOPE)
set(METIS_VERSION_PATCH ${METIS_VERSION_PATCH} PARENT_SCOPE)
set(METIS_VERSION_TWEAK ${METIS_VERSION_TWEAK} PARENT_SCOPE)
set(METIS_IDXTYPEWIDTH ${METIS_IDXTYPEWIDTH} PARENT_SCOPE)
set(METIS_REALTYPEWIDTH ${METIS_REALTYPEWIDTH} PARENT_SCOPE)
else ()
return(PROPAGATE
METIS_VERSION
METIS_VERSION_MAJOR
METIS_VERSION_MINOR
METIS_VERSION_PATCH
METIS_VERSION_TWEAK
METIS_IDXTYPEWIDTH
METIS_REALTYPEWIDTH
)
endif ()
endif ()
8 changes: 8 additions & 0 deletions cmake/METISConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@PACKAGE_INIT@

## Define exported variables
set(METIS_IDXTYPEWIDTH @METIS_IDXTYPEWIDTH@)
set(METIS_REALTYPEWIDTH @METIS_REALTYPEWIDTH@)

## Add all exported targets
include(${CMAKE_CURRENT_LIST_DIR}/METISTargets.cmake)
6 changes: 6 additions & 0 deletions cmake/METIS_PackagesInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set_package_properties(GKlib PROPERTIES
URL https://github.com/KarypisLab/GKlib
DESCRIPTION "A library of various helper routines and frameworks used by many of the KarypisLab's software"
TYPE REQUIRED
PURPOSE "Utilties library"
)
6 changes: 6 additions & 0 deletions cmake/metis.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@

Libs: -L@CMAKE_INSTALL_FULL_LIBDIR@ -lmetis
Cflags: -I@CMAKE_INSTALL_FULL_INCLUDEDIR@
40 changes: 37 additions & 3 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
if(METIS_INSTALL)
install(FILES metis.h DESTINATION include)
endif()
# Generate metis.h file with appropriate exported macros
if (METIS_IDX64)
set(IDXTYPEWIDTH 64)
else ()
set(IDXTYPEWIDTH 32)
endif ()
if (METIS_REAL64)
set(REALTYPEWIDTH 64)
else ()
set(REALTYPEWIDTH 32)
endif ()
configure_file(metis.h.in metis.h @ONLY)

# Configure main target to consume the metis.h
# TODO: Move to FILE_SET for cmake 3.23
set_target_properties(METIS_METIS PROPERTIES
PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/metis.h
)
target_include_directories(METIS_METIS PUBLIC
# Add the path to the generated metis.h
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

# Set namespaced variables to be exported
set(METIS_IDXTYPEWIDTH ${IDXTYPEWIDTH})
set(METIS_REALTYPEWIDTH ${REALTYPEWIDTH})
if (CMAKE_VERSION VERSION_LESS 3.25)
# TODO: Remove when cmake 3.25 is commonly distributed
set(METIS_IDXTYPEWIDTH ${METIS_IDXTYPEWIDTH} PARENT_SCOPE)
set(METIS_REALTYPEWIDTH ${METIS_IDXTYPEWIDTH} PARENT_SCOPE)
else ()
return(PROPAGATE
METIS_IDXTYPEWIDTH
METIS_REALTYPEWIDTH
)
endif ()
69 changes: 48 additions & 21 deletions libmetis/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
# Add this directory for internal users.
set(METIS_NAME "metis" CACHE INTERNAL "")
target_sources(METIS_METIS PRIVATE
auxapi.c
balance.c
bucketsort.c
checkgraph.c
coarsen.c
compress.c
contig.c
debug.c
fm.c
fortran.c
frename.c
gklib.c
graph.c
initpart.c
kmetis.c
kwayfm.c
kwayrefine.c
mcutil.c
mesh.c
meshpart.c
minconn.c
mincover.c
mmd.c
ometis.c
options.c
parmetis.c
pmetis.c
refine.c
separator.c
sfm.c
srefine.c
stat.c
timing.c
util.c
wspace.c
)
target_link_libraries(METIS_METIS PRIVATE GKLib::GKLib)

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include)

# Find sources.
file(GLOB metis_sources *.h *.c)

# Build libmetis.
add_library(${METIS_NAME} ${METIS_LIBRARY_TYPE} ${metis_sources})

if(GKLIB_NAME)
target_link_libraries(${METIS_NAME} ${GKLIB_NAME})
add_dependencies(${METIS_NAME} ${GKLIB_NAME})
endif()

if(METIS_INSTALL)
install(TARGETS ${METIS_NAME}
LIBRARY DESTINATION lib
RUNTIME DESTINATION lib
ARCHIVE DESTINATION lib)
endif()
# Install
if (METIS_INSTALL)
install(TARGETS METIS_METIS
EXPORT METISTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT METIS_Runtime NAMELINK_COMPONENT METIS_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT METIS_Development
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT METIS_Development
RUNTIME DESTINATION ${CMAKE_INSTALL_RUNTIMEDIR} COMPONENT METIS_Runtime
)
endif ()
Loading

0 comments on commit 339f94c

Please sign in to comment.