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,11 @@ def _enable_sparse() -> bool:
4553
4654
4755def _is_cuda () -> bool :
48- return PLATFORM == "cuda"
49-
56+ return PLATFORM == "cuda" or (hasattr (torch , "cuda" ) and torch .cuda .is_available ())
5057
51- def _is_npu () -> bool :
52- return PLATFORM == "ascend"
5358
54-
55- def _is_musa () -> bool :
56- return PLATFORM == "musa"
59+ def _is_maca () -> bool :
60+ return PLATFORM == "maca"
5761
5862
5963class CMakeExtension (Extension ):
@@ -67,6 +71,8 @@ def run(self):
6771 for ext in self .extensions :
6872 self .build_cmake (ext )
6973
74+ self ._copy_so_files_to_build_lib ()
75+
7076 def build_cmake (self , ext : CMakeExtension ):
7177 build_dir = self .build_temp
7278 os .makedirs (build_dir , exist_ok = True )
@@ -93,15 +99,8 @@ def build_cmake(self, ext: CMakeExtension):
9399
94100 if _is_cuda ():
95101 cmake_args .append ("-DRUNTIME_ENVIRONMENT=cuda" )
96- elif _is_npu ():
97- cmake_args .append ("-DRUNTIME_ENVIRONMENT=ascend" )
98- elif _is_musa ():
99- cmake_args .append ("-DRUNTIME_ENVIRONMENT=musa" )
100102 else :
101- raise RuntimeError (
102- "No supported accelerator found. "
103- "Please ensure either CUDA/MUSA or NPU is available."
104- )
103+ cmake_args .append ("-DRUNTIME_ENVIRONMENT=ascend" )
105104
106105 if _enable_sparse ():
107106 cmake_args .append ("-DBUILD_UCM_SPARSE=ON" )
@@ -119,33 +118,58 @@ def build_cmake(self, ext: CMakeExtension):
119118 cwd = build_dir ,
120119 )
121120
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
122125
123- def _get_packages ():
124- """Discover Python packages, optionally filtering out sparse-related ones."""
125- packages = find_packages ()
126- if not _enable_sparse ():
127- packages = [pkg for pkg in packages if not pkg .startswith ("ucm.sparse" )]
128- return packages
126+ packages = _get_packages ()
127+ copied_count = 0
129128
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 ))
130132
131- def _get_package_data_with_so (packages = None ):
132- """Automatically discover all packages and include .so files."""
133- if packages is None :
134- packages = _get_packages ()
135- package_data = {}
133+ # Destination in build_lib
134+ build_package_dir = os .path .join (
135+ self .build_lib , package .replace ("." , os .sep )
136+ )
137+
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+ )
136162
137- for package in packages :
138- # Convert package name to directory path
139- package_dir = os .path .join (ROOT_DIR , package .replace ("." , os .sep ))
140163
141- # Check if this package directory contains .so files
142- so_files = glob (os .path .join (package_dir , "*.so" ))
143- if so_files :
144- package_data [package ] = ["*.so" ]
145- print (f"[INFO] Including .so files for package: { package } " )
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*" )
146170
147- print ( f"[INFO] Package data: { package_data } " )
148- return package_data
171+ packages = find_packages ( exclude = exclude_patterns )
172+ return packages
149173
150174
151175ext_modules = []
@@ -155,13 +179,12 @@ def _get_package_data_with_so(packages=None):
155179
156180setup (
157181 name = "uc-manager" ,
158- version = "0.1.1 " ,
182+ version = "0.1.2 " ,
159183 description = "Unified Cache Management" ,
160184 author = "Unified Cache Team" ,
161185 packages = packages ,
162186 python_requires = ">=3.10" ,
163187 ext_modules = ext_modules ,
164188 cmdclass = {"build_ext" : CMakeBuild },
165- package_data = _get_package_data_with_so (packages ),
166189 zip_safe = False ,
167190)
0 commit comments