summaryrefslogtreecommitdiff
path: root/lib/spack/spack/compilers/__init__.py
blob: facc9c338b26f742505498c70b8cd390592b85ac (plain) (blame)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""This module contains functions related to finding compilers on the
system and configuring Spack to use multiple compilers.
"""
import imp
import os
import platform

from llnl.util.lang import memoized, list_modules
from llnl.util.filesystem import join_path

import spack
import spack.error
import spack.spec
import spack.config
import spack.architecture

from spack.util.multiproc import parmap
from spack.compiler import Compiler
from spack.util.executable import which
from spack.util.naming import mod_to_class
from spack.util.environment import get_path

_imported_compilers_module = 'spack.compilers'
_required_instance_vars = ['cc', 'cxx', 'f77', 'fc']

# TODO: customize order in config file
if platform.system() == 'Darwin':
    _default_order = ['clang', 'gcc', 'intel']
else:
    _default_order = ['gcc', 'intel', 'pgi', 'clang', 'xlc']


def _auto_compiler_spec(function):
    def converter(cspec_like, *args, **kwargs):
        if not isinstance(cspec_like, spack.spec.CompilerSpec):
            cspec_like = spack.spec.CompilerSpec(cspec_like)
        return function(cspec_like, *args, **kwargs)
    return converter


def _to_dict(compiler):
    """Return a dict version of compiler suitable to insert in YAML."""
    return {
        str(compiler.spec) : dict(
            (attr, getattr(compiler, attr, None))
            for attr in _required_instance_vars)
    }


def get_compiler_config(arch=None, scope=None):
    """Return the compiler configuration for the specified architecture.
    """
    # If any configuration file has compilers, just stick with the
    # ones already configured.
    config = spack.config.get_config('compilers', scope=scope)

    my_arch = spack.architecture.sys_type()
    if arch is None:
        arch = my_arch

    if arch in config:
        return config[arch]

    # Only for the current arch in *highest* scope: automatically try to
    # find compilers if none are configured yet.
    if arch == my_arch and scope == 'user':
        config[arch] = {}
        compilers = find_compilers(*get_path('PATH'))
        for compiler in compilers:
            config[arch].update(_to_dict(compiler))
        spack.config.update_config('compilers', config, scope=scope)
        return config[arch]

    return {}


def add_compilers_to_config(compilers, arch=None, scope=None):
    """Add compilers to the config for the specified architecture.

    Arguments:
      - compilers: a list of Compiler objects.
      - arch:      arch to add compilers for.
      - scope:     configuration scope to modify.
    """
    if arch is None:
        arch = spack.architecture.sys_type()

    compiler_config = get_compiler_config(arch, scope)
    for compiler in compilers:
        compiler_config[str(compiler.spec)] = dict(
            (c, getattr(compiler, c, "None"))
            for c in _required_instance_vars)

    update = { arch : compiler_config }
    spack.config.update_config('compilers', update, scope)


@_auto_compiler_spec
def remove_compiler_from_config(compiler_spec, arch=None, scope=None):
    """Remove compilers from the config, by spec.

    Arguments:
      - compiler_specs: a list of CompilerSpec objects.
      - arch:           arch to add compilers for.
      - scope:          configuration scope to modify.
    """
    if arch is None:
        arch = spack.architecture.sys_type()

    compiler_config = get_compiler_config(arch, scope)
    del compiler_config[str(compiler_spec)]
    update = { arch : compiler_config }

    spack.config.update_config('compilers', update, scope)


def all_compilers_config(arch=None, scope=None):
    """Return a set of specs for all the compiler versions currently
       available to build with.  These are instances of CompilerSpec.
    """
    # Get compilers for this architecture.
    arch_config = get_compiler_config(arch, scope)

    # Merge 'all' compilers with arch-specific ones.
    # Arch-specific compilers have higher precedence.
    merged_config = get_compiler_config('all', scope=scope)
    merged_config = spack.config._merge_yaml(merged_config, arch_config)

    return merged_config


def all_compilers(arch=None, scope=None):
    # Return compiler specs from the merged config.
    return [spack.spec.CompilerSpec(s)
            for s in all_compilers_config(arch, scope)]


def default_compiler():
    versions = []
    for name in _default_order:
        versions = find(name)
        if versions:
            break
    else:
        raise NoCompilersError()

    return sorted(versions)[-1]


def find_compilers(*path):
    """Return a list of compilers found in the suppied paths.
       This invokes the find() method for each Compiler class,
       and appends the compilers detected to a list.
    """
    # Make sure path elements exist, and include /bin directories
    # under prefixes.
    filtered_path = []
    for p in path:
        # Eliminate symlinks and just take the real directories.
        p = os.path.realpath(p)
        if not os.path.isdir(p):
            continue
        filtered_path.append(p)

        # Check for a bin directory, add it if it exists
        bin = join_path(p, 'bin')
        if os.path.isdir(bin):
            filtered_path.append(os.path.realpath(bin))

    # Once the paths are cleaned up, do a search for each type of
    # compiler.  We can spawn a bunch of parallel searches to reduce
    # the overhead of spelunking all these directories.
    types = all_compiler_types()
    compiler_lists = parmap(lambda cls: cls.find(*filtered_path), types)

    # ensure all the version calls we made are cached in the parent
    # process, as well.  This speeds up Spack a lot.
    clist = reduce(lambda x,y: x+y, compiler_lists)
    return clist


def supported_compilers():
    """Return a set of names of compilers supported by Spack.

       See available_compilers() to get a list of all the available
       versions of supported compilers.
    """
    return sorted(name for name in list_modules(spack.compilers_path))


@_auto_compiler_spec
def supported(compiler_spec):
    """Test if a particular compiler is supported."""
    return compiler_spec.name in supported_compilers()


@_auto_compiler_spec
def find(compiler_spec, arch=None, scope=None):
    """Return specs of available compilers that match the supplied
       compiler spec.  Return an list if nothing found."""
    return [c for c in all_compilers(arch, scope) if c.satisfies(compiler_spec)]


@_auto_compiler_spec
def compilers_for_spec(compiler_spec, arch=None, scope=None):
    """This gets all compilers that satisfy the supplied CompilerSpec.
       Returns an empty list if none are found.
    """
    config = all_compilers_config(arch, scope)

    def get_compiler(cspec):
        items = config[str(cspec)]

        if not all(n in items for n in _required_instance_vars):
            raise InvalidCompilerConfigurationError(cspec)

        cls  = class_for_compiler_name(cspec.name)
        compiler_paths = []
        for c in _required_instance_vars:
            compiler_path = items[c]
            if compiler_path != "None":
                compiler_paths.append(compiler_path)
            else:
                compiler_paths.append(None)

        return cls(cspec, *compiler_paths)

    matches = find(compiler_spec, arch, scope)
    return [get_compiler(cspec) for cspec in matches]


@_auto_compiler_spec
def compiler_for_spec(compiler_spec):
    """Get the compiler that satisfies compiler_spec.  compiler_spec must
       be concrete."""
    assert(compiler_spec.concrete)
    compilers = compilers_for_spec(compiler_spec)
    assert(len(compilers) == 1)
    return compilers[0]


def class_for_compiler_name(compiler_name):
    """Given a compiler module name, get the corresponding Compiler class."""
    assert(supported(compiler_name))

    file_path = join_path(spack.compilers_path, compiler_name + ".py")
    compiler_mod = imp.load_source(_imported_compilers_module, file_path)
    cls = getattr(compiler_mod, mod_to_class(compiler_name))

    # make a note of the name in the module so we can get to it easily.
    cls.name = compiler_name

    return cls


def all_compiler_types():
    return [class_for_compiler_name(c) for c in supported_compilers()]


class InvalidCompilerConfigurationError(spack.error.SpackError):
    def __init__(self, compiler_spec):
        super(InvalidCompilerConfigurationError, self).__init__(
            "Invalid configuration for [compiler \"%s\"]: " % compiler_spec,
            "Compiler configuration must contain entries for all compilers: %s"
            % _required_instance_vars)


class NoCompilersError(spack.error.SpackError):
    def __init__(self):
        super(NoCompilersError, self).__init__("Spack could not find any compilers!")