1""" 2Information about the current system's architecture. 3 4This module provides information about the current system. It is used to determine 5whether an implementation is suitable for this machine, and to compare different implementations. 6 7For example, it will indicate that: 8 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. 12 13Each dictionary maps from a supported architecture type to a preference level. Lower numbers are 14better, Unsupported architectures are not listed at all. 15""" 16 17# Copyright (C) 2009, Thomas Leonard 18# See the README file for details, or visit http://0install.net. 19 20fromzeroinstallimport_ 21importos 22 23# TODO: "import platform"? 24 25# os_ranks and mapping are mappings from names to how good they are. 26# 1 => Native (best) 27# Higher numbers are worse but usable. 28try: 29_uname=os.uname() 30# On Darwin, machine is wrong. 31if_uname[0]=='Darwin'and_uname[-1]=='i386': 32_cpu64=os.popen('sysctl -n hw.cpu64bit_capable 2>&1').next().strip() 33if_cpu64=='1': 34_uname=tuple(list(_uname[:-1])+['x86_64']) 35exceptAttributeError: 36# No uname. Probably Windows. 37importsys 38p=sys.platform 39ifp=='win64': 40_uname=('Windows','x86_64') 41elifp=='win32': 42fromwin32processimportIsWow64Process 43ifIsWow64Process(): 44_uname=('Windows','x86_64') 45else: 46_uname=('Windows','i486') 47else: 48_uname=(p,'i486') 49
60"""@type target_os: str""" 61target_os=canonicalize_os(target_os) 62 63iftarget_os=='Darwin': 64# Special case Mac OS X, to separate it from Darwin/X 65# (Mac OS X also includes the closed Apple frameworks) 66ifos.path.exists('/System/Library/Frameworks/Carbon.framework'): 67target_os='MacOSX' 68 69# Binaries compiled for _this_ OS are best... 70os_ranks={target_os:1} 71 72# Assume everything supports POSIX except Windows 73# (but Cygwin is POSIX) 74iftarget_os!='Windows': 75os_ranks['POSIX']=len(os_ranks)+1 76 77# If target_os appears in the first column of this table, all 78# following OS types on the line will also run on this one 79# (earlier ones preferred): 80_os_matrix={ 81'Cygwin':['Windows'], 82'MacOSX':['Darwin'], 83} 84 85forsupportedin_os_matrix.get(target_os,[]): 86os_ranks[supported]=len(os_ranks)+1 87 88# At the lowest priority, try an OS-independent implementation 89os_ranks[None]=len(os_ranks)+1 90returnos_ranks
91 92os_ranks=_get_os_ranks(_uname[0]) 93 94# All chosen machine-specific implementations must come from the same group 95# Unlisted archs are in group 0 96machine_groups={ 97'x86_64':64, 98'ppc64':64, 99}100
116"""@type target_machine: str"""117target_machine=canonicalize_machine(target_machine)118119# Binaries compiled for _this_machine are best...120machine_ranks={target_machine:0}121122# If target_machine appears in the first column of this table, all123# following machine types on the line will also run on this one124# (earlier ones preferred):125_machine_matrix={126'i486':['i386'],127'i586':['i486','i386'],128'i686':['i586','i486','i386'],129'x86_64':['i686','i586','i486','i386'],130'ppc':['ppc32'],131'ppc64':['ppc'],132}133forsupportedin_machine_matrix.get(target_machine,[]):134machine_ranks[supported]=len(machine_ranks)135136# At the lowest priority, try a machine-independant implementation137machine_ranks[None]=len(machine_ranks)138returnmachine_ranks
143"""A description of an architecture. Use by L{solver} to make sure it chooses144 compatible versions.145 @ivar os_ranks: supported operating systems and their desirability146 @type os_ranks: {str: int}147 @ivar machine_ranks: supported CPU types and their desirability148 @type machine_ranks: {str: int}149 @ivar child_arch: architecture for dependencies (usually C{self})150 @type child_arch: L{Architecture}151 @ivar use: matching values for <requires use='...'>; otherwise the dependency is ignored152 @type use: set(str)153 """154155use=frozenset([None])156
167"""Matches source code that creates binaries for a particular architecture.168 Note that the L{child_arch} here is the binary; source code depends on binary tools,169 not on other source packages.170 """
177"""Get an Architecture that matches implementations that will run on the host machine.178 @rtype: L{Architecture}"""179returnArchitecture(os_ranks,machine_ranks)
182"""Get an Architecture that matches binaries that will work on the given system.183 @param os: OS type, or None for host's type184 @type os: str185 @param machine: CPU type, or None for host's type186 @type machine: str187 @return: an Architecture object188 @rtype: L{Architecture}"""189190ifosisNone:191target_os_ranks=os_ranks192else:193target_os_ranks=_get_os_ranks(os)194ifmachineisNone:195target_machine_ranks=machine_ranks196else:197target_machine_ranks=_get_machine_ranks(machine)198199returnArchitecture(target_os_ranks,target_machine_ranks)