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

github.setup        gyoto Gyoto 1.2.0
revision            2
license             GPL-3+
categories          science yorick
platforms           darwin
maintainers         {thibaut @paumard} openmaintainer
description         General relativistic geodesic integration and ray-tracing
long_description    Gyoto aims at providing a framework for computing orbits \
                    and ray-traced images in General relativity. It consists \
                    in a C++ shared library (libgyoto), utility programs \
                    (gyoto, gyotoy), and a plug-in for the Yorick programming \
                    language. Gyoto can be extended with plug-ins.
homepage            http://gyoto.obspm.fr

checksums           rmd160  db4745d6cefcd4718442beca9c718583e737d73b \
                    sha256  790868d1b3cb4344d2b980a82192421f355b279f7bbe1de8f8bf217179ef291e

depends_build       port:LORENE \
                    port:pkgconfig

depends_lib         port:cfitsio \
                    port:xercesc3 \
                    port:udunits2 \
                    path:bin/yorick:yorick

depends_run         port:yorick-yutils \
                    port:yorick-gy

# yorick is not universal
universal_variant   no

configure.args      --with-yorick=${prefix}/bin/yorick \
                    --with-lorene=${prefix}/lib/lorene \
                    --with-xerces \
                    --disable-doc \
                    --with-cfitsio \
                    --with-udunits-inc=${prefix}/include/udunits2/ \
                    --with-udunits-lib=-L${prefix}/lib/ \
                    --enable-release \
                    --without-mpi \
                    --without-python

build.args-append   Y_CFLAGS="${configure.cxxflags}" Y_CPPFLAGS="${configure.cppflags}" CC="${configure.cc}" COPT_DEFAULT="" Y_LDFLAGS="${configure.ldflags}"


test.target         check check-lorene
test.run            yes

compilers.setup     -gcc
compilers.enforce_c lorene

if {![catch {set result [active_variants boost openmpi {}]}]} {
  if {$result} {
    default_variants +openmpi
  } else {
      if {[active_variants boost mpich {}]} {
          default_variants +mpich
      }
  }
}

foreach nodot [list 27 33 34 35 36] wdot [list 2.7 3.3 3.4 3.5 3.6] {
    subport py${nodot}-gyoto {
        depends_build-append port:doxygen port:swig-python
        depends_lib-append   port:python${nodot} port:py${nodot}-numpy \
                             port:Gyoto
        compilers.enforce_c  Gyoto
        categories    python science
        configure.args-delete --without-python
        configure.args-append PYTHON=${prefix}/bin/python${wdot}
        use_parallel_build no
        build.target  python plugins/python
        destroot.cmd  PYTHONPATH=${frameworks_dir}/Python.framework/Versions/${wdot}:\$PYHTONPATH make
        destroot.args -C python INSTALL_DATA=true prefix=${frameworks_dir}/Python.framework/Versions/${wdot}
        post-destroot {
            system -W ${worksrcpath}/plugins/python "${build.cmd} install DESTDIR=${destroot}"
            xinstall -d ${destroot}/${prefix}/share/doc/${subport}/examples
            xinstall {*}[glob ${worksrcpath}/plugins/python/doc/examples/*] \
                ${destroot}/${prefix}/share/doc/${subport}/examples/
            xinstall {*}[glob ${worksrcpath}/python/example*.py] \
                ${destroot}/${prefix}/share/doc/${subport}/examples/
        }
        test.run no
    }
}

subport Gyoto {
    PortGroup conflicts_build 1.0
    conflicts_build ${name}
    post-destroot {
        xinstall -W ${worksrcpath}/python \
            gyoto.i gyoto_std.i gyoto_lorene.i numpy.i \
            ${destroot}/${prefix}/include/Gyoto/
        xinstall -d ${destroot}/${prefix}/share/doc/${subport}/examples/plugins
        xinstall {*}[glob ${worksrcpath}/doc/examples/*] \
            ${destroot}/${prefix}/share/doc/${subport}/examples/
        copy ${worksrcpath}/plugins/null \
            ${destroot}/${prefix}/share/doc/${subport}/examples/plugins/null
        system \
            -W ${destroot}/${prefix}/share/doc/${subport}/examples/plugins/null \
            "touch aclocal.m4 configure Makefile.in */Makefile.in"
    }
}

if {[string match libc++ ${configure.cxx_stdlib}]} {

    depends_lib-append port:boost
    compilers.enforce_c boost

    variant openmpi conflicts mpich \
        description {Add MPI parallelization using OpenMPI} {
        set c_variant [c_variant_name]
        if {${c_variant} == ""} {
            set mpi_port openmpi
            set mpi_suffix mp
        } {
            set mpi_port openmpi-${c_variant}
            set mpi_suffix ${c_variant}
        }
        require_active_variants boost openmpi {}
            depends_lib-append port:${mpi_port}
        configure.args-delete --without-mpi
        configure.args-append --with-mpi \
            MPICC="mpicc-openmpi-${mpi_suffix}" \
            MPICXX="mpicxx-openmpi-${mpi_suffix}" \
            CC="mpicc-openmpi-${mpi_suffix}" \
            CXX="mpicxx-openmpi-${mpi_suffix}"
        build.args-delete CC="${configure.cc}"
        build.args-append CC="mpicc-openmpi-${mpi_suffix}"
        notes "
For multi-processing, wrap gyoto in mpirun-openmpi-${mpi_suffix}, e.g.:
  $ mpirun-openmpi-${mpi_suffix} gyoto -P4 in.xml out.fits
"    
    }

    variant mpich conflicts openmpi \
        description {Add MPI parallelization using MPICH} {
        set c_variant [c_variant_name]
        if {${c_variant} == ""} {
            set mpi_port mpich
            set mpi_suffix mp
        } {
            set mpi_port mpich-${c_variant}
            set mpi_suffix ${c_variant}
        }
        require_active_variants boost mpich {}
        depends_lib-append port:${mpi_port}
        configure.args-delete --without-mpi
        configure.args-append --with-mpi \
            MPICC="mpicc-mpich-${mpi_suffix}" \
            MPICXX="mpicxx-mpich-${mpi_suffix}" \
            CC="mpicc-mpich-${mpi_suffix}" \
            CXX="mpicxx-mpich-${mpi_suffix}"
        build.args-delete CC="${configure.cc}"
        build.args-append CC="mpicc-mpich-${mpi_suffix}"
        notes "
+-- Gyoto Usage note ------------------------------------------------------
| For multi-processing, wrap gyoto in mpirun-mpich-${mpi_suffix}, e.g.:
|   $ mpirun-mpich-${mpi_suffix} gyoto -P4 in.xml out.fits
+--------------------------------------------------------------------------
"    
    }

} else {
    configure.args-append --disable-c++11
}