# -*- 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 bc409f3fe3fab2e540f8630aced0655be68dfac3

set ruby_ver        3.4
set ruby_patch      0
set ruby_ver_nodot  [string map {. {}} ${ruby_ver}]
name                ruby-devel
version             2024.11.16
revision            0

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  be783086ae20f0567841acc35256c79e7b85e6e6 \
                    sha256  160d55522d17f8170aea9e4da42ef2b0f7d5367260e127e6e3269a0ffffdf568 \
                    size    16234334
github.tarball_from archive

universal_variant   no

# We need baseruby to build from upstream/master.
# Minimum required version is 2.7. However ruby27 is broken on PPC: https://trac.macports.org/ticket/64906
# Since this is a devel port, let it depend on recent a Ruby version.
# Darwin PPC support was re-introduced from 3.1.
set baseruby_ver    3.3
set baseruby_ver_nodot \
                    [string map {. {}} ${baseruby_ver}]

use_autoreconf      yes
autoreconf.cmd      ./autogen.sh

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

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_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   0001-Use-numerical-macOS-versions.patch

post-patch {
    # rewrite "gem" to "gem${ruby_ver}"
    # def gem_command
    #  ENV["GEM_COMMAND"]&.shellsplit || ["gem"]
    # end
    reinplace -E "s/(shellsplit .. .)(\"gem\")/\\1\"gem${ruby_ver}\"/g" ${worksrcpath}/lib/bundler/gem_helper.rb
    # get meaningful output during the build:
    reinplace "s/\$(Q) \$(CC)/\$(CC)/g" ${worksrcpath}/template/Makefile.in
}

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 \
                    --enable-shared \
                    --enable-install-static-library \
                    --disable-install-doc \
                    --mandir="${prefix}/share/man" \
                    --enable-pthread \
                    --disable-yjit \
                    --without-gmp \
                    --with-opt-dir="${prefix}" \
                    --program-suffix=${ruby_ver} \
                    --with-rubyhdrdir=${prefix}/include/ruby-${ruby_ver}.${ruby_patch} \
                    --with-rubylibprefix="${prefix}/lib/ruby${ruby_ver}" \
                    --with-openssl-dir=[openssl::install_area] \
                    --with-baseruby=${prefix}/bin/ruby${baseruby_ver}

# 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} < 9} {
        known_fail   yes
        pre-fetch {
            ui_error "${name} is not supported on Darwin ${os.major}"
            return -code error "incompatible macOS version"
        }
    }
}

# 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:rb32*
    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" {
        configure.args-delete   --without-gmp
        depends_lib-append      port:gmp
}

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

variant yjit description "use YJIT" {
        configure.args-delete   --disable-yjit
        configure.args-append   --enable-yjit
        depends_build-append    port:rust
}

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
