Added pkg-config detection of libcp2k #3172
[alexxy/gromacs.git] / cmake / gmxManageCP2K.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2021, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 if(GMX_CP2K)
36     
37     # CMake flags for CP2K linking 
38     set(CP2K_DIR "" CACHE STRING "Path to the directory with libcp2k.a library")
39     set(CP2K_LINKER_FLAGS "" CACHE STRING "List of flags and libraries required for linking libcp2k. Typically this should be combination of LDFLAGS and LIBS variables from ARCH file used to compile CP2K")
40
41     # Check is CP2K_DIR present (this flags is required)
42     if (NOT CP2K_DIR)
43         message(FATAL_ERROR "To build GROMACS with CP2K Interface CP2K_DIR should be defined")
44     endif()
45
46     # if CP2K_LINKER_FLAGS defined then it should be used for linking instead pkg-config
47     if (CP2K_LINKER_FLAGS)
48         message(STATUS "CP2K_LINKER_FLAGS will be used to link libcp2k")
49
50         # Add directory with libcp2k.h into system include directories
51         include_directories(SYSTEM "${CP2K_DIR}/../../../src/start")
52
53         # Add libcp2k and DBCSR for linking 
54         set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -Wl,--allow-multiple-definition -L${CP2K_DIR} -lcp2k -L${CP2K_DIR}/exts/dbcsr -ldbcsr")
55
56         # Add User provided libraries
57         set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} ${CP2K_LINKER_FLAGS}")
58     else()
59         # In other case pkg-config should be used to search for libcp2k
60         find_package(PkgConfig QUIET)
61
62         # if pkg-config not found then ask user to provide CP2K_LINKER_FLAGS
63         if(NOT PKG_CONFIG_FOUND)
64             message(FATAL_ERROR "pkg-config not found, define CP2K_LINKER_FLAGS for custom linking of libcp2k")
65         endif()
66
67         # Append PKG_CONFIG_PATH_PATH with ${CP2K_DIR}/pkgconfig which should contain libcp2k.pc
68         set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${CP2K_DIR}/pkgconfig")
69  
70         # Search for libcp2k
71         pkg_check_modules(LIBCP2K QUIET libcp2k)
72
73         # if libcp2k not found then ask user to provide CP2K_LINKER_FLAGS
74         if(NOT LIBCP2K_FOUND)
75             message(FATAL_ERROR "pkg-config could not find libcp2k, define CP2K_LINKER_FLAGS for custom linking of libcp2k")
76         endif()
77
78         # libcp2k found: add libraries and paths to the respecting GROMACS variables
79         message(STATUS "Found libcp2k in ${LIBCP2K_LIBDIR}")
80         include_directories(SYSTEM "${LIBCP2K_INCLUDE_DIRS}")
81         link_directories(${LIBCP2K_LIBRARY_DIRS})
82         list(APPEND GMX_COMMON_LIBRARIES ${LIBCP2K_LIBRARIES})
83     endif()
84  
85     # If we use GNU compilers then also libgfortran should be linked
86     if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
87         list(APPEND GMX_COMMON_LIBRARIES "gfortran")
88     endif()
89
90     # If we use external MPI then Fortran MPI library should be linked
91     if (GMX_LIB_MPI)
92         # If Fortran MPI library is found then add it to GMX_COMMON_LIBRARIES
93         if (MPI_Fortran_FOUND)
94             list(APPEND GMX_COMMON_LIBRARIES ${MPI_Fortran_LIBRARIES})
95         else()
96             message(FATAL_ERROR "Could not find Fortran MPI library which is required for CP2K")
97         endif()
98     endif()
99
100 endif()