Skip to content

Commit 413719e

Browse files
authored
Python packaging (#60)
1 parent 5611f8f commit 413719e

3 files changed

Lines changed: 101 additions & 3 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ py/neon/tool/Neon.log
6565
py/neon/Neon.log
6666

6767
py/tests/Neon.log
68+
69+
py/neon/liblibNeonPy.so
70+
71+
py/dist/
72+
73+
py/build/
74+
75+
py/neon.egg-info/

py/neon/gate.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import ctypes
22
import os
33
import warp as wp
4-
4+
import platform
55

66
class Gate(object):
77
def __init__(self):
88
self.handle_type = ctypes.c_void_p
99
# get the path of this python file
1010
current_file_path = os.path.abspath(__file__)
1111
# get the directory containing the script
12-
lib_path = os.path.dirname(current_file_path) + "/../../cmake-build-debug/libNeonPy/liblibNeonPy.so"
13-
# move up two folders with respec to script_dir
12+
# Determine platform and architecture
13+
current_platform = platform.system().lower()
14+
machine = platform.machine().lower()
15+
16+
if current_platform.startswith("linux"):
17+
platform_key = f"linux_{machine}"
18+
lib_extension = ".so"
19+
elif current_platform.startswith("darwin"):
20+
platform_key = f"macos_{machine}"
21+
lib_extension = ".dylib"
22+
elif current_platform.startswith("windows"):
23+
platform_key = f"windows_{machine}"
24+
lib_extension = ".dll"
25+
else:
26+
raise RuntimeError(f"Unsupported platform: {current_platform}")
27+
28+
lib_path = os.path.join(os.path.dirname(current_file_path), "lib", platform_key, f"liblibNeonPy{lib_extension}")
1429

1530
try:
1631
self.lib = ctypes.CDLL(lib_path)

py/setup.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import shutil
3+
import sys
4+
import platform
5+
from setuptools import setup, find_packages
6+
from setuptools.dist import Distribution
7+
from setuptools.command.build_py import build_py as build_py_orig
8+
9+
class BinaryDistribution(Distribution):
10+
"""Distribution that includes binary components."""
11+
def has_ext_modules(self):
12+
return True
13+
14+
class CustomBuildPy(build_py_orig):
15+
"""Custom build command to copy the appropriate shared library."""
16+
def run(self):
17+
# Determine platform and architecture
18+
current_platform = sys.platform
19+
machine = platform.machine().lower()
20+
21+
# Map sys.platform to your binaries directory
22+
if current_platform.startswith("linux"):
23+
platform_key = "linux_" + machine
24+
lib_extension = ".so"
25+
elif current_platform.startswith("darwin"):
26+
platform_key = "macos_" + machine
27+
lib_extension = ".dylib"
28+
elif current_platform.startswith("win"):
29+
platform_key = "windows_" + machine
30+
lib_extension = ".dll"
31+
else:
32+
raise RuntimeError(f"Unsupported platform: {current_platform}")
33+
34+
# Define the path to the shared library. TODO: This is a hack to get the path to the shared library. We need to find a better way to do this for multiple platforms.
35+
source_so = os.path.abspath(os.path.join(os.path.dirname(__file__), f'../cmake-build-debug/libNeonPy/liblibNeonPy{lib_extension}'))
36+
37+
# Define the destination directory within the build directory
38+
destination_dir = os.path.join(self.build_lib, 'neon', 'lib', platform_key)
39+
destination_so = os.path.join(destination_dir, os.path.basename(source_so))
40+
41+
# Ensure the destination directory exists
42+
os.makedirs(destination_dir, exist_ok=True)
43+
44+
# Copy the shared library to the destination directory
45+
try:
46+
shutil.copy2(source_so, destination_so)
47+
print(f"Copied {source_so} to {destination_so}")
48+
except IOError as e:
49+
print(f"Error copying {source_so} to {destination_so}: {e}")
50+
raise e
51+
52+
# Continue with the standard build process
53+
super().run()
54+
55+
setup(
56+
name="neon",
57+
version="0.1.0",
58+
packages=find_packages(),
59+
package_data={
60+
"neon": [
61+
"**/*.h", # Include all .h files recursively
62+
]
63+
},
64+
include_package_data=True,
65+
distclass=BinaryDistribution,
66+
cmdclass={
67+
'build_py': CustomBuildPy,
68+
},
69+
python_requires=">=3.10", # This should be the minimum version of python that warp supports.
70+
# TODO: We need to add warp to the requirements later. Currently we're using a custom build of warp.
71+
install_requires=[
72+
"numpy>=2.0",
73+
"nvtx"
74+
],
75+
)

0 commit comments

Comments
 (0)