فهرست منبع

crypto: link against system libb2, if possible

Marian Beermann 8 سال پیش
والد
کامیت
a31ca4f022
6فایلهای تغییر یافته به همراه35 افزوده شده و 3 حذف شده
  1. 1 0
      docs/global.rst.inc
  2. 1 0
      docs/installation.rst
  3. 3 0
      docs/usage.rst
  4. 24 2
      setup.py
  5. 5 0
      src/borg/blake2-libselect.h
  6. 1 1
      src/borg/crypto.pyx

+ 1 - 0
docs/global.rst.inc

@@ -15,6 +15,7 @@
 .. _libacl: https://savannah.nongnu.org/projects/acl/
 .. _libacl: https://savannah.nongnu.org/projects/acl/
 .. _libattr: https://savannah.nongnu.org/projects/attr/
 .. _libattr: https://savannah.nongnu.org/projects/attr/
 .. _liblz4: https://github.com/Cyan4973/lz4
 .. _liblz4: https://github.com/Cyan4973/lz4
+.. _libb2: https://github.com/BLAKE2/libb2
 .. _OpenSSL: https://www.openssl.org/
 .. _OpenSSL: https://www.openssl.org/
 .. _`Python 3`: https://www.python.org/
 .. _`Python 3`: https://www.python.org/
 .. _Buzhash: https://en.wikipedia.org/wiki/Buzhash
 .. _Buzhash: https://en.wikipedia.org/wiki/Buzhash

+ 1 - 0
docs/installation.rst

@@ -210,6 +210,7 @@ following dependencies first:
 * some Python dependencies, pip will automatically install them for you
 * some Python dependencies, pip will automatically install them for you
 * optionally, the llfuse_ Python package is required if you wish to mount an
 * 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.
   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
 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.
 distribution specific sections below and also at the Vagrantfile in our repo.

+ 3 - 0
docs/usage.rst

@@ -194,6 +194,9 @@ Building:
         Adds given OpenSSL header file directory to the default locations (setup.py).
         Adds given OpenSSL header file directory to the default locations (setup.py).
     BORG_LZ4_PREFIX
     BORG_LZ4_PREFIX
         Adds given LZ4 header file directory to the default locations (setup.py).
         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:
 Please note:

+ 24 - 2
setup.py

@@ -128,8 +128,18 @@ def detect_lz4(prefixes):
                     return prefix
                     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 = []
 include_dirs = []
 library_dirs = []
 library_dirs = []
+define_macros = []
+crypto_libraries = ['crypto']
 
 
 possible_openssl_prefixes = ['/usr', '/usr/local', '/usr/local/opt/openssl', '/usr/local/ssl', '/usr/local/openssl',
 possible_openssl_prefixes = ['/usr', '/usr/local', '/usr/local/opt/openssl', '/usr/local/ssl', '/usr/local/openssl',
                              '/usr/local/borg', '/opt/local', '/opt/pkg', ]
                              '/usr/local/borg', '/opt/local', '/opt/pkg', ]
@@ -153,6 +163,18 @@ if lz4_prefix:
 elif not on_rtd:
 elif not on_rtd:
     raise Exception('Unable to find LZ4 headers. (Looked here: {})'.format(', '.join(possible_lz4_prefixes)))
     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:
 with open('README.rst', 'r') as fd:
     long_description = fd.read()
     long_description = fd.read()
@@ -326,8 +348,8 @@ cmdclass = {
 ext_modules = []
 ext_modules = []
 if not on_rtd:
 if not on_rtd:
     ext_modules += [
     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.chunker', [chunker_source]),
     Extension('borg.hashindex', [hashindex_source])
     Extension('borg.hashindex', [hashindex_source])
 ]
 ]

+ 5 - 0
src/borg/blake2-libselect.h

@@ -0,0 +1,5 @@
+#ifdef BORG_USE_LIBB2
+#include <blake2.h>
+#else
+#include "blake2/blake2b-ref.c"
+#endif

+ 1 - 1
src/borg/crypto.pyx

@@ -6,7 +6,7 @@ from cpython.buffer cimport PyBUF_SIMPLE, PyObject_GetBuffer, PyBuffer_Release
 API_VERSION = 3
 API_VERSION = 3
 
 
 
 
-cdef extern from "blake2/blake2b-ref.c":
+cdef extern from "blake2-libselect.h":
     ctypedef struct blake2b_state:
     ctypedef struct blake2b_state:
         pass
         pass