# -*- Mode: Tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:et:sw=4:ts=4:sts=4
# $Id: Portfile 91789 2012-04-10 16:12:25Z jeremyhu@macports.org $

PortSystem      1.0
PortGroup       muniversal 1.0

name            libgcrypt
version         1.5.0
categories      devel security
# libs are LGPL, executables and docs are GPL
license         {GPL-2+ LGPL-2.1+}
maintainers     sfiera openmaintainer
description     Crypto library
homepage        http://www.gnupg.org/
platforms       darwin

long_description \
    This is a general purpose cryptographic library based \
    on the code from GnuPG. It provides functions for all \
    cryptograhic building blocks: symmetric ciphers (AES, \
    DES, Blowfish, CAST5, Twofish, Arcfour), hash algorithms \
    (MD4, MD5, RIPE-MD160, SHA-1, TIGER-192), MACs (HMAC for \
    all hash algorithms), public key algorithms (RSA, ElGamal, \
    DSA), large integer functions, random numbers and a lot of \
    supporting functions.

master_sites    gnupg
use_bzip2       yes

checksums       sha1    3e776d44375dc1a710560b98ae8437d5da6e32cf \
                rmd160  f01e8198dcc379ff2fa5e8d3ac39e7b32fc41dad

depends_lib     port:libgpg-error

patchfiles      clang-asm.patch

configure.args  --disable-asm

# Build fix for compilers that default to c99 (clang)
configure.cflags-append "-std=gnu89"

if {[string match "*gcc-4.2" ${configure.compiler}] ||
    (${configure.compiler} == "gcc-4.0" && [vercmp ${xcodeversion} 3.0] >= 0)} {
    # Some versions of gcc fail to build this for i386 including:
    #     gcc-4.0 from XCode 3.1.6 (5493) -- Note that gcc-4.0 from XCode 2.5 (5370) works 
    #     gcc-4.2 from XCode 3.1.6 (5577)
    #     gcc-4.2 from XCode 3.2.6 (5666.3)
    # rijndael.c: In function 'do_aesni_ctr':
    # rijndael.c:895: error: can't find a register in class 'GENERAL_REGS' while reloading 'asm'
    # rijndael.c:895: error: 'asm' operand has impossible constraints
    configure.compiler llvm-gcc-4.2

    if {(![file exists ${configure.cc}]) && ([variant_isset universal] || ${build_arch} == "i386")} {
        depends_build port:clang-3.0
        depends_skip_archcheck-append clang-3.0

        if {[variant_isset universal]} {
            # In order to build universal on Leopard, you need MacPorts 2.1
            set merger_configure_compiler(i386) macports-clang-3.0
            lappend merger_configure_cflags(i386) -fheinous-gnu-extensions
        } else {
            # configure.compiler macports-clang-3.0 is not available in macports 2.0.4
            # configure.compiler macports-clang-3.0
            configure.cc    ${prefix}/bin/clang-mp-3.0
            configure.objc  ${prefix}/bin/clang-mp-3.0
            configure.cxx   ${prefix}/bin/clang++-mp-3.0
            configure.cflags-append "-fheinous-gnu-extensions"
        }
    }
}

if {[string match "*clang*" ${configure.compiler}]} {
    # libgcrypt-1.5.0 does some ugly stuff with their udiv_qrnnd macro in mpih-div.c
    # error: invalid use of a cast in a inline asm context requiring an l-value: remove the cast
    configure.cflags-append "-fheinous-gnu-extensions"
}

use_parallel_build  yes

post-patch {
    if {[variant_isset universal]} {
        reinplace "s/@LIBGCRYPT_CONFIG_HOST@/${os.arch}-apple-darwin${os.major}/" ${worksrcpath}/src/libgcrypt-config.in
    }
}

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} AUTHORS COPYING COPYING.LIB ChangeLog \
        NEWS README THANKS TODO VERSION ${destroot}${docdir}
}

test.run        yes
test.target     check

livecheck.type  regex
livecheck.url   ftp://ftp.gnupg.org/gcrypt/${name}/
livecheck.regex ${name}-(\\d+\\.\\d+\\.\\d+)
