2222]
2323
2424# Headers whose preprocessed output will be fed into cdef().
25- HEADERS = [os .path .join (HERE , "zstd" , p ) for p in ("zstd.h" , "zdict.h" )]
25+ HEADERS = [os .path .join (HERE , "zstd" , p ) for p in ("zstd_errors.h" , " zstd.h" , "zdict.h" )]
2626
2727INCLUDE_DIRS = [
2828 os .path .join (HERE , "zstd" ),
@@ -76,8 +76,8 @@ def preprocess(path):
7676 # boilerplate. This can lead to duplicate declarations. So we strip
7777 # this include from the preprocessor invocation.
7878 #
79- # The same things happens for including zstd.h, so give it the same
80- # treatment.
79+ # The same things happens for including zstd.h and zstd_errors.h, so
80+ # give them the same treatment.
8181 #
8282 # We define ZSTD_STATIC_LINKING_ONLY, which is redundant with the inline
8383 # #define in zstdmt_compress.h and results in a compiler warning. So drop
@@ -86,11 +86,17 @@ def preprocess(path):
8686 (
8787 b"#include <stddef.h>" ,
8888 b'#include "zstd.h"' ,
89+ b'#include "zstd_errors.h"' ,
8990 b"#define ZSTD_STATIC_LINKING_ONLY" ,
9091 )
9192 ):
9293 continue
9394
95+ # There's a naked `static` before the declaration of ZSTD_customMem that
96+ # confuses the cffi parser. Strip it.
97+ if line == b'static\n ' :
98+ continue
99+
94100 # The preprocessor environment on Windows doesn't define include
95101 # paths, so the #include of limits.h fails. We work around this
96102 # by removing that import and defining INT_MAX ourselves. This is
@@ -158,18 +164,18 @@ def normalize_output(output):
158164 return b"\n " .join (lines )
159165
160166
167+
168+
161169ffi = cffi .FFI ()
162- # zstd.h uses a possible undefined MIN(). Define it until
163- # https://github.com/facebook/zstd/issues/976 is fixed.
164170# *_DISABLE_DEPRECATE_WARNINGS prevents the compiler from emitting a warning
165171# when cffi uses the function. Since we statically link against zstd, even
166172# if we use the deprecated functions it shouldn't be a huge problem.
167173ffi .set_source (
168174 "zstandard._cffi" ,
169175 """
170- #define MIN(a,b) ((a)<(b) ? (a) : (b))
171176#define ZSTD_STATIC_LINKING_ONLY
172177#define ZSTD_DISABLE_DEPRECATE_WARNINGS
178+ #include <zstd_errors.h>
173179#include <zstd.h>
174180#define ZDICT_STATIC_LINKING_ONLY
175181#define ZDICT_DISABLE_DEPRECATE_WARNINGS
@@ -179,7 +185,7 @@ def normalize_output(output):
179185 include_dirs = INCLUDE_DIRS ,
180186)
181187
182- DEFINE = re .compile (b"^ \\ #define ([a-zA-Z0-9_]+) " )
188+ DEFINE = re .compile (rb"^ #define\s+ ([a-zA-Z0-9_]+)\s+(\S+) " )
183189
184190sources = []
185191
@@ -204,9 +210,14 @@ def normalize_output(output):
204210 if m .group (1 ) in (b"ZSTD_LIB_VERSION" , b"ZSTD_VERSION_STRING" ):
205211 continue
206212
213+ # These defines create aliases from old (camelCase) type names
214+ # to the new PascalCase names, which breaks CFFI.
215+ if m .group (1 ).lower () == m .group (2 ).lower ():
216+ continue
217+
207218 # The ... is magic syntax by the cdef parser to resolve the
208219 # value at compile time.
209- sources .append (m .group (0 ) + b" ..." )
220+ sources .append (b"#define " + m .group (1 ) + b" ..." )
210221
211222cdeflines = b"\n " .join (sources ).splitlines ()
212223cdeflines = [line for line in cdeflines if line .strip ()]
0 commit comments