# $Id: Portfile 107476 2013-07-01 01:02:13Z mmoll@macports.org $

PortSystem 1.0
PortGroup  muniversal 1.0
PortGroup  active_variants 1.1

name          arpack
version       3.1.3
categories    math
license       BSD
platforms     darwin
maintainers		mmoll
description		Package for solving large-scale eigenvalue problems
long_description  \
    ARPACK is a collection of Fortran77 subroutines designed to solve large \
    scale eigenvalue problems. Parallel ARPACK (PARPACK) is included if built with an MPI variant.
homepage            http://forge.scilab.org/index.php/p/arpack-ng
master_sites        ${homepage}/downloads/get
distfiles           arpack-ng-${version}.tar.gz
checksums           md5     90ba42eae70b517ebdb72921cb4186df \
                    sha1    c1ac96663916a4e11618e9557636ba1bd1a7b556 \
                    rmd160  30a3b8a707417afeb3f8a794ee3aa0160fa3e745
worksrcdir          ${name}-ng-${version}

configure.args-append home=${worksrcpath} --disable-mpi
if { ${build_arch}=="x86_64" || ${build_arch}=="ppc64" } {
    configure.args-append FFLAGS='-O2 -m64'
} else {
    configure.args-append FFLAGS='-O2 -m32'
}
# LDFLAGS needs to be cleared to avoid it having a "-arch" option, considered illegal by gfortran
# also, if it has ${prefix}/lib it will prevent +accelerate from working if atlas is present.
configure.args-append LDFLAGS=''

pre-configure {
   if {"darwin" == ${os.platform} && ${os.major} < 9} {
	ui_error "${name} ${version} requires Mac OS X 10.5 or greater"
	return -code error "incompatible Mac OS X version"
   }

   if {[variant_isset openmpi]} {
      if { ![active_variants openmpi gcc43] && ![active_variants openmpi gcc44] && \
           ![active_variants openmpi gcc45] && ![active_variants openmpi gcc46] && \
           ![active_variants openmpi gcc47] && ![active_variants openmpi gcc48] && \
           ![active_variants openmpi g95] } {
	ui_error "${name} with +openmpi requires that openmpi was built with a Fortran variant"
	return -code error "openmpi needs Fortran variant"
      }
      # see g95 variant below for explanation
      if { [active_variants openmpi g95] } {
        configure.args-append --disable-shared
      }
   }

   if {[variant_isset mpich]} {
      # mpich has a path dependency on mpif77, which could be satisfied by mpich-devel instead
      # Fortunately, if mpif77 is present, this check is not needed.
      if { [_portnameactive mpich] } {
         if { ![active_variants mpich gcc43] && ![active_variants mpich gcc44] && \
           ![active_variants mpich gcc45] && ![active_variants mpich gcc46] && \
           ![active_variants mpich gcc47] } {
	  ui_error "${name} with +mpich requires that mpich was built with a Fortran variant"
	  return -code error "mpich needs Fortran variant"
        }
      }
    }
}

# Fortran compilers can not cross-compile
if { ${os.arch}=="i386" } {
    set universal_archs_supported "i386 x86_64"
} else {
    set universal_archs_supported "ppc ppc64"
}

variant openmpi description {build PARPACK, with OpenMPI} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 g95 mpich universal {
    depends_lib-append     port:openmpi
    configure.args-delete  --disable-mpi
    configure.args-append  --enable-mpi
    configure.env-append   F77=${prefix}/bin/openmpif77 MPIF77=${prefix}/bin/openmpif77
}

variant mpich description {build PARPACK, with MPICH} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 g95 openmpi {
    depends_lib-append     path:bin/mpif77:mpich
    # unfortunately, by default mpich builds without Fortran support, which won't work here. ticket #39428
    configure.args-delete  --disable-mpi
    configure.args-append  --enable-mpi
    configure.env-append   F77=${prefix}/bin/mpif77 MPIF77=${prefix}/bin/mpif77
}

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


