Skip to content

Commit 8a9ddf7

Browse files
committed
cffi: Support --system-zstd
Support using the system zstd library with the CFFI backend. Use the GCC / Clang preprocessor output to find the paths to header files for preprocessing. Link to the system library, matching the behavior for the C backend.
1 parent 6ec59f1 commit 8a9ddf7

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

make_cffi.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,39 @@ def normalize_output(output):
145145
return b"\n".join(lines)
146146

147147

148-
def get_ffi():
149-
here = os.path.abspath(os.path.dirname(__file__))
148+
def get_ffi(system_zstd = False):
149+
zstd_sources = []
150+
include_dirs = []
151+
libraries = []
150152

151-
zstd_sources = [
152-
"zstd/zstd.c",
153-
]
153+
if not system_zstd:
154+
here = os.path.abspath(os.path.dirname(__file__))
154155

155-
# Headers whose preprocessed output will be fed into cdef().
156-
headers = [os.path.join(here, "zstd", p) for p in ("zstd.h", "zdict.h")]
156+
zstd_sources += [
157+
"zstd/zstd.c",
158+
]
159+
160+
# Headers whose preprocessed output will be fed into cdef().
161+
headers = [os.path.join(here, "zstd", p) for p in ("zstd.h", "zdict.h")]
157162

158-
include_dirs = [
159-
os.path.join(here, "zstd"),
160-
]
163+
include_dirs += [
164+
os.path.join(here, "zstd"),
165+
]
166+
else:
167+
libraries += ["zstd"]
168+
169+
# Locate headers using the preprocessor.
170+
include_re = re.compile(r'^# \d+ "([^"]+/(?:zstd|zdict)\.h)"')
171+
with tempfile.TemporaryDirectory() as temp_dir:
172+
with open(os.path.join(temp_dir, "input.h"), "w") as f:
173+
f.write("#include <zstd.h>\n#include <zdict.h>\n")
174+
compiler.preprocess(os.path.join(temp_dir, "input.h"),
175+
os.path.join(temp_dir, "output.h"))
176+
with open(os.path.join(temp_dir, "output.h"), "r") as f:
177+
headers = list({
178+
m.group(1) for m in map(include_re.match, f)
179+
if m is not None
180+
})
161181

162182
ffi = cffi.FFI()
163183
# zstd.h uses a possible undefined MIN(). Define it until
@@ -178,6 +198,7 @@ def get_ffi():
178198
""",
179199
sources=zstd_sources,
180200
include_dirs=include_dirs,
201+
libraries=libraries,
181202
)
182203

183204
DEFINE = re.compile(b"^\\#define ([a-zA-Z0-9_]+) ")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
if CFFI_BACKEND and cffi:
113113
import make_cffi
114114

115-
extensions.append(make_cffi.get_ffi().distutils_extension())
115+
extensions.append(make_cffi.get_ffi(system_zstd=SYSTEM_ZSTD).distutils_extension())
116116

117117
version = None
118118

0 commit comments

Comments
 (0)