# -*- Mode: Tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:et:sw=4:ts=4:sts=4

PortSystem      1.0
PortGroup       gnu_info 1.0
PortGroup       legacysupport 1.1
PortGroup       muniversal 1.0

name            libgcrypt
version         1.12.2
revision        0

categories      devel security
# libs are LGPL, executables and docs are GPL
license         {GPL-2+ LGPL-2.1+}
maintainers     {mps @Schamschula} openmaintainer
description     Crypto library
long_description \
    Libgcrypt is a general purpose cryptographic library originally \
    based on code from GnuPG. It provides functions for all \
    cryptographic building blocks: symmetric cipher algorithms and \
    modes, hash algorithms, MACs, public key algorithms, large integer \
    functions, random numbers and a lot of supporting functions.

homepage        https://www.gnupg.org/
master_sites    gnupg
use_bzip2       yes

checksums       rmd160  972a14e34ec1bc317432e68fa10ff78d54c6c4cb \
                sha256  7ce33c2492221a0436f96a8500215e9f3e3dcb5fd26a757cd415e7a843babd5e \
                size    4441855

depends_lib     port:libgpg-error

configure.args  --disable-asm

# libgcrypt detects GNU grep in $prefix/bin/ggrep when it is installed and uses
# it in its libgcrypt-config file; If GNU grep is later uninstalled, building
# against libgcrypt will fail. This can either be fixed by not relying on
# $prefix/bin/ggrep, or by declaring a runtime dependency on the grep port.
# This does the former.
configure.args-append \
                GREP="/usr/bin/grep"

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

# 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
if {${build_arch} eq "i386" || ([variant_isset universal] && [lsearch ${configure.universal_archs} i386] != -1)} {
    if {[vercmp ${xcodeversion} 3.0] >= 0} {
        compiler.blacklist-append gcc-4.0
    }
    compiler.blacklist-append gcc-4.2
}

platform darwin 9 {
    # ppc on leopard needs compiler.thread_local_storage
    if {${build_arch} eq "ppc"} {
	compiler.thread_local_storage true
    }
}

platform darwin 10 {
    # Rosetta misdetects the arch: warning: `-no-install' is ignored for x86_64-apple-darwin10.8.0
    if {${build_arch} eq "ppc"} {
        configure.args-append --build=powerpc-apple-darwin${os.major}
    }
}

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"

    compiler.blacklist-append {clang < 900}
}

use_parallel_build  yes

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

# strip host= from pkgconfig file https://trac.macports.org/ticket/63635
# could be added to muniversal PG but Ryan asked that it remain in each Port needing it
# so that upstream could be notified and fix it properly
post-build {
    if {[variant_exists universal] && [variant_isset universal]} {
        set dirs {}
        foreach arch ${universal_archs_to_use} {
            lappend dirs ${worksrcpath}-${arch}
        }
    } else {
        set dirs ${worksrcpath}
    }
    foreach dir ${dirs} {
        # Remove architecture-specific differences to allow merging.
        reinplace -E {s|host=[^ ]+||g}               ${dir}/src/libgcrypt.pc
    }
}

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

gnu_info.files  ${prefix}/share/info/gcrypt.info \
                ${prefix}/share/info/gcrypt.info-1 \
                ${prefix}/share/info/gcrypt.info-2

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