# $Id: Portfile 53135 2009-06-30 07:13:08Z ryandesign@macports.org $

PortSystem          1.0

categories          math
name                atlas
version             3.8.3
revision            1

# additional versions
set lapackversion   3.2.1
set lapackname      lapack
set atlasdist       ${name}${version}.tar.bz2
set lapackdist      ${lapackname}.tgz

maintainers         cornell.edu:ajb78
platforms           darwin

description         Portable optimal linear algebra software
long_description    The current version provides a complete BLAS and LAPACK API.\
                    For many operations, ATLAS achieves performance on par with\
                    machine-specific tuned libraries.

homepage            http://math-atlas.sourceforge.net/

master_sites        sourceforge:math-atlas/${name}${version}.tar.bz2:atlas \
                    http://www.netlib.org/lapack:lapack

distfiles           ${atlasdist}:atlas \
                    ${lapackdist}:lapack

checksums           atlas${version}.tar.bz2 \
                    md5     6c13be94a87178e7582111c08e9503bc \
                    sha1    c7546210df4796457d9e96a00444adc4c0f2e77f \
                    rmd160  0425e630d9d0c5b7aae81427a41b7eaa18447e90 \
                    lapack.tgz \
                    md5     a3202a4f9e2f15ffd05d15dab4ac7857 \
                    sha1    c75223fdef3258c461370af5d2b889d580d7f38a \
                    rmd160  e8fabba2fdd944afbc02a83599cf45a0d721030d


# the following patch corrects extension conventions for osx's dynamic libs
set make_patch      patch-build-Make.top.diff
use_parallel_build  no
build.target build
build.dir           ${workpath}/${name}-${version}/build

destroot.dir        ${build.dir}
destroot.destdir    DESTDIR=${destroot}${prefix}
depends_lib         port:gcc43 \
                    port:bzip2 \
                    port:gzip

# Set fortran compiler flags
set myf77           ${prefix}/bin/gfortran-mp-4.3

configure.args      -C xc ${prefix}/bin/gcc-mp-4.3 \
                    -C gc ${prefix}/bin/gcc-mp-4.3 \
                    -C if ${myf77} \
                    -C ic ${prefix}/bin/gcc-mp-4.3 \
                    -C dm ${prefix}/bin/gcc-mp-4.3 \
                    -C sm ${prefix}/bin/gcc-mp-4.3 \
                    -C dk ${prefix}/bin/gcc-mp-4.3 \
                    -C sk ${prefix}/bin/gcc-mp-4.3 \
                    --cc=${prefix}/bin/gcc-mp-4.3 \
                    -b 32 \
                    -Fa alg -fPIC 
                    
configure.compiler macports-gcc-4.3

        

# we are configuring from the build directory
configure.dir       ${workpath}/${name}-${version}/build
configure.cmd       ../configure

# change the default compilers to those of gcc43 and make into shared library

extract {
    # extract atlas and move to a consistent working directory name
    system "cd ${workpath} && bunzip2 -dc ${distpath}/${atlasdist} | \
         gnutar --no-same-owner -xf -"
    system "mv ${workpath}/ATLAS ${workpath}/${name}-${version}"

    # extract lapack
    system "cd ${workpath} && gunzip -dc ${distpath}/${lapackdist} | \
         gnutar --no-same-owner -xf -"
}



