target_enable_petsc.cmake 5.02 KB
Newer Older
1
macro(target_enable_mpi _TARGET_ _SCOPE_)
2 3 4 5 6 7
    if (${ARGC} GREATER 2)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

8 9 10 11 12 13
    find_package(MPI REQUIRED)
    target_include_directories(${_TARGET_} ${_SCOPE_}
      ${MPI_CXX_INCLUDE_PATH})
    target_compile_options(${_TARGET_} ${_SCOPE_}
      ${MPI_CXX_COMPILE_FLAGS})
    string(STRIP " ${MPI_CXX_LINK_FLAGS} " MPI_CXX_LINK_FLAGS_)
14 15 16 17 18

    if (LINK_EXECUTABLE)
      target_link_libraries(${_TARGET_} ${_SCOPE_}
        ${MPI_CXX_LINK_FLAGS_} ${MPI_CXX_LIBRARIES})
    endif (LINK_EXECUTABLE)
19 20
endmacro(target_enable_mpi)

21

22 23 24 25 26 27 28 29
macro(target_enable_petsc _TARGET_ _SCOPE_)
    if (${ARGC} GREATER 2)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

    # PETSc library is required
30 31 32 33 34 35 36 37 38
    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
            /usr/lib/petsc/linux-gnu-c-opt/lib/pkgconfig)
    if (PETSC_PKG_CONFIG)
39 40 41 42
      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})
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    endif (PETSC_PKG_CONFIG)

    # check whether PETSc installation and configuration is valid
    if (NOT PETSC_INCLUDE_DIRS)
      set(PETSC_HEADER_FILE-NOTFOUND ON)
    else ()
      find_file(PETSC_HEADER_FILE "petsc.h" PATHS ${PETSC_INCLUDE_DIRS} NO_DEFAULT_PATH)
    endif (NOT PETSC_INCLUDE_DIRS)

    # if not valid, search again
    if (PETSC_HEADER_FILE-NOTFOUND)
      find_package(PETSc REQUIRED)
      set(PETSC_INCLUDE_DIRS ${PETSC_INCLUDES})
      set(PETSC_LIBRARY_DIRS ${PETSC_LIB_DIR})
      message(STATUS "  Found PETSc, version ${PETSC_VERSION}")
    endif (PETSC_HEADER_FILE-NOTFOUND)
59

60 61 62 63 64 65 66 67 68 69 70
    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})

    if (LINK_EXECUTABLE)
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${PETSC_LIBRARIES})
    endif (LINK_EXECUTABLE)


71 72
    # parmetis is required
    find_file(PARMETIS_HEADER_FILE "parmetis.h"
73
      HINTS ${PARMETIS_DIR}/include ${PETSC_INCLUDE_DIRS} /usr/include/parmetis)
74
    if (PARMETIS_HEADER_FILE)
75 76
      get_filename_component(PARMETIS_INCLUDE_PATH "${PARMETIS_HEADER_FILE}" PATH CACHE)
      target_include_directories(${_TARGET_} ${_SCOPE_} ${PARMETIS_INCLUDE_PATH})
77
    else()
78
      message(FATAL_ERROR "Could not find ParMetis header file 'parmetis.h'!")
79 80 81 82 83
    endif (PARMETIS_HEADER_FILE)

    if (LINK_EXECUTABLE)
      find_library(PARMETIS_LIB parmetis
        HINTS ${PARMETIS_DIR}/lib ${PETSC_LIBRARY_DIRS})
84
      if (NOT PARMETIS_LIB)
85
        message(FATAL_ERROR "Could not find the ParMetis libraries needed by amdis")
86
      endif (NOT PARMETIS_LIB)
87
      message(STATUS "  Found ParMetis library ${PARMETIS_LIB}")
88
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${PARMETIS_LIB})
89 90
    endif (LINK_EXECUTABLE)

91

92 93
    # metis is required
    find_file(METIS_HEADER_FILE "metis.h"
94
      HINTS ${METIS_DIR}/include ${PETSC_INCLUDE_DIRS} /usr/include/metis)
95
    if (METIS_HEADER_FILE)
96 97
      get_filename_component(METIS_INCLUDE_PATH "${METIS_HEADER_FILE}" PATH CACHE)
      target_include_directories(${_TARGET_} ${_SCOPE_} ${METIS_INCLUDE_PATH})
98
    else()
99
      message(FATAL_ERROR "Could not find Metis header file 'metis.h'!")
100 101 102 103
    endif (METIS_HEADER_FILE)

    if (LINK_EXECUTABLE)
      find_library(METIS_LIB metis
104
        HINTS ${METIS_DIR}/lib ${PETSC_LIBRARY_DIRS})
105
      if (NOT METIS_LIB)
106
        message(FATAL_ERROR "Could not find the Metis libraries needed by amdis")
107
      endif (NOT METIS_LIB)
108
      message(STATUS "  Found Metis library ${METIS_LIB}")
109
      target_link_libraries(${_TARGET_} ${_SCOPE_} ${METIS_LIB})
110 111
    endif (LINK_EXECUTABLE)

112

113
    # blas library is required
114 115 116 117 118 119 120 121 122 123 124
    if (LINK_EXECUTABLE)
      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})
    endif (LINK_EXECUTABLE)
125 126


127 128 129 130 131 132 133 134 135 136 137 138
    # lapack library is required
    if (LINK_EXECUTABLE)
      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})
    endif (LINK_EXECUTABLE)
139
endmacro(target_enable_petsc)