# -*- 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
# $Id: Portfile 118841 2014-04-12 12:46:34Z mcalhoun@macports.org $

PortSystem 1.0
PortGroup  muniversal  1.0

name            gmp
version         6.0.0
categories      devel math
license         LGPL-3+
maintainers     mcalhoun openmaintainer
platforms       darwin
description     GNU multiple precision arithmetic library
long_description \
    GNU MP is a library for arbitrary precision arithmetic, operating on\
    signed integers, rational numbers, and floating point numbers. It   \
    has a rich set of functions, and the functions have a regular       \
    interface. GNU MP is designed to be as fast as possible, both for   \
    small operands and for huge operands. The speed is achieved by using\
    fullwords as the basic arithmetic type, by using fast algorithms, by\
    carefully optimized assembly code for the most common inner loops   \
    for a lots of CPUs, and by a general emphasis on speed (instead of  \
    simplicity or elegance).

homepage        http://gmplib.org/
master_sites    gnu

checksums           rmd160  2322e21e2042c974dcfaca2da18ae22d43a23ec4 \
                    sha256  7f8e9a804b9c6d07164cf754207be838ece1219425d64e28cfa3e70d5c759aaf

use_bzip2       yes
use_parallel_build yes

# There seems to have been errors in the documentation of 6.0.0,
#    so they released a version 6.0.0a shortly thereafter.
# See https://gmplib.org/list-archives/gmp-announce/2014-March/000042.html
distfiles ${name}-${version}a${extract.suffix}

configure.args  --enable-cxx

# Prevent precompiled binaries.
# See #41614.
archive_sites

configure.universal_args-delete --disable-dependency-tracking

# Clear all options that affect CFLAGS and CXXFLAGS, since the configure
# script tries to build the fastest library for the build machine if
# CFLAGS and CXXFLAGS are undefined.
#
# Append the -stdlib flags to CXX, since we still want to select the C++ STL.
#
# On PowerPC machines, CFLAGS must be empty to get -force_cpusubtype_ALL.

if {[info exists configure.cxx_stdlib] &&
    ${configure.cxx_stdlib} ne {} &&
    [string match *clang* ${configure.cxx}]
} then {
    configure.cxx-append -stdlib=${configure.cxx_stdlib}
    configure.cxx_stdlib
}

# See #43262
if {${configure.sdkroot} ne ""} {
    configure.cc-append     -isysroot${configure.sdkroot}
    configure.cxx-append    -isysroot${configure.sdkroot}
    configure.sdkroot
}

configure.cc_archflags
configure.cxx_archflags
configure.ld_archflags
configure.cflags
configure.cxxflags
configure.pipe  no

test.run        yes
test.target     check

if {![variant_isset universal]} {
    if {${build_arch} eq "x86_64"} {
        configure.env   ABI=64
        # Newer processors may not be detected correctly.
        pre-configure {
            set build_triplet [split [exec /usr/bin/env CC=${configure.cc} ${worksrcpath}/config.guess] -]
            set build_cpu [lindex $build_triplet 0]
            if { ${build_cpu} ne "core2" && ![string match "corei*" ${build_cpu}] } {
                ui_warn "No processor dependent assembly code being used. gmp might be slower."
                configure.args-append --build=core2-[join [lrange $build_triplet 1 end] -]
            }
        }
    } elseif {${build_arch} eq "ppc64"} {
        configure.env   ABI=mode64
    } else {
        configure.env   ABI=32
    }
} else {
    # Keep configure.cflags and configure.cxxflags empty.
    set merger_arch_flag no

    array set merger_configure_env {
        ppc     ABI=32
        i386    ABI=32
        ppc64   ABI=mode64
        x86_64  ABI=64
    }

    # Since CFLAGS and CXXFLAGS must be empty, append -arch ... to CC and CXX.
    merger_arch_compiler yes
    
    # -arch i386 on causes the test suite to fail, so override muniversal_get_arch_flag in muniversal portgroup
    # Only append -arch ... to compiler if cross compiling.
    # i386 code is generated by setting ABI=32
    proc muniversal_get_arch_flag {arch {fortran ""}} {
        global os.arch
        
        if {${os.arch} eq "i386" && (${arch} eq "i386" || ${arch} eq "x86_64")} {
            set archf ""
        } elseif {${os.arch} eq "powerpc" && (${arch} eq "ppc" && ${arch} eq "ppc64")} {
            set archf ""
        } else {
            set archf "-arch ${arch}"
        }
        return ${archf}
    }
    
    # universal_archs_to_use might not be set before pre-fetch.
    pre-destroot {
        global merger_dont_diff merger_configure_env

        # PortGroup muniversal has difficulty merging three files.
        if  { [llength ${universal_archs_to_use}] == 3 } {
            set merger_dont_diff "${prefix}/include/gmp.h"
        }
    }

    # For cross-compiling, set C compiler and pre-processor.
    if {${os.arch} eq "i386"} {
        if { ${os.major} >= 10 } {
            lappend merger_configure_env(ppc)  CC_FOR_BUILD=${configure.cc} CPP_FOR_BUILD=${configure.cpp}
        }
        lappend merger_configure_env(ppc64)    CC_FOR_BUILD=${configure.cc} CPP_FOR_BUILD=${configure.cpp}
    } else {
        lappend merger_configure_env(i386)     CC_FOR_BUILD=${configure.cc} CPP_FOR_BUILD=${configure.cpp}
        lappend merger_configure_env(x86_64)   CC_FOR_BUILD=${configure.cc} CPP_FOR_BUILD=${configure.cpp}
    }
}

platform powerpc {
    # See #9053
    patchfiles-append  patch-config.guess.diff
}
