Skip to content

Commit 1459b11

Browse files
committed
use custom build_ext to get rid of hack
1 parent 4ae93c5 commit 1459b11

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

setup.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from setuptools import setup, Extension, find_packages
2+
from setuptools.command.build_ext import build_ext as _build_ext
23
import subprocess
34
import errno
45
import os
@@ -323,33 +324,31 @@ def unix_libraw_compile():
323324
define_macros.append(("_HAS_LIBRAW_CONFIG_H", "1" if libraw_config_found else "0"))
324325

325326
# Package Data
327+
# Always include platform-specific library globs — they harmlessly match
328+
# nothing when libraries are not bundled (e.g. system libraw or sdist).
326329
package_data = {"rawpy": ["py.typed", "*.pyi"]}
327-
328-
# Evil hack to detect if we are building/installing
329-
# (We don't want to compile libraw just for 'python setup.py --version')
330-
cmdline = "".join(sys.argv[1:])
331-
needsCompile = (
332-
any(s in cmdline for s in ["install", "bdist", "build_ext", "wheel", "develop"])
333-
and not useSystemLibraw
334-
)
335-
336-
if needsCompile:
337-
if isWindows:
338-
windows_libraw_compile()
339-
package_data["rawpy"].append("*.dll")
340-
elif isMac or isLinux:
341-
unix_libraw_compile()
342-
if isLinux:
343-
package_data["rawpy"].append("*.so*")
344-
elif isMac:
345-
package_data["rawpy"].append("*.dylib")
346-
347-
# Clean up egg-info if needed
348-
if any(s in cmdline for s in ["clean", "sdist"]):
349-
egg_info = "rawpy.egg-info"
350-
if os.path.exists(egg_info):
351-
print("removing", egg_info)
352-
shutil.rmtree(egg_info, ignore_errors=True)
330+
if isWindows:
331+
package_data["rawpy"].append("*.dll")
332+
elif isLinux:
333+
package_data["rawpy"].append("*.so*")
334+
elif isMac:
335+
package_data["rawpy"].append("*.dylib")
336+
337+
338+
# --- Custom build_ext ---
339+
# Compile LibRaw from source before building the Cython extension.
340+
# By putting this in build_ext.run(), it only runs when setuptools actually
341+
# needs to build extensions — never during metadata-only commands like
342+
# egg_info, sdist, or --version. This replaces the old sys.argv sniffing hack.
343+
344+
class build_ext(_build_ext):
345+
def run(self):
346+
if not useSystemLibraw:
347+
if isWindows:
348+
windows_libraw_compile()
349+
elif isMac or isLinux:
350+
unix_libraw_compile()
351+
super().run()
353352

354353
# Extensions
355354
extensions = cythonize(
@@ -375,4 +374,5 @@ def unix_libraw_compile():
375374
packages=find_packages(),
376375
ext_modules=extensions,
377376
package_data=package_data,
377+
cmdclass={"build_ext": build_ext},
378378
)

0 commit comments

Comments
 (0)