setup.py检查是否存在非python库依赖项 [英] setup.py check if non-python library dependency exists
问题描述
我正在尝试为 cgal-bindings 建立setup.py。要安装此版本,用户需要至少具有特定版本的CGAL。此外,如果用户具有某些库(例如Eigen3),则应构建一些可选目标CGAL。 Python中是否有跨平台的方法来检查这一点?
我可以在。 <-实际上这并不是一直有效,某些库仅是标头,例如eigen3,它是C ++模板库。 ctypes.util $ c $中使用
find_library
c>检查该库是否存在,但没有找到获取版本的任何简便方法
仅使用 setup()
的 install_requires
自变量适用于Python库,而CGAL是C / C ++库。
是否应根据某些库版本的可用性来编译特定的扩展模块,可以通过动态生成< setup.py
中 setup()
的code> ext_modules 个参数。 / p>
对于 ruamel.yaml
的 _yaml.so
模块,只有在我执行的系统上安装了 libyaml
开发库时,才应编译该文件:
import from textwrap import dent
def check_extensions():
检查是否可以通过尝试编译C来构建C模块针对libyaml开发库的小型
程序
导入临时文件
导入shutdownil
导入distutils.sysconfig
导入distutils.ccompiler来自distutils.errors的
导入CompileError,LinkError
库= ['yaml']
#写一个tempora ry .c文件来编译
c_code = dedent(
#include< yaml.h>
int main(int argc,char * argv [])
{
yaml_parser_t解析器;
parser =解析器; / *防止警告* /
返回0;
}
)
tmp_dir = tempfile.mkdtemp(前缀='tmp_ruamel_yaml_')
bin_file_name = os.path.join(tmp_dir,'test_yaml')
file_name = bin_file_name +'.c'
以open(file_name,'w')as fp:
fp.write(c_code)
#并尝试编译
编译器= distutils.ccompiler.new_compiler()
断言isinstance(编译器,distutils.ccompiler.CCompiler)
distutils.sysconfig.customize_compiler(compiler)
try:
Compiler.link_executable(
Compiler.compile([file_name]),
bin_file_name,
library = libraries,
)
除了CompileError:
print('libyaml编译错误')
ret_val =无
除外LinkError:
print('libyaml链接错误')
ret_val =无
其他:
ret_val = [
扩展名(
'_yaml',
来源= ['ext / _yaml.c'],
库=库,
),
]
shutil.rmtree(tmp_dir)
return ret_val
这样,您不需要分发中的其他文件。即使您不能在编译时根据版本号进行编译,您也应该
能够从临时目录运行生成的程序并检查退出值和/或输出。
I'm trying to make a setup.py for cgal-bindings. To install this, the user needs to have at least a certain version of CGAL. In addition, CGAL has a few optional targets that should be built if the user has some libraries (like Eigen3). Is there a cross-platform way in Python to check for this?
I can use . <-- This doesn't actually work all the time, some libraries are header-only like eigen3, which is a C++ template library. find_library
in ctypes.util
to check if the library exists, but I don't see any easy way to get the version
Using the install_requires
argument of setup()
only works for Python libraries and CGAL is a C/C++ library.
Whether a particular extension module should be compiled depending on the availability of some library version, can be accomplished by dynamically generating the ext_modules
argument of setup()
in setup.py
.
For the _yaml.so
module of ruamel.yaml
, that only should be compiled when the libyaml
development libraries have been installed on the system I do:
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
This way you require no extra files in the distribution. Even if you cannot fail to compile based on the version number at compile time, you should be able to run the resulting program from the temporary directory and check the exit value and/or output.
这篇关于setup.py检查是否存在非python库依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!