44import subprocess
55import sys
66
7- from setuptools import setup , Extension , find_packages
7+ from setuptools import setup
88from setuptools .command .build_ext import build_ext
9+ from setuptools .command .install import install
10+ from setuptools .command .bdist_wheel import bdist_wheel
911
1012NAME = "live2d-py"
1113VERSION = "0.4.4" # TODO: edit before push
1820INSTALL_REQUIRES = ["numpy" , "pyopengl" , "pillow" ]
1921
2022
21- class CMakeExtension (Extension ):
22-
23- def __init__ (self , name , sourcedir = "" ):
24- Extension .__init__ (self , name , sources = [], py_limited_api = True )
25- self .sourcedir = os .path .abspath (sourcedir )
26-
27-
2823def is_virtualenv ():
2924 return 'VIRTUAL_ENV' in os .environ
3025
3126
3227def get_base_python_path (venv_path ):
3328 return re .search ("home = (.*)\n " , open (os .path .join (venv_path , "pyvenv.cfg" ), 'r' ).read ()).group (1 )
3429
30+ def run_cmake ():
31+ cmake_args = []
32+ build_args = ["--config" , "Release" ]
3533
36- class CMakeBuild (build_ext ):
34+ if platform .system () == "Windows" :
35+ if platform .python_compiler ().find ("64 bit" ) > 0 :
36+ print ("Building for 64 bit" )
37+ cmake_args += ["-A" , "x64" ]
38+ else :
39+ print ("Building for 32 bit" )
40+ cmake_args += ["-A" , "Win32" ]
41+ # native options
42+ build_args += ["--" , "/m:2" ]
43+ else :
44+ cmake_args += ["-DCMAKE_BUILD_TYPE=" + "Release" ]
45+ build_args += ["--" , "-j2" ]
46+ build_folder = os .path .join (os .getcwd (), "build" )
47+
48+ if not os .path .exists (build_folder ):
49+ os .makedirs (build_folder )
50+
51+ if is_virtualenv ():
52+ python_installation_path = get_base_python_path (os .environ ["VIRTUAL_ENV" ])
53+ else :
54+ python_installation_path = os .path .split (sys .executable )[0 ]
55+ print ("Python installation path: " + python_installation_path )
56+ sys .stdout .flush ()
57+
58+ cmake_args += ["-DPYTHON_INSTALLATION_PATH=" + python_installation_path ]
59+
60+ cmake_setup = ["cmake" , ".." ] + cmake_args
61+ cmake_build = ["cmake" , "--build" , "." ] + build_args
62+
63+ print ("Building extension for Python {}" .format (sys .version .split ('\n ' , 1 )[0 ]))
64+ print ("Invoking CMake setup: '{}'" .format (' ' .join (cmake_setup )))
65+ sys .stdout .flush ()
66+ subprocess .check_call (cmake_setup , cwd = build_folder )
67+ print ("Invoking CMake build: '{}'" .format (' ' .join (cmake_build )))
68+ sys .stdout .flush ()
69+ subprocess .check_call (cmake_build , cwd = build_folder )
3770
38- def get_cmake_version (self ):
39- try :
40- out = subprocess .check_output (["cmake" , "--version" ])
41- except :
42- sys .stderr .write ("CMake must be installed to build the following extensions: " + ", " .join (str (self .extensions )))
43- sys .exit (1 )
44- return re .search (r"cmake version ([0-9.]+)" , out .decode ()).group (1 )
4571
46- def run (self ):
47- cmake_version = self .get_cmake_version ()
48- if platform .system () == "Windows" :
49- if cmake_version < "3.16" :
50- sys .stderr .write ("CMake >= 3.16 is required" )
51- for ext in self .extensions :
52- self .build_extension (ext )
53-
54- def build_extension (self , ext ):
55- cmake_args = []
56- build_args = ["--config" , "Release" ]
57-
58- if platform .system () == "Windows" :
59- if platform .python_compiler ().find ("64 bit" ) > 0 :
60- print ("Building for 64 bit" )
61- cmake_args += ["-A" , "x64" ]
62- else :
63- print ("Building for 32 bit" )
64- cmake_args += ["-A" , "Win32" ]
65- # native options
66- build_args += ["--" , "/m:2" ]
67- else :
68- cmake_args += ["-DCMAKE_BUILD_TYPE=" + "Release" ]
69- build_args += ["--" , "-j2" ]
70- build_folder = os .path .abspath (self .build_temp )
72+ class CMakeBuild (build_ext ):
7173
72- if not os . path . exists ( build_folder ):
73- os . makedirs ( build_folder )
74+ def run ( self ):
75+ run_cmake ( )
7476
75- if is_virtualenv ():
76- python_installation_path = get_base_python_path (os .environ ["VIRTUAL_ENV" ])
77- else :
78- python_installation_path = os .path .split (sys .executable )[0 ]
79- print ("Python installation path: " + python_installation_path )
80- sys .stdout .flush ()
8177
82- cmake_args += ["-DPYTHON_INSTALLATION_PATH=" + python_installation_path ]
78+ class BuildWheel (bdist_wheel ):
79+ def run (self ):
80+ run_cmake ()
81+ bdist_wheel .run (self )
8382
84- cmake_setup = ["cmake" , ext .sourcedir ] + cmake_args
85- cmake_build = ["cmake" , "--build" , "." ] + build_args
8683
87- print ("Building extension for Python {}" .format (sys .version .split ('\n ' , 1 )[0 ]))
88- print ("Invoking CMake setup: '{}'" .format (' ' .join (cmake_setup )))
89- sys .stdout .flush ()
90- subprocess .check_call (cmake_setup , cwd = build_folder )
91- print ("Invoking CMake build: '{}'" .format (' ' .join (cmake_build )))
92- sys .stdout .flush ()
93- subprocess .check_call (cmake_build , cwd = build_folder )
84+ class Install (install ):
85+ def run (self ):
86+ run_cmake ()
87+ install .run (self )
9488
9589
9690setup (
@@ -101,14 +95,16 @@ def build_extension(self, ext):
10195 long_description_content_type = "text/markdown" ,
10296 author = AUTHOR ,
10397 author_email = AUTHOR_EMAIL ,
104- license = "MIT " ,
98+ license = "LICENSE " ,
10599 url = URL ,
106100 install_requires = INSTALL_REQUIRES ,
107- ext_modules = [CMakeExtension ("LAppModelWrapper" , "." )],
108- cmdclass = {"build_ext" : CMakeBuild },
109- packages = find_packages ("package" ),
110- package_data = {"" : ["*.pyd" , "*.so" , "*.pyi" ]},
111- package_dir = {"" : "package" },
101+ cmdclass = {
102+ "build_ext" : CMakeBuild ,
103+ "bdist_wheel" : BuildWheel ,
104+ "install" : Install
105+ },
106+ packages = ["package.live2d" ],
107+ package_data = {"package.live2d" : ["*" , "**/*.pyd" , "**/*.so" , "**/*.pyi" , "**/*.py" ]},
112108 keywords = ["Live2D" , "Cubism Live2D" , "Cubism SDK" , "Cubism SDK for Python" ],
113109 python_requires = REQUIRES_PYTHON
114110)
0 commit comments