setup.py检查是否存在非python库依赖项

sci*_*ctn 5 python distutils setuptools cgal setup.py

我正在尝试为cgal绑定创建一个setup.py .要安装它,用户需要至少具有某个版本的CGAL.此外,如果用户有一些库(如Eigen3),CGAL有一些可选的目标.Python中是否有跨平台方式来检查?

我可以用find_libraryctypes.util检查库中存在,但我看不出有什么简单的方法来获取版本.< - 这实际上并不是一直有效,有些库只是像eigen3这样的头文件,它是一个C++模板库.

使用install_requires的说法setup()只适用于Python库和CGAL是一个C/C++库.

Ant*_*hon 5

是否应根据某些库版本的可用性来编译特定的扩展模块,可以通过动态生成inext_modules的参数来完成。setup()setup.py

对于_yaml.so的模块,只有在系统上安装了开发库ruamel.yaml时才应该编译它:libyaml

import os
from textwrap import dedent

def check_extensions():
    """check if the C module can be build by trying to compile a small 
    program against the libyaml development library"""

    import tempfile
    import shutil

    import distutils.sysconfig
    import distutils.ccompiler
    from distutils.errors import CompileError, LinkError

    libraries = ['yaml']

    # write a temporary .c file to compile
    c_code = dedent("""
    #include <yaml.h>

    int main(int argc, char* argv[])
    {
        yaml_parser_t parser;
        parser = parser;  /* prevent warning */
        return 0;
    }
    """)
    tmp_dir = tempfile.mkdtemp(prefix = 'tmp_ruamel_yaml_')
    bin_file_name = os.path.join(tmp_dir, 'test_yaml')
    file_name = bin_file_name + '.c'
    with open(file_name, 'w') as fp:
        fp.write(c_code)

    # and try to compile it
    compiler = distutils.ccompiler.new_compiler()
    assert isinstance(compiler, distutils.ccompiler.CCompiler)
    distutils.sysconfig.customize_compiler(compiler)

    try:
        compiler.link_executable(
            compiler.compile([file_name]),
            bin_file_name,
            libraries=libraries,
        )
    except CompileError:
        print('libyaml compile error')
        ret_val = None
    except LinkError:
        print('libyaml link error')
        ret_val = None
    else:
        ret_val = [
            Extension(
                '_yaml',
                sources=['ext/_yaml.c'],
                libraries=libraries,
                ),
        ]
    shutil.rmtree(tmp_dir)
    return ret_val
Run Code Online (Sandbox Code Playgroud)

这样您就不需要在发行版中添加额外的文件。即使您不能根据编译时的版本号进行编译失败,您也应该能够从临时目录运行生成的程序并检查退出值和/或输出。

  • 亲,我正在用这个。在我的 setup.py 中给了你信用。 (2认同)