ccp4-config.cmake 11.1 KB
Newer Older
1 2
# - Find CCP4 libraries
# Find one or more of CCP4 libraries: ccp4c, ccp4f, mmdb, ccif,
3 4
# clipper-core, clipper-ccp4, clipper-contrib, clipper-minimol, clipper-mmdb,
# or cctbx.
5 6 7
#
# Once done this will define
#  CCP4_INCLUDE_DIRS - all include directories
8
#  <name>_LIBRARY - library, name is one of CCP4C, CCP4F, MMDB, MMDB2, CCIF,
9
#                            SSM, CCP4SRS,
10 11
#                            CLIPPER-CORE, CLIPPER-CCP4, CLIPPER-CONTRIB,
#                            CLIPPER-MINIMOL, CLIPPER-MMDB, CLIPPER-CIF,
12 13
#                            RFFTW2, FFTW2,
#                            HKLFILE, CCTBX
14
#  CCIF_LIBRARIES - CCIF_LIBRARY with (if needed) regex library
15
#  CCP4SRS_LIBRARIES - CCP4SRS_LIBRARY with (if needed) mmdb and zlib libraries
16 17 18 19 20
#  CLIPPER-CORE_LIBRARIES - CLIPPER-CORE_LIBRARY with (if needed)
#                           fftw2 and thread library
#  CCP4_LIBRARIES - all requested libraries with necessary dependencies
#  <name>_FOUND - TRUE if library and header files are found.
#
21 22 23 24
#  CCP4_PY2_DIR - when building the CCP4 suite, CCP4_PY2_DIR is set to
#                 preferred installation directory for Python2 modules.
#                 (defaults to ${LIB_INSTALL_DIR}/py2)
#
25 26 27 28 29
# You need to name libraries that you will use as components:
# FIND_PACKAGE(CCP4 COMPONENTS mmdb ccp4c)
# or
# FIND_PACKAGE(CCP4 REQUIRED mmdb ccp4c)
#
30 31
# Checking for ccp4srs in some cases (static linking) sets also ZLIB_LIBRARY.
#
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# Checking for clipper-core sets also variables for FFTW2 library used
# by clipper: FFTW2_LIBRARY, RFFTW2_LIBRARY, FFTW2_INCLUDE_DIRS,
# and if fftw2 is prefixed with 's' FFTW2_DEFINITIONS=-DFFTW2_PREFIX_S.
#
# Sample usage:
#   FIND_PACKAGE(CCP4 REQUIRED mmdb ccp4)
#   INCLUDE_DIRECTORIES(${CCP4_INCLUDE_DIRS})
#   TARGET_LINK_LIBRARIES(<YourTarget> ${CCP4_LIBRARIES})

if(NOT CCP4_FIND_COMPONENTS)
    message(FATAL_ERROR "Please specify CCP4 libraries/components.")
endif()

include(FindPackageHandleStandardArgs)
include(CheckFunctionExists)
include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)

50 51 52 53 54 55
set(_ccp4 $ENV{CCP4})
if(_ccp4)
    set(_clib "${_ccp4}/lib")
    set(_cincl "${_ccp4}/include")
endif()

56 57 58 59 60
foreach(_component ${CCP4_FIND_COMPONENTS})
    string(TOUPPER ${_component} _upper)
    string(TOLOWER ${_component} _lower)
    set(_lib_var "${_upper}_LIBRARY")
    find_library(${_lib_var} NAMES ${_lower}
61 62
                 HINTS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib
                 PATHS ${_clib})
63 64 65 66
    #message("DEBUG: ${_lib_var} (${_lower}) = ${${_upper}_LIBRARY}")

    if (${_upper} STREQUAL "MMDB")
        set(_header "mmdb/mmdb_defs.h")
67 68
    elseif (${_upper} STREQUAL "MMDB2")
        set(_header "mmdb2/mmdb_defs.h")
69 70 71 72 73 74 75 76
    elseif (${_upper} STREQUAL "CCP4C")
        set(_header "ccp4/ccp4_general.h")
    elseif (${_upper} STREQUAL "CCP4F")
        # fortran library, no headers
        set(_header)
    elseif (${_upper} STREQUAL "CCIF")
        set(_header "ccif/ccif_defines.h")
    elseif (${_upper} STREQUAL "SSM")
