Ticket #19320: patch-revert_r6792_r6798.diff

File patch-revert_r6792_r6798.diff, 3.7 KB (added by skymoo (Adam Mercer), 12 years ago)
  • numpy/distutils/fcompiler/gnu.py

    diff -ru numpy/distutils/fcompiler/gnu.py numpy/distutils/fcompiler/gnu.py
     
    1111compilers = ['GnuFCompiler', 'Gnu95FCompiler']
    1212
    1313TARGET_R = re.compile("Target: ([a-zA-Z0-9_\-]*)")
    14 
    15 # XXX: do we really need to check for target ? If the arch is not supported,
    16 # the return code should be != 0
    17 _R_ARCHS = {"ppc": r"^Target: (powerpc-.*)$",
    18     "i686": r"^Target: (i686-.*)$",
    19     "x86_64": r"^Target: (i686-.*)$",
    20     "ppc64": r"^Target: (powerpc-.*)$",}
    21 
    2214class GnuFCompiler(FCompiler):
    2315    compiler_type = 'gnu'
    2416    compiler_aliases = ('g77',)
     
    244236    # Note that this is here instead of GnuFCompiler as gcc < 4 uses a
    245237    # different output format (which isn't as useful) than gcc >= 4,
    246238    # and we don't have to worry about g77 being universal (as it can't be).
    247     def _can_target(self, cmd, arch):
    248         """Return true is the compiler support the -arch flag for the given
    249         architecture."""
    250         newcmd = cmd[:]
    251         newcmd.extend(["-arch", arch, "-v"])
    252         st, out = exec_command(" ".join(newcmd))
    253         if st == 0:
    254             for line in out.splitlines():
    255                 m = re.search(_R_ARCHS[arch], line)
    256                 if m:
    257                     return True
    258         return False
    259            
    260     def _universal_flags(self, cmd):
    261         """Return a list of -arch flags for every supported architecture."""
    262         if not sys.platform == 'darwin':
    263             return []
    264         arch_flags = []
    265         for arch in ["ppc", "i686"]:
    266             if self._can_target(cmd, arch):
    267                 arch_flags.extend(["-arch", arch])
    268         return arch_flags
     239    def target_architecture(self, extra_opts=()):
     240        """Return the architecture that the compiler will build for.
     241        This is most useful for detecting universal compilers in OS X."""
     242        extra_opts = list(extra_opts)
     243        status, output = exec_command(self.compiler_f90 + ['-v'] + extra_opts,
     244                                      use_tee=False)
     245        if status == 0:
     246            m = re.match(r'(?m)^Target: (.*)$', output)
     247            if m:
     248                return m.group(1)
     249        return None
     250
     251    def is_universal_compiler(self):
     252        """Return True if this compiler can compile universal binaries
     253        (for OS X).
     254
     255        Currently only checks for i686 and powerpc architectures (no 64-bit
     256        support yet).
     257        """
     258        if sys.platform != 'darwin':
     259            return False
     260        i686_arch = self.target_architecture(extra_opts=['-arch', 'i686'])
     261        if not i686_arch or not i686_arch.startswith('i686-'):
     262            return False
     263        ppc_arch = self.target_architecture(extra_opts=['-arch', 'ppc'])
     264        if not ppc_arch or not ppc_arch.startswith('powerpc-'):
     265            return False
     266        return True
     267
     268    def _add_arches_for_universal_build(self, flags):
     269        if self.is_universal_compiler():
     270            flags[:0] = ['-arch', 'i686', '-arch', 'ppc']
     271        return flags
    269272
    270273    def get_flags(self):
    271274        flags = GnuFCompiler.get_flags(self)
    272         arch_flags = self._universal_flags(self.compiler_f90)
    273         if arch_flags:
    274             flags[:0] = arch_flags
    275         return flags
     275        return self._add_arches_for_universal_build(flags)
    276276
    277277    def get_flags_linker_so(self):
    278278        flags = GnuFCompiler.get_flags_linker_so(self)
    279         arch_flags = self._universal_flags(self.linker_so)
    280         if arch_flags:
    281             flags[:0] = arch_flags
    282         return flags
     279        return self._add_arches_for_universal_build(flags)
    283280
    284281    def get_library_dirs(self):
    285282        opt = GnuFCompiler.get_library_dirs(self)