# $Id$

# Much of this Portfile is borrowed from clang-3.2, with adaptations where
# needed.

PortSystem              1.0
PortGroup select        1.0
PortGroup compiler_blacklist_versions 1.0

name                    eero-devel
version                 3.4.2014-06-01
revision		1
set llvm_tag            3.4.2014-06-01
set clang_tag           3.4.2014-06-01
set sub_prefix          ${prefix}/libexec/eero-devel

categories              lang
platforms               darwin
maintainers             vince

license                 NCSA
description             Eero is Objective-C evolved
long_description        The Eero language is Obj-C but replaces much \
                        of the underlying C syntax by a more modern \
                        Python like syntax. Binaries produced are \
                        100% compatible with standard Obj-C. The Eero \
                        compiler is based on Clang and thus can \
                        also compile regular C, C++ and Obj-C.
                        
homepage                http://eerolanguage.org/
master_sites            https://github.com/eerolanguage/llvm/tarball/${llvm_tag}:llvm \
                        https://github.com/eerolanguage/clang/tarball/${clang_tag}:clang

distfiles               llvm-${llvm_tag}${extract.suffix}:llvm \
                        clang-${clang_tag}${extract.suffix}:clang

checksums               llvm-${llvm_tag}${extract.suffix} \
                        rmd160  7c29e09de9770e8f08119035fc34c0d164f5726f \
                        sha256  49aad0faa72741a1fea3f24cf7954fde516e997be5b1a5527193ed8e8f24fdcc \
                        clang-${clang_tag}${extract.suffix} \
                        rmd160  a6b83d61f7c795d088e1d50c0fc139534bda424c \
                        sha256  f96cc18c01c21859a3523cbcccc047b21f3f162d0677f32ac7503ade45245384


dist_subdir             eero

depends_lib             port:libffi

default_variants        +analyzer

post-extract {
    move [glob ${workpath}/eerolanguage-llvm*] ${worksrcpath}
    move [glob ${workpath}/eerolanguage-clang*] ${worksrcpath}/tools/clang
}

#patchfiles              tiger.patch  \
#                        tiger-shlib.patch \
#                        tiger-shlib-clang.patch \
#                        scan-build-PR-35006.patch
patch.pre_args          -p1

build.env-append        VERBOSE=1 REQUIRE_RTTI=1
destroot.env-append     VERBOSE=1 REQUIRE_RTTI=1
configure.cppflags
configure.ldflags
configure.pre_args-delete --prefix=${prefix}
configure.args          --enable-bindings=none --enable-libffi \
                        --enable-shared --enable-jit \
                        --enable-optimized --disable-profiling \
                        --enable-debug-symbols --disable-debug-runtime \
                        --prefix="${sub_prefix}" \
						--enable-targets=x86,x86_64,arm,powerpc

variant mp_ld64 description "uses MacPorts cctools ld64 - currently broken" {
}

# It seems only clang (>= 3.1 ?) can compile eero
compiler.blacklist-append *gcc* {clang < 300}

variant universal {
    build.env-append \
        UNIVERSAL=1 \
        UNIVERSAL_ARCH="[get_canonical_archs]"
    destroot.env-append \
        UNIVERSAL=1 \
        UNIVERSAL_ARCH="[get_canonical_archs]"
}

variant assertions description "Enable assertions for error detection (has performance impacts, especially on JIT)" {
    configure.args-append --enable-assertions
}

platform darwin {
	if {[variant_isset mp_ld64]} {
		depends_run-append      port:ld64
		depends_skip_archcheck-append ld64
	}

    if {${build_arch} eq "i386"} {
        configure.pre_args-append --build=i686-apple-darwin${os.major}
    } elseif {${build_arch} eq "ppc"} {
        configure.pre_args-append --build=powerpc-apple-darwin${os.major}
    } elseif {${build_arch} eq "ppc64"} {
        configure.pre_args-append --build=powerpc64-apple-darwin${os.major}
    } else {
        configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
    }

    if {[string match "*ppc*" [get_canonical_archs]]} {
        # http://trac.macports.org/ticket/33987
        configure.args-append --with-optimize-option=-Os
    }
}

