# -*- 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 107945 2013-07-09 20:36:36Z michaelld@macports.org $

PortSystem          1.0
PortGroup           active_variants 1.1

name                octave-devel
version             3.6.4
revision            5
conflicts           octave
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/
master_sites        gnu:octave
dist_subdir         octave
distname            octave-${version}
use_bzip2           yes

checksums           rmd160  b0356f0c5d9366b8129d72ac58bbde837fee1a95 \
                    sha256  fa185df9591f5d740f4d37ff6eaf06db576fdd3e0ddd9d0737365799e5d1c962

depends_build       port:bison \
                    port:flex \
                    port:gawk \
                    port:gperf \
                    port:grep \
                    port:gsed \
                    port:less \
                    path:bin/perl:perl5 \
                    port:pkgconfig \
                    port:texinfo

depends_lib         port:arpack \
                    port:curl \
                    port:fftw-3 \
                    port:fftw-3-single \
                    port:glpk \
                    port:GraphicsMagick \
                    port:hdf5-18 \
                    port:ncurses \
                    port:pcre \
                    port:qhull \
                    port:qrupdate \
                    port:readline \
                    port:SuiteSparse \
                    port:zlib

depends_run-append  port:epstool \
                    port:ghostscript \
                    port:gnuplot \
                    port:less \
                    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

# temporary patches; these are fixed upstream already.

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

platform darwin 11 {
    # 10.7 requires an extra patch; this patch will break the build on
    # 10.6 and prior, so apply it only under 10.7.

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

platform darwin 12 {
    # In 10.8 patch is required as above, also
    # the LANG environment variable needs to
    # be set to "C" otherwise /usr/bin/sed
    # fails with an error, if you installed gsed
    # with default name this should have no effect.

    patchfiles-append patch-src-display.cc.diff
    build.args-append LANG="C"
}

variant gcc43 description {build with the macports gcc43 toolchain} \
    conflicts gcc49 gcc48 gcc47 gcc46 gcc45 gcc44 g95 {}

variant gcc44 description {build with the macports gcc44 toolchain} \
    conflicts gcc49 gcc48 gcc47 gcc46 gcc45 gcc43 g95 {}

variant gcc45 description {build with the macports gcc45 toolchain} \
    conflicts gcc49 gcc48 gcc47 gcc46 gcc44 gcc43 g95 {}

variant gcc46 description {build with the macports gcc46 toolchain} \
    conflicts gcc49 gcc48 gcc47 gcc45 gcc44 gcc43 g95 {}

variant gcc47 description {build with the macports gcc47 toolchain} \
    conflicts gcc49 gcc48 gcc46 gcc45 gcc44 gcc43 g95 {}

variant gcc48 description {build with the macports gcc48 toolchain} \
    conflicts gcc49 gcc47 gcc46 gcc45 gcc44 gcc43 g95 {}

variant gcc49 description {build with the macports gcc49 toolchain} \
    conflicts gcc48 gcc47 gcc46 gcc45 gcc44 gcc43 g95 {}

variant g95 description {build with g95 as the Fortran compiler} \
    conflicts gcc49 gcc48 gcc47 gcc46 gcc45 gcc44 gcc43 {}

# check for GCC / G95 variants.  The default here does not have to
# match the default found in the 'atlas' port.

# check for setting the default variant (gcc47)

if { ![variant_isset gcc43] && ![variant_isset gcc44] && \
     ![variant_isset gcc45] && ![variant_isset gcc46] && \
     ![variant_isset gcc47] && ![variant_isset gcc48] && \
     ![variant_isset gcc49] && ![variant_isset g95] } {
    default_variants +gcc47
}

# check if the user disabled just the default variant: -gcc47

if { ![variant_isset gcc43] && ![variant_isset gcc44] && \
     ![variant_isset gcc45] && ![variant_isset gcc46] && \
     ![variant_isset gcc47] && ![variant_isset gcc48] && \
     ![variant_isset gcc49] && ![variant_isset g95] } {
    ui_error "\n\nYou cannot use the -gcc47 variant alone; a Fortran compiler is required.\n"
    return -code error "Invalid variant selection"
}

# use the correct +gccXY, if selected

set gcc_version ""
if {[variant_isset gcc43]} {
    set gcc_version "4.3"
} elseif {[variant_isset gcc44]} {
    set gcc_version "4.4"
} elseif {[variant_isset gcc45]} {
    set gcc_version "4.5"
} elseif {[variant_isset gcc46]} {
    set gcc_version "4.6"
} elseif {[variant_isset gcc47]} {
    set gcc_version "4.7"
} elseif {[variant_isset gcc48]} {
    set gcc_version "4.8"
} elseif {[variant_isset gcc49]} {
    set gcc_version "4.9"
}

if {${gcc_version} != ""} {

    set gcc_version_join [join [split ${gcc_version} "."] ""]
    depends_build-append port:gcc${gcc_version_join}
    configure.ldflags    -L${prefix}/lib/gcc${gcc_version_join} \
        -lstdc++ -lgfortran
    configure.compiler   macports-gcc-${gcc_version}

}

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 g95]} {

    # +g95, so no +gccXY; blacklist all of the MacPorts GCC compilers;
    # all others should work OK, since they do not provide Fortran.
    # When using +gccXY, the Fortran compiler from that variant must
    # be used otherwise symbols will come up as undefined.

    compiler.blacklist \
        macports-gcc-4.2 macports-gcc-4.3 macports-gcc-4.4 \
        macports-gcc-4.5 macports-gcc-4.6 macports-gcc-4.7 \
        macports-gcc-4.8 macports-gcc-4.9

    default_variants     +accelerate

    depends_build-append port:g95
    configure.f77        ${prefix}/bin/g95

} else {

    # no +g95, so using +gccXY; blacklist all the other non-Fortran
    # providing compilers; we cannot mix and match compilers if using
    # +gccXY.

    compiler.blacklist gcc-3.3 gcc-4.0 gcc-4.2 llvm-gcc-4.2 \
        apple-gcc-4.0 apple-gcc-4.2 clang macports-clang-2.9 \
        macports-clang-3.0 macports-clang-3.1 macports-clang-3.2 \
        macports-clang-3.3 macports-llvm-gcc-4.2

    # if using +gccXY, default to +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"

}

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

if {[variant_isset docs]} {

    depends_run-append    port:texlive-basic
    # latex needed for lcircle10 font etc.
    depends_build-append  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 metis 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"
    }
}

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