Skip to content

Commit

Permalink
Instruct Ruby to fail the build if openssl or psych are missing
Browse files Browse the repository at this point in the history
Normally, Ruby `make` step will print a warning about any missing
extensions, but will not abort the build and instead proceed as normal.

Since Ruby installations without openssl or psych are essentially
broken, ruby-build used to have a `verify_openssl` build step to test if
the newly built Ruby can load these extensions, and print helpful
information and abort the build on errors:

    Loading the Ruby openssl extension failed
    ERROR: Ruby install aborted due to missing extensions

The `verify_opensl` implementation was necessary to provide a good
experience for ruby-build users, but was hacky and I would prefer to
eliminate it.

It appears that passing `--with-ext=openssl,psych` to the Ruby configure
step marks those extensions as mandatory and fails the `make` process if
they failed to build. This is exactly the behavior we want, so this
enables the configure option for all Ruby builds.
  • Loading branch information
mislav committed Nov 7, 2023
1 parent e320bdf commit 4789ff8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 64 deletions.
47 changes: 6 additions & 41 deletions bin/ruby-build
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ build_package_standard_build() {
package_option ruby configure --with-openssl-dir="/usr/local"
fi
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-ext* ]]; then
# Fail the `make` step if any of these extensions were not compiled.
package_option ruby configure --with-ext=openssl,psych
fi
fi

( if [ "${CFLAGS+defined}" ] || [ "${!PACKAGE_CFLAGS+defined}" ]; then
Expand Down Expand Up @@ -1212,48 +1216,9 @@ build_package_openssl() {
fi
}

# Post-install check that the openssl extension was built.
# TODO: explore replacing this implementation with scanning the `make` log
# for the "Following extensions are not compiled" block.
# Kept for backward compatibility with 3rd-party definitions.
build_package_verify_openssl() {
local msg
msg="->$(print_command "$RUBY_BIN" -e "<SCRIPT>")"

colorize 36 "$msg"
echo

# shellcheck disable=SC2016
"$RUBY_BIN" -e '
manager = ARGV[0]
packages = {
"apt-get" => Hash.new {|h,k| "lib#{k}-dev" }.update(
"openssl" => "libssl-dev",
"zlib" => "zlib1g-dev"
),
"yum" => Hash.new {|h,k| "#{k}-devel" }.update(
"yaml" => "libyaml-devel"
)
}
ext_name = Hash.new {|h,k| k }.update("yaml" => "psych")
failed = %w[openssl readline zlib yaml].reject do |lib|
begin
require lib
rescue LoadError => e
$stderr.puts "Loading the Ruby #{lib} extension failed (#{e})"
$stderr.puts "See the extension log at #{Dir.pwd}/ext/#{ext_name[lib]}/mkmf.log"
end
end
if failed.size > 0
$stderr.puts "ERROR: Ruby install aborted due to missing extensions"
$stderr.print "Try running `%s install -y %s` to fetch missing dependencies.\n\n" % [
manager,
failed.map { |lib| packages.fetch(manager)[lib] }.join(" ")
] unless manager.empty?
exit 1
end
' "$(basename "$(type -p yum apt-get | head -1)")"
true
}

# Kept for backward compatibility with 3rd-party definitions.
Expand Down
42 changes: 20 additions & 22 deletions test/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ assert_build_log() {
yaml-0.1.6: [--prefix=$INSTALL_ROOT]
make -j 2
make install
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand Down Expand Up @@ -144,7 +144,7 @@ yaml-0.1.6: [--prefix=$INSTALL_ROOT]
make -j 2
make install
patch -p0 --force -i $TMP/ruby-patch.XXX
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand Down Expand Up @@ -177,7 +177,7 @@ yaml-0.1.6: [--prefix=$INSTALL_ROOT]
make -j 2
make install
patch -p1 --force -i $TMP/ruby-patch.XXX
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand Down Expand Up @@ -211,7 +211,7 @@ yaml-0.1.6: [--prefix=$INSTALL_ROOT]
make -j 2
make install
patch -p1 --force -i $TMP/ruby-patch.XXX
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -237,7 +237,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-libyaml-dir=$brew_libdir]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-libyaml-dir=$brew_libdir,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -261,7 +261,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-gmp-dir=$gmp_libdir]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-gmp-dir=$gmp_libdir,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -285,7 +285,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-readline-dir=$readline_libdir]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-readline-dir=$readline_libdir,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -310,7 +310,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-readline-dir=/custom]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych,--with-readline-dir=/custom]
make -j 2
make install
OUT
Expand All @@ -337,7 +337,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand Down Expand Up @@ -374,7 +374,7 @@ DEF
openssl-1.1.1w: [--prefix=${INSTALL_ROOT}/openssl,--openssldir=${INSTALL_ROOT}/openssl/ssl,zlib-dynamic,no-ssl3,shared]
make -j 2
make install_sw install_ssldirs
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-openssl-dir=$INSTALL_ROOT/openssl]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-openssl-dir=$INSTALL_ROOT/openssl,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -398,7 +398,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-openssl-dir=/path/to/openssl]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych,--with-openssl-dir=/path/to/openssl]
make -j 2
make install
OUT
Expand Down Expand Up @@ -448,7 +448,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-openssl-dir=$TMP/homebrew/opt/openssl@3.0]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-openssl-dir=$TMP/homebrew/opt/openssl@3.0,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -471,7 +471,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,cppflags=-DYJIT_FORCE_ENABLE -DRUBY_PATCHLEVEL_NAME=test,--with-openssl-dir=/path/to/openssl,--with-readline-dir=/custom]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,cppflags=-DYJIT_FORCE_ENABLE -DRUBY_PATCHLEVEL_NAME=test,--with-openssl-dir=/path/to/openssl,--with-ext=openssl,psych,--with-readline-dir=/custom]
make -j 2
make install
OUT
Expand All @@ -494,7 +494,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand All @@ -518,7 +518,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 4
make install
OUT
Expand All @@ -532,8 +532,7 @@ OUT
stub_make_install

