-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathsetup.py
More file actions
402 lines (341 loc) · 13.5 KB
/
setup.py
File metadata and controls
402 lines (341 loc) · 13.5 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext as _build_ext
import subprocess
import errno
import os
import shutil
import sys
import glob
import numpy
from Cython.Build import cythonize
# --- Configuration ---
# As rawpy is distributed under the MIT license, it cannot use or distribute
# GPL'd code. This is relevant only for the binary wheels which would have to
# bundle the GPL'd code/algorithms (extra demosaic packs).
buildGPLCode = os.getenv("RAWPY_BUILD_GPL_CODE") == "1"
useSystemLibraw = os.getenv("RAWPY_USE_SYSTEM_LIBRAW") == "1"
# Platform detection
isWindows = os.name == "nt" and "GCC" not in sys.version
isMac = sys.platform == "darwin"
isLinux = sys.platform.startswith("linux")
is64Bit = sys.maxsize > 2**32
# --- Compiler/Linker Flags ---
libraries = ["libraw_r"]
include_dirs = [numpy.get_include()] # Always include numpy headers
library_dirs = []
extra_compile_args = []
extra_link_args = []
define_macros = []
if isWindows:
extra_compile_args += ["/DWIN32"]
if isLinux:
# On Linux, we want the extension to find the bundled libraw_r.so in the same directory
extra_link_args += ["-Wl,-rpath,$ORIGIN"]
if isMac:
# On macOS, @loader_path is the equivalent of $ORIGIN — it resolves to
# the directory containing the binary that references the dylib.
extra_link_args += ["-Wl,-rpath,@loader_path"]
# --- Helper Functions ---
def _ask_pkg_config(resultlist, option, result_prefix="", sysroot=False):
pkg_config = os.environ.get("PKG_CONFIG", "pkg-config")
try:
p = subprocess.Popen([pkg_config, option, "libraw_r"], stdout=subprocess.PIPE)
except OSError as e:
if e.errno != errno.ENOENT:
raise
else:
t = p.stdout.read().decode().strip()
if p.wait() == 0:
res = t.split()
# '-I/usr/...' -> '/usr/...'
for x in res:
assert x.startswith(result_prefix)
res = [x[len(result_prefix) :] for x in res]
sysroot = sysroot and os.environ.get("PKG_CONFIG_SYSROOT_DIR", "")
if sysroot:
res = [
path if path.startswith(sysroot) else sysroot + path for path in res
]
resultlist[:] = res
def use_pkg_config():
pkg_config = os.environ.get("PKG_CONFIG", "pkg-config")
if subprocess.call([pkg_config, "--atleast-version=0.21", "libraw_r"]) != 0:
raise SystemExit("ERROR: System LibRaw is too old or not found. rawpy requires LibRaw >= 0.21.")
_ask_pkg_config(include_dirs, "--cflags-only-I", "-I", sysroot=True)
_ask_pkg_config(extra_compile_args, "--cflags-only-other")
_ask_pkg_config(library_dirs, "--libs-only-L", "-L", sysroot=True)
_ask_pkg_config(extra_link_args, "--libs-only-other")
_ask_pkg_config(libraries, "--libs-only-l", "-l")
def clone_submodules():
if not os.path.exists("external/LibRaw/libraw/libraw.h"):
print(
'LibRaw git submodule is not cloned yet, will invoke "git submodule update --init" now'
)
if os.system("git submodule update --init") != 0:
raise Exception("git failed")
def get_cmake_build_dir():
external_dir = os.path.abspath("external")
return os.path.join(external_dir, "LibRaw-cmake", "build")
def get_install_dir():
return os.path.join(get_cmake_build_dir(), "install")
def windows_libraw_compile():
clone_submodules()
cmake = "cmake"
# openmp dll
# VS 2017 and higher
vc_redist_dir = os.getenv("VCToolsRedistDir")
vs_target_arch = os.getenv("VSCMD_ARG_TGT_ARCH")
if not vc_redist_dir:
# VS 2015
if "VCINSTALLDIR" in os.environ:
vc_redist_dir = os.path.join(os.environ["VCINSTALLDIR"], "redist")
vs_target_arch = "x64" if is64Bit else "x86"
else:
vc_redist_dir = None
if vc_redist_dir and vs_target_arch:
omp_glob = os.path.join(
vc_redist_dir, vs_target_arch, "Microsoft.VC*.OpenMP", "vcomp*.dll"
)
omp_dlls = glob.glob(omp_glob)
else:
omp_dlls = []
if len(omp_dlls) == 1:
has_openmp_dll = True
omp = omp_dlls[0]
elif len(omp_dlls) > 1:
print("WARNING: disabling OpenMP because multiple runtime DLLs were found:")
for omp_dll in omp_dlls:
print(omp_dll)
has_openmp_dll = False
else:
print("WARNING: disabling OpenMP because no runtime DLLs were found")
has_openmp_dll = False
# configure and compile libraw
cwd = os.getcwd()
cmake_build = get_cmake_build_dir()
install_dir = get_install_dir()
libraw_dir = os.path.join(os.path.abspath("external"), "LibRaw")
shutil.rmtree(cmake_build, ignore_errors=True)
os.makedirs(cmake_build, exist_ok=True)
os.chdir(cmake_build)
# Important: always use Release build type, otherwise the library will depend on a
# debug version of OpenMP which is not what we bundle it with, and then it would fail
enable_openmp_flag = "ON" if has_openmp_dll else "OFF"
cmds = [
cmake
+ ' .. -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release '
+ "-DCMAKE_PREFIX_PATH="
+ os.environ["CMAKE_PREFIX_PATH"]
+ " "
+ "-DLIBRAW_PATH="
+ libraw_dir.replace("\\", "/")
+ " "
+ "-DENABLE_X3FTOOLS=ON -DENABLE_6BY9RPI=ON "
+ "-DENABLE_EXAMPLES=OFF -DENABLE_OPENMP="
+ enable_openmp_flag
+ " -DENABLE_RAWSPEED=OFF "
+ (
"-DENABLE_DEMOSAIC_PACK_GPL2=ON -DDEMOSAIC_PACK_GPL2_RPATH=../../LibRaw-demosaic-pack-GPL2 "
+ "-DENABLE_DEMOSAIC_PACK_GPL3=ON -DDEMOSAIC_PACK_GPL3_RPATH=../../LibRaw-demosaic-pack-GPL3 "
if buildGPLCode
else ""
)
+ "-DCMAKE_INSTALL_PREFIX=install",
cmake + " --build . --target install",
]
for cmd in cmds:
print(cmd)
code = os.system(cmd)
if code != 0:
sys.exit(code)
os.chdir(cwd)
# bundle runtime dlls
dll_runtime_libs = [("raw_r.dll", os.path.join(install_dir, "bin"))]
if has_openmp_dll:
# Check if OpenMP was enabled in the CMake build, independent of the flag we supplied.
# If not, we don't have to bundle the DLL.
libraw_configh = os.path.join(
install_dir, "include", "libraw", "libraw_config.h"
)
match = "#define LIBRAW_USE_OPENMP 1"
has_openmp_support = match in open(libraw_configh).read()
if has_openmp_support:
dll_runtime_libs.append((os.path.basename(omp), os.path.dirname(omp)))
else:
print(
'WARNING: "#define LIBRAW_USE_OPENMP 1" not found even though OpenMP was enabled'
)
print("Will not bundle OpenMP runtime DLL")
for filename, folder in dll_runtime_libs:
src = os.path.join(folder, filename)
dest = "rawpy/" + filename
print("copying", src, "->", dest)
shutil.copyfile(src, dest)
def unix_libraw_compile():
"""Compiles LibRaw using CMake on macOS and Linux."""
clone_submodules()
external_dir = os.path.abspath("external")
libraw_dir = os.path.join(external_dir, "LibRaw")
cmake_build = get_cmake_build_dir()
install_dir = get_install_dir()
cwd = os.getcwd()
if not os.path.exists(cmake_build):
os.makedirs(cmake_build, exist_ok=True)
os.chdir(cmake_build)
# Use @rpath so the dylib's install name becomes @rpath/libraw_r.<ver>.dylib.
# Combined with -rpath @loader_path on the extension, dyld will find the
# bundled dylib next to the .so at runtime. delocate (used in CI wheel
# builds) rewrites these paths anyway, so this is compatible with both
# plain pip installs and CI wheel builds.
install_name_dir = "@rpath" if isMac else os.path.join(install_dir, "lib")
# OpenMP: enable on Linux (GCC supports it), disable on macOS
# (Apple Clang lacks OpenMP support out of the box).
enable_openmp = "ON" if isLinux else "OFF"
# CMake arguments
cmake_args = [
"cmake",
"..",
"-DCMAKE_BUILD_TYPE=Release",
"-DLIBRAW_PATH=" + libraw_dir,
"-DENABLE_X3FTOOLS=ON",
"-DENABLE_6BY9RPI=ON",
"-DENABLE_OPENMP=" + enable_openmp,
"-DENABLE_EXAMPLES=OFF",
"-DENABLE_RAWSPEED=OFF",
"-DCMAKE_INSTALL_PREFIX=install",
"-DCMAKE_INSTALL_LIBDIR=lib",
"-DCMAKE_INSTALL_NAME_DIR=" + install_name_dir,
]
if buildGPLCode:
cmake_args.extend(
[
"-DENABLE_DEMOSAIC_PACK_GPL2=ON",
"-DDEMOSAIC_PACK_GPL2_RPATH=../../LibRaw-demosaic-pack-GPL2",
"-DENABLE_DEMOSAIC_PACK_GPL3=ON",
"-DDEMOSAIC_PACK_GPL3_RPATH=../../LibRaw-demosaic-pack-GPL3",
]
)
cmds = [" ".join(cmake_args), "cmake --build . --target install"]
for cmd in cmds:
print(f"Running: {cmd}")
if os.system(cmd) != 0:
sys.exit(f"Error executing: {cmd}")
os.chdir(cwd)
# When compiling LibRaw from source (not using system libraw), we
# copy the shared libraries into the package directory so they get
# bundled with the installed package (via package_data globs).
# The extension uses rpath ($ORIGIN on Linux, @loader_path on macOS)
# to find them at runtime.
#
# In CI, auditwheel (Linux) and delocate (macOS) further repair the
# wheel, but for editable installs and plain `pip install .` we need
# the libraries in-tree.
lib_dir = os.path.join(install_dir, "lib")
if isLinux:
libs = glob.glob(os.path.join(lib_dir, "libraw_r.so*"))
else: # macOS
libs = glob.glob(os.path.join(lib_dir, "libraw_r*.dylib"))
for lib in libs:
dest = os.path.join("rawpy", os.path.basename(lib))
if os.path.islink(lib):
if os.path.lexists(dest):
os.remove(dest)
linkto = os.readlink(lib)
os.symlink(linkto, dest)
else:
shutil.copyfile(lib, dest)
print(f"Bundling {lib} -> {dest}")
# --- Main Logic ---
# Determine if we need to compile LibRaw from source
# If using system libraw (e.g. installed via apt), we check pkg-config
libraw_config_found = False
if (isWindows or isMac or isLinux) and not useSystemLibraw:
# Build from source
install_dir = get_install_dir()
include_dirs += [os.path.join(install_dir, "include", "libraw")]
library_dirs += [os.path.join(install_dir, "lib")]
libraries = ["raw_r"]
# If building from source, we know we have the config header
libraw_config_found = True
else:
# Use system library
use_pkg_config()
for include_dir in include_dirs:
if "libraw_config.h" in os.listdir(include_dir):
libraw_config_found = True
break
# Ensure numpy headers are always included (use_pkg_config replaces the list)
if numpy.get_include() not in include_dirs:
include_dirs.insert(0, numpy.get_include())
define_macros.append(("_HAS_LIBRAW_CONFIG_H", "1" if libraw_config_found else "0"))
# Package Data
# Always include platform-specific library globs — they harmlessly match
# nothing when libraries are not bundled (e.g. system libraw or sdist).
package_data = {"rawpy": ["py.typed", "*.pyi"]}
if isWindows:
package_data["rawpy"].append("*.dll")
elif isLinux:
package_data["rawpy"].append("*.so*")
elif isMac:
package_data["rawpy"].append("*.dylib")
# --- Custom build_ext ---
# Compile LibRaw from source before building the Cython extension.
# By putting this in build_ext.run(), it only runs when setuptools actually
# needs to build extensions — never during metadata-only commands like
# egg_info, sdist, or --version. This replaces the old sys.argv sniffing hack.
class build_ext(_build_ext):
def run(self):
if not useSystemLibraw:
if isWindows:
windows_libraw_compile()
elif isMac or isLinux:
unix_libraw_compile()
super().run()
# Copy bundled shared libraries into the build output directory.
# build_py (which collects package_data) runs *before* build_ext,
# so the libraries compiled above aren't in build_lib yet.
if not useSystemLibraw:
self._copy_bundled_libs()
def _copy_bundled_libs(self):
dest_dir = os.path.join(self.build_lib, "rawpy")
os.makedirs(dest_dir, exist_ok=True)
if isWindows:
libs = glob.glob("rawpy/*.dll")
elif isLinux:
libs = glob.glob("rawpy/libraw_r.so*")
elif isMac:
libs = glob.glob("rawpy/libraw_r*.dylib")
else:
return
for lib in libs:
dest = os.path.join(dest_dir, os.path.basename(lib))
if os.path.islink(lib):
if os.path.lexists(dest):
os.remove(dest)
os.symlink(os.readlink(lib), dest)
else:
shutil.copyfile(lib, dest)
# Extensions
extensions = cythonize(
[
Extension(
"rawpy._rawpy",
include_dirs=include_dirs,
sources=[os.path.join("rawpy", "_rawpy.pyx")],
libraries=libraries,
library_dirs=library_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
)
]
)
# Version
exec(open("rawpy/_version.py").read())
setup(
version=__version__,
packages=find_packages(),
ext_modules=extensions,
package_data=package_data,
cmdclass={"build_ext": build_ext},
)