Python numpy distutils——如果失败,尝试编译一些东西并设置标志

Python numpy distutils——如果失败,尝试编译一些东西并设置标志,python,numpy,distutils,Python,Numpy,Distutils,在*NIX平台上构建源代码的一种常见方法是使用configure脚本。在后台,“配置”尝试构建一组测试程序,以确定您可以访问哪些库。然后,它生成一个头文件,该头文件包含在项目中,该项目有条件地定义了一组宏,以便程序员在缺少特定的“依赖项”时可以提供一个替代方案或构建库/程序的精简版本。使用numpy.distutils是否有任何功能等效 例如,下面是我的setup.py: from numpy.distutils.misc_util import Configuration def confi

在*NIX平台上构建源代码的一种常见方法是使用
configure
脚本。在后台,“配置”尝试构建一组测试程序,以确定您可以访问哪些库。然后,它生成一个头文件,该头文件包含在项目中,该项目有条件地定义了一组宏,以便程序员在缺少特定的“依赖项”时可以提供一个替代方案或构建库/程序的精简版本。使用
numpy.distutils
是否有任何功能等效

例如,下面是我的
setup.py

from numpy.distutils.misc_util import Configuration

def configuration(parent_package='',top_path=None):
    config = Configuration('pyggcm',parent_package,top_path)

    #TODO: Currently, I have some macros to conditionally build the seek-code
    #Unfortunately, that's not the best solution (by far).  Perhaps if we
    #changed to using stream access it would work better, without the need
    #for these silly macros.
    config.add_extension('_fortfile',sources=['_fortfile/_fortfile.F90'],
                         define_macros=[
                             ('FSEEKABLE',1),  #compiler provides fseek and ftell
                             ('HAVE_STREAM',1) #compiler provides access='stream' for opening files. (f2003 standard)
                             ])  

    config.add_extension('jrrle',sources=['jrrle/jrrle.f90'])
    config.add_scripts(['scripts/ggcm_timehist',
                        'scripts/ggcm_plasmasheet',
                        'scripts/ggcm_plot'])
    return config


from numpy.distutils.core import setup    
setup(configuration=configuration)

这是无条件地构建
FSEEKABLE
代码,如果用户Fortran编译器不支持,则需要手动编辑(宏包装
fseek
ftell
GNU内部函数)。有没有办法确定Fortran编译器是否提供这些内在函数?

将代码片段生成到文件中,并尝试编译它检查错误代码是正常的方法,应该可以很好地工作-但是,恐怕没有人作为distutils的一部分为您这样做。我怀疑另一个(可能是基于python的)构建工具(如)可能支持此功能。我认为你最好的选择是GNU自动配置。

试试这个:

import os
import shutil
import tempfile
from distutils.ccompiler import new_compiler

def hasfunction(cc, funcname, include=None, extra_postargs=None):
    tmpdir = tempfile.mkdtemp(prefix='hasfunction-')
    devnull = oldstderr = None
    try:
        try:
            fname = os.path.join(tmpdir, 'funcname.c')
            f = open(fname, 'w')
            if include is not None:
                f.write('#include %s\n' % include)
            f.write('int main(void) {\n')
            f.write('    %s;\n' % funcname)
            f.write('}\n')
            f.close()
            devnull = open(os.devnull, 'w')
            oldstderr = os.dup(sys.stderr.fileno())
            os.dup2(devnull.fileno(), sys.stderr.fileno())
            objects = cc.compile([fname], output_dir=tmpdir,
                                 extra_postargs=extra_postargs)
            cc.link_executable(objects, os.path.join(tmpdir, 'a.out'))
        except Exception as e:
            return False
        return True
    finally:
        if oldstderr is not None:
            os.dup2(oldstderr, sys.stderr.fileno())
        if devnull is not None:
            devnull.close()
        shutil.rmtree(tmpdir)
例如:

def detect_sse3():
    "Does this compiler support SSE3 intrinsics?"
    compiler = new_compiler()
    return hasfunction(compiler, '__m128 v; _mm_hadd_ps(v,v)',
                       include='<pmmintrin.h>',
                       extra_postargs=['-msse3'])
def detect_sse3():
“此编译器是否支持SSE3内部函数?”
编译器=新编译器()
返回hasfunction(编译器,''uuum128 v;'umm'uhadd_ps(v,v'),
包括=“”,
额外邮资=['-msse3'])
缺少单词:…构建工具,如may。。。?