# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           conflicts_build 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0
PortGroup           select 1.0

# MAP_ANONYMOUS
# TODO: submit a fix to the upstream.
legacysupport.newest_darwin_requires_legacy 14

# ruby/openssl since ruby-3.2 supports openssl-3
openssl.branch      3

github.setup        ruby ruby 9a90ac21ffd17ef8165ab8324514d2f4c7ed81a8

# Change to 4.1 on the next update.
set ruby_ver        4.0
set ruby_patch      0
set ruby_ver_nodot  [string map {. {}} ${ruby_ver}]
name                ruby-devel
version             ${ruby_ver}.${ruby_patch}-2026.06.13
revision            0
epoch               1
categories          lang ruby
maintainers         {@barracuda156 gmail.com:vital.had} {kimuraw @kimuraw} openmaintainer

description         Powerful and clean object-oriented scripting language
long_description    Ruby is the interpreted scripting language \
                    for quick and easy object-oriented programming. \
                    It has many features to process text files \
                    and to do system management tasks (as in Perl). \
                    It is simple, straight-forward, extensible and portable.

homepage            https://www.ruby-lang.org
license             {Ruby BSD}

checksums           rmd160  71df70136566ceeae406f93469b0db7a3871958d \
                    sha256  996f763556cddf40a8aabed23d557f64ff5d3d7cd2d9379acb1b7d26b4d89546 \
                    size    16029304
github.tarball_from archive

universal_variant   no

set baseruby_ver    3.4
set baseruby_ver_nodot \
                    [string map {. {}} ${baseruby_ver}]

use_autoreconf      yes
autoreconf.cmd      ./autogen.sh

depends_build-append \
                    port:autoconf \
                    port:automake \
                    port:bison \
                    port:cctools \
                    port:gperf \
                    port:libtool \
                    path:bin/pkg-config:pkgconfig \
                    port:ruby${baseruby_ver_nodot}

depends_lib-append  port:gdbm \
                    port:libffi \
                    port:libyaml \
                    port:readline \
                    port:zlib

depends_run-append  port:ruby_select

depends_skip_archcheck \
                    path:bin/pkg-config:pkgconfig

select.group        ruby
select.file         ${filespath}/ruby${ruby_ver_nodot}

patch.pre_args-replace  -p0 -p1
patchfiles-append   patch-sources.diff

patchfiles-append   0001-Use-numerical-macOS-versions.patch

# TODO: test this properly, revise the PR if needed:
# https://github.com/ruby/ruby/pull/13007
patchfiles-append   0002-ppc-ppc64-coroutines-save-restore-CR-as-well.patch

# version.c:85:38: error: 'RUBY_API_VERSION_NAME' undeclared here (not in a function); did you mean 'RUBY_API_VERSION_CODE'?
patchfiles-append   0003-version.c.patch

post-patch {
    reinplace "s,@RUBYVER@,${ruby_ver}," ${worksrcpath}/lib/bundler/gem_helper.rb
}

compiler.blacklist-append {clang < 901}

compiler.thread_local_storage yes

if { [string match macports-clang-* ${configure.compiler}] } {
    # clang-mp-14 => dsymutil-mp-14; fix POSTLINK
    configure.env-append dsymutil=[string map {clang dsymutil} ${configure.cc}]
}

configure.args-append \
                    --disable-install-doc \
                    --disable-rjit \
                    --disable-yjit \
                    --enable-mkmf-verbose \
                    --enable-pthread \
                    --enable-shared \
                    --enable-install-static-library \
                    --mandir="${prefix}/share/man" \
                    --program-suffix=${ruby_ver} \
                    --with-baseruby=${prefix}/bin/ruby${baseruby_ver} \
                    --with-opt-dir="${prefix}" \
                    --with-rubyhdrdir=${prefix}/include/ruby-${ruby_ver} \
                    --with-rubylibprefix="${prefix}/lib/ruby${ruby_ver}" \
                    --with-openssl-dir=[openssl::install_area] \
                    --without-gmp

# prefer Apple cctools to GNU binutils, build with binutils may fail.
configure.args-append   AR=${prefix}/bin/ar RANLIB=${prefix}/bin/ranlib

# Add the architecture flag as required
if {[info exists build_arch] && ${build_arch} != ""} {
    configure.args-append "--with-arch=${build_arch}"
}

platform darwin {
    if {${os.major} < 11} {
        depends_build-append    port:gmake
        build.cmd               ${prefix}/bin/gmake
        configure.args-append   --disable-dtrace
    }

    if {${os.major} == 8} {
        configure.cppflags-append -DCPU_SUBTYPE_MASK=0xff000000
    }
}

# https://github.com/ruby/ruby/pull/5975#issuecomment-1279751636
conflicts_build-append libunwind-headers

post-destroot {
    foreach type {site vendor} {
        set libdir ${destroot}${prefix}/lib/ruby${ruby_ver}/${type}_ruby/${version}
        xinstall -m 0755 -d ${libdir}
        foreach subdir [exec find ${libdir} -type d -empty] {
            destroot.keepdirs-append ${subdir}
        }
    }

    # install destination of commands from port:rb35*
    xinstall -m 0755 -d ${destroot}${prefix}/libexec/ruby${ruby_ver}
    destroot.keepdirs-append ${destroot}${prefix}/libexec/ruby${ruby_ver}
}

variant doc description "Install rdoc indexes and C API documents" {
        configure.args-delete   --disable-install-doc
}

variant gmp description "Use gmp" {
        depends_lib-append      port:gmp
        configure.args-delete   --without-gmp
}

variant jemalloc description "Use jemalloc" {
        depends_lib-append      path:lib/pkgconfig/jemalloc.pc:jemalloc
        configure.args-delete   --without-jemalloc
}

variant relative description "Enable relative loading of libraries to allow for relocation of binaries" {
        #enable relative loading
        configure.args-append  --enable-load-relative
}

notes-append "
To make this the default Ruby (i.e., the version run\
by the 'ruby', 'gem' or 'bundle' commands), run:
    sudo port select --set ruby ruby${ruby_ver_nodot}
"

test.run        yes
test.target     check
