Project

General

Profile

Bug #19592 ยป ruby_static_test.sh

npic1 (Nat Pic1), 04/12/2023 01:47 PM

 
#!/usr/bin/env bash

set -e

# WORKING
# RUBY_VERSION="3.1.3"
# RUBY_SHA256="5ea498a35f4cd15875200a52dde42b6eb179e1264e17d78732c3a57cd1c6ab9e"

# NOT WORKING
RUBY_VERSION="3.1.4"
RUBY_SHA256="a3d55879a0dfab1d7141fdf10d22a07dbf8e5cdc4415da1bde06127d5cc3c7b6"

# NOT WORKING
# RUBY_VERSION="3.2.2"
# RUBY_SHA256="96c57558871a6748de5bc9f274e93f4b5aad06cd8f37befa0e8d94e7b8a423bc"

# download and unpack ruby

if [ ! -f "ruby-${RUBY_VERSION}.tar.gz" ]; then
curl -LO "https://cache.ruby-lang.org/pub/ruby/${RUBY_VERSION%.*}/ruby-${RUBY_VERSION}.tar.gz"
fi

if ! (echo "$RUBY_SHA256 ruby-${RUBY_VERSION}.tar.gz" | shasum -a 256 -c); then
exit 1
fi

rm -rf "ruby-${RUBY_VERSION}"
tar xzf "ruby-${RUBY_VERSION}.tar.gz"

cd "ruby-${RUBY_VERSION}"

# add a simple extension

mkdir ext/simple_ext

cat > ext/simple_ext/simple_ext.c <<- EOF
#include "ruby.h"
#include "extconf.h"

VALUE rb_do_nothing(VALUE class)
{
return Qnil;
}

void Init_simple_ext()
{
VALUE module = rb_define_module("SimpleExt");
VALUE class = rb_define_class_under(module, "AClass", rb_cObject);

rb_define_module_function(class, "do_nothing", rb_do_nothing, 0);
}
EOF

cat > ext/simple_ext/extconf.rb <<- EOF
require 'mkmf'

append_cppflags(['-fPIC'])
append_ldflags(['-Wl,-z,relro,-z,now'])

create_header
create_makefile 'simple_ext'
EOF

# statically link the extension

echo "simple_ext" >> ext/Setup

# build

./configure --disable-install-doc
make

# check that the extension works

if ! ./ruby -e "require 'simple_ext'; SimpleExt::AClass.do_nothing"; then
echo 'Failed to load the extension'
exit 1
fi

echo 'ALL GOOD'
    (1-1/1)