77 78 79
        set(_header "ssm/ssm_vxedge.h")
    elseif (${_upper} STREQUAL "CCP4SRS")
        set(_header "ccp4srs/ccp4srs_manager.h")
80 81 82 83 84 85 86 87 88 89 90 91 92
    elseif (${_upper} STREQUAL "CLIPPER-CORE")
        find_package(Threads)
        set(_header "clipper/clipper.h")
    elseif (${_upper} STREQUAL "CLIPPER-CCP4")
        set(_header "clipper/clipper-ccp4.h")
    elseif (${_upper} STREQUAL "CLIPPER-CONTRIB")
        set(_header "clipper/clipper-contrib.h")
    elseif (${_upper} STREQUAL "CLIPPER-MINIMOL")
        set(_header "clipper/clipper-minimol.h")
    elseif (${_upper} STREQUAL "CLIPPER-MMDB")
        set(_header "clipper/clipper-mmdb.h")
    elseif (${_upper} STREQUAL "CLIPPER-CIF")
        set(_header "clipper/clipper-cif.h")
93 94
    elseif (${_upper} STREQUAL "HKLFILE")
        set(_header "hklfile/columngroups.hh")
95 96
    elseif (${_upper} STREQUAL "CCTBX")
        set(_header "cctbx/crystal/symmetry.h")
97 98 99 100 101 102 103
    else()
        message(FATAL_ERROR "'${_component}' is not a valid CCP4 library.")
    endif()

    if (_header)
        set(_incl_var "${_upper}_INCLUDE_DIR")
        find_path(${_incl_var} ${_header}
104
                  HINTS ${CMAKE_INSTALL_PREFIX}/include
105
                  HINTS ${CMAKE_INSTALL_PREFIX}/include/cctbx
106
                  PATHS ${_cincl})
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        message(STATUS "Looking for ${_header} - ${${_incl_var}}")
        find_package_handle_standard_args(CCP4 DEFAULT_MSG
                                          ${_lib_var} ${_incl_var})
        set(CCP4_INCLUDE_DIRS ${CCP4_INCLUDE_DIRS} ${${_incl_var}})
    else()
        find_package_handle_standard_args(CCP4 DEFAULT_MSG
                                          ${_lib_var})
    endif()

    # check if libccif needs -lregex
    if (${_upper} STREQUAL "CCIF")
        set(_CCIF_SRC "int main() {zzs_undump(0,0,0);}")
        foreach (_REGEX_LIB "" regex)
            message (STATUS "Trying _REGEX_LIB=${_REGEX_LIB}")
            set(CMAKE_REQUIRED_LIBRARIES ${CCIF_LIBRARY} ${_REGEX_LIB} m)
            set(_VAR _LINKING_WITH_CCIF_${_REGEX_LIB})
            check_c_source_compiles("${_CCIF_SRC}" ${_VAR})
            if (${_VAR})
                set(CCIF_LIBRARIES ${CCIF_LIBRARY} ${_REGEX_LIB} m)
                break()
            endif()
        endforeach()
        if (NOT ${_VAR})
            message(FATAL_ERROR "Linking with libccif failed.")
        endif()
    endif()

134 135 136 137 138
    # check if libccp4srs needs mmdb and zlib
    if (${_upper} STREQUAL "CCP4SRS")
        set(_SAVE ${CMAKE_REQUIRED_INCLUDES})
        set(CMAKE_REQUIRED_INCLUDES "${_SAVE};${CCP4SRS_INCLUDE_DIR}")
        set(_SRS_SRC "#include <ccp4srs/memio_.h>\n"
139
                      "int main() { ccp4srs::MemIO m; m.read(\"f\");}")