pre-configure {
    # Get some system specific variables for maximum optimization
    set cpufreq [expr {[exec sysctl -n hw.cpufrequency]/1000000}]
    ui_debug "Setting CPU Frequency for: ${cpufreq}"
    configure.args-append -D c -DPentiumCPS=${cpufreq}

    # first do a 'fake configure' of atlas so we get optimized lapack libraries
    ui_debug "performing fake configure of atlas for optimized lapack"
    # atlas docs: 3.1.2
    set atlas_path ${workpath}/${name}-${version}
    ui_debug "Clean out old temp directory if exists"
    file delete -force ${atlas_path}/temp
    file mkdir ${atlas_path}/temp
    ui_debug "Assembled Command: '${configure.cmd} ${configure.args}"
    system "cd ${atlas_path}/temp && \
            ${configure.cmd} ${configure.args}"
    
    # Get the F77FLAGS 
    set atlas_f77    [join [lrange [split [exec grep "F77 = " $atlas_path/temp/Make.inc     ] =] 1 end] =]
    ui_debug "Found atlas F77: ${atlas_f77}"
    
    set atlas_fflags [join [lrange [split [exec grep "F77FLAGS = " $atlas_path/temp/Make.inc] =] 1 end] =]
    ui_debug "Found atlas F77FLAGS: ${atlas_fflags}"
    
    ui_debug "Remove the old test configure directory"
    file delete -force ${atlas_path}/temp
    
    # compile lapack
    system "cd ${workpath}/${lapackname}-${lapackversion} && \
            cp INSTALL/make.inc.gfortran make.inc"
    reinplace "s|gfortran|${atlas_f77}|" \
           ${workpath}/${lapackname}-${lapackversion}/make.inc
    reinplace "s|OPTS     = -O2|OPTS = ${atlas_fflags}|" \
           ${workpath}/${lapackname}-${lapackversion}/make.inc
    
    reinplace "s|NOOPT    = -O0|NOOPT = -O2 -m32|" \
           ${workpath}/${lapackname}-${lapackversion}/make.inc

    reinplace "s|PLAT = _LINUX|PLAT = _darwin|" \
           ${workpath}/${lapackname}-${lapackversion}/make.inc
    
    reinplace "s|LOADOPTS =|LOADOPTS = ${atlas_fflags}|" \
           ${workpath}/${lapackname}-${lapackversion}/make.inc
    
    system "cd ${workpath}/${lapackname}-${lapackversion} && make lib"

    # create a build directory for atlas
    file mkdir ${workpath}/${name}-${version}/build


    configure.args-append --with-netlib-lapack=${workpath}/${lapackname}-${lapackversion}/lapack_darwin.a 
}

post-configure {
    # recursively remove directories
    ui_debug "reinplace for make to recursively remove directories on clean"
    reinplace "s|rm -f|rm -rf|g" ${workpath}/${name}-${version}/build/Makefile

    ui_debug "patch ${name}-${version}/build/Make.top to install dynamic libs"
    system "cd ${worksrcpath}/build/ && \
            patch -p0 < ${filespath}/${make_patch}"
}

pre-destroot {
  # We have to manually create the dylib shared libs as the default build does not do this
  if {[string equal "${os.arch}" "powerpc"]} {
    set myarch "ppc"
    } {
      set myarch "i386"
    }
  
  set ld "ld -arch ${myarch} -dynamic -dylib -single_module -dead_strip -x \
          -all_load -L. -L${prefix}/lib/gcc43 -ldylib1.o -dylib_install_name"
  
  system "cd ${workpath}/${name}-${version}/build/lib && \
          ${ld} ${prefix}/lib/libatlas.dylib libatlas.a -o libatlas.dylib \
           -lSystem"
  
  system "cd ${workpath}/${name}-${version}/build/lib && \
          ${ld} ${prefix}/lib/libptcblas.dylib libptcblas.a -o \
           libptcblas.dylib -latlas \
           -lSystem"
  
  system "cd ${workpath}/${name}-${version}/build/lib && \
          ${ld} ${prefix}/lib/libptf77blas.dylib \
          libptf77blas.a -o libptf77blas.dylib \
           -lgfortran -latlas -lSystem"

  system "cd ${workpath}/${name}-${version}/build/lib && \
          ${ld} ${prefix}/lib/liblapack.dylib liblapack.a -o liblapack.dylib \
           -lptf77blas -lgfortran -lptcblas -latlas -lgcc_s.1 -lSystem"

}

post-destroot {
  # we make soft links to the parallel built libs
  system "cd ${destroot}${prefix}/lib && \
          ln -sf ./libptcblas.dylib libcblas.dylib && \
          ln -sf ./libptf77blas.dylib libf77blas.dylib"
}
