# -*- 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
# $Id: Portfile 116266 2014-01-23 16:04:23Z michaelld@macports.org $

PortSystem          1.0
PortGroup           active_variants 1.1
PortGroup           compiler_blacklist_versions 1.0

name                octave

version             3.6.4
revision            12
#conflicts           octave-devel
categories          math science
maintainers         michaelld openmaintainer
license             GPL-3
platforms           darwin

description         a Matlab-like environment for numerical analysis
long_description    Octave provides a convenient command line interface \
                    for solving linear and nonlinear problems numerically, \
                    using a language that is mostly compatible with Matlab. \
                    It is easily extensible and customizable via \
                    user-defined functions or using dynamically loaded \
                    modules written in e.g. C++, C or Fortran.

homepage            http://www.gnu.org/software/octave/
dist_subdir         octave
distname            octave-${version}
master_sites        gnu:octave
use_bzip2           yes

checksums \
    rmd160  b0356f0c5d9366b8129d72ac58bbde837fee1a95 \
    sha256  fa185df9591f5d740f4d37ff6eaf06db576fdd3e0ddd9d0737365799e5d1c962

# temporary patches; these are already, or will be, fixed upstream.

patchfiles-append \
    patch-liboctave-eigs-base.cc.diff \
    patch-liboctave-regexp.h.diff \
    patch-configure.diff \
    texinfo5.patch

if {${os.major} >= 11} {

    # 10.7 and newer requires an extra patch; this patch will
    # break the build on 10.6 and prior.

    patchfiles-append patch-src-display.cc.diff

}

depends_build-append \
                    port:bison \
                    port:flex \
                    port:gperf \
                    path:bin/perl:perl5 \
                    port:pkgconfig

depends_lib-append \
                    path:lib/libgcc/libgcc_s.1.dylib:libgcc \
                    port:arpack \
                    port:curl \
                    port:fftw-3 \
                    port:fftw-3-single \
                    port:gawk \
                    port:glpk \
                    port:gnuplot \
                    port:GraphicsMagick \
                    port:grep \
                    port:gsed \
                    port:hdf5-18 \
                    port:less \
                    port:ncurses \
                    port:pcre \
                    port:qhull \
                    port:qrupdate \
                    port:readline \
                    port:SuiteSparse \
                    port:zlib

depends_run-append  port:epstool \
                    port:ghostscript \
                    port:transfig \
                    port:pstoedit

universal_variant   no

# common configure arguments

configure.args \
    --disable-dependency-tracking \
    --with-umfpack="-lumfpack -lSuiteSparse"

# octave uses a number of other ports to create sources from template:
# perl, gawk, gsed, flex, bison, texinfo.  Make sure these are the
# MacPorts' versions.  Python is not used if perl is available, so
# clear it out.  grep is checked for in 'configure', but not used
# except inside that script; include it here for completion.

configure.perl      ${prefix}/bin/perl
configure.python    ' '
configure.awk       ${prefix}/bin/gawk
configure.env-append GREP="${prefix}/bin/grep" \
                     SED="${prefix}/bin/gsed" \
                     TEXI2DVI="${prefix}/bin/texi2dvi" \
                     TEXI2PDF="${prefix}/bin/texi2pdf"
configure.cppflags
configure.ldflags

test.run            yes
test.target         check

# Block compilers: Some older versions of CLANG do not honor the CPATH
# environment variables, which is required for compiling this port
# when using MacPorts.  The versions seem to be: MacPorts CLANG 2.9 or
# earlier, and Apple CLANG 318.0.58 or older.
#
# See also < http://llvm.org/bugs/show_bug.cgi?id=8971 >
#          < https://trac.macports.org/ticket/40250 >.

compiler.blacklist-append { clang <= 318.0.61 } macports-clang-2.9

platform darwin {
    if {${os.major} >= 12} {

        # In 10.8+, the LANG environment variable needs to be set to
        # "C" otherwise /usr/bin/sed fails with an error.  Ideally,
        # octave's build system would honor the environment variable
        # ${SED} throughout; reality is that it does not, so this
        # addition is required because /usr/bin/sed will be used.

        build.args-append LANG="C"
    }
}

set gcc_versions {4.3 4.4 4.5 4.6 4.7 4.8 4.9}
set gcc_default "4.8"

# create g95 variant, with full conflicts of GCC versions

set gcc_conflicts ""
foreach ver ${gcc_versions} {
    set gcc_ver_no_dot gcc[join [split ${ver} "."] ""]
    set gcc_conflicts "${gcc_conflicts} ${gcc_ver_no_dot}"
}

eval [subst {
    variant g95 conflicts ${gcc_conflicts} requires accelerate \
        description "Build with MacPorts g95 as the Fortran Compiler" {
            depends_lib-append port:g95
            configure.fc ${prefix}/bin/g95
            configure.f77 ${prefix}/bin/g95
            configure.f90 ${prefix}/bin/g95
        }
}]

# create GCC variants, with partial conflicts of other GCC versions

foreach ver1 ${gcc_versions} {

    set gcc_ver1_no_dot gcc[join [split ${ver1} "."] ""]

    set gcc_conflicts "conflicts g95"
    foreach ver2 ${gcc_versions} {
        if {${ver1} eq ${ver2}} {
            continue
        }
        set gcc_ver2_no_dot gcc[join [split ${ver2} "."] ""]
        set gcc_conflicts "${gcc_conflicts} ${gcc_ver2_no_dot}"
    }

    eval [subst {
        variant ${gcc_ver1_no_dot} ${gcc_conflicts} \
            description "Build with the MacPorts ${gcc_ver1_no_dot} as the Fortran Compiler" {
                depends_lib-append port:${gcc_ver1_no_dot}
                configure.fc  ${prefix}/bin/gfortran-mp-${ver1}
                configure.f77 ${prefix}/bin/gfortran-mp-${ver1}
                configure.f90 ${prefix}/bin/gfortran-mp-${ver1}
            }
    }]
}

