| 1 |
cmake_minimum_required(VERSION 3.22) |
| 2 |
cmake_policy(SET CMP0042 NEW) |
| 3 |
cmake_policy(SET CMP0077 NEW) |
| 4 |
|
| 5 |
set(CMAKE_USER_MAKE_RULES_OVERRIDE_Fortran |
| 6 |
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/fortran_flags_override.cmake") |
| 7 |
|
| 8 |
project(libMBD |
| 9 |
DESCRIPTION "Many-body dispersion library" |
| 10 |
LANGUAGES Fortran C) |
| 11 |
|
| 12 |
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
| 13 |
include(libMBDVersion) |
| 14 |
include(GNUInstallDirs) |
| 15 |
include(CMakeDependentOption) |
| 16 |
include(CTest) |
| 17 |
|
| 18 |
option(ENABLE_SCALAPACK_MPI "Enable parallelisation with ScaLAPACK/MPI") |
| 19 |
CMAKE_DEPENDENT_OPTION(ENABLE_ELSI "Enable ELSI interface" OFF ENABLE_SCALAPACK_MPI OFF) |
| 20 |
CMAKE_DEPENDENT_OPTION(ENABLE_MPIFH "Build against legacy mpif.h MPI interface" OFF ENABLE_SCALAPACK_MPI OFF) |
| 21 |
option(ENABLE_C_API "Enable C API" ON) |
| 22 |
|
| 23 |
set(CMAKE_INSTALL_MODULEDIR ${CMAKE_INSTALL_INCLUDEDIR} |
| 24 |
CACHE STRING |
| 25 |
"Fortran module installation path (Not a cmake native variable)" |
| 26 |
) |
| 27 |
|
| 28 |
option(BUILD_SHARED_LIBS "Build shared rather than static library" ON) |
| 29 |
|
| 30 |
set(DEFAULT_BUILD_TYPE "Release") |
| 31 |
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git") |
| 32 |
set(DEFAULT_BUILD_TYPE "Debug") |
| 33 |
endif() |
| 34 |
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 35 |
message(STATUS "Setting build type to ${DEFAULT_BUILD_TYPE} as none was set") |
| 36 |
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}") |
| 37 |
endif() |
| 38 |
|
| 39 |
if(NOT TARGET LAPACK::LAPACK) |
| 40 |
find_package(LAPACK REQUIRED) |
| 41 |
if(CMAKE_VERSION VERSION_LESS 3.18) |
| 42 |
add_library(LAPACK::LAPACK INTERFACE IMPORTED) |
| 43 |
target_link_libraries(LAPACK::LAPACK INTERFACE ${LAPACK_LINKER_FLAGS} ${LAPACK_LIBRARIES}) |
| 44 |
endif() |
| 45 |
endif() |
| 46 |
|
| 47 |
if(ENABLE_SCALAPACK_MPI) |
| 48 |
if(NOT TARGET MPI::MPI_Fortran) |
| 49 |
find_package(MPI REQUIRED) |
| 50 |
if(APPLE) |
| 51 |
# -flat_namespace (Darwin-specific) is causing a crash (seg fault) when the |
| 52 |
# Fortran library is called from Python and one writes into a character |
| 53 |
# variable, but only when the kind is the default one. It causes the |
| 54 |
# written to variable to appear as being four times shorter than it is. |
| 55 |
# Only mention of anything possibly related I could find is at |
| 56 |
# |
| 57 |
# https://trac.mpich.org/projects/mpich/ticket/1590 |
| 58 |
get_target_property(_VALUE MPI::MPI_Fortran INTERFACE_LINK_OPTIONS) |
| 59 |
string(REGEX REPLACE "-Wl\\$<COMMA>-flat_namespace ?" "" _VALUE "${_VALUE}") |
| 60 |
set_target_properties(MPI::MPI_Fortran PROPERTIES INTERFACE_LINK_OPTIONS "${_VALUE}") |
| 61 |
endif() |
| 62 |
endif() |
| 63 |
if(ENABLE_MPIFH) |
| 64 |
message(STATUS "Will use the mpif.h Fortran MPI interface") |
| 65 |
elseif(MPI_Fortran_HAVE_F08_MODULE) |
| 66 |
message(STATUS "Will use the mpi_f08 Fortran module") |
| 67 |
endif() |
| 68 |
if(NOT TARGET scalapack AND NOT DEFINED SCALAPACK_LIBRARY) |
| 69 |
find_package(scalapack QUIET) |
| 70 |
if(scalapack_FOUND) |
| 71 |
message(STATUS "ScaLAPACK CMake package found in ${scalapack_DIR}") |
| 72 |
else() |
| 73 |
find_package(PkgConfig) |
| 74 |
if(PkgConfig_FOUND) |
| 75 |
unset(scalapack_FOUND CACHE) |
| 76 |
pkg_search_module(scalapack scalapack scalapack-openmpi) |
| 77 |
if(scalapack_FOUND) |
| 78 |
message(STATUS "ScaLAPACK pkg-config package found, version ${scalapack_VERSION}") |
| 79 |
set(SCALAPACK_LIBRARY "${scalapack_LDFLAGS}") |
| 80 |
endif() |
| 81 |
endif() |
| 82 |
endif() |
| 83 |
if(NOT scalapack_FOUND) |
| 84 |
message(SEND_ERROR "ScaLAPACK Cmake or pkg-config package not found, \ |
| 85 |
specify custom installation with SCALAPACK_LIBRARY") |
| 86 |
endif() |
| 87 |
endif() |
| 88 |
if(NOT TARGET scalapack AND DEFINED SCALAPACK_LIBRARY) |
| 89 |
add_library(scalapack INTERFACE IMPORTED) |
| 90 |
target_link_libraries(scalapack INTERFACE "${SCALAPACK_LIBRARY}") |
| 91 |
endif() |
| 92 |
endif() |
| 93 |
|
| 94 |
if(ENABLE_ELSI AND NOT TARGET elsi::elsi) |
| 95 |
find_package(elsi 2.0 QUIET) |
| 96 |
if(elsi_FOUND) |
| 97 |
message(STATUS "ELSI CMake package found in ${elsi_DIR}") |
| 98 |
else() |
| 99 |
find_package(PkgConfig) |
| 100 |
if(PkgConfig_FOUND) |
| 101 |
unset(elsi_FOUND CACHE) |
| 102 |
pkg_search_module(elsi elsi) |
| 103 |
if(elsi_FOUND) |
| 104 |
message(STATUS "ELSI pkg-config package found, version ${elsi_VERSION}") |
| 105 |
add_library(elsi::elsi INTERFACE IMPORTED) |
| 106 |
target_link_libraries(elsi::elsi INTERFACE "${elsi_LINK_LIBRARIES}") |
| 107 |
target_include_directories(elsi::elsi INTERFACE "${elsi_INCLUDE_DIRS}") |
| 108 |
endif() |
| 109 |
endif() |
| 110 |
endif() |
| 111 |
if(NOT elsi_FOUND) |
| 112 |
message(SEND_ERROR "ELSI Cmake or pkg-config package not found") |
| 113 |
endif() |
| 114 |
endif() |
| 115 |
|
| 116 |
add_subdirectory(src) |
| 117 |
|
| 118 |
if(BUILD_TESTING) |
| 119 |
add_subdirectory(tests) |
| 120 |
endif() |