Skip to content

Commit

Permalink
pass build config before setuptools command; add build_editable to cu…
Browse files Browse the repository at this point in the history
…stom build backend
  • Loading branch information
nulano committed Dec 30, 2023
1 parent aaf99d1 commit 3f7f3e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
paths:
- ".ci/requirements-cibw.txt"
- ".github/workflows/wheel*"
- "setup.py"
- "wheels/*"
- "winbuild/build_prepare.py"
- "winbuild/fribidi.cmake"
Expand All @@ -14,6 +15,7 @@ on:
paths:
- ".ci/requirements-cibw.txt"
- ".github/workflows/wheel*"
- "setup.py"
- "wheels/*"
- "winbuild/build_prepare.py"
- "winbuild/fribidi.cmake"
Expand Down
52 changes: 16 additions & 36 deletions _custom_build/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,11 @@
class _CustomBuildMetaBackend(backend_class):
def run_setup(self, setup_script="setup.py"):
if self.config_settings:

def config_has(key, value):
settings = self.config_settings.get(key)
if settings:
if not isinstance(settings, list):
settings = [settings]
return value in settings

flags = []
for dependency in (
"zlib",
"jpeg",
"tiff",
"freetype",
"raqm",
"lcms",
"webp",
"webpmux",
"jpeg2000",
"imagequant",
"xcb",
):
if config_has(dependency, "enable"):
flags.append("--enable-" + dependency)
elif config_has(dependency, "disable"):
flags.append("--disable-" + dependency)
for dependency in ("raqm", "fribidi"):
if config_has(dependency, "vendor"):
flags.append("--vendor-" + dependency)
if self.config_settings.get("platform-guessing") == "disable":
flags.append("--disable-platform-guessing")
if self.config_settings.get("debug") == "true":
flags.append("--debug")
if flags:
sys.argv = sys.argv[:1] + ["build_ext"] + flags + sys.argv[1:]
sys.argv = (
sys.argv[:1]
+ [f"-C{key}={value}" for key, value in self.config_settings.items()]
+ sys.argv[1:]
)
return super().run_setup(setup_script)

def build_wheel(
Expand All @@ -54,5 +24,15 @@ def build_wheel(
self.config_settings = config_settings
return super().build_wheel(wheel_directory, config_settings, metadata_directory)

def build_editable(
self, wheel_directory, config_settings=None, metadata_directory=None
):
self.config_settings = config_settings
return super().build_editable(
wheel_directory, config_settings, metadata_directory
)


build_wheel = _CustomBuildMetaBackend().build_wheel
_backend = _CustomBuildMetaBackend()
build_wheel = _backend.build_wheel
build_editable = _backend.build_editable
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ version = {attr = "PIL.__version__"}
[tool.cibuildwheel]
before-all = ".github/workflows/wheels-dependencies.sh"
build-verbosity = 1
config-settings = "raqm=enable raqm=vendor fribidi=vendor imagequant=disable"
config-settings = "raqm=vendor fribidi=vendor imagequant=disable"
test-command = "cd {project} && .github/workflows/wheels-test.sh"
test-extras = "tests"

Expand Down
33 changes: 29 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def get_version():
return locals()["__version__"]


configuration = {}


PILLOW_VERSION = get_version()
FREETYPE_ROOT = None
HARFBUZZ_ROOT = None
Expand Down Expand Up @@ -334,15 +337,24 @@ def __iter__(self):
+ [("add-imaging-libs=", None, "Add libs to _imaging build")]
)

@staticmethod
def check_configuration(option, value):
return True if configuration.get(option) == value else None

def initialize_options(self):
self.disable_platform_guessing = None
self.disable_platform_guessing = self.check_configuration(
"platform-guessing", "disable"
)
self.add_imaging_libs = ""
build_ext.initialize_options(self)
for x in self.feature:
setattr(self, f"disable_{x}", None)
setattr(self, f"enable_{x}", None)
setattr(self, f"disable_{x}", self.check_configuration(x, "disable"))
setattr(self, f"enable_{x}", self.check_configuration(x, "enable"))
for x in ("raqm", "fribidi"):
setattr(self, f"vendor_{x}", None)
setattr(self, f"vendor_{x}", self.check_configuration(x, "vendor"))
if self.check_configuration("debug", "true"):
self.debug = True
self.parallel = configuration.get("parallel")

def finalize_options(self):
build_ext.finalize_options(self)
Expand Down Expand Up @@ -390,6 +402,9 @@ def finalize_options(self):
raise ValueError(msg)
_dbg("Using vendored version of %s", x)
self.feature.vendor.add(x)
if x == "raqm":
_dbg("--vendor-raqm implies --enable-raqm")
self.feature.required.add(x)

def _update_extension(self, name, libraries, define_macros=None, sources=None):
for extension in self.extensions:
Expand Down Expand Up @@ -985,6 +1000,16 @@ def debug_build():
Extension("PIL._imagingmorph", ["src/_imagingmorph.c"]),
]


while len(sys.argv[1]) >= 2 and sys.argv[1].startswith("-C"):
key, value = sys.argv[1][2:].split("=", 1)
old = configuration.get(key)
if old is not None:
msg = f"Conflicting options: '-C {key}={old}' and '-C {key}={value}'"
raise ValueError(msg)
configuration[key] = value
del sys.argv[1]

try:
setup(
cmdclass={"build_ext": pil_build_ext},
Expand Down

0 comments on commit 3f7f3e0

Please sign in to comment.