platform darwin 8 {
    # Tiger's DevTools make has issues with the nested ifs in compiler-rt
    depends_build-append    port:gmake
    build.cmd               ${prefix}/bin/gmake

    post-configure {
        reinplace "/^RPATH/s/=.*/=/" ${worksrcpath}/Makefile.config
    }
}

variant arm_runtime description {Build and install the arm runtime for iOS development (requires iOS SDK)} {}

variant analyzer description {Install clang static analyzer} {
    depends_run-append  port:python27 port:perl5
    post-patch {
        reinplace "s|/usr/bin/env perl|${prefix}/bin/perl5|g" \
            ${worksrcpath}/tools/clang/tools/scan-build/ccc-analyzer \
            ${worksrcpath}/tools/clang/tools/scan-build/c++-analyzer \
            ${worksrcpath}/tools/clang/tools/scan-build/scan-build
        reinplace "s|/usr/bin/env python|${prefix}/bin/python2.7|g" \
            ${worksrcpath}/tools/clang/tools/scan-build/set-xcode-analyzer \
            ${worksrcpath}/tools/clang/tools/scan-view/scan-view
    }

    post-destroot {
        file mkdir ${destroot}${sub_prefix}/libexec
        file copy ${worksrcpath}/tools/clang/tools/scan-build ${destroot}${sub_prefix}/libexec/scan-build
        file copy ${worksrcpath}/tools/clang/tools/scan-view ${destroot}${sub_prefix}/libexec/scan-view

        file delete -force ${destroot}${sub_prefix}/libexec/scan-build/.svn
        file delete -force ${destroot}${sub_prefix}/libexec/scan-view/.svn
        file delete -force ${destroot}${sub_prefix}/libexec/scan-view/Resources/.svn

        ln -s ${sub_prefix}/libexec/scan-build/scan-build ${destroot}${sub_prefix}/bin/scan-build
        ln -s ${sub_prefix}/libexec/scan-view/scan-view ${destroot}${sub_prefix}/bin/scan-view
        ln -s ${sub_prefix}/bin ${destroot}${sub_prefix}/libexec/scan-build/bin
    }
}

post-destroot {
    # rename Clang as Eero
    file rename ${destroot}${sub_prefix}/bin/clang \
                ${destroot}${sub_prefix}/bin/eero
    file delete ${destroot}${sub_prefix}/bin/clang++
    file link   ${destroot}${sub_prefix}/bin/eero++ \
                ${destroot}${sub_prefix}/bin/eero
                
	# Install just eero binaries, not llvm ones
    foreach bin [glob ${destroot}${sub_prefix}/bin/*eero*] {
        set bin_filename [string map \
		 "${sub_prefix} ${prefix}" ${bin}]
        set exec_path [string map "${destroot}${sub_prefix} ${sub_prefix}" ${bin}]

        xinstall -m 755 "${filespath}/llvm-bin" "${bin_filename}"
        reinplace "s:EXEC_PATH:${exec_path}:" "${bin_filename}"
    }
    
    # http://trac.macports.org/ticket/33207
	# This is borrowed from clang-3.2, but breaks linking with ld64 v.128
	if {[variant_isset mp_ld64]} {
		ln -s ${prefix}/libexec/ld64/ld ${destroot}${sub_prefix}/bin/ld
	}

    system "install_name_tool -id \
        ${sub_prefix}/lib/libclang.dylib\
        ${destroot}${sub_prefix}/lib/libclang.dylib"
        
    system "install_name_tool -id \
        ${sub_prefix}/lib/libLLVM-3.4.1.dylib \
        ${destroot}${sub_prefix}/lib/libLLVM-3.4.1.dylib"
        
    system "install_name_tool -change \
        @executable_path/../lib/libLLVM-3.4.1.dylib \
        ${sub_prefix}/lib/libLLVM-3.4.1.dylib \
        ${destroot}${sub_prefix}/lib/libclang.dylib"
}