File: ecbuild_find_mpi.cmake

package info (click to toggle)
metview 5.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 242,296 kB
  • sloc: cpp: 437,117; ansic: 41,433; xml: 19,944; f90: 13,059; sh: 6,562; python: 3,953; yacc: 1,774; lex: 1,121; perl: 701; makefile: 92
file content (256 lines) | stat: -rw-r--r-- 7,514 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# (C) Copyright 2011- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

##############################################################################
#.rst:
#
# ecbuild_find_mpi
# ================
#
# Find MPI and check if MPI compilers successfully compile C/C++/Fortran. ::
#
#   ecbuild_find_mpi( [ COMPONENTS <component1> [ <component2> ... ] ]
#                     [ REQUIRED ] )
#
# Options
# -------
#
# COMPONENTS : optional, defaults to C
#   list of required languages bindings
#
# REQUIRED : optional
#   fail if MPI was not found
#
# Input variables
# ---------------
#
# ECBUILD_FIND_MPI : optional, defaults to TRUE
#   test C/C++/Fortran MPI compiler wrappers (assume working if FALSE)
#
# Output variables
# ----------------
#
# The following CMake variables are set if MPI was found: ::
#
#   MPI_FOUND
#   MPI_LIBRARY
#   MPI_EXTRA_LIBRARY
#
# The following CMake variables are set if C bindings were found: ::
#
#   MPI_C_FOUND
#   MPI_C_COMPILER
#   MPI_C_COMPILE_FLAGS
#   MPI_C_INCLUDE_PATH
#   MPI_C_LIBRARIES
#   MPI_C_LINK_FLAGS
#
# The following CMake variables are set if C++ bindings were found: ::
#
#   MPI_CXX_FOUND
#   MPI_CXX_COMPILER
#   MPI_CXX_COMPILE_FLAGS
#   MPI_CXX_INCLUDE_PATH
#   MPI_CXX_LIBRARIES
#   MPI_CXX_LINK_FLAGS
#
# The following CMake variables are set if Fortran bindings were found: ::
#
#   MPI_Fortran_FOUND
#   MPI_Fortran_COMPILER
#   MPI_Fortran_COMPILE_FLAGS
#   MPI_Fortran_INCLUDE_PATH
#   MPI_Fortran_LIBRARIES
#   MPI_Fortran_LINK_FLAGS
#
##############################################################################

macro( ecbuild_find_mpi )

    # parse parameters

    set( options REQUIRED )
    set( single_value_args )
    set( multi_value_args COMPONENTS )

    cmake_parse_arguments( _PAR "${options}" "${single_value_args}" "${multi_value_args}"  ${_FIRST_ARG} ${ARGN} )

    if(_PAR_UNPARSED_ARGUMENTS)
      ecbuild_critical("Unknown keywords given to ecbuild_find_mpi(): \"${_PAR_UNPARSED_ARGUMENTS}\"")
    endif()

    if( NOT _PAR_COMPONENTS )
      set( _PAR_COMPONENTS C )
    endif()

    # if user defined compilers are MPI compliant, then we use them ...
    if( NOT DEFINED ECBUILD_FIND_MPI )
      set( ECBUILD_FIND_MPI TRUE )
    endif()
    if( ECBUILD_FIND_MPI )

        if( NOT _PAR_REQUIRED )
            find_package( MPI COMPONENTS ${_PAR_COMPONENTS} )
        else()
            find_package( MPI REQUIRED COMPONENTS ${_PAR_COMPONENTS} )
        endif()

        if( C_COMPILER_SUPPORTS_MPI )
            set( MPI_C_FOUND TRUE )
        endif()
        if( CXX_COMPILER_SUPPORTS_MPI )
            set( MPI_CXX_FOUND TRUE )
        endif()
        if( Fortran_COMPILER_SUPPORTS_MPI )
            set( MPI_Fortran_FOUND TRUE )
        endif()

    else()

        if( CMAKE_C_COMPILER_LOADED )
            set( C_COMPILER_SUPPORTS_MPI TRUE )
            set( MPI_C_FOUND TRUE )
        endif()
        if( CMAKE_CXX_COMPILER_LOADED )
            set( CXX_COMPILER_SUPPORTS_MPI TRUE )
            set( MPI_CXX_FOUND TRUE )
        endif()
        if( CMAKE_Fortran_COMPILER_LOADED )
            set( Fortran_COMPILER_SUPPORTS_MPI TRUE )
            set( MPI_Fortran_FOUND TRUE )
        endif()

    endif( ECBUILD_FIND_MPI )

    # hide these variables from UI

    mark_as_advanced( MPI_LIBRARY MPI_EXTRA_LIBRARY )

    set( MPI_FOUND TRUE )
    foreach( _lang ${_PAR_COMPONENTS} )
      if( NOT MPI_${_lang}_FOUND )
        set( MPI_FOUND FALSE )
      endif()
      if( NOT MPI_${_lang}_INCLUDE_DIRS )
        set( MPI_${_lang}_INCLUDE_DIRS ${MPI_${_lang}_INCLUDE_PATH} )
      endif()
    endforeach()