export -n MAKE_OPTS
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/test"
run_inline_definition <<DEF
RUBY_CONFIGURE_OPTS="--with-openssl-dir=/test" 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
Expand All @@ -543,7 +542,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-openssl-dir=/test]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych,--with-openssl-dir=/test]
make -j 1
make install
OUT
Expand All @@ -566,7 +565,7 @@ DEF
unstub make

assert_build_log <<OUT
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install --globalmake RUBYMAKE=true with spaces
OUT
Expand Down Expand Up @@ -594,8 +593,7 @@ CONF
stub apply 'echo apply "$@" >> build.log'
stub_make_install

export RUBY_CONFIGURE="${TMP}/custom-configure"
run_inline_definition <<DEF
RUBY_CONFIGURE="${TMP}/custom-configure" run_inline_definition <<DEF
install_package "ruby-2.0.0" "http://ruby-lang.org/pub/ruby-2.0.0.tar.gz"
DEF
assert_success
Expand All @@ -606,7 +604,7 @@ DEF

assert_build_log <<OUT
apply -p1 -i /my/patch.diff
ruby-2.0.0: [--prefix=$INSTALL_ROOT]
ruby-2.0.0: [--prefix=$INSTALL_ROOT,--with-ext=openssl,psych]
make -j 2
make install
OUT
Expand Down
2 changes: 1 addition & 1 deletion test/compiler.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ DEF
assert_success
run cat build.log
assert_output <<OUT
./configure --prefix=$INSTALL_ROOT
./configure --prefix=$INSTALL_ROOT --with-ext=openssl,psych
CC=clang
CFLAGS=no
make -j 2
Expand Down

0 comments on commit 4789ff8

Please sign in to comment.