Skip to content

Commit

Permalink
ctypesgen: allow passing in custom pre-processor
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Jan 18, 2024
1 parent 7248bc4 commit 4558229
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pypdfium2 includes helpers to simplify common use cases, while the raw PDFium/ct
* <a id="user-content-install-source" class="anchor" href="#install-source">From source 🔗</a>

* Dependencies:
- System: git, C pre-processor (e.g. gcc, has to be in `$PATH`)
- System: git, C pre-processor (gcc/clang - alternatively, specify the command to envoke via `$CPP`)
- Python: ctypesgen (pypdfium2-team fork), wheel, setuptools. Usually installed automatically.

* Get the code
Expand Down
20 changes: 14 additions & 6 deletions setupsrc/pypdfium2_setup/packaging_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,9 @@ def run_ctypesgen(target_dir, headers_dir, flags=[], guard_symbols=False, compil
assert getattr(ctypesgen, "PYPDFIUM2_SPECIFIC", False), "pypdfium2 requires fork of ctypesgen"
import ctypesgen.__main__

args = ["--library", "pdfium", "--no-macro-guards"]
args = ["-l", "pdfium"]

# library loading
if run_lds:
args += ["--runtime-libdirs", *run_lds]
if not allow_system_despite_libdirs:
Expand All @@ -445,21 +446,28 @@ def run_ctypesgen(target_dir, headers_dir, flags=[], guard_symbols=False, compil
else:
args += ["--no-load-library"]

# style
args += ["--no-macro-guards"]
if not guard_symbols:
args += ["--no-symbol-guards"]

# pre-processor - if not given, pypdfium2-ctypesgen will try to auto-select as available (gcc/clang)
c_preproc = os.environ.get("CPP", None)
if c_preproc:
args += ["--cpp", c_preproc]
if flags:
args += ["-D"] + [PdfiumFlagsDict[f] for f in flags]
if Host.system == SysNames.windows:
# If we are on a windows host, add the relevant define to expose windows-only members.
# Note, this is not currently active for our wheels, since we're packaging everything on Linux. It might be possible to divide packaging in native OS hosts in the future, or specify external headers for cross compilation.
# If we are on a Windows host, add the relevant define to expose Windows-only members.
# Note, this is not currently active for our wheels, since we're packaging everything on Linux. It might be possible to divide packaging in native OS hosts in the future, or specify external headers for symbol spoofing.
args += ["-D", "_WIN32"]

# try to exclude some garbage aliases that get pulled in from struct tags
# symbols - try to exclude some garbage aliases that get pulled in from struct tags
# (this captures anything that ends with _, _t, or begins with _, and is not needed by other symbols)
args += ["--symbol-rules", r"if_needed=\w+_$|\w+_t$|_\w+"]

bindings = target_dir / BindingsFN
args += ["--headers"] + [h.name for h in sorted(headers_dir.glob("*.h"))] + ["-o", bindings]
# input / output
args += ["--headers"] + [h.name for h in sorted(headers_dir.glob("*.h"))] + ["-o", target_dir/BindingsFN]

with tmp_cwd_context(headers_dir):
ctypesgen.__main__.main([str(a) for a in args])
Expand Down

0 comments on commit 4558229

Please sign in to comment.