-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
107 lines (90 loc) · 3.82 KB
/
setup.py
File metadata and controls
107 lines (90 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# *****************************************************************************
# Copyright (c) 2025 AISS Group at Harbin Institute of Technology. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# *****************************************************************************
import os
import re
import sys
import subprocess
import shutil
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
def get_version():
init_file_path = os.path.join(os.path.dirname(__file__), "asnumpy", "__init__.py")
with open(init_file_path, "r", encoding="utf-8") as f:
content = f.read()
version_match = re.search(r'^__version__ = [\'"]([^\'"]*)[\'"]', content, re.MULTILINE)
if version_match:
return version_match.group(1)
else:
raise RuntimeError("Unable to find version string in asnumpy/__init__.py")
class CMakeClean(build_ext):
def run(self):
project_root = os.path.abspath(os.path.dirname(__file__))
build_temp = self.build_temp
if os.path.exists(build_temp):
print(f"Cleaning build directory: {build_temp}")
shutil.rmtree(build_temp)
super().run()
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
project_root = os.path.abspath(os.path.dirname(__file__))
build_temp = self.build_temp
build_lib = self.build_lib
os.makedirs(build_temp, exist_ok=True)
cmake_args = [
f"-DCMAKE_BUILD_TYPE=Release",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={os.path.abspath(os.path.join(build_lib, 'asnumpy', 'lib'))}",
f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={os.path.abspath(os.path.join(build_lib, 'asnumpy', 'lib'))}",
]
try:
subprocess.check_output(["ninja", "--version"])
ninja_path = shutil.which("ninja")
print(f"Found ninja at: {ninja_path}")
cmake_args += [
"-G", "Ninja",
f"-DCMAKE_MAKE_PROGRAM={ninja_path}"
]
except (OSError, subprocess.CalledProcessError):
print("Ninja not found or not working. Falling back to default generator.")
print(f"Configuring CMake in {build_temp}...")
subprocess.check_call(["cmake", project_root] + cmake_args, cwd=build_temp)
build_args = ["--config", "Release"]
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
if hasattr(self, "parallel") and self.parallel:
build_args += [f"-j{self.parallel}"]
else:
build_args += [f"-j{os.cpu_count() or 1}"]
print(f"Building with CMake in {build_temp}...")
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp)
npu_array_extension = Extension(
name="asnumpy.lib.asnumpy_core",
sources=[]
)
# Package configuration
setup(
ext_modules=[npu_array_extension],
cmdclass={
"clean": CMakeClean,
"build_ext": CMakeBuild,
},
packages=["asnumpy", "asnumpy.lib"],
zip_safe = False,
)