Skip to content

Commit 4ddb5d7

Browse files
committed
Add support for pkg-config
When pkg-config is installed and finds the libraries, use those rather than relying on Homebrew or bundling OpenSSL. Fixes #2544
1 parent 5693b62 commit 4ddb5d7

2 files changed

Lines changed: 208 additions & 4 deletions

File tree

bin/ruby-build

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,16 +675,16 @@ build_package_standard() {
675675
local ruby_semver="$(normalize_semver "${package_name#ruby-}")"
676676
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-readline-dir=* && "$ruby_semver" -lt 300300 ]]; then
677677
# Ruby 3.3+ does not need external readline: https://github.com/rbenv/ruby-build/issues/2330
678-
use_homebrew_readline || use_freebsd_readline || true
678+
use_pkgconfig_readline || use_homebrew_readline || use_freebsd_readline || true
679679
fi
680680
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libffi-dir=* ]]; then
681-
use_freebsd_libffi || true
681+
use_pkgconfig_libffi || use_freebsd_libffi || true
682682
fi
683683
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libyaml-dir=* ]]; then
684-
use_homebrew_yaml || use_freebsd_yaml || true
684+
use_pkgconfig_yaml || use_homebrew_yaml || use_freebsd_yaml || true
685685
fi
686686
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-gmp-dir=* ]]; then
687-
use_homebrew_gmp || true
687+
use_pkgconfig_gmp || use_homebrew_gmp || true
688688
fi
689689
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-openssl-dir=* ]]; then
690690
if is_freebsd && [ -f /usr/local/include/openssl/ssl.h ]; then
@@ -1027,9 +1027,20 @@ require_llvm() {
10271027

10281028
needs_yaml() {
10291029
[[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libyaml-dir=* ]] &&
1030+
! use_pkgconfig_yaml &&
10301031
! use_homebrew_yaml
10311032
}
10321033

1034+
use_pkgconfig_yaml() {
1035+
local libdir
1036+
libdir="$(pkg-config --variable=prefix yaml-0.1 2>/dev/null || true)"
1037+
if [ -d "$libdir" ]; then
1038+
package_option ruby configure --with-libyaml-dir="$libdir"
1039+
else
1040+
return 1
1041+
fi
1042+
}
1043+
10331044
use_homebrew_yaml() {
10341045
local libdir
10351046
libdir="$(brew --prefix libyaml 2>/dev/null || true)"
@@ -1051,6 +1062,16 @@ use_freebsd_yaml() {
10511062
fi
10521063
}
10531064

1065+
use_pkgconfig_gmp() {
1066+
local libdir
1067+
libdir="$(pkg-config --variable=prefix gmp 2>/dev/null || true)"
1068+
if [ -d "$libdir" ]; then
1069+
package_option ruby configure --with-gmp-dir="$libdir"
1070+
else
1071+
return 1
1072+
fi
1073+
}
1074+
10541075
use_homebrew_gmp() {
10551076
local libdir
10561077
libdir="$(brew --prefix gmp 2>/dev/null || true)"
@@ -1076,6 +1097,16 @@ use_freebsd_readline() {
10761097
fi
10771098
}
10781099

1100+
use_pkgconfig_readline() {
1101+
local libdir
1102+
libdir="$(pkg-config --variable=prefix readline 2>/dev/null || true)"
1103+
if [ -d "$libdir" ]; then
1104+
package_option ruby configure --with-readline-dir="$libdir"
1105+
else
1106+
return 1
1107+
fi
1108+
}
1109+
10791110
use_homebrew_readline() {
10801111
local libdir
10811112
libdir="$(brew --prefix readline 2>/dev/null || true)"
@@ -1087,6 +1118,16 @@ use_homebrew_readline() {
10871118
fi
10881119
}
10891120

1121+
use_pkgconfig_libffi() {
1122+
local libdir
1123+
libdir="$(pkg-config --variable=prefix libffi 2>/dev/null || true)"
1124+
if [ -d "$libdir" ]; then
1125+
package_option ruby configure --with-libffi-dir="$libdir"
1126+
else
1127+
return 1
1128+
fi
1129+
}
1130+
10901131
use_freebsd_libffi() {
10911132
if is_freebsd; then
10921133
local libffi_prefix
@@ -1114,6 +1155,14 @@ OPENSSL_VERSION_TEXT
11141155
EOF
11151156
}
11161157

1158+
pkgconfig_openssl_version() {
1159+
pkg-config --modversion openssl 2>/dev/null || true
1160+
}
1161+
1162+
pkgconfig_openssl_prefix() {
1163+
pkg-config --variable=prefix openssl 2>/dev/null || true
1164+
}
1165+
11171166
# List all Homebrew-installed OpenSSL versions and their filesystem prefixes.
11181167
homebrew_openssl_versions() {
11191168
local formula version prefix
@@ -1167,6 +1216,18 @@ needs_openssl() {
11671216
# Return early if system openssl satisfies the requirement.
11681217
(( system_version < lower_bound || system_version >= upper_bound )) || return 1
11691218

1219+
# Look for OpenSSL using pkg-config
1220+
local pkgconfig_version pkgconfig_prefix
1221+
pkgconfig_version="$(pkgconfig_openssl_version)"
1222+
pkgconfig_prefix="$(pkgconfig_openssl_prefix)"
1223+
if [ -n "$pkgconfig_version" ] && [ -d "$pkgconfig_prefix" ]; then
1224+
pkgconfig_version="$(normalize_semver "$pkgconfig_version")"
1225+
if (( pkgconfig_version >= lower_bound && pkgconfig_version < upper_bound )); then
1226+
package_option ruby configure --with-openssl-dir="$pkgconfig_prefix"
1227+
return 1
1228+
fi
1229+
fi
1230+
11701231
# Look for the latest Homebrew-installed OpenSSL that satisfies the requirement
11711232
local brew_installs
11721233
brew_installs="$(homebrew_openssl_versions)"

0 commit comments

Comments
 (0)