-
Notifications
You must be signed in to change notification settings - Fork 92
/
setup.py
190 lines (167 loc) · 6.78 KB
/
setup.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright HeteroCL authors. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import os
import sys
import subprocess
import setuptools
import distutils
from distutils.command.build import build as _build
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
args = sys.argv[1:]
nthreads = args[args.index("--nthreads") + 1] if "--nthreads" in args else 4
class CustomBuild(_build):
def finalize_options(self):
# call parent finalize_options method
_build.finalize_options(self)
self.src_dir = os.path.abspath(os.path.dirname(__file__))
self.llvm_dir = os.path.join(self.src_dir, "hcl-dialect/externals/llvm-project")
if not os.path.exists(os.path.join(self.llvm_dir, "llvm")):
raise RuntimeError(
"`llvm-project` not found. Please run `git submodule update --init --recursive` first"
)
def run(self):
self.run_command("build_py")
self.run_command("build_ext")
self.run_command("build_scripts")
class CMakeBuild(build_py):
"""Custom build command."""
def initialize_options(self):
# call parent initialize_options method
build_py.initialize_options(self)
def finalize_options(self):
# call parent finalize_options method
build_py.finalize_options(self)
def build_llvm(self):
self.announce(
f"Building LLVM 15.0.0 in {self.llvm_cmake_build_dir}...",
level=distutils.log.INFO,
)
llvm_cmake_args = [
"-DLLVM_ENABLE_PROJECTS=mlir",
"-DLLVM_BUILD_EXAMPLES=ON",
"-DLLVM_TARGETS_TO_BUILD=host",
"-DCMAKE_BUILD_TYPE=Release",
"-DLLVM_ENABLE_ASSERTIONS=ON",
"-DLLVM_INSTALL_UTILS=ON",
"-DMLIR_ENABLE_BINDINGS_PYTHON=ON",
"-DPython3_EXECUTABLE=`which python3`",
]
os.makedirs(self.llvm_cmake_build_dir, exist_ok=True)
result = subprocess.run(
["cmake", "-G", "Unix Makefiles", self.llvm_dir + "/llvm"]
+ llvm_cmake_args,
cwd=self.llvm_cmake_build_dir,
env=os.environ.copy(),
check=True,
)
if result.returncode != 0:
raise RuntimeError(
"LLVM CMake failed. Please check the error message above."
)
result = subprocess.run(
["make", f"-j{nthreads}"], cwd=self.llvm_cmake_build_dir
)
if result.returncode != 0:
raise RuntimeError(
"LLVM make failed. Please check the error message above."
)
def build_hcl_dialect(self):
self.announce(
f"Building HeteroCL Dialect in {self.hcl_cmake_build_dir}...",
level=distutils.log.INFO,
)
hcl_cmake_args = [
f"-DMLIR_DIR={self.llvm_cmake_build_dir}/lib/cmake/mlir",
f"-DLLVM_EXTERNAL_LIT={self.llvm_cmake_build_dir}/bin/llvm-lit",
"-DPYTHON_BINDING=ON",
"-DOPENSCOP=OFF",
"-DPython3_EXECUTABLE=`which python3`",
]
os.makedirs(self.hcl_cmake_build_dir, exist_ok=True)
result = subprocess.run(
["cmake", "-G", "Unix Makefiles", self.hcl_dialect_dir] + hcl_cmake_args,
cwd=self.hcl_cmake_build_dir,
env=os.environ.copy(),
check=True,
)
if result.returncode != 0:
raise RuntimeError(
"HCL-MLIR CMake failed. Please check the error message above."
)
result = subprocess.run(["make", f"-j{nthreads}"], cwd=self.hcl_cmake_build_dir)
if result.returncode != 0:
raise RuntimeError(
"HCL-MLIR make failed. Please check the error message above."
)
def install_hcl_dialect(self):
self.announce("Installing hcl_mlir python package...", level=distutils.log.INFO)
result = subprocess.run(
["pip", "install", "-e", "tools/hcl/python_packages/hcl_core"],
cwd=self.hcl_cmake_build_dir,
)
if result.returncode != 0:
raise RuntimeError(
"HCL-MLIR Python package installation failed. Please check the error message above."
)
def run(self):
self.src_dir = os.path.abspath(os.path.dirname(__file__))
self.llvm_dir = os.path.join(self.src_dir, "hcl-dialect/externals/llvm-project")
if not os.path.exists(os.path.join(self.llvm_dir, "llvm")):
raise RuntimeError(
"`llvm-project` not found. Please run `git submodule update --init --recursive` first"
)
self.llvm_cmake_build_dir = os.path.join(self.llvm_dir, "build")
if not os.path.exists(
os.path.join(self.llvm_cmake_build_dir, "bin/llvm-config")
):
self.build_llvm()
self.hcl_dialect_dir = os.path.join(self.src_dir, "hcl-dialect")
self.hcl_cmake_build_dir = os.path.join(self.hcl_dialect_dir, "build")
if not os.path.exists(
os.path.join(self.src_dir, "hcl-dialect/build/bin/hcl-opt")
):
self.build_hcl_dialect()
self.install_hcl_dialect()
build_py.run(self)
class NoopBuildExtension(build_ext):
def build_extension(self, ext):
pass
def parse_requirements(filename):
"""Load requirements from a pip requirements file."""
lineiter = (line.strip() for line in open(filename))
return [line for line in lineiter if line and not line.startswith("#")]
def setup():
with open("README.md", encoding="utf-8") as fp:
long_description = fp.read()
setuptools.setup(
name="heterocl",
description="HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Reconfigurable Computing",
version="0.5",
author="HeteroCL",
long_description=long_description,
long_description_content_type="text/markdown",
cmdclass={
"build": CustomBuild,
"build_py": CMakeBuild,
"build_ext": NoopBuildExtension,
},
setup_requires=["numpy", "pybind11", "pip", "cmake"],
install_requires=parse_requirements("requirements.txt"),
packages=setuptools.find_packages(),
package_data={"heterocl": ["harness/**/*"]},
url="https://github.com/cornell-zhang/heterocl",
python_requires=">=3.7",
classifiers=[
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering",
"Topic :: System :: Hardware",
"Operating System :: OS Independent",
],
zip_safe=True,
)
if __name__ == "__main__":
setup()