-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetup.py
180 lines (149 loc) · 6.06 KB
/
setup.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from time import time, gmtime, strftime
import os
import pathlib
import platform
import subprocess
import sys
from setuptools import setup
from setuptools import Extension
from setuptools.command.build_ext import build_ext
MODULE_NAME = 'LingmoUIPy'
class CMakeExtension(Extension):
"""
自定义了 Extension 类,忽略原来的 sources、libraries 等参数,交给 CMake 来处理这些事情
"""
def __init__(self, name: str, sourcedir: str = ""):
super().__init__(name, sources=[])
self.sourcedir = os.fspath(pathlib.Path(sourcedir).resolve())
class BuildExt(build_ext):
"""
自定义了 build_ext 类,对 CMakeExtension 的实例,调用 CMake 和 Make 命令来编译它们
"""
def build_extension(self, ext: CMakeExtension) -> None:
cwd = pathlib.Path().absolute()
build_temp = f"{pathlib.Path(self.build_temp)}/{ext.name}"
os.makedirs(build_temp, exist_ok=True)
# Must be in this form due to bug in .resolve() only fixed in Python 3.10+
ext_fullpath = pathlib.Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve() / MODULE_NAME
debug = int(os.environ.get("DEBUG", 0)
) if self.debug is None else self.debug
config = "Debug" if debug else "Release"
cmake_args = [
"-S", str(cwd),
"-B", str(build_temp),
"-DBUILD_PYTHON=ON",
"-DCMAKE_CXX_FLAGS_INIT:STRING=",
"-DCMAKE_GENERATOR:STRING=Ninja",
"-DCMAKE_BUILD_TYPE:STRING=" + config,
"-DCMAKE_INSTALL_PREFIX:PATH=" + str(extdir),
]
# 如果是Windows,强制使用CL.exe 作为C_compiler
if platform.system() == "Windows":
cmake_args += [
"-DCMAKE_C_COMPILER=cl.exe",
"-DCMAKE_CXX_COMPILER=cl.exe",
]
build_args = [
"--config", config,
"--", "-j8"
]
env = os.environ.copy() # 获取当前环境变量副本
subprocess.run(["cmake"] + cmake_args, check=True, env=env)
if not self.dry_run:
subprocess.run(["cmake", "--build", str(build_temp)
] + build_args, check=True, env=env)
subprocess.run(
["cmake", "--install", str(build_temp)], check=True, env=env)
def check_qt_version():
# using subprocess to get the version of qt using CMake
# result = subprocess.run(["cmake", "--find-package", "-DNAME=Qt", "-DCOMPILER_ID=GNU", "-DLANGUAGE=CXX", "-DMODE=VERSION"], capture_output=True, text=True)
# using files in $(cwd)/.cmake/CheckForQtVersion.cmake
# it will output a file to ${CMAKE_BINARY_DIR}/qt_version.txt "${QT_VERSION}"
# Configure the CMake command
cwd = pathlib.Path().absolute()
build_temp = f"{pathlib.Path(cwd)}" + "/build/qt_version_checker"
os.makedirs(build_temp, exist_ok=True)
debug = None
config = "Debug" if debug else "Release"
cmake_args = [
"-S", str(cwd) + "/.cmake/CheckForQtVersion",
"-B", str(build_temp),
"-DBUILD_PYTHON=ON",
"-DCMAKE_CXX_FLAGS_INIT:STRING=",
"-DCMAKE_GENERATOR:STRING=Ninja",
"-DCMAKE_BUILD_TYPE:STRING=" + config,
]
# 如果是Windows,强制使用CL.exe 作为C_compiler
if platform.system() == "Windows":
cmake_args += [
"-DCMAKE_C_COMPILER=cl.exe",
"-DCMAKE_CXX_COMPILER=cl.exe",
]
build_args = [
"--config", config,
"--", "-j8"
]
env = os.environ.copy() # 获取当前环境变量副本
subprocess.run(["cmake"] + cmake_args, check=True, env=env)
try:
with open(str(build_temp) + "/qt_version.txt", 'r') as file:
qt_version = file.read().strip()
print(f"Qt Version: {qt_version}")
return qt_version
except FileNotFoundError:
print(f"Error: File {str(build_temp)}/qt_version.txt not found.")
return None
except Exception as e:
print(f"Unexpected error: {e}")
return None
lingmoui = CMakeExtension("LingmoUI")
qt_version = check_qt_version()
if qt_version is None:
print("Warning: Qt version not found, please install Qt and try again.")
qt_version = "6.7.3"
# Get time in YYYMMDDHHMMSS format using time module
# 获取当前时间的时间戳
current_time = time()
# 将时间戳转换为UTC时间
utc_time = gmtime(current_time)
# 格式化为 YYYYMMDDHH
formatted_time = strftime('%Y%m%d%H', utc_time)
setup(name="LingmoUIPy",
version="3.1.1b" + formatted_time,
description="This is LingmoUI for Python",
ext_modules=[lingmoui],
packages=['LingmoUIPy'],
cmdclass={"build_ext": BuildExt}, # 使用自定义的 build_ext 类
install_requires=[
f"pyside6=={qt_version}",
],
classifiers=[
"Environment :: Console",
"Environment :: MacOS X",
"Environment :: X11 Applications :: Qt",
"Environment :: Win32 (MS Windows)",
"Intended Audience :: Developers",
"License :: Other/Proprietary License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft",
"Operating System :: Microsoft :: Windows",
"Programming Language :: C++",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Database",
"Topic :: Software Development",
"Topic :: Software Development :: Code Generators",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Topic :: Software Development :: User Interfaces",
"Topic :: Software Development :: Widget Sets",
],
python_requires=">=3.9"
)