# -*- 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
PortGroup           openssl 1.0
PortGroup           deprecated 1.0

# Reached EOL on 2021-03-31
deprecated.upstream_support no

name                ruby25
version             2.5.9
revision            4

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, \
                    straightforward, extensible, and portable.

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

master_sites        ruby:2.5
use_bzip2           yes
distname            ruby-${version}
dist_subdir         ruby25

checksums           md5 9e905a545a729af1f1620ddfc2976fe5 \
                    rmd160 46a69aeab46021457c27c1f15aa3d5a4eee76263 \
                    sha1 6ac21486996aa38a71f858d28d01ada5593d0b45 \
                    sha256 bebbe3fe7899acd3ca2f213de38158709555e88a13f85ba5dc95239654bcfeeb \
                    size 13805484

# ruby/openssl does not support openssl-3
openssl.branch      1.1

depends_lib         port:readline \
                    port:zlib \
                    port:libyaml \
                    port:libffi \
                    port:gdbm
depends_run         port:ruby_select
depends_build       port:pkgconfig
depends_skip_archcheck pkgconfig

# requires gmake 3.81 or later
if {${os.major} < 10} {
    depends_build-append    port:gmake
    build.cmd               ${prefix}/bin/gmake
}

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

# Fix build on 10.4 i386
patchfiles          patch-tiger.diff
# Fix build on < 10.6
patchfiles-append   patch-osversions.diff
# patch-configure_cxx11.diff: fix "invalid suffix on literal" with C++11
#                             from RUBY_ARCH and RUBY_PLATFORM in config.h
#                             https://trac.macports.org/ticket/58255
patchfiles-append   patch-configure_cxx11.diff
# Fix test failure at ext/fiddle
patchfiles-append   patch-test-fiddle-helper.rb.diff
# Fix ext/openssl uses wrong pkgconfig (#63845)
patchfiles-append   patch-ext-openssl-extconf.rb.diff

# replace old config.{guess,sub} with recent versions from automake
depends_build-append    port:automake
post-patch {
    set automake_dirs [glob -directory ${prefix}/share automake-*]
    set automake_dir [lindex [lsort -command vercmp $automake_dirs] end]
    copy -force ${automake_dir}/config.guess ${automake_dir}/config.sub \
        ${worksrcpath}/tool
}

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.5 \
                    --with-rubylibprefix="${prefix}/lib/ruby2.5" \
                    --with-openssl-include=[openssl::include_dir] \
                    --with-openssl-lib=[openssl::lib_dir]

# [NOTE] workaround for mismatch of sdk versions on macOS 11.x,
# such as MacOSX11.0.sdk (buildbot) <=> MacOSX11.1.sdk (user's Mac).
# (see ruby27)
post-build {
    if {${os.major} >= 20} {
        # rewrite MacOSX11.1.sdk -> MacOSX.sdk in rbconfig.rb
        file copy ${worksrcpath}/rbconfig.rb ${worksrcpath}/rbconfig.rb.orig
        reinplace -E -q {s|(/MacOSX)[0-9\.]+(\.sdk[\'\"]?[[:blank:]])|\1\2|g} \
            ${worksrcpath}/rbconfig.rb \
            ${worksrcpath}/ruby-2.5.pc
        reinplace -E -q {s|(/MacOSX)[0-9\.]+(\.sdk[\'\"]?)$|\1\2|g} \
            ${worksrcpath}/rbconfig.rb \
            ${worksrcpath}/ruby-2.5.pc
    }
}

# 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
    }
}

# 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.5/${type}_ruby/2.5.0
            xinstall -m 0755 -d ${libdir}
    }

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

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

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

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

variant universal {
        # Disable parallel builds for Universal case
        # See: https://trac.macports.org/ticket/24240
        use_parallel_build      no

        # 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.5/
livecheck.regex ruby-(2\\.5\\.\\d+)[quotemeta ${extract.suffix}]