# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=portfile:et:sw=4:ts=4:sts=4
# $Id$

PortSystem          1.0
PortGroup           active_variants 1.1
PortGroup           compiler_blacklist_versions 1.0

name                rust
version             0.7
categories          lang devel
platforms           darwin
supported_archs     i386 x86_64
# https://github.com/mozilla/rust/issues/2024
universal_variant   no
license             {MIT Apache-2} BSD zlib NCSA Permissive
maintainers         g5pw larryv

description         A safe, concurrent, practical language
long_description    Rust is a curly-brace, block-structured expression \
                    language. It visually resembles the C language \
                    family, but differs significantly in syntactic and \
                    semantic details. Its design is oriented toward \
                    concerns of “programming in the large”, that is, of \
                    creating and maintaining boundaries — both abstract \
                    and operational — that preserve large-system \
                    integrity, availability and concurrency.
homepage            http://www.rust-lang.org/

pre-fetch {
    if {${os.platform} == "darwin" && ${os.major} < 10} {
        ui_error "${name} is only supported on OS X 10.6 Snow Leopard or later."
        return -code error "unsupported platform version"
    }
}

master_sites        http://static.rust-lang.org/dist:dist
distfiles           [suffix ${distname}]:dist
checksums           [suffix ${distname}] \
                        rmd160  986ec4f776c9b02d66cf191aa52aceff0743e5c1 \
                        sha256  0b88b8a4489382e0a69214eaab88e2e7c316ec33c164af0d3b53630b17590df0 \

# Select the snapshot compiler; see src/snapshots.txt in distribution.
set stage0(distdir)             rust-stage0
set stage0(date)                2013-06-23
set stage0(rev)                 f827561
platform darwin i386 {
    set stage0(platform)        macos-${configure.build_arch}
    switch ${configure.build_arch} {
        x86_64 {
            set stage0(hash)    b34fdf3845f8ef4760817007d8ef820cd32f2e07
            set stage0(rmd160)  9ee3de67efe0ae4404f64cf71072a015269d7936
            set stage0(sha256)  ec7b5a6cd25cb85b45cc354cab9fa266c193b6be2581e3624494171a86114b6b
        }
        i386 -
        default {
            set stage0(hash)    63ffbcf99b6853d7840bdfe01380068518d0e466
            set stage0(rmd160)  33799fbefb2d958c7c729ae01c32f5f8aa3f0eb2
            set stage0(sha256)  62617c3c2cb0b942afebda8c02315a5c23cdb344a4f047ea9ab73b648525376d
        }
    }
    set stage0(distfile)        [join "rust stage0 ${stage0(date)}
                                                   ${stage0(rev)}
                                                   ${stage0(platform)}
                                                   ${stage0(hash)}" -].tar.bz2

    master_sites-append         http://static.rust-lang.org/stage0-snapshots:stage0
    distfiles-append            ${stage0(distfile)}:stage0
    checksums-append            ${stage0(distfile)} \
                                    rmd160  ${stage0(rmd160)} \
                                    sha256  ${stage0(sha256)}

    depends_extract             bin:bzip2:bzip2
    extract.only-delete         ${stage0(distfile)}
    post-extract {
        set expand "bzip2 -dc ${distpath}/${stage0(distfile)}"
        set untar "${portutil::autoconf::tar_command} -xf -"
        system -W ${workpath} "${expand} | ${untar}"
    }
}


# Upstream only supports gcc 4.4 and newer and clang based on LLVM
# 3.0svn and newer. Allegedly.
#
# Unsupported compilers are blacklisted unless they have been verified
# to build Rust correctly on OS X 10.8.3. Supported compilers that fail
# to build correctly are blacklisted as well.
compiler.blacklist  apple-gcc-4.0 \
                    {clang < 211} \
                    gcc-3.3 gcc-4.0 gcc-4.2 \
                    macports-clang-2.9 macports-clang-3.3 \
                    macports-dragonegg-3.0 macports-dragonegg-3.1 \
                    macports-dragonegg-3.2 macports-dragonegg-3.3 \
                    macports-gcc-4.2 macports-gcc-4.3 macports-gcc-4.4

patchfiles          patch-configure.diff

# Here to fix a bug in local_stage0.sh
# Pull request submitted upstream
# TODO: remove patch when http://github.com/mozilla/rust/pull/7586
# is resolved.
patchfiles-append   src-etc-local_stage0.sh.diff

post-patch {
    # TODO: Use patches for these when the build system stops changing
    # so much between releases.

    # Pre-release software builds with a ton of warnings.
    reinplace {s/ -Werror//} ${worksrcpath}/mk/platform.mk

    # FSF GCC older than 4.7 does not accept -arch.
    if {[string match {macports-gcc-4.[2-6]} ${configure.compiler}] ||
        [string match {macports-dragonegg-3.[0-2]} ${configure.compiler}]
    } then {
        reinplace -E {s/ -arch (i386|x86_64)//} ${worksrcpath}/mk/platform.mk
    }
}

# Building the bundled LLVM requires Python 2.4-2.7. All supported
# OS X releases have 2.6. (Using MacPorts' LLVM ports fails either
# during build or during testing.)
depends_build           bin:perl:perl5 \
                        bin:python2:python27
depends_skip_archcheck  python27

# The libs for both targets link to libgcc and libstdc++.
if {[regexp {^macports-gcc-(\d+)\.(\d+)$} ${configure.compiler} \
                                            -> major minor]} {
    depends_lib-append      {path:lib/libstdc\\+\\+\\.6\\.dylib:libstdcxx}
    require_active_variants gcc${major}${minor} universal
    require_active_variants {path:lib/libstdc\\+\\+\\.6\\.dylib:libstdcxx} \
                                universal
}

# TODO: Test whether i386 machines can cross-compile for x86_64.
platform darwin i386 {
    set tgts {i686-apple-darwin x86_64-apple-darwin}
    configure.pre_args-append       --target-triples=[join $tgts ,]
    if {${configure.build_arch} eq "i386"} {
        configure.pre_args-append   --build-triple=[lindex $tgts 0]
    } else {
        configure.pre_args-append   --build-triple=[lindex $tgts 1]
    }
}
# TODO: Build docs also, probably in a subport.
configure.args              --disable-docs \
                            --enable-local-rust \
                            --local-rust-root=${workpath}/${stage0(distdir)}
if {[string first clang ${configure.compiler}] != -1} {
    configure.args-append   --enable-clang
}

build.type          gnu
build.args          VERBOSE=1 \
                    CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CPP=${configure.cc}

# TODO: Add path-style dependency on python, whenever test dependencies
# are implemented (#38208). Not critical, since all supported versions
# of OS X come with Python 2.6.
test.run            yes
test.target         check
test.env            VERBOSE=1

destroot.args       VERBOSE=1
post-destroot {
    foreach dir {after/syntax ftdetect indent syntax} {
        xinstall -d ${destroot}${prefix}/share/vim/vimfiles/${dir}
        xinstall -m 644 ${worksrcpath}/src/etc/vim/${dir}/rust.vim \
            ${destroot}${prefix}/share/vim/vimfiles/${dir}
    }
    xinstall -d ${destroot}${prefix}/share/${name}
    xinstall -m 644 ${worksrcpath}/src/etc/ctags.rust \
        ${destroot}${prefix}/share/${name}
}

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     {/release-(\d\.\d)/}