From c8927eefd07ffd4f829bbbc53f6e8eea839352cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 11 Oct 2023 15:05:03 +0200 Subject: [PATCH 1/6] Expand `parse_options` to preserve positional arguments after "--" --- bin/ruby-build | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bin/ruby-build b/bin/ruby-build index 2e655ccbfb..58d86e87f1 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -26,10 +26,15 @@ lib() { parse_options() { OPTIONS=() ARGUMENTS=() + EXTRA_ARGUMENTS=() local arg option index - for arg in "$@"; do - if [ "${arg:0:1}" = "-" ]; then + while [ $# -gt 0 ]; do + arg="$1" + if [ "$arg" == "--" ]; then + shift 1 + break + elif [ "${arg:0:1}" = "-" ]; then if [ "${arg:1:1}" = "-" ]; then OPTIONS[${#OPTIONS[*]}]="${arg:2}" else @@ -40,10 +45,14 @@ lib() { index=$(($index+1)) done fi + shift 1 else ARGUMENTS[${#ARGUMENTS[*]}]="$arg" + shift 1 fi done + + EXTRA_ARGUMENTS=("$@") } if [ "$1" == "--$FUNCNAME" ]; then From f7d63b7dcb1d80c50df6c062fe05c1463dcbfc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 11 Oct 2023 15:08:35 +0200 Subject: [PATCH 2/6] Rework argument parsing to error out on invalid flags --- bin/ruby-build | 70 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/bin/ruby-build b/bin/ruby-build index 58d86e87f1..74a72a46a4 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -1268,6 +1268,7 @@ unset KEEP_BUILD_PATH unset HAS_PATCH unset IPV4 unset IPV6 +unset EARLY_EXIT RUBY_BUILD_INSTALL_PREFIX="$(abs_dirname "$0")/.." @@ -1279,17 +1280,13 @@ parse_options "$@" for option in "${OPTIONS[@]}"; do case "$option" in "h" | "help" ) - version - echo - usage 0 + EARLY_EXIT=help ;; "definitions" ) - list_definitions - exit 0 + EARLY_EXIT=list_definitions ;; "l" | "list") - list_maintained_versions - exit 0 + EARLY_EXIT=list_maintained_versions ;; "k" | "keep" ) KEEP_BUILD_PATH=true @@ -1307,18 +1304,57 @@ for option in "${OPTIONS[@]}"; do IPV6=true ;; "version" ) - version - exit 0 + EARLY_EXIT=version + ;; + * ) + printf "ruby-build: invalid flag '%s'\n" "$option" >&2 + EARLY_EXIT=usage_error ;; esac done -[ "${#ARGUMENTS[@]}" -eq 2 ] || usage 1 >&2 - DEFINITION_PATH="${ARGUMENTS[0]}" -if [ -z "$DEFINITION_PATH" ]; then +PREFIX_PATH="${ARGUMENTS[1]}" + +if [ -z "$EARLY_EXIT" ] && [ -z "$DEFINITION_PATH" ]; then + echo "ruby-build: missing definition argument" >&2 + EARLY_EXIT=usage_error +fi + +if [ -z "$EARLY_EXIT" ] && [ -z "$PREFIX_PATH" ]; then + echo "ruby-build: missing prefix argument" >&2 + EARLY_EXIT=usage_error +fi + +if [ "${#ARGUMENTS[@]}" -gt 2 ]; then + echo "ruby-build: expected at most 2 arguments, got [${ARGUMENTS[*]}]" >&2 + EARLY_EXIT=usage_error +fi + +case "$EARLY_EXIT" in +help ) + version + echo + usage 0 + ;; +version | list_definitions | list_maintained_versions ) + "$EARLY_EXIT" + exit 0 + ;; +usage_error ) + echo >&2 usage 1 >&2 -elif [ ! -f "$DEFINITION_PATH" ]; then + ;; +'' ) + ;; +* ) + echo "unimplemented EARLY_EXIT: $EARLY_EXIT" >&2 + exit 1 + ;; +esac + +# expand the argument to full path of the definition file +if [ ! -f "$DEFINITION_PATH" ]; then for DEFINITION_DIR in "${RUBY_BUILD_DEFINITIONS[@]}"; do if [ -f "${DEFINITION_DIR}/${DEFINITION_PATH}" ]; then DEFINITION_PATH="${DEFINITION_DIR}/${DEFINITION_PATH}" @@ -1332,10 +1368,8 @@ elif [ ! -f "$DEFINITION_PATH" ]; then fi fi -PREFIX_PATH="${ARGUMENTS[1]}" -if [ -z "$PREFIX_PATH" ]; then - usage 1 >&2 -elif [ "${PREFIX_PATH#/}" = "$PREFIX_PATH" ]; then +# normalize the argument +if [ "${PREFIX_PATH#/}" = "$PREFIX_PATH" ]; then PREFIX_PATH="${PWD}/${PREFIX_PATH}" fi @@ -1366,7 +1400,7 @@ if [ -n "$noexec" ]; then fi if [ -z "$MAKE" ]; then - if is_freebsd && [[ $1 == jruby-* ]]; then + if is_freebsd && [[ ${ARGUMENTS[0]} == jruby-* ]]; then # jruby-launcher requires gmake: https://github.com/ruby/ruby/pull/8591 export MAKE="gmake" else From fbdf88cc138e6954818ef18215b1329fe6084695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 11 Oct 2023 15:10:02 +0200 Subject: [PATCH 3/6] Accept ruby configuration flags as extra position arguments on the command line --- bin/rbenv-install | 5 ++++- bin/ruby-build | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/rbenv-install b/bin/rbenv-install index 4f6a3e5aad..0f8a399152 100755 --- a/bin/rbenv-install +++ b/bin/rbenv-install @@ -221,9 +221,12 @@ cleanup() { trap cleanup SIGINT +build_args=(${KEEP:+--keep} ${VERBOSE:+--verbose} ${HAS_PATCH:+--patch} "$DEFINITION" "$PREFIX") +[ ${#EXTRA_ARGUMENTS[@]} -eq 0 ] || build_args+=(-- "${EXTRA_ARGUMENTS[@]}") + # Invoke `ruby-build` and record the exit status in $STATUS. STATUS=0 -ruby-build $KEEP $VERBOSE $HAS_PATCH "$DEFINITION" "$PREFIX" || STATUS="$?" +ruby-build "${build_args[@]}" || STATUS="$?" # Display a more helpful message if the definition wasn't found. if [ "$STATUS" == "2" ]; then diff --git a/bin/ruby-build b/bin/ruby-build index 74a72a46a4..064128d816 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -1331,6 +1331,10 @@ if [ "${#ARGUMENTS[@]}" -gt 2 ]; then EARLY_EXIT=usage_error fi +if [ "${#EXTRA_ARGUMENTS[@]}" -gt 0 ]; then + RUBY_CONFIGURE_OPTS_ARRAY=("${EXTRA_ARGUMENTS[@]}") +fi + case "$EARLY_EXIT" in help ) version From d9bdc904b93a0ba0731cf59a1dc1564a6268c017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 11 Oct 2023 15:11:27 +0200 Subject: [PATCH 4/6] :nail_care: RUBY_CONFIGURE_OPTS_ARRAY --- bin/ruby-build | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bin/ruby-build b/bin/ruby-build index 064128d816..720454634b 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -529,6 +529,7 @@ build_package() { package_option() { local package_name="$1" local command_name="$2" + # e.g. RUBY_CONFIGURE_OPTS_ARRAY, OPENSSL_MAKE_OPTS_ARRAY local variable="$(capitalize "${package_name}_${command_name}")_OPTS_ARRAY" local array="$variable[@]" shift 2 @@ -650,18 +651,18 @@ build_package_ruby() { build_package_ree_installer() { build_package_auto_tcltk - local options="" - is_mac && options="--no-tcmalloc" + local options=() + is_mac && options+=(--no-tcmalloc) local option - for option in ${RUBY_CONFIGURE_OPTS_ARRAY[@]} $RUBY_CONFIGURE_OPTS; do - options="$options -c $option" + for option in "${RUBY_CONFIGURE_OPTS_ARRAY[@]}" $RUBY_CONFIGURE_OPTS; do + options+=(-c "$option") done # Work around install_useful_libraries crash with --dont-install-useful-gems mkdir -p "$PREFIX_PATH/lib/ruby/gems/1.8/gems" - { ./installer --auto "$PREFIX_PATH" --dont-install-useful-gems $options $CONFIGURE_OPTS + { ./installer --auto "$PREFIX_PATH" --dont-install-useful-gems "${options[@]}" $CONFIGURE_OPTS } >&4 2>&1 } From 6b6fa457a0bb2e2966e1460f7119cdd73e305600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 11 Oct 2023 16:44:52 +0200 Subject: [PATCH 5/6] Enable test assertions to spot erraneous word-splitting in bash --- test/build.bats | 84 ++++++++++++++++++---------------------------- test/compiler.bats | 2 +- test/stubs/stub | 15 +++++++++ 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/test/build.bats b/test/build.bats index 0e1d6c9a5e..953ce201f6 100755 --- a/test/build.bats +++ b/test/build.bats @@ -35,7 +35,8 @@ tarball() { executable "$configure" <> build.log +IFS=, +echo "$name: [\$*]" \${RUBYOPT:+RUBYOPT=\$RUBYOPT} >> build.log OUT for file; do @@ -48,8 +49,8 @@ OUT stub_make_install() { stub "$MAKE" \ - " : echo \"$MAKE \$@\" >> build.log" \ - "install : echo \"$MAKE \$@\" >> build.log && cat build.log >> '$INSTALL_ROOT/build.log'" + " : echo \"$MAKE \$(inspect_args \"\$@\")\" >> build.log" \ + "install : echo \"$MAKE \$(inspect_args \"\$@\")\" >> build.log && cat build.log >> '$INSTALL_ROOT/build.log'" } assert_build_log() { @@ -74,10 +75,10 @@ assert_build_log() { unstub make assert_build_log <> '$INSTALL_ROOT'/build.log +IFS=, +echo "\$0 [\$*]" >> '$INSTALL_ROOT'/build.log mkdir -p build/host/bin touch build/host/bin/{mruby,mirb} chmod +x build/host/bin/{mruby,mirb} @@ -477,7 +458,7 @@ install_package "mruby-1.0" "http://ruby-lang.org/pub/mruby-1.0.tar.gz" mruby DEF assert_success assert_build_log <> ../build.log +IFS=, +echo "jruby [\$*]" >> ../build.log OUT executable "${RUBY_BUILD_CACHE_PATH}/jruby-1.7.9/bin/gem" < ./configure < Date: Wed, 11 Oct 2023 16:54:58 +0200 Subject: [PATCH 6/6] Add tests for extra configure flags on the command-line --- test/build.bats | 28 +++++++++++++++++++++++++++- test/rbenv.bats | 11 +++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/test/build.bats b/test/build.bats index 953ce201f6..3c2829c246 100755 --- a/test/build.bats +++ b/test/build.bats @@ -261,7 +261,10 @@ OUT @test "readline is not linked from Homebrew when explicitly defined" { cached_tarball "ruby-2.0.0" - stub_repeated brew false + readline_libdir="$TMP/homebrew-readline" + mkdir -p "$readline_libdir" + + stub_repeated brew "--prefix readline : echo '$readline_libdir'" ' : false' stub_make_install export RUBY_CONFIGURE_OPTS='--with-readline-dir=/custom' @@ -280,6 +283,29 @@ make install OUT } +@test "forward extra command-line arguments as configure flags" { + cached_tarball "ruby-2.0.0" + + stub_repeated brew false + stub_make_install + + cat > "$TMP/build-definition" <