Skip to content

Commit fd0f332

Browse files
baardemislav
andauthored
Consult pkg-config when detecting system OpenSSL (#2547)
Skip downloading and building OpenSSL if a compatible version was found with pkg-config, but only if that openssl version isn't under Homebrew's "cellar". --------- Co-authored-by: Mislav Marohnić <git@mislav.net>
1 parent 266b94f commit fd0f332

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

bin/ruby-build

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,20 @@ has_broken_mac_openssl() {
11061106
[[ $openssl_version = "OpenSSL 0.9.8"?* || $openssl_version = "LibreSSL"* ]]
11071107
}
11081108

1109+
# Print the prefix of a library found by pkg-config, but only if it
1110+
# doesn't come from Homebrew's cellar.
1111+
pkgconfig_prefix() {
1112+
local prefix brew_prefix
1113+
prefix="$(pkg-config --variable=prefix "$1" 2>/dev/null || true)"
1114+
[ -n "$prefix" ] || return 1
1115+
brew_prefix="$(brew --prefix 2>/dev/null || true)"
1116+
if [[ -n $prefix_prefix && ( $prefix == "$brew_prefix"/Cellar/* || \
1117+
$prefix == "$(brew --repository 2>/dev/null || true)"/Cellar/* ) ]]; then
1118+
return 1
1119+
fi
1120+
printf '%s\n' "$prefix"
1121+
}
1122+
11091123
# Detect the OpenSSL version that a compiler can reasonably link to.
11101124
system_openssl_version() {
11111125
cc -xc -E - <<EOF 2>/dev/null | sed -n 's/"\{0,1\}OpenSSL \([0-9][0-9.]*\).*/\1/p'
@@ -1148,7 +1162,10 @@ needs_openssl() {
11481162
[[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-openssl-dir=* ]] || return 1
11491163

11501164
local system_version
1151-
if ! has_broken_mac_openssl; then
1165+
if pkgconfig_prefix "openssl" >/dev/null; then
1166+
system_version="$(pkg-config --modversion openssl 2>/dev/null || true)"
1167+
fi
1168+
if [ -z "$system_version" ] && ! has_broken_mac_openssl; then
11521169
system_version="$(system_openssl_version)"
11531170
fi
11541171

test/build.bats

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ OUT
349349
stub_repeated brew false
350350
# shellcheck disable=SC2016
351351
stub cc '-xc -E - : [[ "$(cat)" == *OPENSSL_VERSION_TEXT* ]] && printf "# <unrelated> 4.0.2\n\"OpenSSL 1.0.3a 1 Aug 202\"\n0 errors.\n"'
352+
stub_repeated pkg-config false
352353
stub_make_install
353354

354355
mkdir -p "$INSTALL_ROOT"/openssl/ssl # OPENSSLDIR
@@ -360,6 +361,37 @@ DEF
360361

361362
unstub uname
362363
unstub brew
364+
unstub pkg-config
365+
unstub make
366+
367+
assert_build_log <<OUT
368+
ruby-3.2.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych,+]
369+
make -j 2
370+
make install
371+
OUT
372+
}
373+
374+
@test "use pkg-config OpenSSL" {
375+
cached_tarball "ruby-3.2.0" configure
376+
377+
openssl_libdir="$TMP/opt/local/libexec/openssl3"
378+
379+
stub_repeated uname '-s : echo Linux'
380+
stub_repeated brew false
381+
stub pkg-config \
382+
"--variable=prefix openssl : echo '$openssl_libdir'" \
383+
"--modversion openssl : echo 3.0.0"
384+
stub_make_install
385+
386+
run_inline_definition <<DEF
387+
install_package "openssl-1.1.1w" "https://www.openssl.org/source/openssl-1.1.1w.tar.gz" openssl --if needs_openssl_102_300
388+
install_package "ruby-3.2.0" "http://ruby-lang.org/ruby/2.0/ruby-3.2.0.tar.gz"
389+
DEF
390+
assert_success
391+
392+
unstub uname
393+
unstub brew
394+
unstub pkg-config
363395
unstub make
364396

365397
assert_build_log <<OUT
@@ -380,6 +412,7 @@ OUT
380412
stub_repeated brew false
381413
stub cc '-xc -E - : echo "OpenSSL 1.0.1a 1 Aug 2023"' # system_openssl_version
382414
stub openssl "version -d : echo 'OPENSSLDIR: \"${TMP}/ssl\"'"
415+
stub_repeated pkg-config false
383416
stub_make_install "install_sw"
384417
stub_make_install
385418

@@ -393,6 +426,7 @@ DEF
393426
unstub uname
394427
unstub brew
395428
unstub cc
429+
unstub pkg-config
396430
# Depending on certain system certificate files being present under /etc/,
397431
# `openssl version -d` might not have been called, so avoid unstubbing it
398432
# since that would verify the number of invocations.
@@ -420,6 +454,7 @@ OUT
420454
stub_repeated brew false
421455
stub cc '-xc -E - : echo "OpenSSL 1.0.1a 1 Aug 2023"' # system_openssl_version
422456
stub openssl
457+
stub_repeated pkg-config false
423458
stub_make_install "install_sw"
424459
stub_make_install
425460

@@ -433,6 +468,7 @@ DEF
433468
unstub uname
434469
unstub security
435470
unstub brew
471+
unstub pkg-config
436472
# Depending on the state of system `/usr/bin/openssl` in the test runner,
437473
# `cc` might not have been called, so avoid unstubbing it since that would
438474
# verify the number of invocations.
@@ -533,7 +569,9 @@ EXE
533569
stub cc '-xc -E - : echo "OpenSSL 1.0.1a 1 Aug 2023"'
534570
stub_repeated brew \
535571
'list : printf "git\nopenssl@3\nopenssl-utils\nopenssl@1.1\nopenssl@3.0\nwget\nopenssl@3.1"' \
536-
"--prefix : echo '$homebrew_prefix'/opt/\$2 "
572+
"--prefix : if [ \$# -ge 2 ]; then echo '$homebrew_prefix'/opt/\$2; else echo '$homebrew_prefix'; fi " \
573+
"--repository : echo '$homebrew_prefix'"
574+
stub_repeated pkg-config false
537575
stub_make_install
538576

539577
run_inline_definition <<DEF
@@ -545,6 +583,7 @@ DEF
545583
unstub uname
546584
unstub cc
547585
unstub brew
586+
unstub pkg-config
548587
unstub make
549588

550589
assert_build_log <<OUT

0 commit comments

Comments
 (0)