target_enable_petsc.cmake 6.77 KB
Newer Older
1 2
include(FindPkgConfig)

3
macro(target_enable_mpi _TARGET_ _SCOPE_)
4 5 6 7 8 9
    if (${ARGC} GREATER 2)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

10 11 12 13 14
    find_package(MPI REQUIRED)
    target_include_directories(${_TARGET_} ${_SCOPE_}
      ${MPI_CXX_INCLUDE_PATH})
    target_compile_options(${_TARGET_} ${_SCOPE_}
      ${MPI_CXX_COMPILE_FLAGS})
15

16
    if (LINK_EXECUTABLE OR APPLE)
17 18 19 20 21 22 23
      # the parameter --enable-new-dtags causes a linker problem, i.e. some libraries are
      # linked without an rpath (or any other path information) and can not be found while
      # running the executable. The hack below removes this flag manually from the linker flags.
      string(REPLACE "-Wl,--enable-new-dtags" "" MY_MPI_CXX_LINK_FLAGS " ${MPI_CXX_LINK_FLAGS} ")
      string(STRIP "${MY_MPI_CXX_LINK_FLAGS}" MY_MPI_CXX_LINK_FLAGS)

      target_link_libraries(${_TARGET_} ${_SCOPE_} ${MPI_CXX_LIBRARIES} ${MY_MPI_CXX_LINK_FLAGS})
24
    endif (LINK_EXECUTABLE OR APPLE)
25 26 27 28 29 30 31 32 33 34
endmacro(target_enable_mpi)

macro(target_enable_petsc _TARGET_ _SCOPE_)
    if (${ARGC} GREATER 2)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

    # PETSc library is required
35 36 37 38 39 40 41
    find_file(PETSC_PKG_CONFIG "PETSc.pc"
      HINTS ${PETSC_DIR}/lib/pkgconfig
            $ENV{PETSC_DIR}/lib/pkgconfig
            ${PETSC_ROOT}/lib/pkgconfig
            $ENV{PETSC_ROOT}/lib/pkgconfig
            /usr/lib/petsc/lib/pkgconfig
            /usr/lib/petsc/linux-gnu-cxx-opt/lib/pkgconfig
42 43
            /usr/lib/petsc/linux-gnu-c-opt/lib/pkgconfig
      PATHS $ENV{PKG_CONFIG_PATH})
44
    if (PETSC_PKG_CONFIG)
45 46 47 48
      get_filename_component(PETSC_PKG_CONFIG_PATH "${PETSC_PKG_CONFIG}" PATH CACHE)
      set(ENV{PKG_CONFIG_PATH} ${PETSC_PKG_CONFIG_PATH})
      pkg_check_modules(PETSC REQUIRED PETSc>=3.3)
      set(PETSC_DEFINITIONS ${PETSC_CFLAGS} ${PETSC_CFLAGS_OTHER})
49 50 51 52
    endif (PETSC_PKG_CONFIG)

    # check whether PETSc installation and configuration is valid
    if (NOT PETSC_INCLUDE_DIRS)
53
      set(PETSC_VALID OFF)
54 55
    else ()
      find_file(PETSC_HEADER_FILE "petsc.h" PATHS ${PETSC_INCLUDE_DIRS} NO_DEFAULT_PATH)
56 57 58 59 60
      if (PETSC_HEADER_FILE)
        set(PETSC_VALID ON)
      else ()
        set(PETSC_VALID OFF)
      endif ()
61 62 63
    endif (NOT PETSC_INCLUDE_DIRS)

    # if not valid, search again
64
    if (NOT PETSC_VALID)
65 66
      message(WARNING "  ---> pkg-config of PETSc is broken!")
      find_package(PETSc REQUIRED CXX)
67 68 69
      set(PETSC_INCLUDE_DIRS ${PETSC_INCLUDES})
      set(PETSC_LIBRARY_DIRS ${PETSC_LIB_DIR})
      message(STATUS "  Found PETSc, version ${PETSC_VERSION}")
70
    endif (NOT PETSC_VALID)
71

72 73 74 75 76 77
    target_include_directories(${_TARGET_} ${_SCOPE_} ${PETSC_INCLUDE_DIRS})
    target_compile_definitions(${_TARGET_} ${_SCOPE_}
        HAVE_PARALLEL_PETSC=1
        PETSC_VERSION=${PETSC_VERSION})
    target_compile_options(${_TARGET_} ${_SCOPE_} ${PETSC_DEFINITIONS})

78
    if (LINK_EXECUTABLE OR APPLE)
79 80 81 82 83 84 85
      set(PETSC_LINK_LIBRARIES "")
      foreach (_PETSC_LIB ${PETSC_LIBRARIES})
        find_library(_PETSC_LINK_LIB ${_PETSC_LIB} PATHS ${PETSC_LIBRARY_DIRS} NO_DEFAULT_PATH)
        list(APPEND PETSC_LINK_LIBRARIES ${_PETSC_LINK_LIB})
      endforeach()
      message(STATUS "  Found PETSc library ${PETSC_LINK_LIBRARIES}")
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${PETSC_LINK_LIBRARIES})
86
    endif (LINK_EXECUTABLE OR APPLE)
87 88


