| 1 |
# Copyright © Advanced Micro Devices, Inc., or its affiliates. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
# setup.py |
| 5 |
from setuptools import setup, Extension |
| 6 |
from setuptools.command.build_ext import build_ext |
| 7 |
import nanobind |
| 8 |
import glob |
| 9 |
import sys |
| 10 |
import argparse |
| 11 |
import os |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
# Get ROCM_PATH from environment or use default |
| 15 |
ROCM_PATH = os.environ.get("ROCM_PATH", "/opt/rocm") |
| 16 |
HIPCC_PATH = os.path.join(ROCM_PATH, "bin", "hipcc") |
| 17 |
|
| 18 |
|
| 19 |
def parse_args(): |
| 20 |
parser = argparse.ArgumentParser(description="Origami Python Bindings Setup Script") |
| 21 |
parser.add_argument("--source", "-s", type=str, default=Path(__file__).parent.parent.resolve() / "src", help="Path to origami source directory.") |
| 22 |
args, unknown = parser.parse_known_args() |
| 23 |
return args, unknown |
| 24 |
|
| 25 |
|
| 26 |
class HIPCCBuildExt(build_ext): |
| 27 |
def build_extensions(self): |
| 28 |
if hasattr(self.compiler, 'compiler_so'): |
| 29 |
self.compiler.set_executable("compiler_so", HIPCC_PATH) |
| 30 |
if hasattr(self.compiler, 'compiler_cxx'): |
| 31 |
self.compiler.set_executable("compiler_cxx", HIPCC_PATH) |
| 32 |
# self.compiler.set_executable("linker_so", HIPCC_PATH) # optional |
| 33 |
super().build_extensions() |
| 34 |
|
| 35 |
|
| 36 |
if __name__ == "__main__": |
| 37 |
args, unknown = parse_args() |
| 38 |
sys.argv = [sys.argv[0]] + unknown # Preserve unrecognized arguments for setuptools |
| 39 |
|
| 40 |
project_root = Path(__file__).parent.parent.resolve() |
| 41 |
|
| 42 |
source_dir = Path(args.source) |
| 43 |
include_dir = project_root / "include" |
| 44 |
cpp_path = source_dir / "origami" / "*.cpp" |
| 45 |
cpp_files = sorted(glob.glob(str(cpp_path))) |
| 46 |
|
| 47 |
nanobind_base = os.path.dirname(nanobind.include_dir()) # Get nanobind base path |
| 48 |
|
| 49 |
nanobind_src = os.path.join(nanobind_base, "src", "nb_combined.cpp") # Add nanobind's runtime support source file |
| 50 |
cpp_files = ["origami_module.cpp", nanobind_src] + cpp_files |
| 51 |
|
| 52 |
print(include_dir) |
| 53 |
print(os.path.join(ROCM_PATH, "include")) |
| 54 |
|
| 55 |
ext_modules = [ |
| 56 |
Extension( |
| 57 |
"origami", |
| 58 |
cpp_files, |
| 59 |
include_dirs=[ |
| 60 |
nanobind.include_dir(), |
| 61 |
str(include_dir), |
| 62 |
os.path.join(ROCM_PATH, "include"), |
| 63 |
os.path.join(nanobind_base, "ext", "robin_map", "include"), # Add nanobind's external dependencies |
| 64 |
], |
| 65 |
language="c++", |
| 66 |
extra_compile_args=[ |
| 67 |
"-D__HIP_PLATFORM_AMD__", |
| 68 |
"-fPIC", |
| 69 |
"-std=c++17", |
| 70 |
"-O3", |
| 71 |
"-Wall", |
| 72 |
"-DNB_SHARED", |
| 73 |
"-fvisibility=hidden", |
| 74 |
], |
| 75 |
extra_link_args=[f"-L{os.path.join(ROCM_PATH, 'lib')}"], |
| 76 |
), |
| 77 |
] |
| 78 |
|
| 79 |
setup( |
| 80 |
name="origami", |
| 81 |
ext_modules=ext_modules, |
| 82 |
cmdclass={"build_ext": HIPCCBuildExt}, |
| 83 |
setup_requires=["nanobind>=2.0.0"], |
| 84 |
install_requires=["nanobind>=2.0.0"], |
| 85 |
) |