使用distutils进行编译时禁用输出

Ada*_*dam 10 python distutils

我有一个setup.py脚本需要探测编译器的某些东西,如对TR1的支持,windows.h的存在(添加NOMINMAX定义)等等.我通过创建一个简单的程序并尝试编译来做这些检查它与Distutils的编译器类.存在/没有错误是我的答案.

这很好用,但这意味着编译器的丑陋错误消息会打印到控制台.有没有办法在compile手动调用函数时抑制错误消息?

这是我的函数,它试图编译程序,现在通过将错误流传递给文件来消除错误消息(回答我自己的问题):

def see_if_compiles(program, include_dirs, define_macros):
    """ Try to compile the passed in program and report if it compiles successfully or not. """
    from distutils.ccompiler import new_compiler, CompileError
    from shutil import rmtree
    import tempfile
    import os

    try:
        tmpdir = tempfile.mkdtemp()
    except AttributeError:
        # Python 2.2 doesn't have mkdtemp().
        tmpdir = "compile_check_tempdir"
        try:
            os.mkdir(tmpdir)
        except OSError:
            print "Can't create temporary directory. Aborting."
            sys.exit()

    old = os.getcwd()

    os.chdir(tmpdir)

    # Write the program
    f = open('compiletest.cpp', 'w')
    f.write(program)
    f.close()

    # redirect the error stream to keep ugly compiler error messages off the command line
    devnull = open('errors.txt', 'w')
    oldstderr = os.dup(sys.stderr.fileno())
    os.dup2(devnull.fileno(), sys.stderr.fileno())
    #
    try:
        c = new_compiler()
        for macro in define_macros:
            c.define_macro(name=macro[0], value=macro[1])
        c.compile([f.name], include_dirs=include_dirs)
        success = True
    except CompileError:
        success = False
    # undo the error stream redirect
    os.dup2(oldstderr, sys.stderr.fileno())
    devnull.close()

    os.chdir(old)
    rmtree(tmpdir)
    return success
Run Code Online (Sandbox Code Playgroud)

这是一个使用上述功能来检查标题是否存在的函数.

def check_for_header(header, include_dirs, define_macros):
    """Check for the existence of a header file by creating a small program which includes it and see if it compiles."""
    program = "#include <%s>\n" % header
    sys.stdout.write("Checking for <%s>... " % header)
    success = see_if_compiles(program, include_dirs, define_macros)
    if (success):
        sys.stdout.write("OK\n");
    else:
        sys.stdout.write("Not found\n");
    return success
Run Code Online (Sandbox Code Playgroud)

Ada*_*dam 5

Zac的评论促使我看起来更多,我发现Mercurial的setup.py脚本有一个适合他的方法的工作方法.您不能只分配流,因为更改不会被编译器进程继承,但显然Python以我们的好朋友dup2()的形式os.dup2().这允许我们都知道和喜爱的相同操作系统级别的流诡计,这些诡计继承到子进程.

Mercurial的函数重定向到/ dev/null,但为了保持Windows兼容性,我只需重定向到文件然后删除它.

Quoth Mercurial:

# simplified version of distutils.ccompiler.CCompiler.has_function
# that actually removes its temporary files.
def hasfunction(cc, funcname):
    tmpdir = tempfile.mkdtemp(prefix='hg-install-')
    devnull = oldstderr = None
    try:
        try:
            fname = os.path.join(tmpdir, 'funcname.c')
            f = open(fname, 'w')
            f.write('int main(void) {\n')
            f.write('    %s();\n' % funcname)
            f.write('}\n')
            f.close()
            # Redirect stderr to /dev/null to hide any error messages
            # from the compiler.
            # This will have to be changed if we ever have to check
            # for a function on Windows.
            devnull = open('/dev/null', 'w')
            oldstderr = os.dup(sys.stderr.fileno())
            os.dup2(devnull.fileno(), sys.stderr.fileno())
            objects = cc.compile([fname], output_dir=tmpdir)
            cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
        except:
            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)
Run Code Online (Sandbox Code Playgroud)