endmacro( ecbuild_find_mpi )

##############################################################################
#.rst:
#
# ecbuild_enable_mpi
# ==================
#
# Find MPI, add include directories and set compiler flags. ::
#
#   ecbuild_enable_mpi( [ COMPONENTS <component1> [ <component2> ... ] ]
#                       [ REQUIRED ] )
#
# For each MPI language binding found, set the corresponding compiler flags
# and add the include directories.
#
# See ``ecbuild_find_mpi`` for input and output variables.
#
# Options
# -------
#
# COMPONENTS : optional, defaults to C
#   list of required languages bindings
#
# REQUIRED : optional
#   fail if MPI was not found
#
##############################################################################

macro( ecbuild_enable_mpi )

    set( options REQUIRED )
    set( single_value_args )
    set( multi_value_args COMPONENTS )

    cmake_parse_arguments( _PAR "${options}" "${single_value_args}" "${multi_value_args}"  ${_FIRST_ARG} ${ARGN} )

    if(_PAR_UNPARSED_ARGUMENTS)
        ecbuild_critical("Unknown keywords given to ecbuild_find_mpi(): \"${_PAR_UNPARSED_ARGUMENTS}\"")
    endif()

    if( NOT _PAR_COMPONENTS )
      set (_PAR_COMPONENTS C )
    endif()

    if( NOT _PAR_REQUIRED )
       ecbuild_find_mpi( COMPONENTS ${_PAR_COMPONENTS} )
    else()
       ecbuild_find_mpi( COMPONENTS ${_PAR_COMPONENTS} REQUIRED )
    endif()

    if( MPI_C_FOUND AND NOT C_COMPILER_SUPPORTS_MPI )
        ecbuild_add_c_flags("${MPI_C_COMPILE_FLAGS}")
        include_directories(${MPI_C_INCLUDE_DIRS})
    endif()

    if( MPI_CXX_FOUND AND NOT CXX_COMPILER_SUPPORTS_MPI )
        ecbuild_add_cxx_flags("${MPI_CXX_COMPILE_FLAGS}")
        include_directories(${MPI_CXX_INCLUDE_DIRS})
    endif()

    if( MPI_Fortran_FOUND AND NOT Fortran_COMPILER_SUPPORTS_MPI )
        include( ecbuild_check_fortran_source_return )
        ecbuild_add_fortran_flags("${MPI_Fortran_COMPILE_FLAGS}")
        include_directories(${MPI_Fortran_INCLUDE_DIRS})
    endif()

endmacro( ecbuild_enable_mpi )

##############################################################################
#.rst:
#
# ecbuild_include_mpi
# ===================
#
# Add MPI include directories and set compiler flags, assuming MPI was found.
#
# For each MPI language binding found, set corresponding compiler flags and
# add include directories. ``ecbuild_find_mpi`` must have been called before.
#
##############################################################################

macro( ecbuild_include_mpi )

    set( options )
    set( single_value_args )
    set( multi_value_args )

    cmake_parse_arguments( _PAR "${options}" "${single_value_args}" "${multi_value_args}"  ${_FIRST_ARG} ${ARGN} )

    if(_PAR_UNPARSED_ARGUMENTS)
        ecbuild_critical("Unknown keywords given to ecbuild_find_mpi(): \"${_PAR_UNPARSED_ARGUMENTS}\"")
    endif()

    if( MPI_C_FOUND AND NOT C_COMPILER_SUPPORTS_MPI )
        include( ecbuild_check_c_source_return )
        ecbuild_add_c_flags("${MPI_C_COMPILE_FLAGS}")
        include_directories(${MPI_C_INCLUDE_DIRS})
    endif()

    if( MPI_CXX_FOUND AND NOT CXX_COMPILER_SUPPORTS_MPI )
        include( ecbuild_check_cxx_source_return )
        ecbuild_add_cxx_flags("${MPI_CXX_COMPILE_FLAGS}")
        include_directories(${MPI_CXX_INCLUDE_DIRS})
    endif()

    if( MPI_Fortran_FOUND AND NOT Fortran_COMPILER_SUPPORTS_MPI )
        include( ecbuild_check_fortran_source_return )
        ecbuild_add_fortran_flags("${MPI_Fortran_COMPILE_FLAGS}")
        include_directories(${MPI_Fortran_INCLUDE_DIRS})
    endif()

endmacro( ecbuild_include_mpi )