From c12af594b5a3f3bd88439ca986bb7fae08a06824 Mon Sep 17 00:00:00 2001
From: Donald Stufft <donald@stufft.io>
Date: Tue, 24 Sep 2013 20:12:36 -0400
Subject: [PATCH] Dynamically add the ASM extensions if they exist

---
 setup.py | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/setup.py b/setup.py
index 66a78943..7d19f834 100644
--- a/setup.py
+++ b/setup.py
@@ -71,6 +71,22 @@ class build_clib(_build_clib):
                         "crypto_scalarmult/curve25519/ref/smult_curve25519_ref.c",
                     ])
 
+                # Dynamically modify the implementation based on if we have
+                #   AMD64 ASM or not.
+                if "HAVE_AMD64_ASM" in macros:
+                    sources.extend([
+                        "crypto_stream/salsa20/amd64_xmm6/stream_salsa20_amd64_xmm6.S",
+                    ])
+
+                    self._include_asm = True
+                else:
+                    sources.extend([
+                        "crypto_stream/salsa20/ref/stream_salsa20_ref.c",
+                        "crypto_stream/salsa20/ref/xor_salsa20_ref.c",
+                    ])
+
+                    self._include_asm = False
+
                 # Expand out all of the sources to their full path
                 sources = [
                     here("libsodium/src/libsodium", s) for s in sources
@@ -85,6 +101,14 @@ class build_clib(_build_clib):
         # Call our normal run
         return _build_clib.run(self)
 
+    def build_libraries(self, libraries):
+        # This is a convenient place to modify the compiler so that we can add
+        #   the .S extension
+        if self._include_asm and not ".S" in self.compiler.src_extensions:
+            self.compiler.src_extensions.append(".S")
+
+        return _build_clib.build_libraries(self, libraries)
+
 
 class PyTest(TestCommand):
     def finalize_options(self):
@@ -259,12 +283,6 @@ setup(
                 "sodium/utils.c",
                 "sodium/version.c",
 
-                # THIS STUFF IS UNDEFINED?
-                "crypto_stream/salsa20/ref/stream_salsa20_ref.c",
-                "crypto_stream/salsa20/ref/xor_salsa20_ref.c",
-
-                #
-
                 # 'crypto_auth/try.c',
                 # #'crypto_box/try.c',
                 # 'crypto_generichash/blake2/ref/blake2s-ref.c',
-- 
GitLab