From a31ca4f022b76bcb4d679bf9be86aa9d0236bdd5 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Thu, 10 Nov 2016 22:14:09 +0100 Subject: [PATCH] crypto: link against system libb2, if possible --- docs/global.rst.inc | 1 + docs/installation.rst | 1 + docs/usage.rst | 3 +++ setup.py | 26 ++++++++++++++++++++++++-- src/borg/blake2-libselect.h | 5 +++++ src/borg/crypto.pyx | 2 +- 6 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/borg/blake2-libselect.h diff --git a/docs/global.rst.inc b/docs/global.rst.inc index d34f09651..88c659a36 100644 --- a/docs/global.rst.inc +++ b/docs/global.rst.inc @@ -15,6 +15,7 @@ .. _libacl: https://savannah.nongnu.org/projects/acl/ .. _libattr: https://savannah.nongnu.org/projects/attr/ .. _liblz4: https://github.com/Cyan4973/lz4 +.. _libb2: https://github.com/BLAKE2/libb2 .. _OpenSSL: https://www.openssl.org/ .. _`Python 3`: https://www.python.org/ .. _Buzhash: https://en.wikipedia.org/wiki/Buzhash diff --git a/docs/installation.rst b/docs/installation.rst index 44ba02e3e..abd700c20 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -210,6 +210,7 @@ following dependencies first: * some Python dependencies, pip will automatically install them for you * optionally, the llfuse_ Python package is required if you wish to mount an archive as a FUSE filesystem. See setup.py about the version requirements. +* optionally libb2_. If it is not found a bundled implementation is used instead. If you have troubles finding the right package names, have a look at the distribution specific sections below and also at the Vagrantfile in our repo. diff --git a/docs/usage.rst b/docs/usage.rst index b05b81c48..bfeb6ef0f 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -194,6 +194,9 @@ Building: Adds given OpenSSL header file directory to the default locations (setup.py). BORG_LZ4_PREFIX Adds given LZ4 header file directory to the default locations (setup.py). + BORG_LIBB2_PREFIX + Adds given prefix directory to the default locations. If a 'include/blake2.h' is found Borg + will be linked against the system libb2 instead of a bundled implementation. (setup.py) Please note: diff --git a/setup.py b/setup.py index 02dd18863..92588ef7d 100644 --- a/setup.py +++ b/setup.py @@ -128,8 +128,18 @@ def detect_lz4(prefixes): return prefix +def detect_libb2(prefixes): + for prefix in prefixes: + filename = os.path.join(prefix, 'include', 'blake2.h') + if os.path.exists(filename): + with open(filename, 'r') as fd: + if 'blake2b_init' in fd.read(): + return prefix + include_dirs = [] library_dirs = [] +define_macros = [] +crypto_libraries = ['crypto'] possible_openssl_prefixes = ['/usr', '/usr/local', '/usr/local/opt/openssl', '/usr/local/ssl', '/usr/local/openssl', '/usr/local/borg', '/opt/local', '/opt/pkg', ] @@ -153,6 +163,18 @@ if lz4_prefix: elif not on_rtd: raise Exception('Unable to find LZ4 headers. (Looked here: {})'.format(', '.join(possible_lz4_prefixes))) +possible_libb2_prefixes = ['/usr', '/usr/local', '/usr/local/opt/libb2', '/usr/local/libb2', + '/usr/local/borg', '/opt/local', '/opt/pkg', ] +if os.environ.get('BORG_LIBB2_PREFIX'): + possible_libb2_prefixes.insert(0, os.environ.get('BORG_LIBB2_PREFIX')) +libb2_prefix = detect_libb2(possible_libb2_prefixes) +if libb2_prefix: + print('Detected and preferring libb2 over bundled BLAKE2') + include_dirs.append(os.path.join(libb2_prefix, 'include')) + library_dirs.append(os.path.join(libb2_prefix, 'lib')) + crypto_libraries.append('b2') + define_macros.append(('BORG_USE_LIBB2', 'YES')) + with open('README.rst', 'r') as fd: long_description = fd.read() @@ -326,8 +348,8 @@ cmdclass = { ext_modules = [] if not on_rtd: ext_modules += [ - Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs), - Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs), + Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs, define_macros=define_macros), + Extension('borg.crypto', [crypto_source], libraries=crypto_libraries, include_dirs=include_dirs, library_dirs=library_dirs, define_macros=define_macros), Extension('borg.chunker', [chunker_source]), Extension('borg.hashindex', [hashindex_source]) ] diff --git a/src/borg/blake2-libselect.h b/src/borg/blake2-libselect.h new file mode 100644 index 000000000..5486400eb --- /dev/null +++ b/src/borg/blake2-libselect.h @@ -0,0 +1,5 @@ +#ifdef BORG_USE_LIBB2 +#include +#else +#include "blake2/blake2b-ref.c" +#endif diff --git a/src/borg/crypto.pyx b/src/borg/crypto.pyx index eefd0a23f..7fa8b8913 100644 --- a/src/borg/crypto.pyx +++ b/src/borg/crypto.pyx @@ -6,7 +6,7 @@ from cpython.buffer cimport PyBUF_SIMPLE, PyObject_GetBuffer, PyBuffer_Release API_VERSION = 3 -cdef extern from "blake2/blake2b-ref.c": +cdef extern from "blake2-libselect.h": ctypedef struct blake2b_state: pass