variant gcc43 description {build with gfortran-mp-4.3} conflicts gcc44 gcc45 gcc46 gcc47 g95 universal openmpi mpich {
	depends_build-append    port:gcc43
	configure.compiler      macports-gcc-4.3
}

variant gcc44 description {build with gfortran-mp-4.4} conflicts gcc43 gcc45 gcc46 gcc47 g95 openmpi mpich {
	depends_build-append    port:gcc44
	configure.compiler      macports-gcc-4.4
}

variant gcc45 description {build with gfortran-mp-4.5} conflicts gcc43 gcc44 gcc46 gcc47 g95 openmpi mpich {
	depends_build-append    port:gcc45
	configure.compiler      macports-gcc-4.5
}

variant gcc46 description {build with gfortran-mp-4.6} conflicts gcc43 gcc44 gcc45 gcc47 g95 openmpi mpich {
	depends_build-append    port:gcc46
	configure.compiler      macports-gcc-4.6
}

variant gcc47 description {build with gfortran-mp-4.7} conflicts gcc43 gcc44 gcc45 gcc46 g95 openmpi mpich {
	depends_build-append    port:gcc47
	configure.compiler      macports-gcc-4.7
}

# does not work without --shared-lib, will not accept -force_load in linking stage
variant g95 description {build with g95 (static only)} conflicts gcc43 gcc44 gcc45 gcc46 gcc47 universal openmpi mpich {
        depends_build-append    port:g95
        configure.args-append   --disable-shared
        configure.env-append    F77=${prefix}/bin/g95
}

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

variant accelerate conflicts atlas description {build with Accelerate framework} {
    # the change of LDFLAGS is required to avoid linking against the ATLAS ones if they are present;
    # if we specify /usr/lib/libblas.dylib directly, the linking fails. http://forge.scilab.org/index.php/p/arpack-ng/issues/1220/
    configure.args-delete  LDFLAGS=''
    configure.args-append  LDFLAGS='/usr/lib/libblas.dylib /usr/lib/liblapack.dylib' --with-blas="-lblas -llapack"
    patchfiles-append      patch-ARPACK-cdot-and-zdot.diff
    if {[variant_isset openmpi] || [variant_isset mpich]} {
        patchfiles-append patch-PARPACK-cdot-and-zdot.diff
	# PARPACK unwisely uses 'second' from LAPACK (not provided by Accelerate) instead of internal 'arscnd' like ARPACK
        # so, we make a 'second' which wraps 'arscnd'. Really, 'second' should just be replaced in PARPACK source.
        # http://forge.scilab.org/index.php/p/arpack-ng/issues/1245/
	patchfiles-append patch-UTIL-second_NONE.f.diff
    }
}

variant atlas description {build with atlas} {
    depends_lib-append port:atlas
    configure.args-append --with-blas="-L${prefix}/lib -lsatlas"
}

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

post-destroot {
    delete ${destroot}${prefix}/bin/dnsimp
    reinplace "s|/usr|${prefix}|g" ${destroot}${prefix}/lib/pkgconfig/arpack.pc

    if {[variant_isset openmpi] || [variant_isset mpich]} {
       delete ${destroot}${prefix}/bin/pcndrv1
       delete ${destroot}${prefix}/bin/pdndrv1
       delete ${destroot}${prefix}/bin/pdndrv3
       delete ${destroot}${prefix}/bin/pdsdrv1
       delete ${destroot}${prefix}/bin/psndrv3
       delete ${destroot}${prefix}/bin/pssdrv1
       delete ${destroot}${prefix}/bin/pzndrv1
    }
}

livecheck.type  regex
livecheck.url   http://forge.scilab.org/index.php/p/arpack-ng/downloads/
livecheck.regex ${name}-ng (\[0-9.\]+\[0-9\])

variant mpich2 requires mpich description {Legacy compatibility variant} {}
