enable_petsc.cmake 4.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include(FindPkgConfig)

macro(enable_mpi _FLAGS_ _INC_DIRS_ _LIBS_)
    if (${ARGC} GREATER 3)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

    find_package(MPI REQUIRED)
    list(APPEND ${_INC_DIRS_} ${MPI_CXX_INCLUDE_PATH})
    list(APPEND ${_FLAGS_}    ${MPI_CXX_COMPILE_FLAGS})

    if (LINK_EXECUTABLE)
      string(STRIP " ${MPI_CXX_LINK_FLAGS} " MPI_CXX_LINK_FLAGS_)
16
      list(APPEND ${_FLAGS_} ${MPI_CXX_LINK_FLAGS_})
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      list(APPEND ${_LIBS_}  ${MPI_CXX_LIBRARIES})
    endif (LINK_EXECUTABLE)
endmacro(enable_mpi)


macro(enable_petsc _FLAGS_ _INC_DIRS_ _LIBS_)
    if (${ARGC} GREATER 3)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

    # PETSc library is required
    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)
      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})
    endif (PETSC_PKG_CONFIG)

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

    # if not valid, search again
    if (NOT PETSC_VALID)
      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 (NOT PETSC_VALID)

    list(APPEND ${_INC_DIRS_} ${PETSC_INCLUDE_DIRS})
    list(APPEND ${_FLAGS_} "-DHAVE_PARALLEL_PETSC=1" "-DPETSC_VERSION=${PETSC_VERSION}" ${PETSC_DEFINITIONS})

    if (LINK_EXECUTABLE)
69
      link_directories(${PETSC_LIBRARY_DIRS})
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
      list(APPEND ${_LIBS_} ${PETSC_LIBRARIES})
    endif (LINK_EXECUTABLE)


    # parmetis is required
    find_file(PARMETIS_HEADER_FILE "parmetis.h"
      HINTS ${PARMETIS_DIR}/include ${PETSC_INCLUDE_DIRS} /usr/include/parmetis)
    if (PARMETIS_HEADER_FILE)
      get_filename_component(PARMETIS_INCLUDE_PATH "${PARMETIS_HEADER_FILE}" PATH CACHE)
      list(APPEND ${_INC_DIRS_} ${PARMETIS_INCLUDE_PATH})
    else()
      message(FATAL_ERROR "Could not find ParMetis header file 'parmetis.h'!")
    endif (PARMETIS_HEADER_FILE)

    if (LINK_EXECUTABLE)
      find_library(PARMETIS_LIB parmetis
        HINTS ${PARMETIS_DIR}/lib ${PETSC_LIBRARY_DIRS})
      if (NOT PARMETIS_LIB)
        message(FATAL_ERROR "Could not find the ParMetis libraries needed by amdis")
      endif (NOT PARMETIS_LIB)
      message(STATUS "  Found ParMetis library ${PARMETIS_LIB}")
      list(APPEND ${_LIBS_} ${PARMETIS_LIB})
    endif (LINK_EXECUTABLE)


    # metis is required
    find_file(METIS_HEADER_FILE "metis.h"
      HINTS ${METIS_DIR}/include ${PETSC_INCLUDE_DIRS} /usr/include/metis)
    if (METIS_HEADER_FILE)
      get_filename_component(METIS_INCLUDE_PATH "${METIS_HEADER_FILE}" PATH CACHE)
      list(APPEND ${_INC_DIRS_} ${METIS_INCLUDE_PATH})
    else()
      message(FATAL_ERROR "Could not find Metis header file 'metis.h'!")
    endif (METIS_HEADER_FILE)

    if (LINK_EXECUTABLE)
      find_library(METIS_LIB metis
        HINTS ${METIS_DIR}/lib ${PETSC_LIBRARY_DIRS})
      if (NOT METIS_LIB)
        message(FATAL_ERROR "Could not find the Metis libraries needed by amdis")
      endif (NOT METIS_LIB)
      message(STATUS "  Found Metis library ${METIS_LIB}")
      list(APPEND ${_LIBS_} ${METIS_LIB})
    endif (LINK_EXECUTABLE)


    # blas library is required
    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)
      list(APPEND ${_LIBS_} ${BLAS_LIBRARIES})
    endif (LINK_EXECUTABLE)


    # 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)
      list(APPEND ${_LIBS_} ${LAPACK_LIBRARIES})
    endif (LINK_EXECUTABLE)
endmacro(enable_petsc)