PortSystem      1.0

name            elftoolchain
version         0.7.1
categories      devel
platforms       darwin
license         BSD
maintainers     {raimue @raimue}
description     Compilation tools and libraries for ELF
long_description \
    BSD-licensed compilation tools and libaries for handling the ELF file \
    format. This includes implementations of libelf and libdwarf along with \
    the tools nm, ar, as, elfdump and more.

homepage        http://elftoolchain.sourceforge.net/
master_sites    sourceforge
use_bzip2 yes

checksums       rmd160  9e341f86573d80dd2b390d79cf82f239ecd279ff \
                sha256  44f14591fcf21294387215dd7562f3fb4bec2f42f476cf32420a6bbabb2bd2b5

depends_build   port:bmake \
                port:bison \
                port:flex

depends_lib     port:libarchive

patchfiles      patch-mk.diff \
                patch-byteorder-macros.diff

post-patch {
    reinplace "s:@PREFIX@:${prefix}:g" ${worksrcpath}/mk/elftoolchain.prog.mk
}

use_configure no

build.type      bsd
build.cmd       ${prefix}/bin/bmake
build.args      CC=${configure.cc} \
                prefix=${prefix} \
                BINDIR=${prefix}/bin \
                LIBDIR=${prefix}/lib/elftoolchain \
                SHLIBDIR=${prefix}/lib/elftoolchain \
                INCSDIR=${prefix}/include/elftoolchain \
                MANDIR=${prefix}/share/man \
                MANTARGET=man

# Exclude test to avoid pulling in large build targets
build.args-append WITH_TESTS=no

# Exclude docs to avoid pulling in a full TeX distribution
build.args-append MKTEX=no

destroot.target install
destroot.args   prefix=${prefix} \
                BINDIR=${prefix}/bin \
                LIBDIR=${prefix}/lib/elftoolchain \
                SHLIBDIR=${prefix}/lib/elftoolchain \
                INCSDIR=${prefix}/include/elftoolchain \
                MANDIR=${prefix}/share/man \
                MANTARGET=man

# Do not strip binaries as that leads to errors
destroot.args-append STRIP=

pre-destroot {
    file mkdir ${destroot}${prefix}/lib/elftoolchain
    file mkdir ${destroot}${prefix}/include/elftoolchain
    file mkdir ${destroot}${prefix}/libexec/elftoolchain
}

# Add elftc- prefix to all binaries to avoid conflicts
post-destroot {
    foreach f [glob -tails -directory ${destroot}${prefix}/bin *] {
        set nf elftc-$f
        move ${destroot}${prefix}/bin/$f ${destroot}${prefix}/bin/$nf
        ln -s ${prefix}/bin/$nf ${destroot}${prefix}/libexec/${name}/$f
    }
    foreach f [glob ${destroot}${prefix}/share/man/man{1,5}/*] {
        move $f [file dirname $f]/elftc-[file tail $f]
    }
}

notes "
    The tools provided by elftoolchain are prefixed with the string 'elftc-' by\
    default to distinguish them from the commands provided by your default\
    toolchain. For example, nm becomes elftc-nm and size becomes elftc-size.

    If you want to use the tools provided by elftoolchain by default, add this\
    directory to the front of your PATH environment variable:
        ${prefix}/libexec/elftoolchain
"

livecheck.type  regex
livecheck.url   http://sourceforge.net/projects/elftoolchain/files/Sources/
livecheck.regex ${name}-(\\d+(\\.\\d+)+)