2525import os
2626import shutil
2727import subprocess
28+ import sys
2829
2930from setuptools import Extension , find_packages , setup
3031from setuptools .command .build_ext import build_ext
3132from setuptools .command .develop import develop
3233
3334ROOT_DIR = os .path .abspath (os .path .dirname (__file__ ))
34- SRC_DIR = os .path .join (ROOT_DIR , "ucm" , "csrc" , "ucmnfsstore" )
35- INSTALL_DIR = os .path .join (ROOT_DIR , "ucm" , "store" )
35+ FSSTORE_SRC_DIR = os .path .join (ROOT_DIR , "ucm" , "csrc" , "ucmnfsstore" )
36+ GSA_SRC_DIR = os .path .join (ROOT_DIR , "ucm" , "csrc" , "gsaoffloadops" )
37+ PREFETCH_SRC_DIR = os .path .join (ROOT_DIR , "ucm" , "csrc" , "ucmprefetch" )
38+
39+ FSSTORE_INSTALL_DIR = os .path .join (ROOT_DIR , "ucm" , "store" )
40+ GSA_INSTALL_DIR = os .path .join (ROOT_DIR , "ucm" , "ucm_sparse" )
41+
3642PLATFORM = os .getenv ("PLATFORM" )
43+ RUNTIME_ENVIRONMENT = os .getenv ("RUNTIME_ENVIRONMENT" )
3744
3845
3946def _is_cuda () -> bool :
@@ -56,62 +63,95 @@ def run(self):
5663 self .build_cmake (ext )
5764
5865 def build_cmake (self , ext : CMakeExtension ):
59- build_dir = os .path .abspath (self .build_temp )
66+ build_dir = os .path .abspath (os . path . join ( self .build_temp , ext . name ) )
6067 os .makedirs (build_dir , exist_ok = True )
68+
69+ cmake_args = [
70+ "cmake" ,
71+ f"-DCMAKE_BUILD_TYPE=Release" ,
72+ f"-DPYTHON_EXECUTABLE={ sys .executable } " ,
73+ ]
74+
75+ cmake_args .append ("-DDOWNLOAD_DEPENDENCE=ON" )
6176 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- )
77+ cmake_args .append ("-DRUNTIME_ENVIRONMENT=cuda" )
7178 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- )
79+ cmake_args .append ("-DRUNTIME_ENVIRONMENT=ascend" )
8180 else :
8281 raise RuntimeError (
8382 "No supported accelerator found. "
8483 "Please ensure either CUDA or NPU is available."
8584 )
8685
87- subprocess . check_call ([ "make" , "-j" , "8" ], cwd = build_dir )
86+ cmake_args . append ( ext . sourcedir )
8887
89- so_file = None
88+ print (f"[INFO] Building { ext .name } module with CMake" )
89+ print (f"[INFO] Source directory: { ext .sourcedir } " )
90+ print (f"[INFO] Build directory: { build_dir } " )
91+
92+ subprocess .check_call (cmake_args , cwd = build_dir )
93+
94+ if ext .name in ["nfsstore" , "gsa_offload_ops" ]:
95+ subprocess .check_call (["make" , "-j" , "8" ], cwd = build_dir )
96+ else :
97+ # 对于gsa_prefetch使用cmake --build
98+ subprocess .check_call (
99+ ["cmake" , "--build" , "." , "--config" , "Release" , "--" , "-j8" ],
100+ cwd = build_dir ,
101+ )
102+
103+ self ._copy_so_files (ext )
104+
105+ def _copy_so_files (self , ext : CMakeExtension ):
106+ """复制编译好的.so文件"""
90107 so_search_dir = os .path .join (ext .sourcedir , "output" , "lib" )
91108 if not os .path .exists (so_search_dir ):
92109 raise FileNotFoundError (f"{ so_search_dir } does not exist!" )
93110
94- so_file = None
111+ so_files = []
112+ search_patterns = [ext .name ]
113+
114+ if ext .name == "nfsstore" :
115+ search_patterns .extend (["ucmnfsstore" ])
116+ elif ext .name == "gsa_offload_ops" :
117+ search_patterns .extend (["gsa_offload_ops" ])
118+ elif ext .name == "gsa_prefetch" :
119+ search_patterns .extend (["prefetch" ])
120+
95121 for file in os .listdir (so_search_dir ):
96- if file .startswith ("ucmnfsstore" ) and file .endswith (".so" ):
97- so_file = file
98- break
122+ if file .endswith (".so" ) or ".so." in file :
123+ for pattern in search_patterns :
124+ if pattern in file :
125+ so_files .append (file )
126+ break
127+
128+ if ext .name == "nfsstore" :
129+ install_dir = FSSTORE_INSTALL_DIR
130+ build_install_dir = "ucm/store"
131+ else :
132+ install_dir = GSA_INSTALL_DIR
133+ build_install_dir = "ucm_sparse"
134+
135+ for so_file in so_files :
136+ src_path = os .path .join (so_search_dir , so_file )
137+ dev_path = os .path .join (install_dir , so_file )
138+ dst_path = os .path .join (self .build_lib , build_install_dir , so_file )
139+
140+ os .makedirs (os .path .dirname (dst_path ), exist_ok = True )
141+ shutil .copy (src_path , dst_path )
142+ print (f"[INFO] Copied { so_file } → { dst_path } " )
143+
144+ if isinstance (self .distribution .get_command_obj ("develop" ), develop ):
145+ os .makedirs (os .path .dirname (dev_path ), exist_ok = True )
146+ shutil .copy (src_path , dev_path )
147+ print (f"[INFO] Copied in editable mode { so_file } → { dev_path } " )
99148
100- if not so_file :
101- raise FileNotFoundError (
102- "Compiled .so file not found in output/lib directory."
103- )
104149
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 (self .build_lib , "ucm" , "store" , so_file )
108- os .makedirs (os .path .dirname (dst_path ), exist_ok = True )
109- shutil .copy (src_path , dst_path )
110- print (f"[INFO] Copied { src_path } → { dst_path } " )
111- if isinstance (self .distribution .get_command_obj ("develop" ), develop ):
112- shutil .copy (src_path , dev_path )
113- print (f"[INFO] Copied in editable mode { src_path } → { dev_path } " )
150+ ext_modules = []
114151
152+ ext_modules .append (CMakeExtension (name = "nfsstore" , sourcedir = FSSTORE_SRC_DIR ))
153+ ext_modules .append (CMakeExtension (name = "gsa_offload_ops" , sourcedir = GSA_SRC_DIR ))
154+ ext_modules .append (CMakeExtension (name = "gsa_prefetch" , sourcedir = PREFETCH_SRC_DIR ))
115155
116156setup (
117157 name = "ucm" ,
@@ -120,7 +160,7 @@ def build_cmake(self, ext: CMakeExtension):
120160 author = "Unified Cache Team" ,
121161 packages = find_packages (),
122162 python_requires = ">=3.10" ,
123- ext_modules = [ CMakeExtension ( name = "ucmnfsstore" , sourcedir = SRC_DIR )] ,
163+ ext_modules = ext_modules ,
124164 cmdclass = {"build_ext" : CMakeBuild },
125165 zip_safe = False ,
126166)
0 commit comments