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)),
82 "-DCMAKE_OSX_DEPLOYMENT_TARGET=10.15",
83 "-DPython3_EXECUTABLE={}".format(sys.executable.replace(
"\\",
"/")),
84 "-DLLVM_ENABLE_PROJECTS=mlir",
85 "-DLLVM_EXTERNAL_PROJECTS=circt",
86 "-DLLVM_EXTERNAL_CIRCT_SOURCE_DIR={}".format(circt_dir),
87 "-DLLVM_TARGETS_TO_BUILD=host",
88 "-DMLIR_ENABLE_BINDINGS_PYTHON=ON",
89 "-DCIRCT_BINDINGS_PYTHON_ENABLED=ON",
90 "-DCIRCT_RELEASE_TAG_ENABLED=ON",
91 "-DCIRCT_RELEASE_TAG=firtool"
92 ] + cmake_linker + cmake_generator
98 if platform.system() ==
"Linux":
99 python_libdir = sysconfig.get_config_var(
'LIBDIR')
100 python_library = sysconfig.get_config_var(
'LIBRARY')
101 if python_libdir
and not os.path.isabs(python_library):
102 python_library = os.path.join(python_libdir, python_library)
103 if python_library
and not os.path.exists(python_library):
104 print(
"Detected static linked python. Faking a library for cmake.")
105 fake_libdir = os.path.join(cmake_build_dir,
"fake_python",
"lib")
106 os.makedirs(fake_libdir, exist_ok=
True)
107 fake_library = os.path.join(fake_libdir,
108 sysconfig.get_config_var(
'LIBRARY'))
109 subprocess.check_call([
"ar",
"q", fake_library])
110 cmake_args.append(
"-DPython3_LIBRARY:PATH={}".format(fake_library))
112 os.makedirs(cmake_build_dir, exist_ok=
True)
113 if os.path.exists(cmake_install_dir):
114 shutil.rmtree(cmake_install_dir)
115 cmake_cache_file = os.path.join(cmake_build_dir,
"CMakeCache.txt")
116 if os.path.exists(cmake_cache_file):
117 os.remove(cmake_cache_file)
118 subprocess.check_call([
"cmake", llvm_dir] + cmake_args, cwd=cmake_build_dir)
119 subprocess.check_call(
120 [
"cmake",
"--build",
".",
"--target",
"install-CIRCTPythonModules"],
122 if os.path.exists(target_dir):
123 os.remove(target_dir)
124 shutil.copytree(os.path.join(cmake_install_dir,
"python_packages",
139 author=
"Mike Urbach",
140 author_email=
"mikeurbach@gmail.com",
141 description=
"CIRCT Python Bindings",
143 include_package_data=
True,
145 CMakeExtension(
"circt._mlir_libs._mlir"),
146 CMakeExtension(
"circt._mlir_libs._circt"),
149 "build": CustomBuild,
150 "built_ext": NoopBuildExtension,
151 "build_py": CMakeBuild,
154 packages=find_namespace_packages(include=[
def __init__(self, name, sourcedir="")
def build_extension(self, ext)