From a6825f1c0a26d7eb7f414901fc05a63ed19b59ac Mon Sep 17 00:00:00 2001
From: Donald Stufft <donald.stufft@gmail.com>
Date: Fri, 22 Feb 2013 19:55:53 -0500
Subject: [PATCH] Try to build against NaCl and fall back to libsodium

---
 nacl/nacl.py | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/nacl/nacl.py b/nacl/nacl.py
index 57ca2010..592b4173 100644
--- a/nacl/nacl.py
+++ b/nacl/nacl.py
@@ -1,9 +1,9 @@
 """
-CFFI interface to libsodium the library
+CFFI interface to NaCl and libsodium library
 """
 import functools
 
-from cffi import FFI
+from cffi import FFI, VerificationError
 
 
 __all__ = ["ffi", "lib"]
@@ -26,7 +26,23 @@ ffi.cdef(
 )
 
 
-lib = ffi.verify("#include <sodium.h>", libraries=["sodium"])
+# Check to make sure that we have a compiled interface to one of our library
+#   backends. We prefer NaCl here because it has compiled speed ups.
+# TODO: Include some way to specify which backend you want and hard fail if
+#   that one doesn't exist?
+try:
+    # Try to compile the ffi interface with NaCl
+    lib = ffi.verify(
+        """
+            #include "crypto_hash.h"
+            #include "crypto_hash_sha256.h"
+            #include "crypto_hash_sha512.h"
+        """,
+        libraries=["nacl"],
+    )
+except VerificationError:
+    # Try to compile the ffi interface with libsodium if NaCl wasn't available
+    lib = ffi.verify("#include <sodium.h>", libraries=["sodium"])
 
 
 # A lot of the functions in nacl return 0 for success and a negative integer
-- 
GitLab