2323#
2424
2525import os
26+ import shutil
2627import subprocess
2728import sys
2829import sysconfig
30+ import warnings
2931from glob import glob
3032
3133import pybind11
3436from setuptools import Extension , find_packages , setup
3537from setuptools .command .build_ext import build_ext
3638
39+ # Suppress warnings about packages absent from packages configuration
40+ # These are expected for C++ source directories, test directories, etc.
41+ warnings .filterwarnings (
42+ "ignore" , message = ".*Package.*is absent from the `packages` configuration.*"
43+ )
44+
3745ROOT_DIR = os .path .abspath (os .path .dirname (__file__ ))
3846PLATFORM = os .getenv ("PLATFORM" )
3947
@@ -45,15 +53,7 @@ def _enable_sparse() -> bool:
4553
4654
4755def _is_cuda () -> bool :
48- return PLATFORM == "cuda"
49-
50-
51- def _is_npu () -> bool :
52- return PLATFORM == "ascend"
53-
54-
55- def _is_musa () -> bool :
56- return PLATFORM == "musa"
56+ return PLATFORM == "cuda" or (hasattr (torch , "cuda" ) and torch .cuda .is_available ())
5757
5858
5959def _is_maca () -> bool :
@@ -71,6 +71,8 @@ def run(self):
7171 for ext in self .extensions :
7272 self .build_cmake (ext )
7373
74+ self ._copy_so_files_to_build_lib ()
75+
7476 def build_cmake (self , ext : CMakeExtension ):
7577 build_dir = self .build_temp
7678 os .makedirs (build_dir , exist_ok = True )
@@ -97,18 +99,8 @@ def build_cmake(self, ext: CMakeExtension):
9799
98100 if _is_cuda ():
99101 cmake_args .append ("-DRUNTIME_ENVIRONMENT=cuda" )
100- elif _is_npu ():
101- cmake_args .append ("-DRUNTIME_ENVIRONMENT=ascend" )
102- elif _is_musa ():
103- cmake_args .append ("-DRUNTIME_ENVIRONMENT=musa" )
104- elif _is_maca ():
105- cmake_args .append ("-DRUNTIME_ENVIRONMENT=maca" )
106- cmake_args .append ("-DBUILD_UCM_SPARSE=OFF" )
107102 else :
108- raise RuntimeError (
109- "No supported accelerator found. "
110- "Please ensure either CUDA/MUSA or NPU is available."
111- )
103+ cmake_args .append ("-DRUNTIME_ENVIRONMENT=ascend" )
112104
113105 if _enable_sparse ():
114106 cmake_args .append ("-DBUILD_UCM_SPARSE=ON" )
@@ -126,33 +118,58 @@ def build_cmake(self, ext: CMakeExtension):
126118 cwd = build_dir ,
127119 )
128120
121+ def _copy_so_files_to_build_lib (self ):
122+ """Copy .so files from source directories to build_lib for installation."""
123+ if not hasattr (self , "build_lib" ) or not self .build_lib :
124+ return
129125
130- def _get_packages ():
131- """Discover Python packages, optionally filtering out sparse-related ones."""
132- packages = find_packages ()
133- if not _enable_sparse ():
134- packages = [pkg for pkg in packages if not pkg .startswith ("ucm.sparse" )]
135- return packages
126+ packages = _get_packages ()
127+ copied_count = 0
136128
129+ for package in packages :
130+ # Source directory where CMake outputs .so files
131+ source_package_dir = os .path .join (ROOT_DIR , package .replace ("." , os .sep ))
137132
138- def _get_package_data_with_so (packages = None ):
139- """Automatically discover all packages and include .so files."""
140- if packages is None :
141- packages = _get_packages ()
142- package_data = {}
133+ # Destination in build_lib
134+ build_package_dir = os .path .join (
135+ self .build_lib , package .replace ("." , os .sep )
136+ )
143137
144- for package in packages :
145- # Convert package name to directory path
146- package_dir = os .path .join (ROOT_DIR , package .replace ("." , os .sep ))
138+ # Find all .so files in the source package directory
139+ so_files = glob (os .path .join (source_package_dir , "*.so" ))
140+
141+ if so_files :
142+ # Ensure destination directory exists
143+ os .makedirs (build_package_dir , exist_ok = True )
144+
145+ # Copy each .so file
146+ for so_file in so_files :
147+ dest_file = os .path .join (
148+ build_package_dir , os .path .basename (so_file )
149+ )
150+ shutil .copy2 (so_file , dest_file )
151+ copied_count += 1
152+ print (
153+ f"[INFO] Copied { os .path .basename (so_file )} to { build_package_dir } "
154+ )
155+
156+ if copied_count > 0 :
157+ print (f"[INFO] Successfully copied { copied_count } .so file(s) to build_lib" )
158+ else :
159+ print (
160+ "[WARNING] No .so files found to copy. Extensions may not have been built."
161+ )
147162
148- # Check if this package directory contains .so files
149- so_files = glob (os .path .join (package_dir , "*.so" ))
150- if so_files :
151- package_data [package ] = ["*.so" ]
152- print (f"[INFO] Including .so files for package: { package } " )
153163
154- print (f"[INFO] Package data: { package_data } " )
155- return package_data
164+ def _get_packages ():
165+ """Discover Python packages, optionally filtering out sparse-related ones."""
166+ sparse_enabled = _enable_sparse ()
167+ exclude_patterns = []
168+ if not sparse_enabled :
169+ exclude_patterns .append ("ucm.sparse*" )
170+
171+ packages = find_packages (exclude = exclude_patterns )
172+ return packages
156173
157174
158175ext_modules = []
@@ -169,6 +186,5 @@ def _get_package_data_with_so(packages=None):
169186 python_requires = ">=3.10" ,
170187 ext_modules = ext_modules ,
171188 cmdclass = {"build_ext" : CMakeBuild },
172- package_data = _get_package_data_with_so (packages ),
173189 zip_safe = False ,
174190)
0 commit comments