140 141 142 143 144 145 146 147 148 149 150 151
        set(CMAKE_REQUIRED_LIBRARIES ${CCP4SRS_LIBRARY})
        set(_VAR _LINKING_WITH_CCP4SRS)
        check_cxx_source_compiles("${_SRS_SRC}" ${_VAR})
        if (${_VAR})
            set(CCP4SRS_LIBRARIES ${CCP4SRS_LIBRARY})
        else()
            find_library(ZLIB_LIBRARY NAMES z zlib zlib1 zdll
                 HINTS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib
                 PATHS ${_clib} c:/MinGW/lib)
            if (NOT ZLIB_LIBRARY)
             message(FATAL_ERROR "Linking with ccp4srs fails. zlib not found.")
            endif()
152 153
            if (NOT MMDB2_LIBRARY)
                find_library(MMDB2_LIBRARY NAMES mmdb2
154 155 156
                         HINTS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib
                         PATHS ${_clib})
            endif()
157
            set(_ADD_LIBS ${MMDB2_LIBRARY} ${ZLIB_LIBRARY})
158 159 160 161 162 163 164 165 166 167 168
            set(CMAKE_REQUIRED_LIBRARIES ${CCP4SRS_LIBRARY} ${_ADD_LIBS})
            set(_VAR _LINKING_WITH_CCP4SRS_MMDB_AND_ZLIB)
            check_cxx_source_compiles("${_SRS_SRC}" ${_VAR})
            if (NOT ${_VAR})
                message(FATAL_ERROR "Linking with ccp4srs failed.")
            endif()
            set(CCP4SRS_LIBRARIES ${CCP4SRS_LIBRARY} ${_ADD_LIBS})
        endif()
        SET(CMAKE_REQUIRED_INCLUDES ${_SAVE})
    endif()

169 170
    # check if clipper-core needs rfftw fftw
    if (${_upper} STREQUAL "CLIPPER-CORE")
