# -*- 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 compilers 1.0
PortGroup compiler_blacklist_versions 1.0
PortGroup active_variants 1.1

name                        R
# Remember to set revision to 0 when bumping version
version                     4.1.2
revision                    1

set branch                  [join [lrange [split ${version} .] 0 1] .]
categories                  math science
maintainers                 {me.com:kjell.konis @kjellpk} {i0ntempest @i0ntempest} openmaintainer
license                     {GPL-2 GPL-3}
platforms                   macosx

description \
    R is GNU S - an interpreted language for statistical computing

long_description \
    R is a language and environment for statistical computing and graphics. \
    R provides a wide variety of statistical (linear and nonlinear modeling, \
    classical statistical tests, time-series analysis, classification, \
    clustering, ...) and graphical techniques, and is highly extensible.

homepage                    https://www.r-project.org/

master_sites                https://cran.rstudio.com/src/base/R-4/ \
                            https://cran.r-project.org/src/base/R-4/

checksums                   rmd160  36166eb518e8e33ba5dfc196b6d57380144c0393 \
                            sha256  2036225e9f7207d4ce097e54972aecdaa8b40d7d9911cd26491fac5a0fab38af \
                            size    34009074

# Fails to configure with Xcode clang 4.6.3
compiler.blacklist-append   *clang* macports-gcc-8 macports-gcc-9 macports-gcc-10 macports-gcc-11
compiler.whitelist macports-gcc-7

compilers.choose            fc f77
compilers.setup             require_fortran

depends_build               port:pkgconfig

depends_lib                 port:readline \
                            port:icu \
                            port:libiconv \
                            port:zlib \
                            port:pcre \
                            port:pcre2 \
                            path:include/turbojpeg.h:libjpeg-turbo \
                            port:tiff \
                            port:libpng \
                            port:curl \
                            port:less \
                            port:texinfo \
                            port:bzip2 \
                            port:gzip \
                            port:zip \
                            port:unzip \
                            port:gnutar \
                            port:xz

# avoid finding ${prefix}/bin/xdg-open first
configure.env-append R_PDFVIEWER=/usr/bin/open \
                     R_BROWSER=/usr/bin/open

universal_variant           no

set resources               ${frameworks_dir}/R.framework/Versions/${branch}/Resources

post-patch {
    reinplace "s|R_HOME|\"${resources}\"|" "${worksrcpath}/src/unix/Rscript.c"

    ## Check to see if this is fixed post 3.3.0
    reinplace "s|<libintl.h>|\"libintl.h\"|" "${worksrcpath}/src/include/Defn.h"
}

# Note: gcc cannot be used for the C compiler. It will give:
#:info:build In file included from /usr/include/dispatch/dispatch.h:51:0,
#:info:build                  from /System/Library/Frameworks/CoreFoundation.framework/Headers/CFStream.h:15,
#:info:build                  from /System/Library/Frameworks/CoreFoundation.framework/Headers/CFPropertyList.h:13,
#:info:build                  from langprefs.c:30:
#:info:build /usr/include/dispatch/object.h:143:15: error: expected identifier or '(' before '^' token
#:info:build  typedef void (^dispatch_block_t)(void);
#:info:build                ^
#:info:build /usr/include/dispatch/object.h:362:3: error: unknown type name 'dispatch_block_t'
#:info:build    dispatch_block_t notification_block);
#:info:build    ^
# However, use of compiler.blacklist *gcc* would remove the GCC Fortran compilers too.

configure.pre_args          --prefix=${frameworks_dir}

configure.args              --disable-openmp \
                            --enable-R-framework \
                            --enable-memory-profiling \
                            --enable-R-shlib \
                            --enable-BLAS-shlib \
                            --without-tcltk \
                            --without-cairo \
                            --disable-java \
                            --without-recommended-packages \
                            --without-x \
                            --with-included-gettext

if {${os.platform} eq "darwin" && ${os.major} < 13} {
    configure.args-append --disable-openmp
}

platform darwin 13 {
    configure.cflags-append -flax-vector-conversions
}

variant accelerate conflicts atlas builtin_lapack openblas description {build using the BLAS and Lapack in Apple's Accelerate framework} {
    configure.args-append   --with-blas="-framework Accelerate" --with-lapack
}

variant atlas conflicts accelerate builtin_lapack openblas description {build using the BLAS in the atlas port} {
    depends_lib-append      port:atlas
    configure.args-append   --with-blas="-L${prefix}/lib -lptf77blas -latlas"
    #See A.3.2 in R Installation and Administration for why atlas LAPACK not used
}

variant builtin_lapack conflicts accelerate atlas openblas description {build using reference BLAS and Lapack} {
    configure.args-append   --without-blas --without-lapack
}

variant openblas conflicts builtin_lapack accelerate atlas description {build using the BLAS and Lapack in the OpenBLAS port} {
    depends_lib-append      port:OpenBLAS
    configure.args-delete   --enable-BLAS-shlib
    configure.args-append   --with-blas="-L${prefix}/lib -lopenblas" --with-lapack
}

variant openmp description {enable parallelism support using OpenMP} {
    compiler.openmp_version 4.5
    compiler.blacklist-append   {macports-clang-[0-9].*}
    configure.args-replace  --disable-openmp --enable-openmp
    notes-append "
        Clang compilers provided by Xcode do not support OpenMP. Some R packages using\
        OpenMP may require additional flags in the Makevar file (e.g. data.table) to\
        build. Please consult package documentation."
}

