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

PortSystem      1.0
PortGroup       muniversal 1.0
PortGroup       select 1.0
PortGroup       openssl 1.0
PortGroup       deprecated 1.0

# Reached EOL on 2014-07-31
deprecated.upstream_support no

name            ruby
version         1.8.7-p374
revision        15

categories      lang ruby
maintainers     {kimuraw @kimuraw}

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 GPL-2}
master_sites    ruby:1.8
                
dist_subdir     ruby

checksums       rmd160  314efaa961c80c9294202c6e40dec0e38865ed94 \
                sha256  b4e34703137f7bfb8761c4ea474f7438d6ccf440b3d35f39cc5e4d4e239c07e3 \
                size    4250351

use_bzip2       yes

platforms       darwin

depends_lib     port:libiconv \
                port:readline \
                port:zlib \
                port:ncurses \
                port:gdbm
depends_run     port:ruby_select

openssl.branch    1.0
openssl.configure pkgconfig build_flags

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

# vendordir: enable vendor-specific.rb
# #3604: gcc4 fails to detect linking at configure
# #19050: use $(CC) not cc, this change has been merged at ruby-1.8 trunk
# #15528: on some Mac, TCPServer.open("localhost", 0) raises SocketError
#         like "getaddrinfo: nodename nor servname provided, or not
#         known (SocketError)"
# c99: gperf generates code that doesn't work with clang (https://trac.macports.org/ticket/29974)
# lib-drb-ssl.rb: 512bits rsa key is too small for openssl-1.0.1.
#                 (http://bugs.ruby-lang.org/issues/6221)
# ext/openssl/ossl.h: asn1_mac.h was deprecated. use asn1.h.
# Makefile.in: force libruby without suffix (backward compatibility)
# --- clang support ----
# numeric.c: fix wrong optimizaton of Numeric/Bignum build with clang.
# configure: correct frame_address and stack_direction for +universal with clang.
# ext-tk-extconf.rb: use linker option "-rpath," not "-R".
patchfiles      patch-vendordir.diff \
                patch-bug3604.diff \
                patch-bug19050.diff \
                patch-bug15528.diff \
                patch-c99.diff \
                patch-lib-drb-ssl.rb.diff \
                patch-Makefile.in \
                patch-numeric.c.diff \
                patch-configure.diff \
                patch-ext-tk-extconf.rb.diff \
                patch-ext_openssl_extconf_rb.diff \
                patch-ext_openssl_ossl_ssl_c.diff \
                patch-ext_openssl_ossl.h.diff \
                implicit.patch \
                conversion.patch

# ignore getcontext() and setcontext()
# on 10.5 or later, these functions have some problems (SEGV on ppc, slower than 1.8.6)
# http://chopine.be/lrz/ruby-osx-patches/ignore-gsetcontext.diff
patchfiles-append patch-node.h.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}
}

use_parallel_build      no

# [OK] apple-gcc-4.2
#      clang (Xcode 4.2 or later) with -O[0s]
#      clang (Xcode 11 on macOS 10.15) with -O[01]
# [NG] llvm-gcc-4.2
#      clang (Xcode 4.1 or earlier)
#      clang (Xcode 4.2 or later) with -O[12]
#      clang (Xcode 11 on macOS 10.15) with -O[s2]
# TODO: When was this last verified?  Is there a ticket open? Radar?
compiler.blacklist-append *llvm-gcc-4.2 {clang < 211}

if {[string match *clang* ${configure.compiler}]} {
    configure.optflags -Os
    if {${os.major} >= 19} {
        # https://trac.macports.org/ticket/59216
        configure.optflags -O1
    }
}

configure.args  --enable-shared \
                --mandir="${prefix}/share/man" \
                --enable-pthread \
                --without-tk \
                --with-vendordir=${prefix}/lib/ruby/vendor_ruby \
                --program-suffix=1.8
# ignore minor version for archdir, like i686-darwin9
configure.env   UNAME_RELEASE=${os.major}

