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

name                fricas
version             1.3.12
revision            2
categories          math
maintainers         {@pietvo vanoostrum.org:pieter} openmaintainer
license             BSD

description         The Fricas computer algebra system
long_description \
    FriCAS is a computer algebra system. FriCAS is a fork of Axiom. \
    The basic goal of FriCAS is to create a free advanced \
    world-class CAS. FriCAS builds on the Axiom codebase. The FriCAS \
    algebra library is one of the largest and most advanced free \
    general purpose computer algebra systems -- this gives a good \
    foundation to build on. Additionally, the FriCAS algebra library \
    is written in a high level strongly typed language (Spad), which \
    allows natural expression of mathematical algorithms. This makes \
    FriCAS easier to understand and extend.
       
homepage            http://fricas.sourceforge.net/
master_sites        sourceforge:${name}/${version}
distname            ${name}-${version}-full
use_bzip2           yes
worksrcdir          ${name}-${version}
                
checksums           rmd160  ce0f047efa02865e4f44e3258e9d435db60569f9 \
                    sha256  33201f9f56c20b1266d38f5290efe7486a38422ea90f707f0345f6a589e31c8d \
                    size    10951028

configure.dir       ${workpath}/fricas-build
configure.cmd       ${worksrcpath}/configure
pre-configure       {file mkdir ${configure.dir}}

build.dir           ${configure.dir}
build.args          MAYBE_VIEWPORTS=viewports

test.run            yes
test.target         check

variant x11 description {Enable X11 support.\
    Without X11, Fricas does not support graphics and hyperdoc} {
    configure.args-append   --with-x -x-includes=${prefix}/include \
    -x-libraries=${prefix}/lib
    depends_lib-append  port:xorg-libX11 \
                        port:xpm \
                        port:xorg-libice \
                        port:xorg-libsm
}

universal_variant       no

set sbcl_script         {sbcl --control-stack-size 512 --dynamic-space-size 6000}
if {${build_arch} in [list ppc i386]} {
    # 32bit system can't address large dynamic space, 1Gb is safe upper limit
    set sbcl_script     {sbcl --control-stack-size 512 --dynamic-space-size 1024}
}
set orig_sbcl_script    $sbcl_script

# emulate behavior from la.lisp from hasbcl-XXX.tar
variant hunchentoot description {Include Hunchentoot into fricas} {
    depends_build-append    port:cl-hunchentoot
    set sbcl_script         ${workpath}/hsbcl
    pre-configure {
        set loadcmd ${orig_sbcl_script}
        append loadcmd " --non-interactive"
        append loadcmd " --eval '(require \"asdf\")'"
        append loadcmd " --eval '(require \"hunchentoot\")'"
        append loadcmd " --eval '(sb-ext::save-lisp-and-die \"${sbcl_script}\" :executable t) '"
        set tempdir [mkdtemp "/tmp/fricas.XXXXXXXX"]
        if { ! [catch {system -W ${tempdir} "${loadcmd} 2>&1"}] } {
            file delete -force ${tempdir}
        } else {
            file delete -force ${tempdir}
            return -code error "${loadcmd} cannot be executed"
        }
    }
}

variant sbcl conflicts ccl ecl \
    description {Use SBCL as lisp implementation} {
    depends_lib-append      path:bin/sbcl:sbcl
    configure.args-append   --with-lisp='${sbcl_script}'
}

if { ${configure.build_arch} eq "x86_64" } {
    set ccl_script ccl64
} elseif { ${configure.build_arch} eq "i386" } {
    set ccl_script ccl
}

variant ccl conflicts gmp sbcl ecl \
    description {Use CCL (Clozure) instead of SBCL as lisp implementation} {
    depends_lib-append      port:ccl
    configure.args-append   --with-lisp=${ccl_script}
}

variant ecl conflicts gmp sbcl ccl \
    description {Use ECL instead of SBCL as lisp implementation} {
    depends_lib-append      path:bin/ecl:ecl
    configure.args-append   --with-lisp=ecl
}

# Fricas doesn't compile under GCL
# Fricas compiles under CLISP but doesn't run correctly
# So we leave these variants out until we get them working

variant gmp conflicts ccl ecl \
    description {Use libgmp for faster calculations} {
    configure.args-append   --with-gmp=${prefix}
    depends_lib-append      port:gmp
}

default_variants    +x11

if {   ![variant_isset ccl] && ![variant_isset ecl] } {
    default_variants +sbcl
}
