-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
95 lines (79 loc) · 3 KB
/
Copy pathsetup.py
File metadata and controls
95 lines (79 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
import sys
import os
import subprocess
import platform
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_call(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake必须安装才能构建扩展")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
# 必要的CMake参数
cmake_args = [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable
]
# 配置参数
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
# 多核编译
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j4']
# 创建build目录
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# 运行CMake命令
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
# 复制.so文件到包目录
if not os.path.exists(os.path.join(extdir, 'pysagittarius')):
os.makedirs(os.path.join(extdir, 'pysagittarius'))
# 确保__init__.py存在
init_path = os.path.join(extdir, 'pysagittarius', '__init__.py')
if not os.path.exists(init_path):
with open(init_path, 'w') as f:
f.write('from pysagittarius import *\n\n__version__ = "0.1.0"\n')
# 获取长描述
with open('README.md', 'r', encoding='utf-8') as f:
long_description = f.read()
setup(
name="pysagittarius",
version="0.1.0",
description="Python绑定库用于控制Sagittarius机械臂",
long_description=long_description,
long_description_content_type="text/markdown",
author="Chengleng Han",
author_email="hanchengleng@whut.edu.cn",
url="https://github.com/yourusername/pysagittarius",
packages=find_packages(),
ext_modules=[CMakeExtension('pysagittarius')],
cmdclass=dict(build_ext=CMakeBuild),
python_requires=">=3.6",
install_requires=[
'numpy',
],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: POSIX :: Linux",
],
# 包含额外文件
package_data={
"": ["*.so", "*.md"],
},
include_package_data=True,
)