171 172 173 174 175 176 177 178 179 180 181 182
        set(_fftw_names sfftw fftw)
        if (${${_lib_var}} MATCHES ".a$")
            # fftw should also be static
            set(_fftw_names libsfftw.a libfftw.a ${_fftw_names})
        endif()
        # first search in the install path, then in system and $CCP4
        find_library(FFTW2_LIBRARY NAMES ${_fftw_names}
                     HINTS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib
                     NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
        find_library(FFTW2_LIBRARY NAMES ${_fftw_names}
                     PATHS ${_clib})
        message(STATUS "FFTW2 libraries - ${FFTW2_LIBRARY}")
183
        if (${FFTW2_LIBRARY} MATCHES "sfftw")
184
            set (_fftw_prefix s)
185
            set(FFTW2_DEFINITIONS "-DFFTW2_PREFIX_S")
186 187 188 189
        endif()
        set(_rfftw_names ${_fftw_prefix}rfftw)
        if (${FFTW2_LIBRARY} MATCHES ".a$")
            set(_rfftw_names lib${_fftw_prefix}rfftw.a ${_rfftw_names})
190 191
        else()
        endif()
192 193 194 195 196 197 198 199
        find_library(RFFTW2_LIBRARY NAMES ${_rfftw_names}
                     HINTS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib
                     PATHS ${_clib})
        message(STATUS "                - ${RFFTW2_LIBRARY}")
        find_path(FFTW2_INCLUDE_DIRS ${_fftw_prefix}rfftw.h
                  HINTS ${CMAKE_INSTALL_PREFIX}/include
                  PATHS ${_cincl})
        message(STATUS "FFTW2 header directory - ${FFTW2_INCLUDE_DIRS}")
200 201 202 203 204 205 206 207 208 209 210 211 212 213

        set(_SAVE ${CMAKE_REQUIRED_INCLUDES})
        set(CMAKE_REQUIRED_INCLUDES "${_SAVE};${CLIPPER-CORE_INCLUDE_DIR}")
        set(_CLIP_SRC "#include <clipper/clipper.h>\n"
                      "int main() {clipper::FFTmap().fft_h_to_x();}")
            set(CMAKE_REQUIRED_LIBRARIES ${CLIPPER-CORE_LIBRARY})
            set(_VAR _LINKING_WITH_CLIPPER_CORE)
            check_cxx_source_compiles("${_CLIP_SRC}" ${_VAR})
        if (NOT ${_VAR})
            set(_ADD_LIBS ${CMAKE_THREAD_LIBS_INIT})
            set(CMAKE_REQUIRED_LIBRARIES ${CLIPPER-CORE_LIBRARY} ${_ADD_LIBS})
            set(_VAR _LINKING_WITH_CLIPPER_CORE_AND_THREADS)
            check_cxx_source_compiles("${_CLIP_SRC}" ${_VAR})
        endif()
214
        if ((NOT ${_VAR}) AND FFTW2_LIBRARY)
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
            set(_ADD_LIBS ${RFFTW2_LIBRARY} ${FFTW2_LIBRARY}
                          ${CMAKE_THREAD_LIBS_INIT})
            set(CMAKE_REQUIRED_LIBRARIES ${CLIPPER-CORE_LIBRARY} ${_ADD_LIBS})
            set(_VAR _LINKING_WITH_CLIPPER_CORE_AND_FFTW)
            check_cxx_source_compiles("${_CLIP_SRC}" ${_VAR})
        endif()
        if (NOT ${_VAR})
            message(FATAL_ERROR "Linking with clipper-core failed.")
        endif()
        set(CLIPPER-CORE_LIBRARIES ${CLIPPER-CORE_LIBRARY} ${_ADD_LIBS})
        SET(CMAKE_REQUIRED_INCLUDES ${_SAVE})
    endif()
endforeach()


230
set(CCP4_LIBRARIES ${CCTBX_LIBRARY}
231
                   ${HKLFILE_LIBRARY}
232
                   ${CLIPPER-CCP4_LIBRARY}
233
                   ${CLIPPER-CIF_LIBRARY}
234 235 236 237
                   ${CLIPPER-CONTRIB_LIBRARY}
                   ${CLIPPER-MINIMOL_LIBRARY}
                   ${CLIPPER-MMDB_LIBRARY}
                   ${CLIPPER-CORE_LIBRARIES}
238
                   ${CCP4SRS_LIBRARIES}
239 240 241 242
                   ${SSM_LIBRARY}
                   ${CCIF_LIBRARIES}
                   ${CCP4F_LIBRARY}
                   ${CCP4C_LIBRARY}
243
                   ${MMDB2_LIBRARY}
244 245 246 247 248 249 250
                   ${MMDB_LIBRARY})

if(CCP4_INCLUDE_DIRS)
    list(REMOVE_DUPLICATES CCP4_INCLUDE_DIRS)
endif()
message(STATUS "CCP4 include directory: ${CCP4_INCLUDE_DIRS}")

251 252 253 254 255 256 257
if(CCP4_LIBRARIES)
    # reverse twice, because REMOVE_DUPLICATES leaves the first
    list(REVERSE CCP4_LIBRARIES)
    list(REMOVE_DUPLICATES CCP4_LIBRARIES)
    list(REVERSE CCP4_LIBRARIES)
endif()

258 259 260 261 262 263 264
if(DEFINED LIB_INSTALL_DIR)
    set(CCP4_PY2_DIR ${LIB_INSTALL_DIR}/py2)
elseif(DEFINED CMAKE_INSTALL_PREFIX)
    set(CCP4_PY2_DIR ${CMAKE_INSTALL_PREFIX}/lib/py2)
endif()

mark_as_advanced(CCP4C_INCLUDE_DIR MMDB_INCLUDE_DIR MMDB2_INCLUDE_DIR
265 266
                 CCIF_INCLUDE_DIR SSM_INCLUDE_DIR
                 CCP4SRS_INCLUDE_DIR
267
                 CLIPPER-CORE_INCLUDE_DIR CLIPPER-CCP4_INCLUDE_DIR
268
                 CLIPPER-CIF_INCLUDE_DIR
269
                 CLIPPER-CONTRIB_INCLUDE_DIR CLIPPER-MINIMOL_INCLUDE_DIR
270
                 CLIPPER-MMDB_INCLUDE_DIR FFTW2_INCLUDE_DIRS
271
                 CCTBX_INCLUDE_DIR HKLFILE_INCLUDE_DIR CCP4_PY2_DIR)
272