| 1 |
# Copyright (c) 2016-present, Gregory Szorc |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software may be modified and distributed under the terms |
| 5 |
# of the BSD license. See the LICENSE file for details. |
| 6 |
|
| 7 |
from __future__ import absolute_import |
| 8 |
|
| 9 |
import cffi |
| 10 |
import distutils.ccompiler |
| 11 |
import distutils.sysconfig |
| 12 |
import os |
| 13 |
import re |
| 14 |
import subprocess |
| 15 |
import tempfile |
| 16 |
|
| 17 |
|
| 18 |
HERE = os.path.abspath(os.path.dirname(__file__)) |
| 19 |
|
| 20 |
SOURCES = [ |
| 21 |
"zstd/zstd.c", |
| 22 |
] |
| 23 |
|
| 24 |
# Headers whose preprocessed output will be fed into cdef(). |
| 25 |
HEADERS = [os.path.join(HERE, "zstd", p) for p in ("zstd.h", "zdict.h")] |
| 26 |
|
| 27 |
INCLUDE_DIRS = [ |
| 28 |
os.path.join(HERE, "zstd"), |
| 29 |
] |
| 30 |
|
| 31 |
# cffi can't parse some of the primitives in zstd.h. So we invoke the |
| 32 |
# preprocessor and feed its output into cffi. |
| 33 |
compiler = distutils.ccompiler.new_compiler() |
| 34 |
|
| 35 |
# Needed for MSVC. |
| 36 |
if hasattr(compiler, "initialize"): |
| 37 |
compiler.initialize() |
| 38 |
|
| 39 |
# This performs platform specific customizations, including honoring |
| 40 |
# environment variables like CC. |
| 41 |
distutils.sysconfig.customize_compiler(compiler) |
| 42 |
|
| 43 |
# Distutils doesn't always set compiler.preprocessor, so invoke the |
| 44 |
# preprocessor manually when needed. |
| 45 |
args = getattr(compiler, "preprocessor", None) |
| 46 |
if compiler.compiler_type == "unix": |
| 47 |
if not args: |
| 48 |
# Using .compiler respects the CC environment variable. |
| 49 |
args = [compiler.compiler[0], "-E"] |
| 50 |
args.extend( |
| 51 |
[ |
| 52 |
"-DZSTD_STATIC_LINKING_ONLY", |
| 53 |
"-DZDICT_STATIC_LINKING_ONLY", |
| 54 |
] |
| 55 |
) |
| 56 |
elif compiler.compiler_type == "msvc": |
| 57 |
if not args: |
| 58 |
args = [compiler.cc, "/EP"] |
| 59 |
args.extend( |
| 60 |
[ |
| 61 |
"/DZSTD_STATIC_LINKING_ONLY", |
| 62 |
"/DZDICT_STATIC_LINKING_ONLY", |
| 63 |
] |
| 64 |
) |
| 65 |
else: |
| 66 |
raise Exception("unsupported compiler type: %s" % compiler.compiler_type) |
| 67 |
|
| 68 |
|
| 69 |
def preprocess(path): |
| 70 |
with open(path, "rb") as fh: |
| 71 |
lines = [] |
| 72 |
it = iter(fh) |
| 73 |
|
| 74 |
for l in it: |
| 75 |
# zstd.h includes <stddef.h>, which is also included by cffi's |
| 76 |
# boilerplate. This can lead to duplicate declarations. So we strip |
| 77 |
# this include from the preprocessor invocation. |
| 78 |
# |
| 79 |
# The same things happens for including zstd.h, so give it the same |
| 80 |
# treatment. |
| 81 |
# |
| 82 |
# We define ZSTD_STATIC_LINKING_ONLY, which is redundant with the inline |
| 83 |
# #define in zstdmt_compress.h and results in a compiler warning. So drop |
| 84 |
# the inline #define. |
| 85 |
if l.startswith( |
| 86 |
( |
| 87 |
b"#include <stddef.h>", |
| 88 |
b'#include "zstd.h"', |
| 89 |
b"#define ZSTD_STATIC_LINKING_ONLY", |
| 90 |
) |
| 91 |
): |
| 92 |
continue |
| 93 |
|
| 94 |
# The preprocessor environment on Windows doesn't define include |
| 95 |
# paths, so the #include of limits.h fails. We work around this |
| 96 |
# by removing that import and defining INT_MAX ourselves. This is |
| 97 |
# a bit hacky. But it gets the job done. |
| 98 |
# TODO make limits.h work on Windows so we ensure INT_MAX is |
| 99 |
# correct. |
| 100 |
if l.startswith(b"#include <limits.h>"): |
| 101 |
l = b"#define INT_MAX 2147483647\n" |
| 102 |
|
| 103 |
# ZSTDLIB_API may not be defined if we dropped zstd.h. It isn't |
| 104 |
# important so just filter it out. Ditto for ZSTDLIB_STATIC_API and |
| 105 |
# ZDICTLIB_STATIC_API. |
| 106 |
for prefix in ( |
| 107 |
b"ZSTDLIB_API", |
| 108 |
b"ZSTDLIB_STATIC_API", |
| 109 |
b"ZDICTLIB_STATIC_API", |
| 110 |
): |
| 111 |
if l.startswith(prefix): |
| 112 |
l = l[len(prefix) :] |
| 113 |
|
| 114 |
lines.append(l) |
| 115 |
|
| 116 |
fd, input_file = tempfile.mkstemp(suffix=".h") |
| 117 |
os.write(fd, b"".join(lines)) |
| 118 |
os.close(fd) |
| 119 |
|
| 120 |
try: |
| 121 |
env = dict(os.environ) |
| 122 |
# cffi attempts to decode source as ascii. And the preprocessor |
| 123 |
# may insert non-ascii for some annotations. So try to force |
| 124 |
# ascii output via LC_ALL. |
| 125 |
env["LC_ALL"] = "C" |
| 126 |
|
| 127 |
if getattr(compiler, "_paths", None): |
| 128 |
env["PATH"] = compiler._paths |
| 129 |
process = subprocess.Popen( |
| 130 |
args + [input_file], stdout=subprocess.PIPE, env=env |
| 131 |
) |
| 132 |
output = process.communicate()[0] |
| 133 |
ret = process.poll() |
| 134 |
if ret: |
| 135 |
raise Exception("preprocessor exited with error") |
| 136 |
|
| 137 |
return output |
| 138 |
finally: |
| 139 |
os.unlink(input_file) |
| 140 |
|
| 141 |
|
| 142 |
def normalize_output(output): |
| 143 |
lines = [] |
| 144 |
for line in output.splitlines(): |
| 145 |
# CFFI's parser doesn't like __attribute__ on UNIX compilers. |
| 146 |
if line.startswith(b'__attribute__ ((visibility ("default"))) '): |
| 147 |
line = line[len(b'__attribute__ ((visibility ("default"))) ') :] |
| 148 |
|
| 149 |
if line.startswith(b"__attribute__((deprecated"): |
| 150 |
continue |
| 151 |
elif b"__declspec(deprecated(" in line: |
| 152 |
continue |
| 153 |
elif line.startswith(b"__attribute__((__unused__))"): |
| 154 |
continue |
| 155 |
|
| 156 |
lines.append(line) |
| 157 |
|
| 158 |
return b"\n".join(lines) |
| 159 |
|
| 160 |
|
| 161 |
ffi = cffi.FFI() |
| 162 |
# zstd.h uses a possible undefined MIN(). Define it until |
| 163 |
# https://github.com/facebook/zstd/issues/976 is fixed. |
| 164 |
# *_DISABLE_DEPRECATE_WARNINGS prevents the compiler from emitting a warning |
| 165 |
# when cffi uses the function. Since we statically link against zstd, even |
| 166 |
# if we use the deprecated functions it shouldn't be a huge problem. |
| 167 |
ffi.set_source( |
| 168 |
"zstandard._cffi", |
| 169 |
""" |
| 170 |
#define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| 171 |
#define ZSTD_STATIC_LINKING_ONLY |
| 172 |
#define ZSTD_DISABLE_DEPRECATE_WARNINGS |
| 173 |
#include <zstd.h> |
| 174 |
#define ZDICT_STATIC_LINKING_ONLY |
| 175 |
#define ZDICT_DISABLE_DEPRECATE_WARNINGS |
| 176 |
#include <zdict.h> |
| 177 |
""", |
| 178 |
sources=SOURCES, |
| 179 |
include_dirs=INCLUDE_DIRS, |
| 180 |
) |
| 181 |
|
| 182 |
DEFINE = re.compile(b"^\\#define ([a-zA-Z0-9_]+) ") |
| 183 |
|
| 184 |
sources = [] |
| 185 |
|
| 186 |
# Feed normalized preprocessor output for headers into the cdef parser. |
| 187 |
for header in HEADERS: |
| 188 |
preprocessed = preprocess(header) |
| 189 |
sources.append(normalize_output(preprocessed)) |
| 190 |
|
| 191 |
# #define's are effectively erased as part of going through preprocessor. |
| 192 |
# So perform a manual pass to re-add those to the cdef source. |
| 193 |
with open(header, "rb") as fh: |
| 194 |
for line in fh: |
| 195 |
line = line.strip() |
| 196 |
m = DEFINE.match(line) |
| 197 |
if not m: |
| 198 |
continue |
| 199 |
|
| 200 |
if m.group(1) == b"ZSTD_STATIC_LINKING_ONLY": |
| 201 |
continue |
| 202 |
|
| 203 |
# The parser doesn't like some constants with complex values. |
| 204 |
if m.group(1) in (b"ZSTD_LIB_VERSION", b"ZSTD_VERSION_STRING"): |
| 205 |
continue |
| 206 |
|
| 207 |
# The ... is magic syntax by the cdef parser to resolve the |
| 208 |
# value at compile time. |
| 209 |
sources.append(m.group(0) + b" ...") |
| 210 |
|
| 211 |
cdeflines = b"\n".join(sources).splitlines() |
| 212 |
cdeflines = [l for l in cdeflines if l.strip()] |
| 213 |
ffi.cdef(b"\n".join(cdeflines).decode("latin1")) |
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
ffi.compile() |