2323#
2424
2525import os
26+ import shutil
2627import subprocess
27- from distutils .core import setup
28- from pathlib import Path
2928
30- from setuptools import Extension , find_packages
29+ from setuptools import Extension , find_packages , setup
3130from setuptools .command .build_ext import build_ext
31+ from setuptools .command .develop import develop
3232
33- ROOT_DIR = os .path .dirname (__file__ )
33+ ROOT_DIR = os .path .abspath (os .path .dirname (__file__ ))
34+ SRC_DIR = os .path .join (ROOT_DIR , "unifiedcache" , "csrc" , "ucmnfsstore" )
35+ INSTALL_DIR = os .path .join (ROOT_DIR , "unifiedcache" , "ucm_connector" )
3436PLATFORM = os .getenv ("PLATFORM" )
3537
3638
37- def get_path (* filepath ) -> str :
38- return os .path .join (ROOT_DIR , * filepath )
39-
40-
4139def _is_cuda () -> bool :
4240 return PLATFORM == "cuda"
4341
@@ -46,84 +44,85 @@ def _is_npu() -> bool:
4644 return PLATFORM == "ascend"
4745
4846
49- class BuildUCMExtension (build_ext ):
50- """Build UCM Extensions Using Cmake"""
47+ class CMakeExtension (Extension ):
48+ def __init__ (self , name : str , sourcedir : str = "" ):
49+ super ().__init__ (name , sources = [])
50+ self .sourcedir = os .path .abspath (sourcedir )
51+
5152
53+ class CMakeBuild (build_ext ):
5254 def run (self ):
53- package_path = os .path .abspath (
54- os .path .join (os .path .dirname (__file__ ), "unifiedcache" )
55- )
56- ucm_nfs_path = os .path .join (package_path , "csrc" , "ucmnfsstore" )
57- if not os .path .exists (ucm_nfs_path ):
58- raise RuntimeError (f"Expected directory { ucm_nfs_path } does not exist" )
59-
60- build_path = os .path .join (ucm_nfs_path , "build" )
61- if not os .path .exists (build_path ):
62- os .makedirs (build_path )
63-
64- os .chdir (build_path )
65- if _is_npu ():
66- cmake_command = [
67- "cmake" ,
68- "-DDOWNLOAD_DEPENDENCE=ON" ,
69- "-DRUNTIME_ENVIRONMENT=ascend" ,
70- ".." ,
71- ucm_nfs_path ,
72- ]
73- elif _is_cuda ():
74- cmake_command = [
75- "cmake" ,
76- "-DDOWNLOAD_DEPENDENCE=ON" ,
77- "-DRUNTIME_ENVIRONMENT=cuda" ,
78- ".." ,
79- ucm_nfs_path ,
80- ]
55+ for ext in self .extensions :
56+ self .build_cmake (ext )
57+
58+ def build_cmake (self , ext : CMakeExtension ):
59+ build_dir = os .path .abspath (self .build_temp )
60+ os .makedirs (build_dir , exist_ok = True )
61+ if _is_cuda ():
62+ subprocess .check_call (
63+ [
64+ "cmake" ,
65+ "-DDOWNLOAD_DEPENDENCE=ON" ,
66+ "-DRUNTIME_ENVIRONMENT=cuda" ,
67+ ext .sourcedir ,
68+ ],
69+ cwd = build_dir ,
70+ )
71+ elif _is_npu ():
72+ subprocess .check_call (
73+ [
74+ "cmake" ,
75+ "-DDOWNLOAD_DEPENDENCE=ON" ,
76+ "-DRUNTIME_ENVIRONMENT=ascend" ,
77+ ext .sourcedir ,
78+ ],
79+ cwd = build_dir ,
80+ )
8181 else :
8282 raise RuntimeError (
8383 "No supported accelerator found. "
8484 "Please ensure either CUDA or NPU is available."
8585 )
86- subprocess .check_call (cmake_command )
8786
88- make_command = ["make" , "-j" , "8" ]
89- subprocess .check_call (make_command )
87+ subprocess .check_call (["make" , "-j" , "8" ], cwd = build_dir )
9088
91- output_lib_path = os .path .join (ucm_nfs_path , "output" , "lib" )
92- so_files = [f for f in os .listdir (output_lib_path ) if f .endswith (".so" )]
93- for so_file in so_files :
94- src = os .path .join (output_lib_path , so_file )
95- dest = os .path .join (package_path , "ucm_connector" , so_file )
96- os .rename (src , dest )
89+ so_file = None
90+ so_search_dir = os .path .join (ext .sourcedir , "output" , "lib" )
91+ if not os .path .exists (so_search_dir ):
92+ raise FileNotFoundError (f"{ so_search_dir } does not exist!" )
9793
98- os .chdir (os .path .dirname (__file__ ))
99- super ().run ()
94+ so_file = None
95+ for file in os .listdir (so_search_dir ):
96+ if file .startswith ("ucmnfsstore" ) and file .endswith (".so" ):
97+ so_file = file
98+ break
10099
100+ if not so_file :
101+ raise FileNotFoundError (
102+ "Compiled .so file not found in output/lib directory."
103+ )
101104
102- cmdclass = {
103- "build_ext" : BuildUCMExtension ,
104- }
105+ src_path = os .path .join (so_search_dir , so_file )
106+ dev_path = os .path .join (INSTALL_DIR , so_file )
107+ dst_path = os .path .join (
108+ self .build_lib , "unifiedcache" , "ucm_connector" , so_file
109+ )
110+ os .makedirs (os .path .dirname (dst_path ), exist_ok = True )
111+ shutil .copy (src_path , dst_path )
112+ print (f"[INFO] Copied { src_path } → { dst_path } " )
113+ if isinstance (self .distribution .get_command_obj ("develop" ), develop ):
114+ shutil .copy (src_path , dev_path )
115+ print (f"[INFO] Copied in editable mode { src_path } → { dev_path } " )
105116
106- ext_modules = [
107- Extension (
108- name = "unifiedcache.ucm_connector.ucmnfsstore" ,
109- sources = [],
110- )
111- ]
112117
113- print ("FOUND PACKAGES:" , find_packages ())
114118setup (
115119 name = "unifiedcache" ,
116120 version = "0.0.1" ,
117- author = "Unified Cache Team" ,
118121 description = "Unified Cache Management" ,
122+ author = "Unified Cache Team" ,
119123 packages = find_packages (),
120- ext_modules = ext_modules ,
121- cmdclass = cmdclass ,
122- package_data = {
123- "unifiedcache.ucm_connector" : ["*.so" ],
124- },
125- include_package_data = True ,
126- install_requires = [],
127- extras_require = {},
128124 python_requires = ">=3.10" ,
125+ ext_modules = [CMakeExtension (name = "ucmnfsstore" , sourcedir = SRC_DIR )],
126+ cmdclass = {"build_ext" : CMakeBuild },
127+ zip_safe = False ,
129128)
0 commit comments