variant cairo description {use cairo and pango} {
    depends_lib-append      path:lib/pkgconfig/cairo.pc:cairo \
                            path:lib/pkgconfig/pango.pc:pango \
                            path:lib/pkgconfig/glib-2.0.pc:glib2 \
                            port:freetype \
                            port:fontconfig \
                            port:gettext
    configure.args-replace  --without-cairo \
                            --with-cairo
}

variant debug description {build with debug symbols} {
    configure.optflags-append -g
}

variant recommended description {install recommended R packages} {
    configure.args-delete   --without-recommended-packages
    configure.args-append   --with-recommended-packages
}

variant tcltk description {enable use of tcltk} {
    depends_lib-append      port:tcl \
                            port:tk
    configure.args-delete   --without-tcltk
    configure.args-append   --with-tcltk \
                            --with-tcl-config=${prefix}/lib/tclConfig.sh \
                            --with-tk-config=${prefix}/lib/tkConfig.sh
}

variant tests description {include tests of R installation} {
    destroot.target-append install-tests
}

variant x11 conflicts quartz {
    require_active_variants tk x11
    depends_lib-append      port:xorg-libsm \
                            port:xorg-libice \
                            port:xorg-libX11 \
                            port:xorg-libXt
    configure.args-delete   --without-x
    configure.args-append   --with-x \
                            --x-include=${prefix}/include/X11 \
                            --x-lib=${prefix}/lib
}

variant quartz conflicts x11 {
    require_active_variants tk quartz
    notes-append "
        Note that R with quartz variant will not work with R.app because\
        of conflicts over the macOS menu."
}

variant java description {enable Java} {
    PortGroup java 1.0
    configure.args-replace --disable-java --enable-java
}

default_variants +cairo +recommended +openmp

if {![variant_isset quartz]} {
    default_variants-append +x11
}

if {[variant_isset cairo] && [variant_isset x11]} {
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
    require_active_variants path:lib/pkgconfig/pango.pc:pango x11
}

if {[variant_isset cairo] && [variant_isset quartz]} {
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo quartz
    require_active_variants path:lib/pkgconfig/pango.pc:pango quartz
}

if {![variant_isset accelerate] && ![variant_isset atlas] && ![variant_isset openblas] && ![variant_isset builtin_lapack]} {
    default_variants-append +builtin_lapack
}

test.run                    yes
test.target                 check

destroot.destdir prefix=${destroot}${frameworks_dir}

post-destroot {
    move ${destroot}${frameworks_dir}/lib/pkgconfig/libR.pc ${destroot}${prefix}/lib/pkgconfig/libR.pc

    foreach v { "rhome" "rincludedir" } {
        reinplace "s|${v}=${destroot}|${v}=|" "${destroot}${prefix}/lib/pkgconfig/libR.pc"
    }

    foreach dir { "R_HOME_DIR" "R_SHARE_DIR" "R_INCLUDE_DIR" "R_DOC_DIR" } {
        reinplace "s|${dir}=${destroot}|${dir}=|" "${destroot}${resources}/bin/R"
    }

    reinplace "s|-F${destroot}|-F|" "${destroot}${resources}/etc/Makeconf"

    foreach dylib [ exec find ${destroot}${frameworks_dir}/R.framework -name "\*.dylib" ] {
        regsub ":$" ${dylib} "" destroot_dylib_path
        regsub ${destroot} ${destroot_dylib_path} "" dylib_path
        system "install_name_tool -id ${dylib_path} ${destroot_dylib_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libR.dylib ${resources}/lib/libR.dylib \
            ${destroot_dylib_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRblas.dylib ${resources}/lib/libRblas.dylib \
            ${destroot_dylib_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRlapack.dylib ${resources}/lib/libRlapack.dylib \
            ${destroot_dylib_path}"
    }

    foreach so [ exec find ${destroot}${frameworks_dir}/R.framework -name "\*.so" ] {
        regsub ":$" ${so} "" destroot_so_path
        regsub ${destroot} ${destroot_so_path} "" so_path
        system "install_name_tool -id ${so_path} ${destroot_so_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libR.dylib ${resources}/lib/libR.dylib \
            ${destroot_so_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRblas.dylib ${resources}/lib/libRblas.dylib \
            ${destroot_so_path}"
        system "install_name_tool -change ${destroot}${resources}/lib/libRlapack.dylib ${resources}/lib/libRlapack.dylib \
            ${destroot_so_path}"
    }

    system "install_name_tool -change ${destroot}${resources}/lib/libR.dylib ${resources}/lib/libR.dylib \
        ${destroot}${resources}/bin/exec/R"
    system "install_name_tool -change ${destroot}${resources}/lib/libRblas.dylib ${resources}/lib/libRblas.dylib \
        ${destroot}${resources}/bin/exec/R"

    ln -s ${resources}/bin/R ${destroot}${prefix}/bin/R
    ln -s ${resources}/bin/Rscript ${destroot}${prefix}/bin/Rscript
    ln -s ${resources}/man1/R.1 ${destroot}${prefix}/share/man/man1/R.1
    ln -s ${resources}/man1/Rscript.1 ${destroot}${prefix}/share/man/man1/Rscript.1
}

livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     >${name}-(\[0-9.\]+)${extract.suffix}<
