-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathsetup.py
More file actions
332 lines (277 loc) · 10.9 KB
/
Copy pathsetup.py
File metadata and controls
332 lines (277 loc) · 10.9 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#! /usr/bin/env python
# NOTE: You can test building wheels locally with
# `python -m build --wheel`
# Then in the `dist` directory, `pip install dawdreamer`
import contextlib
import glob
import os
import os.path
import platform
import shutil
import subprocess
import sysconfig
from pathlib import Path
import setuptools
from setuptools import setup
from setuptools.dist import Distribution
this_dir = os.path.abspath(os.path.dirname(__file__))
def get_dawdreamer_version():
import xml.etree.ElementTree as ET
tree = ET.parse(Path(__file__).parent / "DawDreamer.jucer")
root = tree.getroot()
version = root.attrib["version"]
return version
DAWDREAMER_VERSION = get_dawdreamer_version()
# Write version file so it's available in installed packages
version_file = Path(this_dir) / "dawdreamer" / "_version.py"
version_file.write_text(f'__version__ = "{DAWDREAMER_VERSION}"\n')
class BinaryDistribution(Distribution):
"""Distribution which always forces a binary package with platform name"""
def has_ext_modules(foo):
return True
def _needs_build(build_so: str, source_dir: str) -> bool:
"""Check if the .so needs rebuilding by comparing mtimes against C++ sources."""
if not os.path.isfile(build_so):
return True
so_mtime = os.path.getmtime(build_so)
for ext in ("*.cpp", "*.h"):
for src_file in glob.glob(os.path.join(source_dir, ext)):
if os.path.getmtime(src_file) > so_mtime:
return True
return False
def _run(cmd: list[str], **kwargs):
"""Run a subprocess, printing the command and raising on failure."""
print(f" Running: {' '.join(cmd)}")
subprocess.check_call(cmd, **kwargs)
def _build_libsamplerate():
"""Build libsamplerate if not already built."""
libsr_dir = os.path.join(this_dir, "thirdparty", "libsamplerate")
build_dir = os.path.join(libsr_dir, "build_release")
# Check if already built (look for the library file)
if os.path.isdir(build_dir) and any(
f.endswith((".a", ".so", ".dylib", ".lib"))
for f in os.listdir(build_dir)
if os.path.isfile(os.path.join(build_dir, f))
):
return
print("Building libsamplerate...")
cmake_args = ["-DCMAKE_BUILD_TYPE=Release", f"-B{build_dir}"]
if platform.system() == "Darwin":
archs = os.environ.get("ARCHS", "arm64" if os.uname().machine == "arm64" else "x86_64")
cmake_args.append(f"-DCMAKE_OSX_ARCHITECTURES={archs}")
cmake_args.append("-DCMAKE_OSX_DEPLOYMENT_TARGET=12.0")
cmake_args.append("-DLIBSAMPLERATE_EXAMPLES=off")
_run(["cmake"] + cmake_args, cwd=libsr_dir)
_run(["cmake", "--build", build_dir, "--config", "Release"], cwd=libsr_dir)
def _build_and_copy_linux() -> str:
"""Build on Linux and return the destination .so path."""
dest_so = os.path.join(this_dir, "dawdreamer", "dawdreamer.so")
build_so = os.path.join(this_dir, "Builds", "LinuxMakefile", "build", "libdawdreamer.so")
source_dir = os.path.join(this_dir, "Source")
if (
not _needs_build(build_so, source_dir)
and os.path.isfile(dest_so)
and os.path.getmtime(dest_so) >= os.path.getmtime(build_so)
):
return dest_so
if _needs_build(build_so, source_dir):
_build_libsamplerate()
python_include = sysconfig.get_path("include")
makefile_dir = os.path.join(this_dir, "Builds", "LinuxMakefile")
print(f"Building DawDreamer (Python include: {python_include})...")
_run(
["make", "CONFIG=Release", f"CXXFLAGS=-I{python_include}", f"-j{os.cpu_count() or 1}"],
cwd=makefile_dir,
)
if not os.path.isfile(build_so):
raise FileNotFoundError(
f"Build output not found: {build_so}\n"
" Try: cd Builds/LinuxMakefile && make CONFIG=Release"
)
print(f"Copying {build_so} -> {dest_so}")
shutil.copy2(build_so, dest_so)
return dest_so
def _build_and_copy_darwin() -> str:
"""Build on macOS and return the destination .so path."""
dest_so = os.path.join(this_dir, "dawdreamer", "dawdreamer.so")
archs = os.environ.get("ARCHS", "arm64" if os.uname().machine == "arm64" else "x86_64")
configuration = f"Release-{archs}"
build_folder = os.path.join(this_dir, "Builds", "MacOSX", "build", configuration)
build_so = os.path.join(build_folder, "dawdreamer.so")
build_dylib = os.path.join(build_folder, "dawdreamer.so.dylib")
source_dir = os.path.join(this_dir, "Source")
# Check if we need to build
effective_build = build_so if os.path.isfile(build_so) else build_dylib
if (
not _needs_build(effective_build, source_dir)
and os.path.isfile(dest_so)
and os.path.getmtime(dest_so) >= os.path.getmtime(effective_build)
):
return dest_so
if _needs_build(effective_build, source_dir):
_build_libsamplerate()
print(f"Building DawDreamer (ARCHS={archs})...")
_run(
[
"xcodebuild",
f"ARCHS={archs}",
f"-configuration={configuration}",
"-project",
os.path.join(this_dir, "Builds", "MacOSX", "DawDreamer.xcodeproj"),
"CODE_SIGN_IDENTITY=",
"CODE_SIGNING_REQUIRED=NO",
"CODE_SIGN_ENTITLEMENTS=",
"CODE_SIGNING_ALLOWED=NO",
]
)
# Xcode produces .dylib, rename to .so
if os.path.isfile(build_dylib) and not os.path.isfile(build_so):
os.rename(build_dylib, build_so)
if not os.path.isfile(build_so):
raise FileNotFoundError(
f"Build output not found: {build_so}\n" f" Try: ARCHS={archs} ./build_macos.sh"
)
print(f"Copying {build_so} -> {dest_so}")
shutil.copy2(build_so, dest_so)
return dest_so
def _build_and_copy_windows() -> str:
"""Build on Windows and return the destination .pyd path."""
dest_pyd = os.path.join(this_dir, "dawdreamer", "dawdreamer.pyd")
build_folder = os.path.join(
this_dir, "Builds", "VisualStudio2022", "x64", "Release", "Dynamic Library"
)
build_dll = os.path.join(build_folder, "dawdreamer.dll")
source_dir = os.path.join(this_dir, "Source")
if (
not _needs_build(build_dll, source_dir)
and os.path.isfile(dest_pyd)
and os.path.getmtime(dest_pyd) >= os.path.getmtime(build_dll)
):
return dest_pyd
if _needs_build(build_dll, source_dir):
_build_libsamplerate()
print("Building DawDreamer...")
_run(
[
"msbuild",
os.path.join(this_dir, "Builds", "VisualStudio2022", "DawDreamer.sln"),
"/property:Configuration=Release",
]
)
if not os.path.isfile(build_dll):
raise FileNotFoundError(
f"Build output not found: {build_dll}\n"
" Try: msbuild Builds/VisualStudio2022/DawDreamer.sln /property:Configuration=Release"
)
print(f"Copying {build_dll} -> {dest_pyd}")
shutil.copy2(build_dll, dest_pyd)
return dest_pyd
ext_modules = []
package_data = []
with contextlib.suppress(Exception):
shutil.copytree("licenses", os.path.join("dawdreamer", "licenses"))
if platform.system() == "Windows":
dest = _build_and_copy_windows()
package_data += [dest]
elif platform.system() == "Linux":
dest = _build_and_copy_linux()
package_data += [dest]
elif platform.system() == "Darwin":
dest = _build_and_copy_darwin()
package_data += [dest]
else:
raise NotImplementedError(f"setup.py hasn't been implemented for platform: {platform}.")
# copy architecture files
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
if ignore is not None and ignore(os.fspath(src), [item]):
continue
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
shutil.copy2(s, d)
destination = copytree(
os.path.join(this_dir, "thirdparty", "faust", "architecture"),
os.path.join(this_dir, "dawdreamer", "architecture"),
ignore=shutil.ignore_patterns(
"*.dll",
"*.so",
"*.html",
"*.wav",
"*.mp3",
"*.png",
"*.jpg",
"*.pdf",
"*.a",
"*.wasm",
"*.data",
),
)
# architecture files
architecture_files = list(
glob.glob(os.path.join(this_dir, "dawdreamer/architecture/*"), recursive=True)
)
if not architecture_files:
raise ValueError("You need to put the architecture directory inside dawdreamer.")
package_data += architecture_files
# Faust libraries
faustlibraries = list(
glob.glob(os.path.join(this_dir, "dawdreamer/faustlibraries/*"), recursive=True)
)
if not faustlibraries:
raise ValueError("You need to put the faustlibraries repo inside dawdreamer.")
package_data += faustlibraries
package_data += list(glob.glob("dawdreamer/licenses/*", recursive=True))
# Every item in package_data should be inside the dawdreamer directory.
# Then we make the paths relative to this directory.
package_data = [
os.path.relpath(os.path.abspath(a), os.path.join(this_dir, "dawdreamer")).replace("\\", "/")
for a in package_data
]
# print("package_data: ", package_data)
long_description = (Path(__file__).parent / "README.md").read_text()
setup(
name="dawdreamer",
url="https://github.com/DBraun/DawDreamer",
project_urls={
"Documentation": "https://dirt.design/DawDreamer",
"Source": "https://github.com/DBraun/DawDreamer",
},
version=DAWDREAMER_VERSION,
author="David Braun",
author_email="braun@ccrma.stanford.edu",
description="An audio-processing Python library supporting core DAW features",
long_description=long_description,
long_description_content_type="text/markdown",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: MacOS",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: C++",
"Programming Language :: Python",
"Topic :: Multimedia :: Sound/Audio",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
keywords="audio music sound",
python_requires=">=3.8",
install_requires=[],
packages=setuptools.find_packages(),
py_modules=["dawdreamer"],
include_package_data=True,
package_data={"": package_data},
zip_safe=False,
distclass=BinaryDistribution,
ext_modules=ext_modules,
)