Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detect and link to Homebrew's libyaml #1929

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions bin/ruby-build
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,21 @@ build_package_standard_build() {
local PACKAGE_CFLAGS="${package_var_name}_CFLAGS"

if [ "$package_var_name" = "RUBY" ]; then
use_homebrew_readline || use_freebsd_pkg ||true
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-readline-dir=* ]]; then
use_homebrew_readline || use_freebsd_readline || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libyaml-dir=* ]]; then
use_homebrew_yaml || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-gmp-dir=* ]]; then
use_homebrew_gmp || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-openssl-dir=* ]]; then
if [ "FreeBSD" = "$(uname -s)" ] && [ -f /usr/local/include/openssl/ssl.h ]; then
# use openssl installed from Ports Collection
package_option ruby configure --with-openssl-dir="/usr/local"
fi
fi
fi

( if [ "${CFLAGS+defined}" ] || [ "${!PACKAGE_CFLAGS+defined}" ]; then
Expand Down Expand Up @@ -1037,18 +1050,10 @@ use_homebrew_gmp() {
fi
}

use_freebsd_pkg() {
# check if FreeBSD
use_freebsd_readline() {
if [ "FreeBSD" = "$(uname -s)" ]; then
# use openssl if installed from Ports Collection
if [ -f /usr/local/include/openssl/ssl.h ]; then
package_option ruby configure --with-openssl-dir="/usr/local"
fi

# check if 11-R or later
release="$(uname -r)"
local release="$(uname -r)"
if [ "${release%%.*}" -ge 11 ]; then
# prefers readline to compile most of ruby versions
if pkg info -e readline > /dev/null; then
# use readline from Ports Collection
package_option ruby configure --with-readline-dir="/usr/local"
Expand All @@ -1062,14 +1067,12 @@ use_freebsd_pkg() {
}

use_homebrew_readline() {
if [[ "$RUBY_CONFIGURE_OPTS" != *--with-readline-dir=* ]]; then
local libdir="$(brew --prefix readline 2>/dev/null || true)"
if [ -d "$libdir" ]; then
echo "ruby-build: using readline from homebrew"
package_option ruby configure --with-readline-dir="$libdir"
else
return 1
fi
local libdir="$(brew --prefix readline 2>/dev/null || true)"
if [ -d "$libdir" ]; then
echo "ruby-build: using readline from homebrew"
package_option ruby configure --with-readline-dir="$libdir"
else
return 1
fi
}

Expand Down
48 changes: 27 additions & 21 deletions test/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ assert_build_log() {
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install

install_fixture definitions/needs-yaml
assert_success

unstub uname
unstub brew
unstub make

assert_build_log <<OUT
Expand All @@ -86,8 +87,8 @@ OUT
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install
stub patch ' : echo patch "$@" | sed -E "s/\.[[:alnum:]]+$/.XXX/" >> build.log'
Expand All @@ -100,6 +101,7 @@ PATCH
assert_success

unstub uname
unstub brew
unstub make
unstub patch

Expand All @@ -118,8 +120,8 @@ OUT
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install
stub patch ' : echo patch "$@" | sed -E "s/\.[[:alnum:]]+$/.XXX/" >> build.log'
Expand All @@ -132,6 +134,7 @@ PATCH
assert_success

unstub uname
unstub brew
unstub make
unstub patch

Expand All @@ -150,8 +153,8 @@ OUT
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install
stub patch ' : echo patch "$@" | sed -E "s/\.[[:alnum:]]+$/.XXX/" >> build.log'
Expand All @@ -165,6 +168,7 @@ PATCH
assert_success

unstub uname
unstub brew
unstub make
unstub patch

Expand All @@ -185,11 +189,13 @@ OUT
brew_libdir="$TMP/homebrew-yaml"
mkdir -p "$brew_libdir"

stub uname '-s : echo Linux'
stub brew "--prefix libyaml : echo '$brew_libdir'" false false
stub_repeated uname '-s : echo Linux'
stub_repeated brew "--prefix libyaml : echo '$brew_libdir'"
stub_make_install

install_fixture definitions/needs-yaml
run_inline_definition <<DEF
install_package "ruby-2.0.0" "http://ruby-lang.org/ruby/2.0/ruby-2.0.0.tar.gz"
DEF
assert_success

unstub uname
Expand All @@ -209,7 +215,7 @@ OUT
gmp_libdir="$TMP/homebrew-gmp"
mkdir -p "$gmp_libdir"

stub brew false "--prefix gmp : echo '$gmp_libdir'"
stub_repeated brew "--prefix gmp : echo '$gmp_libdir'"
stub_make_install

run_inline_definition <<DEF
Expand All @@ -233,7 +239,7 @@ OUT
readline_libdir="$TMP/homebrew-readline"
mkdir -p "$readline_libdir"

stub brew "--prefix readline : echo '$readline_libdir'" false
stub_repeated brew "--prefix readline : echo '$readline_libdir'"
stub_make_install

run_inline_definition <<DEF
Expand All @@ -254,7 +260,7 @@ OUT
@test "readline is not linked from Homebrew when explicitly defined" {
cached_tarball "ruby-2.0.0"

stub brew false
stub_repeated brew false
stub_make_install

export RUBY_CONFIGURE_OPTS='--with-readline-dir=/custom'
Expand All @@ -276,7 +282,7 @@ OUT
@test "number of CPU cores defaults to 2" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Darwin' false
stub_repeated uname '-s : echo Darwin'
stub sysctl false
stub_make_install

Expand All @@ -299,7 +305,7 @@ OUT
@test "number of CPU cores is detected on Mac" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Darwin' false
stub_repeated uname '-s : echo Darwin'
stub sysctl '-n hw.ncpu : echo 4'
stub_make_install

Expand All @@ -323,7 +329,7 @@ OUT
@test "number of CPU cores is detected on FreeBSD" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo FreeBSD' false
stub_repeated uname '-s : echo FreeBSD'
stub sysctl '-n hw.ncpu : echo 1'
stub_make_install

Expand All @@ -347,7 +353,7 @@ OUT
@test "setting RUBY_MAKE_INSTALL_OPTS to a multi-word string" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub_repeated uname '-s : echo Linux'
stub_make_install

export RUBY_MAKE_INSTALL_OPTS="DOGE=\"such wow\""
Expand All @@ -369,7 +375,7 @@ OUT
@test "setting MAKE_INSTALL_OPTS to a multi-word string" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub_repeated uname '-s : echo Linux'
stub_make_install

export MAKE_INSTALL_OPTS="DOGE=\"such wow\""
Expand Down Expand Up @@ -400,7 +406,7 @@ OUT
@test "make on FreeBSD defaults to gmake" {
cached_tarball "ruby-2.0.0"

stub uname "-s : echo FreeBSD" false
stub_repeated uname "-s : echo FreeBSD"
MAKE=gmake stub_make_install

MAKE= install_fixture definitions/vanilla-ruby
Expand All @@ -419,7 +425,7 @@ apply -p1 -i /my/patch.diff
exec ./configure "\$@"
CONF

stub uname '-s : echo Linux'
stub_repeated uname '-s : echo Linux'
stub apply 'echo apply "$@" >> build.log'
stub_make_install

Expand Down
30 changes: 20 additions & 10 deletions test/compiler.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export -n CC
export -n RUBY_CONFIGURE_OPTS

@test "require_gcc on OS X 10.9" {
stub uname '-s : echo Darwin'
stub_repeated uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.9.5'
stub gcc '--version : echo 4.2.1' '--version : echo 4.2.1'
stub_repeated gcc '--version : echo 4.2.1'

run_inline_definition <<DEF
require_gcc
Expand All @@ -22,12 +22,16 @@ DEF
CC=${TMP}/bin/gcc
MACOSX_DEPLOYMENT_TARGET=no
OUT

unstub uname
unstub sw_vers
unstub gcc
}

@test "require_gcc on OS X 10.10" {
stub uname '-s : echo Darwin'
stub_repeated uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.10'
stub gcc '--version : echo 4.2.1' '--version : echo 4.2.1'
stub_repeated gcc '--version : echo 4.2.1'

run_inline_definition <<DEF
require_gcc
Expand All @@ -39,6 +43,10 @@ DEF
CC=${TMP}/bin/gcc
MACOSX_DEPLOYMENT_TARGET=10.9
OUT

unstub uname
unstub sw_vers
unstub gcc
}

@test "require_gcc silences warnings" {
Expand All @@ -55,13 +63,10 @@ DEF
mkdir -p "$INSTALL_ROOT"
cd "$INSTALL_ROOT"

stub uname '-s : echo Darwin' '-s : echo Darwin'
stub_repeated uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.10'
stub cc 'false'
stub brew 'false'
stub make \
'echo make $@' \
'echo make $@'
stub_repeated brew 'false'
stub_repeated make 'echo make $@'

cat > ./configure <<CON
#!${BASH}
Expand All @@ -83,4 +88,9 @@ CFLAGS=no
make -j 2
make install
OUT

unstub uname
unstub sw_vers
unstub brew
unstub make
}
3 changes: 2 additions & 1 deletion test/rbenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ OUT
}

@test "nonexistent version" {
stub brew false
stub_repeated brew false
stub_ruby_build 'echo ERROR >&2 && exit 2' \
"--definitions : echo 1.8.7 1.9.3-p0 1.9.3-p194 2.1.2 | tr ' ' $'\\n'"

Expand All @@ -97,6 +97,7 @@ If the version you need is missing, try upgrading ruby-build:
git -C ${BATS_TEST_DIRNAME}/.. pull
OUT

unstub brew
unstub ruby-build
}

Expand Down
10 changes: 6 additions & 4 deletions test/stubs/stub
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
set -e

status=0
status=127
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
[ -n "$TMPDIR" ] || TMPDIR="/tmp"

_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_NOINDEX="${PROGRAM}_STUB_NOINDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
Expand All @@ -32,7 +33,7 @@ index=0
while IFS= read -r line; do
index=$(($index + 1))

if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
if [[ -z "${!_STUB_END}" && -n "${!_STUB_NOINDEX}" || $index -eq "${!_STUB_INDEX}" ]]; then
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
Expand Down Expand Up @@ -72,7 +73,8 @@ while IFS= read -r line; do
( eval "$command" )
status="$?"
set -e
else
[ -z "${!_STUB_NOINDEX}" ] || break
elif [ -z "${!_STUB_NOINDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
fi
Expand All @@ -95,7 +97,7 @@ if [ -n "${!_STUB_END}" ]; then
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
if [ "${!_STUB_INDEX}" -gt $index ]; then
if [[ -z "${!_STUB_NOINDEX}" && "${!_STUB_INDEX}" -gt $index ]]; then
eval "${_STUB_RESULT}"=1
fi

Expand Down
Loading