| 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 |
import distutils.ccompiler |
| 8 |
import distutils.command.build_ext |
| 9 |
import distutils.extension |
| 10 |
import distutils.util |
| 11 |
import glob |
| 12 |
import os |
| 13 |
import shutil |
| 14 |
import subprocess |
| 15 |
import sys |
| 16 |
|
| 17 |
|
| 18 |
ext_includes = [ |
| 19 |
"c-ext", |
| 20 |
] |
| 21 |
|
| 22 |
ext_sources = [ |
| 23 |
"c-ext/backend_c.c", |
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
def get_c_extension( |
| 28 |
support_legacy=False, |
| 29 |
system_zstd=False, |
| 30 |
name="zstandard.backend_c", |
| 31 |
warnings_as_errors=False, |
| 32 |
root=None, |
| 33 |
): |
| 34 |
"""Obtain a distutils.extension.Extension for the C extension. |
| 35 |
|
| 36 |
``support_legacy`` controls whether to compile in legacy zstd format support. |
| 37 |
|
| 38 |
``system_zstd`` controls whether to compile against the system zstd library. |
| 39 |
For this to work, the system zstd library and headers must match what |
| 40 |
python-zstandard is coded against exactly. |
| 41 |
|
| 42 |
``name`` is the module name of the C extension to produce. |
| 43 |
|
| 44 |
``warnings_as_errors`` controls whether compiler warnings are turned into |
| 45 |
compiler errors. |
| 46 |
|
| 47 |
``root`` defines a root path that source should be computed as relative |
| 48 |
to. This should be the directory with the main ``setup.py`` that is |
| 49 |
being invoked. If not defined, paths will be relative to this file. |
| 50 |
""" |
| 51 |
actual_root = os.path.abspath(os.path.dirname(__file__)) |
| 52 |
root = root or actual_root |
| 53 |
|
| 54 |
sources = sorted(set([os.path.join(actual_root, p) for p in ext_sources])) |
| 55 |
local_include_dirs = [os.path.join(actual_root, d) for d in ext_includes] |
| 56 |
|
| 57 |
if not system_zstd: |
| 58 |
local_include_dirs.append(os.path.join(actual_root, "zstd")) |
| 59 |
|
| 60 |
depends = sorted(glob.glob(os.path.join(actual_root, "c-ext", "*"))) |
| 61 |
|
| 62 |
compiler = distutils.ccompiler.new_compiler() |
| 63 |
|
| 64 |
# Needed for MSVC. |
| 65 |
if hasattr(compiler, "initialize"): |
| 66 |
compiler.initialize() |
| 67 |
|
| 68 |
if compiler.compiler_type == "unix": |
| 69 |
compiler_type = "unix" |
| 70 |
elif compiler.compiler_type == "msvc": |
| 71 |
compiler_type = "msvc" |
| 72 |
elif compiler.compiler_type == "mingw32": |
| 73 |
compiler_type = "mingw32" |
| 74 |
else: |
| 75 |
raise Exception("unhandled compiler type: %s" % compiler.compiler_type) |
| 76 |
|
| 77 |
extra_args = [] |
| 78 |
|
| 79 |
if system_zstd: |
| 80 |
extra_args.append("-DZSTD_MULTITHREAD") |
| 81 |
else: |
| 82 |
extra_args.append("-DZSTD_SINGLE_FILE") |
| 83 |
extra_args.append("-DZSTDLIB_VISIBLE=") |
| 84 |
extra_args.append("-DZDICTLIB_VISIBLE=") |
| 85 |
extra_args.append("-DZSTDERRORLIB_VISIBLE=") |
| 86 |
|
| 87 |
if compiler_type == "unix": |
| 88 |
extra_args.append("-fvisibility=hidden") |
| 89 |
|
| 90 |
if not system_zstd and support_legacy: |
| 91 |
extra_args.append("-DZSTD_LEGACY_SUPPORT=1") |
| 92 |
|
| 93 |
if warnings_as_errors: |
| 94 |
if compiler_type in ("unix", "mingw32"): |
| 95 |
extra_args.append("-Werror") |
| 96 |
elif compiler_type == "msvc": |
| 97 |
extra_args.append("/WX") |
| 98 |
else: |
| 99 |
assert False |
| 100 |
|
| 101 |
libraries = ["zstd"] if system_zstd else [] |
| 102 |
|
| 103 |
# Python 3.7 doesn't like absolute paths. So normalize to relative. |
| 104 |
sources = [os.path.relpath(p, root) for p in sources] |
| 105 |
local_include_dirs = [os.path.relpath(p, root) for p in local_include_dirs] |
| 106 |
depends = [os.path.relpath(p, root) for p in depends] |
| 107 |
|
| 108 |
if "ZSTD_EXTRA_COMPILER_ARGS" in os.environ: |
| 109 |
extra_args.extend( |
| 110 |
distutils.util.split_quoted(os.environ["ZSTD_EXTRA_COMPILER_ARGS"]) |
| 111 |
) |
| 112 |
|
| 113 |
# TODO compile with optimizations. |
| 114 |
return distutils.extension.Extension( |
| 115 |
name, |
| 116 |
sources, |
| 117 |
include_dirs=local_include_dirs, |
| 118 |
depends=depends, |
| 119 |
extra_compile_args=extra_args, |
| 120 |
libraries=libraries, |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
class RustExtension(distutils.extension.Extension): |
| 125 |
def __init__(self, name, root): |
| 126 |
super().__init__(name, []) |
| 127 |
|
| 128 |
self.root = root |
| 129 |
|
| 130 |
self.depends.extend( |
| 131 |
[ |
| 132 |
os.path.join(root, "Cargo.toml"), |
| 133 |
os.path.join(root, "rust-ext", "src", "lib.rs"), |
| 134 |
] |
| 135 |
) |
| 136 |
|
| 137 |
def build(self, build_dir, get_ext_path_fn): |
| 138 |
env = os.environ.copy() |
| 139 |
env["PYTHON_SYS_EXECUTABLE"] = sys.executable |
| 140 |
# Needed for try_reserve() |
| 141 |
env["RUSTC_BOOTSTRAP"] = "1" |
| 142 |
|
| 143 |
args = [ |
| 144 |
"cargo", |
| 145 |
"build", |
| 146 |
"--release", |
| 147 |
"--target-dir", |
| 148 |
str(build_dir), |
| 149 |
] |
| 150 |
|
| 151 |
subprocess.run(args, env=env, cwd=self.root, check=True) |
| 152 |
|
| 153 |
dest_path = get_ext_path_fn(self.name) |
| 154 |
|
| 155 |
libname = self.name.split(".")[-1] |
| 156 |
|
| 157 |
if os.name == "nt": |
| 158 |
rust_lib_filename = "%s.dll" % libname |
| 159 |
elif sys.platform == "darwin": |
| 160 |
rust_lib_filename = "lib%s.dylib" % libname |
| 161 |
else: |
| 162 |
rust_lib_filename = "lib%s.so" % libname |
| 163 |
|
| 164 |
rust_lib = os.path.join(build_dir, "release", rust_lib_filename) |
| 165 |
os.makedirs(os.path.dirname(rust_lib), exist_ok=True) |
| 166 |
|
| 167 |
shutil.copy2(rust_lib, dest_path) |
| 168 |
|
| 169 |
|
| 170 |
class RustBuildExt(distutils.command.build_ext.build_ext): |
| 171 |
def build_extension(self, ext): |
| 172 |
if isinstance(ext, RustExtension): |
| 173 |
ext.build( |
| 174 |
build_dir=os.path.abspath(self.build_temp), |
| 175 |
get_ext_path_fn=self.get_ext_fullpath, |
| 176 |
) |
| 177 |
else: |
| 178 |
super().build_extension(ext) |
| 179 |
|
| 180 |
|
| 181 |
def get_rust_extension( |
| 182 |
root=None, |
| 183 |
): |
| 184 |
actual_root = os.path.abspath(os.path.dirname(__file__)) |
| 185 |
root = root or actual_root |
| 186 |
|
| 187 |
return RustExtension("zstandard.backend_rust", root) |