diff --git a/bin/ruby-build b/bin/ruby-build index 0feb80fa96..b2ac1b0a23 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -1106,6 +1106,20 @@ has_broken_mac_openssl() { [[ $openssl_version = "OpenSSL 0.9.8"?* || $openssl_version = "LibreSSL"* ]] } +# Print the prefix of a library found by pkg-config, but only if it +# doesn't come from Homebrew's cellar. +pkgconfig_prefix() { + local prefix brew_prefix + prefix="$(pkg-config --variable=prefix "$1" 2>/dev/null || true)" + [ -n "$prefix" ] || return 1 + brew_prefix="$(brew --prefix 2>/dev/null || true)" + if [[ -n $prefix_prefix && ( $prefix == "$brew_prefix"/Cellar/* || \ + $prefix == "$(brew --repository 2>/dev/null || true)"/Cellar/* ) ]]; then + return 1 + fi + printf '%s\n' "$prefix" +} + # Detect the OpenSSL version that a compiler can reasonably link to. system_openssl_version() { cc -xc -E - </dev/null | sed -n 's/"\{0,1\}OpenSSL \([0-9][0-9.]*\).*/\1/p' @@ -1148,7 +1162,10 @@ needs_openssl() { [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-openssl-dir=* ]] || return 1 local system_version - if ! has_broken_mac_openssl; then + if pkgconfig_prefix "openssl" >/dev/null; then + system_version="$(pkg-config --modversion openssl 2>/dev/null || true)" + fi + if [ -z "$system_version" ] && ! has_broken_mac_openssl; then system_version="$(system_openssl_version)" fi diff --git a/test/build.bats b/test/build.bats index 02ca930880..379097f0ad 100755 --- a/test/build.bats +++ b/test/build.bats @@ -349,6 +349,7 @@ OUT stub_repeated brew false # shellcheck disable=SC2016 stub cc '-xc -E - : [[ "$(cat)" == *OPENSSL_VERSION_TEXT* ]] && printf "# 4.0.2\n\"OpenSSL 1.0.3a 1 Aug 202\"\n0 errors.\n"' + stub_repeated pkg-config false stub_make_install mkdir -p "$INSTALL_ROOT"/openssl/ssl # OPENSSLDIR @@ -360,6 +361,37 @@ DEF unstub uname unstub brew + unstub pkg-config + unstub make + + assert_build_log <