##// END OF EJS Templates
setup: build extensions in parallel by default...
setup: build extensions in parallel by default The build_ext distutils command in Python 3.5+ has a "parallel" option that controls whether to build extensions in parallel. It is disabled by default (None) and can be set to an integer value for number of cores or True to indicate use all available CPU cores. This commit changes our build_ext command override to set "parallel" to True unless a value has been provided by the caller. On my machine, this makes `python setup.py build_ext` 1-4s faster. It is worth noting that at this time, each individual source file constituting the extension is still built serially. For Mercurial, this means that we can't build faster than the slowest-to-build extension, which is the zstd extension by a long shot. This means that setup.py is still not very efficient at utilizing multiple cores. But we're better than before. Differential Revision: https://phab.mercurial-scm.org/D6923 # no-check-commit because of foo_bar naming

File last commit:

r43207:69de49c4 default
r43314:f9d35f01 default
Show More
make_cffi.py
207 lines | 6.3 KiB | text/x-python | PythonLexer
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 # Copyright (c) 2016-present, Gregory Szorc
# All rights reserved.
#
# This software may be modified and distributed under the terms
# of the BSD license. See the LICENSE file for details.
from __future__ import absolute_import
import cffi
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 import distutils.ccompiler
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 import os
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 import re
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 import subprocess
import tempfile
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435
HERE = os.path.abspath(os.path.dirname(__file__))
SOURCES = ['zstd/%s' % p for p in (
Gregory Szorc
zstandard: vendor python-zstandard 0.10.1...
r40157 'common/debug.c',
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 'common/entropy_common.c',
'common/error_private.c',
'common/fse_decompress.c',
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 'common/pool.c',
'common/threading.c',
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 'common/xxhash.c',
'common/zstd_common.c',
'compress/fse_compress.c',
Gregory Szorc
zstandard: vendor python-zstandard 0.10.1...
r40157 'compress/hist.c',
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 'compress/huf_compress.c',
'compress/zstd_compress.c',
Gregory Szorc
zstandard: vendor python-zstandard 0.12...
r43207 'compress/zstd_compress_literals.c',
'compress/zstd_compress_sequences.c',
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 'compress/zstd_double_fast.c',
'compress/zstd_fast.c',
'compress/zstd_lazy.c',
'compress/zstd_ldm.c',
'compress/zstd_opt.c',
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 'compress/zstdmt_compress.c',
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 'decompress/huf_decompress.c',
Gregory Szorc
zstandard: vendor python-zstandard 0.11...
r42237 'decompress/zstd_ddict.c',
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 'decompress/zstd_decompress.c',
Gregory Szorc
zstandard: vendor python-zstandard 0.11...
r42237 'decompress/zstd_decompress_block.c',
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 'dictBuilder/cover.c',
Gregory Szorc
zstandard: vendor python-zstandard 0.10.1...
r40157 'dictBuilder/fastcover.c',
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 'dictBuilder/divsufsort.c',
'dictBuilder/zdict.c',
)]
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 # Headers whose preprocessed output will be fed into cdef().
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 HEADERS = [os.path.join(HERE, 'zstd', *p) for p in (
('zstd.h',),
('dictBuilder', 'zdict.h'),
)]
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 INCLUDE_DIRS = [os.path.join(HERE, d) for d in (
'zstd',
'zstd/common',
'zstd/compress',
'zstd/decompress',
'zstd/dictBuilder',
)]
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 # cffi can't parse some of the primitives in zstd.h. So we invoke the
# preprocessor and feed its output into cffi.
compiler = distutils.ccompiler.new_compiler()
# Needed for MSVC.
if hasattr(compiler, 'initialize'):
compiler.initialize()
# Distutils doesn't set compiler.preprocessor, so invoke the preprocessor
# manually.
if compiler.compiler_type == 'unix':
args = list(compiler.executables['compiler'])
args.extend([
'-E',
'-DZSTD_STATIC_LINKING_ONLY',
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 '-DZDICT_STATIC_LINKING_ONLY',
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 ])
elif compiler.compiler_type == 'msvc':
args = [compiler.cc]
args.extend([
'/EP',
'/DZSTD_STATIC_LINKING_ONLY',
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 '/DZDICT_STATIC_LINKING_ONLY',
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 ])
else:
raise Exception('unsupported compiler type: %s' % compiler.compiler_type)
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 def preprocess(path):
with open(path, 'rb') as fh:
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 lines = []
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 it = iter(fh)
for l in it:
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 # zstd.h includes <stddef.h>, which is also included by cffi's
# boilerplate. This can lead to duplicate declarations. So we strip
# this include from the preprocessor invocation.
#
# The same things happens for including zstd.h, so give it the same
# treatment.
#
# We define ZSTD_STATIC_LINKING_ONLY, which is redundant with the inline
# #define in zstdmt_compress.h and results in a compiler warning. So drop
# the inline #define.
if l.startswith((b'#include <stddef.h>',
b'#include "zstd.h"',
b'#define ZSTD_STATIC_LINKING_ONLY')):
continue
# ZSTDLIB_API may not be defined if we dropped zstd.h. It isn't
# important so just filter it out.
if l.startswith(b'ZSTDLIB_API'):
l = l[len(b'ZSTDLIB_API '):]
lines.append(l)
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 fd, input_file = tempfile.mkstemp(suffix='.h')
os.write(fd, b''.join(lines))
os.close(fd)
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 try:
Gregory Szorc
zstandard: vendor python-zstandard 0.12...
r43207 env = dict(os.environ)
if getattr(compiler, '_paths', None):
env['PATH'] = compiler._paths
process = subprocess.Popen(args + [input_file], stdout=subprocess.PIPE,
env=env)
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 output = process.communicate()[0]
ret = process.poll()
if ret:
raise Exception('preprocessor exited with error')
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 return output
finally:
os.unlink(input_file)
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895
def normalize_output(output):
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 lines = []
for line in output.splitlines():
# CFFI's parser doesn't like __attribute__ on UNIX compilers.
if line.startswith(b'__attribute__ ((visibility ("default"))) '):
line = line[len(b'__attribute__ ((visibility ("default"))) '):]
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 if line.startswith(b'__attribute__((deprecated('):
continue
elif b'__declspec(deprecated(' in line:
continue
Gregory Szorc
zstd: vendor python-zstandard 0.6.0...
r30822 lines.append(line)
return b'\n'.join(lines)
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 ffi = cffi.FFI()
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 # zstd.h uses a possible undefined MIN(). Define it until
# https://github.com/facebook/zstd/issues/976 is fixed.
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 # *_DISABLE_DEPRECATE_WARNINGS prevents the compiler from emitting a warning
# when cffi uses the function. Since we statically link against zstd, even
# if we use the deprecated functions it shouldn't be a huge problem.
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 ffi.set_source('_zstd_cffi', '''
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 #define MIN(a,b) ((a)<(b) ? (a) : (b))
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435 #define ZSTD_STATIC_LINKING_ONLY
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 #include <zstd.h>
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 #define ZDICT_STATIC_LINKING_ONLY
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 #define ZDICT_DISABLE_DEPRECATE_WARNINGS
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 #include <zdict.h>
''', sources=SOURCES,
include_dirs=INCLUDE_DIRS,
extra_compile_args=['-DZSTD_MULTITHREAD'])
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 DEFINE = re.compile(b'^\\#define ([a-zA-Z0-9_]+) ')
sources = []
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 # Feed normalized preprocessor output for headers into the cdef parser.
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 for header in HEADERS:
preprocessed = preprocess(header)
sources.append(normalize_output(preprocessed))
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 # #define's are effectively erased as part of going through preprocessor.
# So perform a manual pass to re-add those to the cdef source.
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 with open(header, 'rb') as fh:
for line in fh:
line = line.strip()
m = DEFINE.match(line)
if not m:
continue
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 if m.group(1) == b'ZSTD_STATIC_LINKING_ONLY':
continue
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 # The parser doesn't like some constants with complex values.
if m.group(1) in (b'ZSTD_LIB_VERSION', b'ZSTD_VERSION_STRING'):
continue
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 # The ... is magic syntax by the cdef parser to resolve the
# value at compile time.
Gregory Szorc
zstd: vendor python-zstandard 0.7.0...
r30895 sources.append(m.group(0) + b' ...')
Gregory Szorc
zstd: vendor python-zstandard 0.8.0...
r31796 cdeflines = b'\n'.join(sources).splitlines()
cdeflines = [l for l in cdeflines if l.strip()]
ffi.cdef(b'\n'.join(cdeflines).decode('latin1'))
Gregory Szorc
zstd: vendor python-zstandard 0.5.0...
r30435
if __name__ == '__main__':
ffi.compile()