# [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
        reinplace -E -q {s|(/MacOSX)[0-9\.]+(\.sdk[\'\"]?)$|\1\2|g} \
            ${worksrcpath}/rbconfig.rb
    }
}

if {![info exists universal_possible]} {
    set universal_possible [expr {${os.universal_supported} && [llength ${configure.universal_archs}] >= 2}]
}
if {${universal_possible} && [variant_isset universal]} {
    # NOTE: directory layout of port:ruby +universal
    #   universal binary
    #     - bin/
    #     - lib/libruby*.dylib, libruby-static.a
    #   single architecture modules and headers, such as ruby.h or config.h
    #     - lib/ruby/[1.8|site_ruby/1.8|vendor_ruby/1.8]/x86_64-apple-darwin*/
    #     -                                             /i386-apple-darwin*/
    #     -                                             /ppc-apple-darwin*/
    #   pure ruby libraries
    #     - lib/ruby/[1.8|site_ruby/1.8|vendor_ruby/1.8]/
    foreach arch ${configure.universal_archs} {
        lappend merger_configure_env(${arch}) "ARCH_FLAG=-arch ${arch}"
        # force environment "ac_cv_build" to specify locations of extension
        # modules(*.bundle), headers(ruby.h, config.h, ..) and rbconfig.rb.
        # like this:
        #   i386   -> ${prefix}/lib/ruby/1.8/i386-apple-darwin10/
        #   x86_64 ->                       /x86_64-apple-darwin10/
        set cpu_type ${arch}
        lappend merger_configure_env(${arch}) \
            "ac_cv_build=${cpu_type}-apple-darwin${os.major}"
    }
} elseif {[info exists build_arch] && ${build_arch} ne ""} {
    configure.env-append "ARCH_FLAG=-arch ${build_arch}"
}

destroot.target install install-doc
post-destroot {
    foreach type {site vendor} {
        set libdir ${destroot}${prefix}/lib/ruby/${type}_ruby/1.8
        xinstall -m 0755 -d ${libdir}
        
        # special file for installation control of modules
        xinstall -m 0644 ${filespath}/${type}-specific.rb ${libdir}
        
        # prevent empty site/vendor directories from being pruned
        foreach subdir [exec find ${libdir} -type d -empty] {
            destroot.keepdirs-append ${subdir}
        }
    }
    # install destination of commands from port:rb-*
    xinstall -m 0755 -d ${destroot}${prefix}/libexec/ruby1.8
    destroot.keepdirs-append ${destroot}${prefix}/libexec/ruby1.8
    # error at activate, for these case sensitive files
    foreach rdoc {Exception2MessageMapper/fail-i.yaml \
                  REXML/Parsers/XPathParser/predicate-i.yaml} {
        if {[file exists ${destroot}${prefix}/share/ri/1.8/system/${rdoc}]} {
            delete ${destroot}${prefix}/share/ri/1.8/system/${rdoc}
        }
    }
}

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

platform darwin {
    # for proper rdoc/ri creation, make sure to link to destroot libruby.dylib
    destroot.env    DYLD_LIBRARY_PATH=${destroot}${prefix}/lib
}

variant tk conflicts mactk description "enable tk support" {
    configure.args-delete   --without-tk
    configure.args-append   --with-tk --disable-tcltk-framework
    depends_lib-append      port:tcl port:tk
}

variant mactk conflicts tk description "enable MacTk (Tk.framework without X11) support" {
    configure.args-delete   --without-tk
    configure.args-append   --enable-tcltk-framework
}

variant no_doc description "do not install rdoc documents" {
    destroot.target-delete  install-doc
}

variant dtrace description "enable dtrace support" {
    # from http://www.opensource.apple.com/source/ruby/ruby-79/
    patchfiles-append       patch-apple-dtrace.diff
    configure.cflags-append -DENABLE_DTRACE
    post-patch {
        system "cd ${worksrcpath} && dtrace -h -s ${filespath}/dtrace.d -o dtrace.h"
    }
}

test.run    yes
