Merge pull request #2306 from reaperhulk/static-linking-osx

Support static linking on OS X
diff --git a/.travis.yml b/.travis.yml
index c61682e..f1c3567 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -71,23 +71,27 @@
         - language: generic
           os: osx
           osx_image: beta-xcode6.3
-          env: TOXENV=py26
+          env: TOXENV=py26 CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1
         - language: generic
           os: osx
           osx_image: beta-xcode6.3
-          env: TOXENV=py27
+          env: TOXENV=py27 CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1
         - language: generic
           os: osx
           osx_image: beta-xcode6.3
-          env: TOXENV=py33
+          env: TOXENV=py33 CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1
         - language: generic
           os: osx
           osx_image: beta-xcode6.3
-          env: TOXENV=py34
+          env: TOXENV=py34 CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1
         - language: generic
           os: osx
           osx_image: beta-xcode6.3
-          env: TOXENV=pypy
+          env: TOXENV=pypy CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1
+        - language: generic
+          os: osx
+          osx_image: beta-xcode6.3
+          env: TOXENV=py27 CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=0
         - language: generic
           os: osx
           osx_image: beta-xcode6.3
diff --git a/.travis/run.sh b/.travis/run.sh
index 1c49911..3667b33 100755
--- a/.travis/run.sh
+++ b/.travis/run.sh
@@ -8,7 +8,12 @@
     if [[ "${OPENSSL}" != "0.9.8" ]]; then
         # set our flags to use homebrew openssl
         export ARCHFLAGS="-arch x86_64"
-        export LDFLAGS="-L/usr/local/opt/openssl/lib"
+        # if the build is static we need different LDFLAGS
+        if [[ "${CRYPTOGRAPHY_OSX_NO_LINK_FLAGS}" == "1" ]]; then
+            export LDFLAGS="/usr/local/opt/openssl/lib/libssl.a /usr/local/opt/openssl/lib/libcrypto.a"
+        else
+            export LDFLAGS="-L/usr/local/opt/openssl/lib"
+        fi
         export CFLAGS="-I/usr/local/opt/openssl/include"
         # The Travis OS X jobs are run for two versions
         # of OpenSSL, but we only need to run the
@@ -26,3 +31,7 @@
 fi
 source ~/.venv/bin/activate
 tox -- $TOX_FLAGS
+# Output information about linking of the OpenSSL library on OS X
+if [[ "$(uname -s)" == "Darwin" ]]; then
+    otool -L `find .tox -name _openssl*.so`
+fi
diff --git a/src/_cffi_src/build_openssl.py b/src/_cffi_src/build_openssl.py
index 6a5bf2d..defa69d 100644
--- a/src/_cffi_src/build_openssl.py
+++ b/src/_cffi_src/build_openssl.py
@@ -4,6 +4,7 @@
 
 from __future__ import absolute_import, division, print_function
 
+import os
 import sys
 
 from _cffi_src.utils import build_ffi_for_binding, extra_link_args
@@ -11,15 +12,27 @@
 
 def _get_openssl_libraries(platform):
     # OpenSSL goes by a different library name on different operating systems.
-    if platform != "win32":
+    if platform == "darwin":
+        return _osx_libraries(
+            os.environ.get("CRYPTOGRAPHY_OSX_NO_LINK_FLAGS")
+        )
+    elif platform == "win32":
+        return ["libeay32", "ssleay32", "advapi32",
+                "crypt32", "gdi32", "user32", "ws2_32"]
+    else:
         # In some circumstances, the order in which these libs are
         # specified on the linker command-line is significant;
         # libssl must come before libcrypto
         # (http://marc.info/?l=openssl-users&m=135361825921871)
         return ["ssl", "crypto"]
+
+
+def _osx_libraries(build_static):
+    # For building statically we don't want to pass the -lssl or -lcrypto flags
+    if build_static == "1":
+        return []
     else:
-        return ["libeay32", "ssleay32", "advapi32",
-                "crypt32", "gdi32", "user32", "ws2_32"]
+        return ["ssl", "crypto"]
 
 
 _OSX_PRE_INCLUDE = """