# check for setting the default variant

set var_selection ""
if {[variant_isset g95]} {
    set var_selection "g95"
} else {
    foreach ver ${gcc_versions} {
        set gcc_ver_no_dot gcc[join [split ${ver} "."] ""]
        if {[variant_isset ${gcc_ver_no_dot}]} {
            set var_selection "${gcc_ver_no_dot}"
        }
    }
}

# check for need for default variant

set gcc_default_no_dot gcc[join [split ${gcc_default} "."] ""]
if {"${var_selection}" eq ""} {
    default_variants +${gcc_default_no_dot}
}

# check if the user disabled just the default variant

set var_selection ""
if {[variant_isset g95]} {
    set var_selection "g95"
} else {
    foreach ver ${gcc_versions} {
        set gcc_ver_no_dot gcc[join [split ${ver} "."] ""]
        if {[variant_isset ${gcc_ver_no_dot}]} {
            set var_selection "${gcc_ver_no_dot}"
        }
    }
}

if {"${var_selection}" eq ""} {
    ui_error "\n\nYou cannot use the -${gcc_default_no_dot} variant alone; a Fortran compiler is required.\n"
    return -code error "Invalid variant selection"
}

# atlas does not work with g95, so always conflict with it

variant atlas description {use BLAS from MacPorts' atlas port} \
    conflicts g95 accelerate {}

variant accelerate description \
    {use BLAS from Apple's Accelerate.framework [might be buggy]} \
    conflicts atlas {}

if {![variant_isset accelerate] && ![variant_isset atlas]} {
    default_variants +atlas
}

# make sure that either +accelerate or +atlas is selected

if {![variant_isset accelerate] && ![variant_isset atlas]} {

    ui_error "\n\nYou must select either the +accelerate or +atlas variant.\n"
    return -code error "Invalid variant selection"

}

if {[variant_isset accelerate]} {

    depends_lib-append port:dotwrp
    configure.args-append \
        --with-blas="-ldotwrp -Wl,-framework -Wl,Accelerate" \
        --with-lapack="-Wl,-framework -Wl,Accelerate"

} else {

    depends_lib-append    port:atlas
    # NOTE: libtatlas does not work as of 3.10.1_5
    configure.args-append \
        --with-blas="-lcblas -lf77blas -latlas" \
        --with-lapack="-llapack -lgfortran"

}

variant docs description {Enable creation and installation of \
                          documentation including manpages} {}

if {[variant_isset docs]} {

    depends_lib-append \
        port:texinfo \
        port:texlive-latex

    configure.args-append --enable-docs

} else {

    configure.args-append --disable-docs

}

variant x11 description {Enable use of X11} {}

if {[variant_isset x11]} {

    configure.args-append --with-x --x-includes=${prefix}

} else {

    configure.args-append --without-x

}

variant fltk description {Enable FLTK as a graphical front-end} {}

if {[variant_isset fltk]} {

    depends_lib-append path:lib/libfltk.dylib:fltk \
                       port:fontconfig \
                       port:freetype
    configure.args-append --with-opengl

} else {

    configure.args-append --without-opengl

}

variant metis description {Use SuiteSparse + Metis for graph partitioning} {
    pre-fetch {
        ui_msg "\nWARNING: ${name} variant +metis uses the metis port, whose license is incompatible with Octave's.  You can use this combination locally, but you cannot distribute them as a single package (e.g., tarball, m/pkg, archive)."
    }
}

if {[variant_isset metis]} {

    depends_lib-append      port:metis
    configure.args-append   --with-cholmod="-lcholmod -lmetis"

    # make sure SuiteSparse is installed with +metis
    require_active_variants SuiteSparse metis

} else {

    configure.args-append   --with-cholmod="-lcholmod"

    # make sure SuiteSparse is installed without +metis
    require_active_variants SuiteSparse {} metis

}

pre-fetch {

    # check for +accelerate here as well as in dependent ports;
    # Apple's VecLib has bugs that cause Octave to crash sometimes,
    # while Atlas does not have these issues.  Print a warning if this
    # variant is in use, but do not force the use of +atlas (for now).

    if {![catch {set result [active_variants arpack accelerate {}]}]} {
        if {$result} {
            ui_msg "\nWARNING: Dependency 'arpack' is installed with the +accelerate variant, using Apple's Vector Libraries which have known bugs that can cause Octave to crash if using certain functions in arpack.  The +atlas variant does not have these issues with Octave, and is considered by Octave developers a better way to go.\n"
        }
    }

    if {[variant_isset accelerate]} {
        ui_msg "\nWARNING: The +accelerate variant has been selected, using Apple's Vector Libraries which have known bugs that can cause Octave to crash.  The +atlas variant does not have these issues with Octave, and is considered by Octave developers a better way to go.\n"
    }
}

# https://trac.macports.org/ticket/41582

use_parallel_build  no

livecheck.type      regex
livecheck.url       http://www.gnu.org/software/octave/download.html
livecheck.regex     GNU Octave (\[\^\ \]*) was
