1# Copyright 2013-2019 The Meson development team
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6
7#     http://www.apache.org/licenses/LICENSE-2.0
8
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import functools
16import typing as T
17
18from .base import DependencyMethods, detect_compiler, SystemDependency
19from .cmake import CMakeDependency
20from .pkgconfig import PkgConfigDependency
21from .factory import factory_methods
22
23if T.TYPE_CHECKING:
24    from . factory import DependencyGenerator
25    from ..environment import Environment, MachineChoice
26
27
28@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE, DependencyMethods.SYSTEM})
29def coarray_factory(env: 'Environment',
30                    for_machine: 'MachineChoice',
31                    kwargs: T.Dict[str, T.Any],
32                    methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']:
33    fcid = detect_compiler('coarray', env, for_machine, 'fortran').get_id()
34    candidates: T.List['DependencyGenerator'] = []
35
36    if fcid == 'gcc':
37        # OpenCoarrays is the most commonly used method for Fortran Coarray with GCC
38        if DependencyMethods.PKGCONFIG in methods:
39            for pkg in ['caf-openmpi', 'caf']:
40                candidates.append(functools.partial(
41                    PkgConfigDependency, pkg, env, kwargs, language='fortran'))
42
43        if DependencyMethods.CMAKE in methods:
44            if 'modules' not in kwargs:
45                kwargs['modules'] = 'OpenCoarrays::caf_mpi'
46            candidates.append(functools.partial(
47                CMakeDependency, 'OpenCoarrays', env, kwargs, language='fortran'))
48
49    if DependencyMethods.SYSTEM in methods:
50        candidates.append(functools.partial(CoarrayDependency, env, kwargs))
51
52    return candidates
53
54
55class CoarrayDependency(SystemDependency):
56    """
57    Coarrays are a Fortran 2008 feature.
58
59    Coarrays are sometimes implemented via external library (GCC+OpenCoarrays),
60    while other compilers just build in support (Cray, IBM, Intel, NAG).
61    Coarrays may be thought of as a high-level language abstraction of
62    low-level MPI calls.
63    """
64    def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
65        super().__init__('coarray', environment, kwargs, language='fortran')
66        kwargs['required'] = False
67        kwargs['silent'] = True
68
69        cid = self.get_compiler().get_id()
70        if cid == 'gcc':
71            # Fallback to single image
72            self.compile_args = ['-fcoarray=single']
73            self.version = 'single image (fallback)'
74            self.is_found = True
75        elif cid == 'intel':
76            # Coarrays are built into Intel compilers, no external library needed
77            self.is_found = True
78            self.link_args = ['-coarray=shared']
79            self.compile_args = self.link_args
80        elif cid == 'intel-cl':
81            # Coarrays are built into Intel compilers, no external library needed
82            self.is_found = True
83            self.compile_args = ['/Qcoarray:shared']
84        elif cid == 'nagfor':
85            # NAG doesn't require any special arguments for Coarray
86            self.is_found = True
87
88    @staticmethod
89    def get_methods() -> T.List[DependencyMethods]:
90        return [DependencyMethods.AUTO, DependencyMethods.CMAKE, DependencyMethods.PKGCONFIG]
91