diff --git a/setup.py b/setup.py index ffcac7c0a..72f6e2a93 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ except ImportError: import setup_lz4 import setup_zstd import setup_b2 +import setup_crypto import setup_docs # True: use the shared liblz4 (>= 1.7.0 / r129) from the system, False: use the bundled lz4 code @@ -100,30 +101,6 @@ else: raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.') -def detect_openssl(prefixes): - for prefix in prefixes: - filename = os.path.join(prefix, 'include', 'openssl', 'evp.h') - if os.path.exists(filename): - with open(filename, 'rb') as fd: - if b'PKCS5_PBKDF2_HMAC(' in fd.read(): - return prefix - - -include_dirs = [] -library_dirs = [] -define_macros = [] - -possible_openssl_prefixes = ['/usr', '/usr/local', '/usr/local/opt/openssl', '/usr/local/ssl', '/usr/local/openssl', - '/usr/local/borg', '/opt/local', '/opt/pkg', ] -if os.environ.get('BORG_OPENSSL_PREFIX'): - possible_openssl_prefixes.insert(0, os.environ.get('BORG_OPENSSL_PREFIX')) -ssl_prefix = detect_openssl(possible_openssl_prefixes) -if not ssl_prefix: - raise Exception('Unable to find OpenSSL >= 1.0 headers. (Looked here: {})'.format(', '.join(possible_openssl_prefixes))) -include_dirs.append(os.path.join(ssl_prefix, 'include')) -library_dirs.append(os.path.join(ssl_prefix, 'lib')) - - with open('README.rst', 'r') as fd: long_description = fd.read() # remove header, but have one \n before first headline @@ -165,6 +142,9 @@ cmdclass = { ext_modules = [] if not on_rtd: + include_dirs = [] + library_dirs = [] + define_macros = [] compress_ext_kwargs = dict(sources=[compress_source], include_dirs=include_dirs, library_dirs=library_dirs, define_macros=define_macros) compress_ext_kwargs = setup_lz4.lz4_ext_kwargs(bundled_path='src/borg/algorithms/lz4', @@ -173,8 +153,9 @@ if not on_rtd: compress_ext_kwargs = setup_zstd.zstd_ext_kwargs(bundled_path='src/borg/algorithms/zstd', prefer_system=prefer_system_libzstd, multithreaded=False, legacy=False, **compress_ext_kwargs) - crypto_ext_kwargs = dict(sources=[crypto_ll_source, crypto_helpers], libraries=['crypto'], - include_dirs=include_dirs, library_dirs=library_dirs, define_macros=define_macros) + crypto_ext_kwargs = dict(sources=[crypto_ll_source, crypto_helpers], include_dirs=include_dirs, + library_dirs=library_dirs, define_macros=define_macros) + crypto_ext_kwargs = setup_crypto.crypto_ext_kwargs(**crypto_ext_kwargs) crypto_ext_kwargs = setup_b2.b2_ext_kwargs(bundled_path='src/borg/algorithms/blake2', prefer_system=prefer_system_libb2, **crypto_ext_kwargs) diff --git a/setup_crypto.py b/setup_crypto.py new file mode 100644 index 000000000..1da9cfd3f --- /dev/null +++ b/setup_crypto.py @@ -0,0 +1,40 @@ +# Support code for building a C extension with crypto from OpenSSL +# +# Copyright (c) 2016-present, Gregory Szorc (original code for zstd) +# 2017-present, Thomas Waldmann (mods to make it more generic, code for openssl) +# 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 + + +def crypto_ext_kwargs(**kwargs): + """amend kwargs with crypto stuff for a distutils.extension.Extension initialization. + + 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] + + system_prefix = os.environ.get('BORG_OPENSSL_PREFIX') + if system_prefix: + print('Detected system OpenSSL') + else: + raise Exception('Could not find OpenSSL lib/headers, please set BORG_OPENSSL_PREFIX') + + include_dirs = kwargs.get('include_dirs', []) + include_dirs += multi_join(['include'], system_prefix) + + library_dirs = kwargs.get('library_dirs', []) + library_dirs += multi_join(['lib'], system_prefix) + + libraries = kwargs.get('libraries', []) + libraries += ['crypto', ] + + ret = dict(**kwargs) + ret.update(dict(include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries)) + return ret