summaryrefslogtreecommitdiff
path: root/lib/spack/spack/architecture.py
blob: e0d42c2077d25cc582a8f581e803e8118b6d2267 (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
##############################################################################
# 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://scalability-llnl.github.io/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
##############################################################################
import os
import platform as py_platform
import subprocess

from llnl.util.lang import memoized

import spack
import spack.error as serr
from spack.version import Version


class InvalidSysTypeError(serr.SpackError):
    def __init__(self, sys_type):
        super(InvalidSysTypeError, self).__init__(
            "Invalid sys_type value for Spack: " + sys_type)


class NoSysTypeError(serr.SpackError):
    def __init__(self):
        super(NoSysTypeError, self).__init__(
            "Could not determine sys_type for this machine.")


def get_sys_type_from_spack_globals():
    """Return the SYS_TYPE from spack globals, or None if it isn't set."""
    if not hasattr(spack, "sys_type"):
        return None
    elif hasattr(spack.sys_type, "__call__"):
        return spack.sys_type()
    else:
        return spack.sys_type


def get_sys_type_from_environment():
    """Return $SYS_TYPE or None if it's not defined."""
    return os.environ.get('SYS_TYPE')


def get_mac_sys_type():
    """Return a Mac OS SYS_TYPE or None if this isn't a mac."""
    mac_ver = py_platform.mac_ver()[0]
    if not mac_ver:
        return None

    return "macosx_%s_%s" % (
        Version(mac_ver).up_to(2), py_platform.machine())


def get_sys_type_from_uname():
    """Return the architecture from uname."""
    try:
        arch_proc = subprocess.Popen(['uname', '-i'],
            stdout=subprocess.PIPE)
        arch, _ = arch_proc.communicate()
        return arch.strip()
    except:
        return None


@memoized
def sys_type():
    """Returns a SysType for the current machine."""
    methods = [get_sys_type_from_spack_globals,
               get_sys_type_from_environment,
               get_mac_sys_type,
               get_sys_type_from_uname]

    # search for a method that doesn't return None
    sys_type = None
    for method in methods:
        sys_type = method()
        if sys_type: break

    # Couldn't determine the sys_type for this machine.
    if sys_type is None:
        return "unknown_arch"

    if not isinstance(sys_type, basestring):
        raise InvalidSysTypeError(sys_type)

    return sys_type