32 from distutils.command.build
import build
as _build
33 from setuptools
import find_namespace_packages, setup, Extension
34 from setuptools.command.build_ext
import build_ext
35 from setuptools.command.build_py
import build_py
42 self.run_command(
"build_py")
43 self.run_command(
"build_ext")
44 self.run_command(
"build_scripts")
50 Extension.__init__(self, name, sources=[])
51 self.
sourcedirsourcedir = os.path.abspath(sourcedir)
57 target_dir = self.build_lib
58 circt_dir = os.path.abspath(
59 os.path.join(os.path.dirname(__file__),
"..",
"..",
".."))
60 cmake_build_dir = os.getenv(
"CIRCT_CMAKE_BUILD_DIR")
61 if not cmake_build_dir:
62 cmake_build_dir = os.path.join(circt_dir,
"build")
63 cmake_install_dir = os.path.join(cmake_build_dir,
"..",
"install")
64 llvm_dir = os.getenv(
"CIRCT_LLVM_DIR")
66 llvm_dir = os.path.join(circt_dir,
"llvm",
"llvm")
69 exist_ninja = shutil.which(
"ninja")
is not None
70 cmake_generator = [
"-G",
"Ninja"]
if exist_ninja
else []
73 exist_lld = shutil.which(
"lld")
is not None
74 cmake_linker = [
"-DLLVM_USE_LINKER=lld"]
if exist_lld
else []
76 "-DCMAKE_BUILD_TYPE=Release",
77 "-DCMAKE_INSTALL_PREFIX={}".format(os.path.abspath(cmake_install_dir)),
78 "-DCMAKE_OSX_DEPLOYMENT_TARGET=10.14",
79 "-DPython3_EXECUTABLE={}".format(sys.executable.replace(
"\\",
"/")),
80 "-DLLVM_ENABLE_PROJECTS=mlir",
81 "-DLLVM_EXTERNAL_PROJECTS=circt",
82 "-DLLVM_EXTERNAL_CIRCT_SOURCE_DIR={}".format(circt_dir),
83 "-DLLVM_TARGETS_TO_BUILD=host",
84 "-DMLIR_ENABLE_BINDINGS_PYTHON=ON",
85 "-DCIRCT_BINDINGS_PYTHON_ENABLED=ON",
86 "-DCIRCT_RELEASE_TAG_ENABLED=ON",
87 "-DCIRCT_RELEASE_TAG=firtool"
88 ] + cmake_linker + cmake_generator
94 if platform.system() ==
"Linux":
95 python_libdir = sysconfig.get_config_var(
'LIBDIR')
96 python_library = sysconfig.get_config_var(
'LIBRARY')
97 if python_libdir
and not os.path.isabs(python_library):
98 python_library = os.path.join(python_libdir, python_library)
99 if python_library
and not os.path.exists(python_library):
100 print(
"Detected static linked python. Faking a library for cmake.")
101 fake_libdir = os.path.join(cmake_build_dir,
"fake_python",
"lib")
102 os.makedirs(fake_libdir, exist_ok=
True)
103 fake_library = os.path.join(fake_libdir,
104 sysconfig.get_config_var(
'LIBRARY'))
105 subprocess.check_call([
"ar",
"q", fake_library])
106 cmake_args.append(
"-DPython3_LIBRARY:PATH={}".format(fake_library))
108 os.makedirs(cmake_build_dir, exist_ok=
True)
109 if os.path.exists(cmake_install_dir):
110 shutil.rmtree(cmake_install_dir)
111 cmake_cache_file = os.path.join(cmake_build_dir,
"CMakeCache.txt")
112 if os.path.exists(cmake_cache_file):
113 os.remove(cmake_cache_file)
114 subprocess.check_call([
"cmake", llvm_dir] + cmake_args, cwd=cmake_build_dir)
115 subprocess.check_call(
116 [
"cmake",
"--build",
".",
"--target",
"install-CIRCTPythonModules"],
118 if os.path.exists(target_dir):
119 os.remove(target_dir)
120 shutil.copytree(os.path.join(cmake_install_dir,
"python_packages",
135 author=
"Mike Urbach",
136 author_email=
"mikeurbach@gmail.com",
137 description=
"CIRCT Python Bindings",
139 include_package_data=
True,
141 CMakeExtension(
"circt._mlir_libs._mlir"),
142 CMakeExtension(
"circt._mlir_libs._circt"),
145 "build": CustomBuild,
146 "built_ext": NoopBuildExtension,
147 "build_py": CMakeBuild,
150 packages=find_namespace_packages(include=[
def __init__(self, name, sourcedir="")
def build_extension(self, ext)