# -*- 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           select 1.0

name                ruby24
version             2.4.10
revision            1

categories          lang ruby
maintainers         {kimuraw @kimuraw} openmaintainer
platforms           darwin

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            http://www.ruby-lang.org/
license             {Ruby BSD}

master_sites        ruby:2.4
use_bzip2           yes
distname            ruby-${version}
dist_subdir         ruby24

# fix #54866: correct install_name of libruby by deleting TEENY
#
#     $ otool -D /opt/local/lib/libruby.2.4.3.dylib
#     /opt/local/lib/libruby.2.4.3.dylib:
#     /opt/local/lib/libruby.2.4.dylib
patchfiles          patch-configure.diff

checksums           md5 b10a7d2fcaf218c98edbaf57efc36e58 \
                    rmd160 0249650b9da5f11b52533827af5db809e1fa6277 \
                    sha1 96737b609f4a82f8696669a17017a46f3bd07549 \
                    sha256 6ea3ce7fd0064524ae06dbdcd99741c990901dfc9c66d8139a02f907d30b95a8

use_parallel_build  no

depends_lib         port:readline \
                    path:lib/libssl.dylib:openssl \
                    port:zlib \
                    port:libyaml \
                    port:libffi \
                    port:gdbm
depends_run         port:ruby_select
depends_build       port:pkgconfig
depends_skip_archcheck pkgconfig

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

configure.args      --enable-shared \
                    --enable-install-static-library \
                    --disable-install-doc \
                    --mandir="${prefix}/share/man" \
                    --enable-pthread \
                    --without-gmp \
                    --with-opt-dir="${prefix}" \
                    --program-suffix=2.4 \
                    --with-rubylibprefix="${prefix}/lib/ruby2.4"

# https://github.com/ruby/ruby/commit/1961c786aab243b3eb60e7238224e87975d88056
# * configure.ac (LIBPATHENV): use DYLD_FALLBACK_LIBRARY_PATH instead of
# DYLD_LIBRARY_PATH on macOS, to honor runtime paths embedded in the
# binaries.  https://bugs.ruby-lang.org/issues/14992
configure.args-append \
                    LIBPATHENV=DYLD_FALLBACK_LIBRARY_PATH

platform darwin {
    if {${os.major} < 10} {
        configure.args-append --disable-dtrace
    }
    if {${os.major} == 8} {
        patchfiles-append    patch-ruby24-tigeri386.diff
    }
}

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

post-destroot {
    foreach type {site vendor} {
            set libdir ${destroot}${prefix}/lib/ruby2.4/${type}_ruby/2.4.0
            xinstall -m 0755 -d ${libdir}
    }

    foreach subdir [exec find ${libdir} -type d -empty] {
            destroot.keepdirs-append ${subdir}
    }

    # workaround to fix #54866 for versions 2.4.0..2.4.2
    # generate past versions of libruby as symlink
    # - libruby.2.4.[012].dylib -> libruby.2.4.x.dylib
    # libruby of 2.4.3 or later has corrected install_name "libruby.2.4.dylib"
    # from patch-configure.diff
    foreach v {2.4.0 2.4.1 2.4.2} {
            copy ${destroot}${prefix}/lib/libruby.2.4.dylib \
                ${destroot}${prefix}/lib/libruby.${v}.dylib
    }
    # install destination of commands from port:rb24-*
    xinstall -m 0755 -d ${destroot}${prefix}/libexec/ruby2.4
    destroot.keepdirs-append ${destroot}${prefix}/libexec/ruby2.4
}

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

# note: use gem or port:rb-tk for ruby/tk since ruby-2.4

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
        configure.args-append   --with-jemalloc
        depends_lib-append      port:jemalloc
}

variant universal {
        # use ruby built-in universal mechanism.
        configure.args-append   --with-arch=[join ${universal_archs} ,]
        # clear macports' universal flags
        configure.universal_cflags
        configure.universal_cppflags
        configure.universal_cxxflags
        configure.universal_ldflags
}

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

livecheck.type  regex
livecheck.url   https://cache.ruby-lang.org/pub/ruby/2.4/
livecheck.regex ruby-(2\\.4\\.\\d+)[quotemeta ${extract.suffix}]
