# -*- 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 108375 2013-07-21 21:46:31Z jeremyhu@macports.org $

PortSystem              1.0
PortGroup select        1.0

set llvm_version        3.1
set llvm_version_no_dot 31
name                    dragonegg-${llvm_version}

subport                 ${name}-gcc-4.5 {
    revision            1
    set gcc_version     4.5
    set gcc_version_no_dot 45
}
subport                 ${name}-gcc-4.6 {
    revision            1
    set gcc_version     4.6
    set gcc_version_no_dot 46
}
subport                 ${name}-gcc-4.7 {
    revision            1
    set gcc_version     4.7
    set gcc_version_no_dot 47
}

dist_subdir             llvm
categories              lang
platforms               darwin
maintainers             jeremyhu
license                 GPL-2

description             DragonEgg is a LLVM plug-in for GCC
long_description        DragonEgg replaces GCC optimizers and code generators \
                        by the LLVM optimizing infrastructure. It supersedes \
                        llvm-gcc.

homepage                http://dragonegg.llvm.org/

if {${subport} == ${name}} {
version                 ${llvm_version}
epoch                   1
revision                5

distfiles

set gcc_version         4.6

depends_run             port:${name}-gcc-${gcc_version}

use_configure           no
build                   {}
destroot {
    # For base
    foreach binary {gcc cpp c++ g++ gcj gcov gfortran} {
        ln -s dragonegg-${llvm_version}-${binary}-mp-${gcc_version} ${destroot}${prefix}/bin/dragonegg-${llvm_version}-${binary}
    }
}

} else {

depends_lib             port:llvm-${llvm_version} port:gcc${gcc_version_no_dot}
depends_run             port:gcc_select

# gcc needs to match our arch, not the other way around
# We build +universal just fine in order to support whatever arch gcc is
depends_skip_archcheck-append gcc${gcc_version_no_dot}

#fetch.type              svn
#svn.revision            155781
#version                 ${llvm_version}-r${svn.revision}
#worksrcdir              trunk
#svn.url                 http://llvm.org/svn/llvm-project/dragonegg/trunk
#worksrcdir              release_31
#svn.url                 http://llvm.org/svn/llvm-project/dragonegg/branches/release_31

version                 ${llvm_version}
epoch                   1
master_sites            http://llvm.org/releases/${version}/
extract.suffix          .tar.gz
distname                dragonegg-${version}.src
checksums           sha1    98e865f16c76bddb3f6969c733042677e306d63c \
                    rmd160  303e98f4d33f87fe018505a03ae1265d3c5a080c \
                    sha256  a11d1379fab772bc4a747e6f5c6a27acf5fe03f27b5a152af67f63d5fbc4a876

platform darwin {
    if {${os.major} >= 13} {
        depends_lib
        depends_run
        pre-fetch {
            ui_error "$name is not supported on Mavericks or later."
            error "unsupported platform"
        }
    }
}

pre-fetch {
    ui_msg "Please remember to keep dragonegg and llvm in phase by updating llvm-${llvm_version} first."
}

use_configure no

select.group    gcc
select.file     ${worksrcpath}/dragonegg-${llvm_version}-gcc${gcc_version_no_dot}

build.target

# g++-4.0 fails to build some of the newer C++ for ppc
# Intel looks ok, but I prefer using gcc-4.2 for consistency
compiler.blacklist gcc-4.0

# TODO: base should do this: http://trac.macports.org/ticket/32542
if {[info exists portconfigure::compiler_name_map(${configure.compiler})]} {
    depends_build-append port:$portconfigure::compiler_name_map(${configure.compiler})

    # base 2.1.x ignores the argument and just use ${configure.compiler}
    if {[portconfigure::arch_flag_supported ${configure.compiler}]} {
        depends_skip_archcheck-append $portconfigure::compiler_name_map(${configure.compiler})
    }

    if {[string match macports-gcc* ${configure.compiler}]} {
        depends_lib-append port:$portconfigure::compiler_name_map(${configure.compiler})
    }
}

patchfiles missing-includes.patch cstring.patch

post-extract {
    file copy ${filespath}/dragonegg-select ${select.file}
    reinplace "s:LLVM_VERSION:${llvm_version}:" ${select.file}
    reinplace "s:GCC_VERSION:${gcc_version}:" ${select.file}
}

variant universal {
    post-patch {
        reinplace "/-MD -MP/d" ${worksrcpath}/Makefile
    }
}

build.env-append \
    LLVM_CONFIG=${prefix}/bin/llvm-config-mp-${llvm_version} \
    GCC=${prefix}/bin/gcc-mp-${gcc_version} \
    CC="${configure.cc} ${configure.cppflags} [get_canonical_archflags cc]" \
    CXX="${configure.cxx} ${configure.cppflags} [get_canonical_archflags cxx]"

destroot {
    xinstall -m 755 -d ${destroot}${prefix}/libexec/dragonegg
    xinstall -m 755 ${worksrcpath}/dragonegg.so ${destroot}${prefix}/libexec/dragonegg/llvm${llvm_version_no_dot}gcc${gcc_version_no_dot}.so

    foreach binary {gcc cpp c++ g++ gcj gcov gfortran} {
        set path ${destroot}${prefix}/bin/dragonegg-${llvm_version}-${binary}-mp-${gcc_version}
        xinstall -m 755 ${filespath}/dragonegg-gcc ${path}
        reinplace "s:PLUGIN:${prefix}/libexec/dragonegg/llvm${llvm_version_no_dot}gcc${gcc_version_no_dot}.so:" ${path}
        reinplace "s:PREFIX:${prefix}:" ${path}
        reinplace "s:GCC_VERSION:${gcc_version}:" ${path}
        reinplace "s:BINARY:${binary}:" ${path}
    }
}

}

livecheck.type          none
