Added support for multi-arch
[zeroinstall/zeroinstall-mseaborn.git] / zeroinstall / injector / arch.py
blobddc2274435bde8a0b17b701d511322621039a5ac
1 """
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.
15 """
17 # Copyright (C) 2006, Thomas Leonard
18 # See the README file for details, or visit http://0install.net.
20 import os
22 # os_ranks and mapping are mappings from names to how good they are.
23 # 1 => Native (best)
24 # Higher numbers are worse but usable.
25 _uname = os.uname()
27 os_ranks = {
28 # 'Linux' : 3, # Linux (lots of systems support emulation)
29 None : 2, # Any OS
30 _uname[0] : 1, # Current OS
33 # All chosen machine-specific implementations must come from the same group
34 # Unlisted archs are in group 0
35 machine_groups = {
36 'x86_64': 64,
37 'ppc64': 64,
40 def _get_machine_ranks(target_machine):
41 # Binaries compiled for _this_machine are best...
42 machine_ranks = {target_machine : 0}
44 # If target_machine appears in the first column of this table, all
45 # following machine types on the line will also run on this one
46 # (earlier ones preferred):
47 _machine_matrix = {
48 'i486': ['i386'],
49 'i586': ['i486', 'i386'],
50 'i686': ['i586', 'i486', 'i386'],
51 'x86_64': ['i686', 'i586', 'i486', 'i386'],
52 'ppc64': ['ppc32'],
54 for supported in _machine_matrix.get(target_machine, []):
55 machine_ranks[supported] = len(machine_ranks)
57 # At the lowest priority, try a machine-independant implementation
58 machine_ranks[None] = len(machine_ranks)
59 return machine_ranks
61 machine_ranks = _get_machine_ranks(_uname[-1])
62 #print machine_ranks
64 class Architecture:
65 """A description of an architecture. Use by L{solver} to make sure it chooses
66 compatible versions.
67 @ivar os_ranks: supported operating systems and their desirability
68 @type os_ranks: {str: int}
69 @ivar machine_ranks: supported CPU types and their desirability
70 @type machine_ranks: {str: int}
71 @ivar child_arch: architecture for dependencies (usually C{self})
72 @type child_arch: L{Architecture}"""
73 def __init__(self, os_ranks, machine_ranks):
74 self.os_ranks = os_ranks
75 self.machine_ranks = machine_ranks
76 self.child_arch = self
78 def __str__(self):
79 return "<Arch: %s %s>" % (self.os_ranks, self.machine_ranks)
81 class SourceArchitecture(Architecture):
82 """Matches source code that creates binaries for a particular architecture.
83 Note that the L{child_arch} here is the binary; source code depends on binary tools,
84 not on other source packages.
85 """
86 def __init__(self, binary_arch):
87 Architecture.__init__(self, binary_arch.os_ranks, {'src': 1})
88 self.child_arch = binary_arch
90 def get_host_architecture():
91 """Get an Architecture that matches implementations that will run on the host machine.
92 @rtype: L{Architecture}"""
93 return Architecture(os_ranks, machine_ranks)
95 def get_architecture(os, machine):
96 """Get an Architecture that matches binaries that will work on the given system.
97 @param os: OS type, or None for host's type
98 @param machine: CPU type, or None for host's type
99 @return: an Architecture object
100 @rtype: L{Architecture}"""
102 if os:
103 target_os_ranks = {
104 os : 1, # Perfer binaries for target OS
105 None : 2, # Otherwise, generic OS is fine
107 else:
108 target_os_ranks = os_ranks
109 if machine:
110 target_machine_ranks = _get_machine_ranks(machine)
111 else:
112 target_machine_ranks = machine_ranks
113 return Architecture(target_os_ranks, target_machine_ranks)