Skip to content

Commit 6a4ad69

Browse files
authored
Include C++ source files in binary wheels (#225)
* Include C++ source files in binary wheels
1 parent b0f2f0b commit 6a4ad69

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

setup.py

+18-25
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323

2424
from distutils.util import get_platform
2525

26+
2627
class CMakeExtension(Extension):
27-
def __init__(self, name, sourcedir = ""):
28-
Extension.__init__(self, name, sources = [])
28+
def __init__(self, name, sourcedir=""):
29+
Extension.__init__(self, name, sources=[])
2930
self.sourcedir = Path(sourcedir).absolute()
3031

3132

@@ -34,9 +35,7 @@ class CMakeBuild(build_ext):
3435
This class is built upon https://github.com/diegoferigo/cmake-build-extension/blob/master/src/cmake_build_extension/build_extension.py and https://github.com/pybind/cmake_example/blob/master/setup.py
3536
"""
3637

37-
user_options = build_ext.user_options + [
38-
("define=", "D", "Define variables for CMake")
39-
]
38+
user_options = build_ext.user_options + [("define=", "D", "Define variables for CMake")]
4039

4140
def initialize_options(self):
4241
super().initialize_options()
@@ -53,21 +52,21 @@ def build_extension(self, ext: CMakeExtension):
5352
extdir = str(Path(self.get_ext_fullpath(ext.name)).parent.absolute())
5453

5554
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
56-
ninja_path = str(shutil.which('ninja'))
55+
ninja_path = str(shutil.which("ninja"))
5756

5857
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
5958
configure_args = [
6059
"-GNinja",
6160
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
6261
f"-DPYTHON_EXECUTABLE={sys.executable}",
6362
f"-DCMAKE_MAKE_PROGRAM={ninja_path}",
64-
"-DENABLE_WARNINGS=OFF", # Ignore warnings
63+
"-DENABLE_WARNINGS=OFF", # Ignore warnings
6564
]
6665

6766
if debug:
6867
configure_args += ["-DCMAKE_BUILD_TYPE=Debug"]
6968
configure_args += self.cmake_defines
70-
69+
7170
build_args = []
7271

7372
# Add more platform dependent options
@@ -79,33 +78,24 @@ def build_extension(self, ext: CMakeExtension):
7978
configure_args += ["-DENABLE_OPENMP=OFF"]
8079
elif platform.system() == "Linux":
8180
if platform.machine() == "x86_64":
82-
configure_args += [
83-
"-DENABLE_AVX=ON"
84-
] # Enable AVX if x64 on Linux
81+
configure_args += ["-DENABLE_AVX=ON"] # Enable AVX if x64 on Linux
8582
elif platform.system() == "Windows":
86-
configure_args += [
87-
"-DENABLE_OPENMP=OFF",
88-
"-DENABLE_BLAS=OFF"
89-
]
83+
configure_args += ["-DENABLE_OPENMP=OFF", "-DENABLE_BLAS=OFF"]
9084
else:
9185
raise RuntimeError(f"Unsupported '{platform.system()}' platform")
9286

9387
if not Path(self.build_temp).exists():
9488
os.makedirs(self.build_temp)
95-
96-
subprocess.check_call(
97-
["cmake", str(ext.sourcedir)] + configure_args, cwd = self.build_temp
98-
)
99-
subprocess.check_call(
100-
["cmake", "--build", "."] + build_args, cwd = self.build_temp
101-
)
89+
90+
subprocess.check_call(["cmake", str(ext.sourcedir)] + configure_args, cwd=self.build_temp)
91+
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=self.build_temp)
10292

10393

10494
with open("pennylane_lightning/_version.py") as f:
10595
version = f.readlines()[-1].split()[-1].strip("\"'")
10696

10797
requirements = [
108-
"ninja",
98+
"ninja",
10999
"numpy",
110100
"pennylane>=0.19",
111101
]
@@ -118,7 +108,8 @@ def build_extension(self, ext: CMakeExtension):
118108
"url": "https://github.com/XanaduAI/pennylane-lightning",
119109
"license": "Apache License 2.0",
120110
"packages": find_packages(where="."),
121-
"package_data": {"pennylane_lightning": ["src/*"]},
111+
"package_data": {"pennylane_lightning": ["src/*", "src/**/*"]},
112+
"include_package_data": True,
122113
"entry_points": {
123114
"pennylane.plugins": [
124115
"lightning.qubit = pennylane_lightning:LightningQubit",
@@ -129,7 +120,9 @@ def build_extension(self, ext: CMakeExtension):
129120
"long_description_content_type": "text/x-rst",
130121
"provides": ["pennylane_lightning"],
131122
"install_requires": requirements,
132-
"ext_modules": [CMakeExtension("lightning_qubit_ops")] if not os.environ.get("SKIP_COMPILATION", False) else [],
123+
"ext_modules": [CMakeExtension("lightning_qubit_ops")]
124+
if not os.environ.get("SKIP_COMPILATION", False)
125+
else [],
133126
"cmdclass": {"build_ext": CMakeBuild},
134127
"ext_package": "pennylane_lightning",
135128
}

0 commit comments

Comments
 (0)