2017-12-05 03:16:25 +00:00
|
|
|
# Support code for building a C extension with zstd files
|
|
|
|
#
|
2017-12-04 22:59:35 +00:00
|
|
|
# Copyright (c) 2016-present, Gregory Szorc
|
2017-12-05 03:16:25 +00:00
|
|
|
# 2017-present, Thomas Waldmann (mods to make it more generic)
|
2017-12-04 22:59:35 +00:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This software may be modified and distributed under the terms
|
|
|
|
# of the BSD license. See the LICENSE file for details.
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2017-12-05 03:16:25 +00:00
|
|
|
# zstd files, structure as seen in zstd project repository:
|
|
|
|
|
|
|
|
zstd_sources = [
|
2019-02-06 13:38:43 +00:00
|
|
|
'lib/common/debug.c',
|
2017-12-05 03:16:25 +00:00
|
|
|
'lib/common/entropy_common.c',
|
|
|
|
'lib/common/error_private.c',
|
|
|
|
'lib/common/fse_decompress.c',
|
|
|
|
'lib/common/pool.c',
|
|
|
|
'lib/common/threading.c',
|
|
|
|
'lib/common/xxhash.c',
|
|
|
|
'lib/common/zstd_common.c',
|
|
|
|
'lib/compress/fse_compress.c',
|
2019-02-06 13:38:43 +00:00
|
|
|
'lib/compress/hist.c',
|
2017-12-05 03:16:25 +00:00
|
|
|
'lib/compress/huf_compress.c',
|
|
|
|
'lib/compress/zstd_compress.c',
|
|
|
|
'lib/compress/zstd_double_fast.c',
|
|
|
|
'lib/compress/zstd_fast.c',
|
|
|
|
'lib/compress/zstd_lazy.c',
|
|
|
|
'lib/compress/zstd_ldm.c',
|
|
|
|
'lib/compress/zstd_opt.c',
|
|
|
|
'lib/compress/zstdmt_compress.c',
|
|
|
|
'lib/decompress/huf_decompress.c',
|
2019-02-06 13:38:43 +00:00
|
|
|
'lib/decompress/zstd_ddict.c',
|
2017-12-05 03:16:25 +00:00
|
|
|
'lib/decompress/zstd_decompress.c',
|
2019-02-06 13:38:43 +00:00
|
|
|
'lib/decompress/zstd_decompress_block.c',
|
2017-12-05 03:16:25 +00:00
|
|
|
'lib/dictBuilder/cover.c',
|
|
|
|
'lib/dictBuilder/divsufsort.c',
|
2019-02-06 13:38:43 +00:00
|
|
|
'lib/dictBuilder/fastcover.c',
|
2017-12-05 03:16:25 +00:00
|
|
|
'lib/dictBuilder/zdict.c',
|
2017-12-04 22:59:35 +00:00
|
|
|
]
|
|
|
|
|
2017-12-05 03:16:25 +00:00
|
|
|
zstd_sources_legacy = [
|
|
|
|
'lib/deprecated/zbuff_common.c',
|
|
|
|
'lib/deprecated/zbuff_compress.c',
|
|
|
|
'lib/deprecated/zbuff_decompress.c',
|
|
|
|
'lib/legacy/zstd_v01.c',
|
|
|
|
'lib/legacy/zstd_v02.c',
|
|
|
|
'lib/legacy/zstd_v03.c',
|
|
|
|
'lib/legacy/zstd_v04.c',
|
|
|
|
'lib/legacy/zstd_v05.c',
|
|
|
|
'lib/legacy/zstd_v06.c',
|
|
|
|
'lib/legacy/zstd_v07.c',
|
2017-12-04 22:59:35 +00:00
|
|
|
]
|
|
|
|
|
2017-12-05 03:16:25 +00:00
|
|
|
zstd_includes = [
|
|
|
|
'lib',
|
|
|
|
'lib/common',
|
|
|
|
'lib/compress',
|
|
|
|
'lib/decompress',
|
|
|
|
'lib/dictBuilder',
|
2017-12-04 22:59:35 +00:00
|
|
|
]
|
|
|
|
|
2017-12-05 03:16:25 +00:00
|
|
|
zstd_includes_legacy = [
|
|
|
|
'lib/deprecated',
|
|
|
|
'lib/legacy',
|
2017-12-04 22:59:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-03-12 01:46:00 +00:00
|
|
|
def zstd_ext_kwargs(bundled_path, prefer_system, multithreaded=False, legacy=False, **kwargs):
|
2017-12-05 03:16:25 +00:00
|
|
|
"""amend kwargs with zstd suff for a distutils.extension.Extension initialization.
|
|
|
|
|
|
|
|
bundled_path: relative (to this file) path to the bundled library source code files
|
2019-03-12 01:46:00 +00:00
|
|
|
prefer_system: prefer the system-installed library (if found) over the bundled C code
|
2017-12-05 03:16:25 +00:00
|
|
|
multithreaded: True: define ZSTD_MULTITHREAD
|
|
|
|
legacy: include legacy API support
|
|
|
|
kwargs: distutils.extension.Extension kwargs that should be amended
|
|
|
|
returns: amended kwargs
|
|
|
|
"""
|
|
|
|
def multi_join(paths, *path_segments):
|
|
|
|
"""apply os.path.join on a list of paths"""
|
|
|
|
return [os.path.join(*(path_segments + (path, ))) for path in paths]
|
|
|
|
|
2019-03-12 01:46:00 +00:00
|
|
|
define_macros = kwargs.get('define_macros', [])
|
|
|
|
|
|
|
|
system_prefix = os.environ.get('BORG_LIBZSTD_PREFIX')
|
|
|
|
if prefer_system and system_prefix:
|
|
|
|
print('Detected and preferring libzstd over bundled ZSTD')
|
|
|
|
define_macros.append(('BORG_USE_LIBZSTD', 'YES'))
|
|
|
|
system = True
|
|
|
|
else:
|
|
|
|
print('Using bundled ZSTD')
|
|
|
|
system = False
|
|
|
|
|
2017-12-05 03:16:25 +00:00
|
|
|
use_system = system and system_prefix is not None
|
|
|
|
|
|
|
|
sources = kwargs.get('sources', [])
|
|
|
|
if not use_system:
|
|
|
|
sources += multi_join(zstd_sources, bundled_path)
|
|
|
|
if legacy:
|
|
|
|
sources += multi_join(zstd_sources_legacy, bundled_path)
|
|
|
|
|
|
|
|
include_dirs = kwargs.get('include_dirs', [])
|
|
|
|
if use_system:
|
|
|
|
include_dirs += multi_join(['include'], system_prefix)
|
|
|
|
else:
|
|
|
|
include_dirs += multi_join(zstd_includes, bundled_path)
|
|
|
|
if legacy:
|
|
|
|
include_dirs += multi_join(zstd_includes_legacy, bundled_path)
|
|
|
|
|
|
|
|
library_dirs = kwargs.get('library_dirs', [])
|
|
|
|
if use_system:
|
|
|
|
library_dirs += multi_join(['lib'], system_prefix)
|
|
|
|
|
|
|
|
libraries = kwargs.get('libraries', [])
|
|
|
|
if use_system:
|
|
|
|
libraries += ['zstd', ]
|
|
|
|
|
|
|
|
extra_compile_args = kwargs.get('extra_compile_args', [])
|
|
|
|
if multithreaded:
|
|
|
|
extra_compile_args += ['-DZSTD_MULTITHREAD', ]
|
|
|
|
if not use_system:
|
|
|
|
extra_compile_args += ['-DZSTDLIB_VISIBILITY=', '-DZDICTLIB_VISIBILITY=', '-DZSTDERRORLIB_VISIBILITY=', ]
|
|
|
|
# '-fvisibility=hidden' does not work, doesn't find PyInit_compress then
|
|
|
|
if legacy:
|
|
|
|
extra_compile_args += ['-DZSTD_LEGACY_SUPPORT=1', ]
|
|
|
|
|
|
|
|
ret = dict(**kwargs)
|
|
|
|
ret.update(dict(sources=sources, extra_compile_args=extra_compile_args,
|
|
|
|
include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
|
|
|
|
return ret
|