From 808de33683dd40ec9d652f677a53cc19de6d67b2 Mon Sep 17 00:00:00 2001 From: geisserml Date: Mon, 27 Nov 2023 18:47:34 +0100 Subject: [PATCH] continue on refbindings --- README.md | 2 +- setupsrc/pypdfium2_setup/autorelease.py | 6 +----- setupsrc/pypdfium2_setup/packaging_base.py | 23 +++++++++++++++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 18f1b7987..798cba6a9 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ As pypdfium2 requires a C extension and has custom setup code, there are some sp - It is possible to prepend `prepared!` to install with existing platform files instead of generating on the fly; the value will be used for metadata / file inclusion. This can be helpful when installing in an isolated env where ctypesgen is not available, but it is not desirable to use the reference bindings (e.g. conda). * `$PYPDFIUM_MODULES=[raw,helpers]` defines the modules to include. Metadata adapts dynamically. - - May be used by packagers to decouple raw bindings and helpers, which can be important if packaging against system pdfium. + - May be used by packagers to decouple raw bindings and helpers, which may be relevant if packaging against system pdfium. - Would also allow to install only the raw module without helpers, or only helpers with a custom raw module. * `$PDFIUM_BINDINGS=reference` allows to override ctypesgen and use the reference bindings file `autorelease/bindings.py` instead. diff --git a/setupsrc/pypdfium2_setup/autorelease.py b/setupsrc/pypdfium2_setup/autorelease.py index 0efa48261..f9f02c9c3 100644 --- a/setupsrc/pypdfium2_setup/autorelease.py +++ b/setupsrc/pypdfium2_setup/autorelease.py @@ -22,12 +22,8 @@ def run_local(*args, **kws): def update_refbindings(version): - # We endeavor to make the reference bindings as universal and robust as possible. - # Thanks to symbol guards, we can define all standalone feature flags. - # We don't currently define flags that depend on external headers, though it should be possible in principle by adding them to $CPATH (or equivalent). Further, if we could get OS headers, this might even allow for cross-compilation with OS flags (e.g. _WIN32). - # Note that Skia is currently a standalone flag because pdfium only provides a typedef void* for a Skia canvas and casts internally RefBindingsFile.unlink() - build_pdfium_bindings(version, guard_symbols=True, flags=["V8", "XFA", "SKIA"], allow_system_despite_libdirs=True) + build_pdfium_bindings(version, guard_symbols=True, flags=REFBINDINGS_FLAGS, allow_system_despite_libdirs=True) shutil.copyfile(DataDir_Bindings/BindingsFN, RefBindingsFile) assert RefBindingsFile.exists() diff --git a/setupsrc/pypdfium2_setup/packaging_base.py b/setupsrc/pypdfium2_setup/packaging_base.py index 4f0ba5fdb..1df8ca1a7 100644 --- a/setupsrc/pypdfium2_setup/packaging_base.py +++ b/setupsrc/pypdfium2_setup/packaging_base.py @@ -63,6 +63,13 @@ ReleaseInfoURL = ReleaseURL.replace("github.com/", "api.github.com/repos/").replace("download/", "tags/") +# We endeavor to make the reference bindings as universal and robust as possible. +# Thanks to symbol guards, we can define all standalone feature flags. +# We don't currently define flags that depend on external headers, though it should be possible in principle by adding them to $CPATH (or equivalent). Further, if we could get OS headers, this might even allow for cross-compilation with OS flags (e.g. _WIN32). +# Note that Skia is currently a standalone flag because pdfium only provides a typedef void* for a Skia canvas and casts internally +REFBINDINGS_FLAGS = ["V8", "XFA", "SKIA"] + + # TODO make SysNames/ExtPlats/PlatNames iterable, consider StrEnum or something class SysNames: @@ -421,7 +428,7 @@ def run_ctypesgen(target_dir, headers_dir, flags=[], guard_symbols=False, compil bindings.write_text(text) -def build_pdfium_bindings(version, headers_dir=None, **kwargs): +def build_pdfium_bindings(version, headers_dir=None, flags=[], **kwargs): defaults = dict(flags=[], run_lds=["."], guard_symbols=False) for k, v in defaults.items(): kwargs.setdefault(k, v) @@ -431,13 +438,19 @@ def build_pdfium_bindings(version, headers_dir=None, **kwargs): if not headers_dir: headers_dir = DataDir_Bindings / "headers" - # TODO move refbindings handling into run_ctypesgen on behalf of sourcebuild? # quick and dirty patch to allow using the pre-built bindings instead of calling ctypesgen + # TODO move handler into run_ctypesgen on behalf of sourcebuild? if BindTarget == BindTarget_Ref: - print("Using refbindings as requested by env var. Note that this will bypass all bindings params (e.g. flags). The refbindings try to be as universal as possible, though.", file=sys.stderr) + print("Using reference bindings as requested by env var. This will bypass all bindings params.", file=sys.stderr) + record = read_json(AR_RecordFile) + bindings_ver = record["pdfium"] + if bindings_ver != version: + print(f"Warning: ABI version mismatch (bindings {bindings_ver}, binary target {version}). This is potentially unsafe!", file=sys.stderr) + flags_diff = set(flags).difference(REFBINDINGS_FLAGS) + if flags_diff: # == not set(...).issubset(...) + print(f"Warning: The following requested flags are not available in the reference bindings and will be discarded: {flags_diff}") shutil.copyfile(RefBindingsFile, DataDir_Bindings/BindingsFN) - ar_record = read_json(AR_RecordFile) - write_json(ver_path, dict(version=ar_record["pdfium"], flags=[], run_lds=["."], source="reference")) + write_json(ver_path, dict(version=bindings_ver, flags=REFBINDINGS_FLAGS, run_lds=["."], source="reference")) return curr_info = dict(