89 90
    # parmetis is required
    find_file(PARMETIS_HEADER_FILE "parmetis.h"
91
      HINTS ${PARMETIS_DIR}/include ${PETSC_INCLUDE_DIRS} /usr/include/parmetis)
92
    if (PARMETIS_HEADER_FILE)
93 94
      get_filename_component(PARMETIS_INCLUDE_PATH "${PARMETIS_HEADER_FILE}" PATH CACHE)
      target_include_directories(${_TARGET_} ${_SCOPE_} ${PARMETIS_INCLUDE_PATH})
95 96 97 98 99 100 101 102 103
      file(READ ${PARMETIS_HEADER_FILE} PARMETIS_HEADER_FILE_CONTENT)

      string(FIND "${PARMETIS_HEADER_FILE_CONTENT}" "ParMETIS_V3_PartKway"       PARMETIS_PARTKWAY_VALID)
      string(FIND "${PARMETIS_HEADER_FILE_CONTENT}" "ParMETIS_V3_Mesh2Dual"      PARMETIS_MESH2DUAL_VALID)
      string(FIND "${PARMETIS_HEADER_FILE_CONTENT}" "ParMETIS_V3_AdaptiveRepart" PARMETIS_ADAPTIVEREPART_VALID)
      string(FIND "${PARMETIS_HEADER_FILE_CONTENT}" "ParMETIS_V3_RefineKway"     PARMETIS_REFINEKWAYVALID)
      if (NOT (PARMETIS_PARTKWAY_VALID AND PARMETIS_MESH2DUAL_VALID AND PARMETIS_ADAPTIVEREPART_VALID AND PARMETIS_REFINEKWAYVALID))
        message(FATAL_ERROR "ParMETIS installation not complete. Not all required symbols found in ${PARMETIS_HEADER_FILE}.")
      endif ()
104
    else()
105
      message(FATAL_ERROR "Could not find ParMetis header file 'parmetis.h'!")
106 107
    endif (PARMETIS_HEADER_FILE)

108
    if (LINK_EXECUTABLE OR APPLE)
109 110
      find_library(PARMETIS_LIB parmetis
        HINTS ${PARMETIS_DIR}/lib ${PETSC_LIBRARY_DIRS})
111
      if (NOT PARMETIS_LIB)
112
        message(FATAL_ERROR "Could not find the ParMetis libraries needed by amdis")
113
      endif (NOT PARMETIS_LIB)
114
      message(STATUS "  Found ParMetis library ${PARMETIS_LIB}")
115
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${PARMETIS_LIB})
116
    endif (LINK_EXECUTABLE OR APPLE)
117

118

119 120
    # metis is required
    find_file(METIS_HEADER_FILE "metis.h"
121
      HINTS ${METIS_DIR}/include ${PETSC_INCLUDE_DIRS} /usr/include/metis)
122
    if (METIS_HEADER_FILE)
123 124
      get_filename_component(METIS_INCLUDE_PATH "${METIS_HEADER_FILE}" PATH CACHE)
      target_include_directories(${_TARGET_} ${_SCOPE_} ${METIS_INCLUDE_PATH})
125
    else()
126
      message(FATAL_ERROR "Could not find Metis header file 'metis.h'!")
127 128
    endif (METIS_HEADER_FILE)

129
    if (LINK_EXECUTABLE OR APPLE)
130
      find_library(METIS_LIB metis
131
        HINTS ${METIS_DIR}/lib ${PETSC_LIBRARY_DIRS})
132
      if (NOT METIS_LIB)
133
        message(FATAL_ERROR "Could not find the Metis libraries needed by amdis")
134
      endif (NOT METIS_LIB)
135
      message(STATUS "  Found Metis library ${METIS_LIB}")
136
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${METIS_LIB})
137
    endif (LINK_EXECUTABLE OR APPLE)
138

139

140
    # blas library is required
141
    if (LINK_EXECUTABLE OR APPLE)
142 143 144 145 146 147 148 149 150
      find_library(BLAS_LIB
        NAMES blas fblas openblas
        HINTS ${BLAS_DIR}/lib ${PETSC_LIBRARY_DIRS} /usr/lib/openblas-base /usr/lib/atlas-base)
      if (BLAS_LIB)
        set(BLAS_LIBRARIES ${BLAS_LIB})
      else (BLAS_LIB)
        find_package(BLAS REQUIRED)
      endif (BLAS_LIB)
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${BLAS_LIBRARIES})
151
    endif (LINK_EXECUTABLE OR APPLE)
152 153


154
    # lapack library is required
155
    if (LINK_EXECUTABLE OR APPLE)
156 157 158 159 160 161 162 163 164
      find_library(LAPACK_LIB
        NAMES lapack flapack
        HINTS ${LAPACK_DIR}/lib ${PETSC_LIBRARY_DIRS} ${PETSC_DIR}/lib)
      if (LAPACK_LIB)
        set(LAPACK_LIBRARIES ${LAPACK_LIB})
      else (LAPACK_LIB)
        find_package(LAPACK REQUIRED)
      endif (LAPACK_LIB)
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${LAPACK_LIBRARIES})
165
    endif (LINK_EXECUTABLE OR APPLE)
166
endmacro(target_enable_petsc)