# -*- 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             1.11.0
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

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} eq "darwin" && ${os.major} < 11} {
        ui_error "${name} is only supported on OS X 10.7 Lion or later."
        return -code error "unsupported platform version"
    }
}

# To take advantage of distfile mirroring and checksumming for the
# snapshot compiler, we let base treat it as the distfile and deal with
# the actual Rust source "manually".

patchfiles		patch-missing-arena.diff

# stage0 is found from
# https://github.com/rust-lang/rust/blob/${version}/src/stage0.txt
set stage0(date)        2016-07-05
set stage0(version)     1.10.0
set stage0(platform)    ${configure.build_arch}-apple-darwin

# Sigh...
if { ${configure.build_arch} eq "i386"} {
    set stage0(platform)    i686-apple-darwin
}

set stage0(distname)     [join "rustc ${stage0(version)}
                                      ${stage0(platform)}" -]
set stage0(distfile)     ${stage0(distname)}${extract.suffix}

master_sites            https://static.rust-lang.org/dist/:main \
                        https://static.rust-lang.org/dist/${stage0(date)}/:snap

distfiles               ${name}c-${version}-src${extract.suffix}:main \
                        ${stage0(distfile)}:snap

worksrcdir              ${name}c-${version}

checksums               ${name}c-${version}-src${extract.suffix} \
                            rmd160  ada0761c38febb423677c1b8bb2fd5c8fee05d53 \
                            sha256  3685034a78e70637bdfa3117619f759f2481002fd9abbc78cc0f737c9974de6a

switch ${configure.build_arch} {
    i386    {checksums-append   ${stage0(distfile)} \
                            rmd160  30a5463f95f2421a29e8c28ab0605358f62adadf \
                            sha256  3ca52d9adfedc68c3509e3cdce232474fe6bbb6b4c284850bdef84fdabb955c3}
    default {checksums-append   ${stage0(distfile)} \
                            rmd160  c1f4c96f5aab786d64080ee4ec5aa70ba870d2d1 \
                            sha256  2971c0e95b854507bdcf3c32f004b20024e6880257842d4b5e06dd542efce814}
}

# Only use compilers supported by upstream
# (https://github.com/rust-lang/rust#building-from-source).
compiler.blacklist  {clang < 211} \
                    *dragonegg* \
                    gcc-3.3 {*gcc-4.[0-6]}

# 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.6:python27 \
                        port:cmake
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.
set tgts {i686-apple-darwin x86_64-apple-darwin}

# TODO: Trying to build a cross-compiler breaks Intel 64 builds as
# of 0.11.0. Worth fixing?
#
#configure.pre_args-append       --target=[join $tgts ,]

if {${configure.build_arch} eq "i386"} {
    configure.pre_args-append   --build=[lindex $tgts 0]
} else {
    configure.pre_args-append   --build=[lindex $tgts 1]
}
# We need to use "--enable-rpath" as of a0546de, otherwise the build
# produces improperly linked binaries.
# (https://github.com/rust-lang/rust/issues/11747)
# TODO: Build docs also, probably in a subport.
configure.args      --disable-docs \
                    --enable-local-rust \
                    --enable-rpath \
                    --local-rust-root=${workpath}/${stage0(distname)}/rustc

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

# 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 {
    if {${subport} eq ${name}} {
        xinstall -d ${destroot}${prefix}/share/${name}
        xinstall -m 644 ${worksrcpath}/src/etc/ctags.rust \
            ${destroot}${prefix}/share/${name}
    }
}

livecheck.type      regex
livecheck.url       https://github.com/rust-lang/rust/tags
livecheck.regex     archive/(\[\\d\\.\]+).zip

subport rust-src {
    # remove dependencies
    depends_build
    depends_lib

    description     Source code for the rust programming language
    long_description ${description}

    use_configure no

    build {}

    destroot {
        xinstall -d ${destroot}${prefix}/share/rust
        move ${worksrcpath}/src ${destroot}${prefix}/share/rust/src

        # correct the permissions
        system -W ${destroot}${prefix}/share/rust "find . -type d -exec chmod 755 {} \\;"
        system -W ${destroot}${prefix}/share/rust "find . -type f -exec chmod 644 {} \\;"

        # delete the test directories (which for some god awful reason contains binaries)
        system -W ${destroot}${prefix}/share/rust "find . -type d -name test -print0 | xargs -0 rm -rf"
    }
}