2 Information about the current system's architecture.
4 This module provides information about the current system. It is used to determine
5 whether an implementation is suitable for this machine, and to compare different implementations.
7 For example, it will indicate that:
9 - An i486 machine cannot run an i686 binary.
10 - An i686 machine can run an i486 binary, but would prefer an i586 one.
11 - A Windows binary cannot run on a Linux machine.
13 Each dictionary maps from a supported architecture type to a preference level. Lower numbers are
14 better, Unsupported architectures are not listed at all.
17 # Copyright (C) 2009, Thomas Leonard
18 # See the README file for details, or visit http://0install.net.
20 from zeroinstall
import _
23 # TODO: "import platform"?
25 # os_ranks and mapping are mappings from names to how good they are.
27 # Higher numbers are worse but usable.
30 except AttributeError:
31 # No uname. Probably Windows.
35 bits
, linkage
= platform
.architecture()
36 if p
== 'win32' and (bits
== '' or bits
== '32bit'):
37 _uname
= ('Windows', 'i486')
38 elif p
== 'win64' or (p
== 'win32' and bits
== '64bit'):
39 _uname
= ('Windows', 'x86_64')
43 def canonicalize_os(os_
):
44 if os_
.startswith('CYGWIN_NT'):
50 def _get_os_ranks(target_os
):
51 target_os
= canonicalize_os(target_os
)
53 if target_os
== 'Darwin':
54 # Special case Mac OS X, to separate it from Darwin/X
55 # (Mac OS X also includes the closed Apple frameworks)
56 if os
.path
.exists('/System/Library/Frameworks/Carbon.framework'):
59 # Binaries compiled for _this_ OS are best...
60 os_ranks
= {target_os
: 1}
62 # If target_os appears in the first column of this table, all
63 # following OS types on the line will also run on this one
64 # (earlier ones preferred):
66 'Cygwin': ['Windows'],
70 for supported
in _os_matrix
.get(target_os
, []):
71 os_ranks
[supported
] = len(os_ranks
) + 1
73 # At the lowest priority, try an OS-independent implementation
74 os_ranks
[None] = len(os_ranks
) + 1
77 os_ranks
= _get_os_ranks(_uname
[0])
79 # All chosen machine-specific implementations must come from the same group
80 # Unlisted archs are in group 0
86 def canonicalize_machine(machine
):
89 elif machine
== 'amd64':
91 elif machine
== 'Power Macintosh':
93 elif machine
== 'i86pc':
97 def _get_machine_ranks(target_machine
):
98 target_machine
= canonicalize_machine(target_machine
)
100 # Binaries compiled for _this_machine are best...
101 machine_ranks
= {target_machine
: 0}
103 # If target_machine appears in the first column of this table, all
104 # following machine types on the line will also run on this one
105 # (earlier ones preferred):
108 'i586': ['i486', 'i386'],
109 'i686': ['i586', 'i486', 'i386'],
110 'x86_64': ['i686', 'i586', 'i486', 'i386'],
114 for supported
in _machine_matrix
.get(target_machine
, []):
115 machine_ranks
[supported
] = len(machine_ranks
)
117 # At the lowest priority, try a machine-independant implementation
118 machine_ranks
[None] = len(machine_ranks
)
121 machine_ranks
= _get_machine_ranks(_uname
[-1])
124 """A description of an architecture. Use by L{solver} to make sure it chooses
126 @ivar os_ranks: supported operating systems and their desirability
127 @type os_ranks: {str: int}
128 @ivar machine_ranks: supported CPU types and their desirability
129 @type machine_ranks: {str: int}
130 @ivar child_arch: architecture for dependencies (usually C{self})
131 @type child_arch: L{Architecture}
132 @ivar use: matching values for <requires use='...'>; otherwise the dependency is ignored
136 use
= frozenset([None])
138 def __init__(self
, os_ranks
, machine_ranks
):
139 self
.os_ranks
= os_ranks
140 self
.machine_ranks
= machine_ranks
141 self
.child_arch
= self
144 return _("<Arch: %(os_ranks)s %(machine_ranks)s>") % {'os_ranks': self
.os_ranks
, 'machine_ranks': self
.machine_ranks
}
146 class SourceArchitecture(Architecture
):
147 """Matches source code that creates binaries for a particular architecture.
148 Note that the L{child_arch} here is the binary; source code depends on binary tools,
149 not on other source packages.
151 def __init__(self
, binary_arch
):
152 Architecture
.__init
__(self
, binary_arch
.os_ranks
, {'src': 1})
153 self
.child_arch
= binary_arch
155 def get_host_architecture():
156 """Get an Architecture that matches implementations that will run on the host machine.
157 @rtype: L{Architecture}"""
158 return Architecture(os_ranks
, machine_ranks
)
160 def get_architecture(os
, machine
):
161 """Get an Architecture that matches binaries that will work on the given system.
162 @param os: OS type, or None for host's type
163 @param machine: CPU type, or None for host's type
164 @return: an Architecture object
165 @rtype: L{Architecture}"""
168 target_os_ranks
= os_ranks
170 target_os_ranks
= _get_os_ranks(os
)
172 target_machine_ranks
= machine_ranks
174 target_machine_ranks
= _get_machine_ranks(machine
)
176 return Architecture(target_os_ranks
, target_machine_ranks
)