# -*- 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           mpi 1.0
PortGroup           xcode_workaround 1.0
PortGroup           makefile 1.0

github.setup        stamatak standard-RAxML 8.2.12 v
github.tarball_from archive
revision            0
checksums           sha256  338f81b52b54e16090e193daf36c1d4baa9b902705cfdc7f4497e3e09718533b \
                    rmd160  eaaa1dc2bc9a55ad070d14f2a4ceab55afa05aac \
                    size    10138831
name                raxml
description         Estimation of phylogenetic trees
long_description    RAxML is a program for sequential and parallel \
                    estimation of phylogenetic trees written by \
                    Alexandros Stamatakis.
license             GPL-2+
maintainers         gmail.com:mattoates
categories          science biology
homepage            http://www.exelixis-lab.org/
platforms           darwin

compilers.choose    cc
mpi.setup

# respect MacPorts settings
patchfiles-append   patch-Makefiles.diff

#Strings used by the variant options
set HPCm ""
set HPCb ""
set EXTm ""
set EXTb ""

#Use the HYBRID implementation
variant hybrid conflicts pthreads description {Hybrid MPI implementation} {
    if {![mpi_variant_isset]} {
        return -code error "$name: Variant hybrid requires an mpi variant"
    }
    set HPCm ".HYBRID"
    set HPCb "-HYBRID"
}

#Use the MPI implementation if hybrid is not selected
if {[mpi_variant_isset] && ![variant_isset hybrid]} {
    set HPCm ".MPI"
    set HPCb "-MPI"
}

#Use the pthreads implementation (default)
variant pthreads conflicts hybrid description {Pthreads implementation} {
    if {[mpi_variant_isset]} {
        return -code error "$name: Variant pthreads conflicts with mpi"
    }
    set HPCm ".PTHREADS"
    set HPCb "-PTHREADS"
}

if {![mpi_variant_isset]} {
    if {[variant_isset hybrid]} {
        default_variants    +openmpi
    } else {
        default_variants    +pthreads
    }
}

#For Intel machines add SSE and AVX as a variant and use it by default with pthreads
if {${build_arch} eq "i386" || ${build_arch} eq "x86_64"} {
    variant sse3 conflicts avx avx2 description {Use the SSE3 extended instruction set from Intel since 2004} {
        set EXTm ".SSE3"
        set EXTb "-SSE3"
    }

    variant avx conflicts avx2 sse3 description {Use the AVX extended instruction set from Intel i7 (sandy bridge) and AMD Bulldozer} {
        pre-fetch {
            if {![avx_compiler_isset]} {
                return -code error "$name: Variant avx needs a clang-derived compiler"
            }
        }
        set EXTm ".AVX"
        set EXTb "-AVX"
    }

    variant avx2 conflicts avx sse3 description {Use the AVX2 extended instruction set from Intel Haswell and AMD Excavator} {
        pre-fetch {
            if {![avx_compiler_isset]} {
                return -code error "$name: Variant avx2 needs a clang-derived compiler"
            }
        }
        set EXTm ".AVX2"
        set EXTb "-AVX2"
    }

    if {![variant_isset avx] && ![variant_isset sse3] && ![catch {sysctl hw.optional.avx2_0} result] && ${result} == 1} {
        default_variants-append +avx2
    } elseif {![variant_isset avx2] && ![variant_isset sse3] && ![catch {sysctl hw.optional.avx1_0} result] && ${result} == 1} {
        default_variants-append +avx
    } elseif {![variant_isset avx] && ![variant_isset avx2] && ![catch {sysctl hw.optional.sse3} result] && ${result} == 1} {
        default_variants-append  +sse3
    }
} else {
    post-patch {
        reinplace "s/-msse//" ${worksrcpath}/Makefile.gcc \
                              ${worksrcpath}/Makefile.PTHREADS.gcc \
                              ${worksrcpath}/Makefile.MPI.gcc
    }
}

#Make sure we are using the correct Makefile for this variant combo
pre-build {
    build.pre_args      -f Makefile${EXTm}${HPCm}.gcc
}

#We only need the single raxml binary
destroot {
    copy ${worksrcpath}/raxmlHPC${HPCb}${EXTb} ${destroot}${prefix}/bin/raxml
}
