Skip to content

Commit 5fd00c3

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. Signed-off-by: Michał Górny <[email protected]>
1 parent 8598beb commit 5fd00c3

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

make_cffi.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,45 @@ def normalize_output(output):
153153

154154

155155
def get_ffi(system_zstd = False):
156-
here = os.path.abspath(os.path.dirname(__file__))
156+
zstd_sources = []
157+
include_dirs = []
158+
libraries = []
157159

158-
zstd_sources = [
159-
"zstd/zstd.c",
160-
]
160+
if not system_zstd:
161+
here = os.path.abspath(os.path.dirname(__file__))
161162

162-
# Headers whose preprocessed output will be fed into cdef().
163-
headers = [
164-
os.path.join(here, "zstd", p)
165-
for p in ("zstd_errors.h", "zstd.h", "zdict.h")
166-
]
163+
zstd_sources += [
164+
"zstd/zstd.c",
165+
]
166+
167+
# Headers whose preprocessed output will be fed into cdef().
168+
headers = [
169+
os.path.join(here, "zstd", p)
170+
for p in ("zstd_errors.h", "zstd.h", "zdict.h")
171+
]
167172

168-
include_dirs = [
169-
os.path.join(here, "zstd"),
170-
]
173+
include_dirs += [
174+
os.path.join(here, "zstd"),
175+
]
176+
else:
177+
libraries += ["zstd"]
178+
179+
# Locate headers using the preprocessor.
180+
include_re = re.compile(r'^# \d+ "([^"]+/(?:zstd_errors|zstd|zdict)\.h)"')
181+
with tempfile.TemporaryDirectory() as temp_dir:
182+
with open(os.path.join(temp_dir, "input.h"), "w") as f:
183+
f.write("""
184+
#include <zstd_errors.h>
185+
#include <zstd.h>
186+
#include <zdict.h>
187+
""")
188+
compiler.preprocess(os.path.join(temp_dir, "input.h"),
189+
os.path.join(temp_dir, "output.h"))
190+
with open(os.path.join(temp_dir, "output.h"), "r") as f:
191+
headers = list({
192+
m.group(1) for m in map(include_re.match, f)
193+
if m is not None
194+
})
171195

172196
# musl 1.1 doesn't define qsort_r. We need to force using the C90
173197
# variant.
@@ -195,6 +219,7 @@ def get_ffi(system_zstd = False):
195219
sources=zstd_sources,
196220
include_dirs=include_dirs,
197221
define_macros=define_macros,
222+
libraries=libraries,
198223
)
199224

200225
define = re.compile(rb"^#define\s+([a-zA-Z0-9_]+)\s+(\S+)")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
if CFFI_BACKEND and cffi:
139139
import make_cffi
140140

141-
extensions.append(make_cffi.get_ffi().distutils_extension())
141+
extensions.append(make_cffi.get_ffi(system_zstd=SYSTEM_ZSTD).distutils_extension())
142142

143143
version = None
144144

0 commit comments

Comments
 (0)