Skip to content

Commit

Permalink
fixup: detect openssl installed from homebrew on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Jan 2, 2019
1 parent f9bdd66 commit c4cab14
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bin/ci
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ format() {
prepare_build() {
on_linux verify_linux_environment

on_osx brew install crystal
on_osx brew install crystal openssl

# Make sure binaries from llvm are available in PATH
on_osx brew install jq
Expand Down
4 changes: 3 additions & 1 deletion src/openssl/lib_crypto.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
end
{% end %}

@[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'`")]
{% begin %}
@[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '{{LibCrypto::LDFLAGS.id}} -lcrypto'`")]
{% end %}
lib LibCrypto
alias Char = LibC::Char
alias Int = LibC::Int
Expand Down
4 changes: 3 additions & 1 deletion src/openssl/lib_ssl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ require "./lib_crypto"
end
{% end %}

@[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'`")]
{% begin %}
@[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '{{LibSSL::LDFLAGS.id}} -lssl -lcrypto'`")]
{% end %}
lib LibSSL
alias Int = LibC::Int
alias Char = LibC::Char
Expand Down
35 changes: 31 additions & 4 deletions src/openssl/version.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
#! /bin/sh

# use pkg-config when available:
CFLAGS=`command -v pkg-config > /dev/null && pkg-config --cflags --silence-errors $1 || true`
LIBNAME=$1

# use pkg-config if available:
OPENSSL_CFLAGS=$(command -v pkg-config > /dev/null && pkg-config --cflags --silence-errors $LIBNAME)

if [ "$OPENSSL_CFLAGS" = "" ]; then
if [ "`uname -s`" = "Darwin" ]; then
PREFIX=/usr/local/opt/openssl

# check default homebrew install directory (fast):
if [ -d $PREFIX ]; then
OPENSSL_CFLAGS="-I$PREFIX/include"
else
# ask homebrew (slower):
PREFIX=$(command -v brew > /dev/null && brew --prefix openssl)
if [ "$PREFIX" != "" ]; then
OPENSSL_CFLAGS="-I$PREFIX/include"
fi
fi
fi
fi

# extract version numbers from OpenSSL/LibreSSL C headers:
LIBRESSL_VERSION_NUMBER=$(printf "#include <openssl/opensslv.h>\nLIBRESSL_VERSION_NUMBER" | ${CC:-cc} $CFLAGS -E - 2> /dev/null | tail -n 1)
OPENSSL_VERSION_NUMBER=$(printf "#include <openssl/opensslv.h>\nOPENSSL_VERSION_NUMBER" | ${CC:-cc} $CFLAGS -E - 2> /dev/null | tail -n 1)
LIBRESSL_VERSION_NUMBER=$(printf "#include <openssl/opensslv.h>\nLIBRESSL_VERSION_NUMBER" | ${CC:-cc} $OPENSSL_CFLAGS -E - 2> /dev/null | tail -n 1)
OPENSSL_VERSION_NUMBER=$(printf "#include <openssl/opensslv.h>\nOPENSSL_VERSION_NUMBER" | ${CC:-cc} $OPENSSL_CFLAGS -E - 2> /dev/null | tail -n 1)

if [ $LIBRESSL_VERSION_NUMBER = LIBRESSL_VERSION_NUMBER ]; then
# not libressl:
echo LIBRESSL_VERSION_NUMBER = 0x0

if [ $OPENSSL_VERSION_NUMBER = OPENSSL_VERSION_NUMBER ]; then
# failed:
echo '{% raise "Error: failed to find OpenSSL or LibreSSL development headers" %}'
else
# detected OpenSSL:
Expand All @@ -22,3 +42,10 @@ else
echo LIBRESSL_VERSION_NUMBER = $(echo $LIBRESSL_VERSION_NUMBER | sed 's/L//')
echo OPENSSL_VERSION_NUMBER = 0x0
fi

# pass LDFLAGS information too, required for homebrew:
if [ "$PREFIX" != "" ]; then
echo LDFLAGS = \"-L$PREFIX/lib\"
else
echo LDFLAGS = \"\"
fi

0 comments on commit c4cab14

Please sign in to comment.