--- Portfile.orig	2018-03-12 04:52:47.000000000 -0700
+++ Portfile	2018-03-10 10:54:46.000000000 -0700
@@ -1,6 +1,7 @@
 # -*- 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
 
 PortSystem          1.0
+PortGroup           muniversal 1.0
 
 name                rust
 version             1.24.1
@@ -35,49 +36,65 @@
 
 patchfiles          patch-src-librustc-llvm-lib.diff
 
-set rust_platform   ${build_arch}-apple-darwin
-
-if {${build_arch} eq "i386"} {
-    set rust_platform i686-apple-darwin
+set ruststd_version 1.23.0
+set rustc_version   1.23.0
+set cargo_version   0.24.0
+
+if {![variant_isset universal]} {
+    if {${build_arch} eq "i386"} {
+        set architectures i686
+    } else {
+        set architectures ${build_arch}
+    }
+} else {
+    set architectures {}
+    foreach arch ${universal_archs} {
+        if {${arch} eq "i386"} {
+            lappend architectures i686
+        } else {
+            lappend architectures ${arch}
+        }
+    }
 }
 
-set stage0(ruststd) rust-std-1.23.0-${rust_platform}
-set stage0(rustc)   rustc-1.23.0-${rust_platform}
-set stage0(cargo)   cargo-0.24.0-${rust_platform}
-
-distfiles-append    ${stage0(ruststd)}${extract.suffix} \
-                    ${stage0(rustc)}${extract.suffix} \
-                    ${stage0(cargo)}${extract.suffix}
+foreach arch ${architectures} {
+    distfiles-append rust-std-${ruststd_version}-${arch}-apple-${os.platform}${extract.suffix} \
+                     rustc-${rustc_version}-${arch}-apple-${os.platform}${extract.suffix} \
+                     cargo-${cargo_version}-${arch}-apple-${os.platform}${extract.suffix}
+}
 
 checksums           ${distname}${extract.suffix} \
                     rmd160  e4c92648fbd3499dcc54c6efdce53d5cdde010c4 \
-                    sha256  3ea53d45e8d2e9a41afb3340cf54b9745f845b552d802d607707cf04450761ef
+                    sha256  3ea53d45e8d2e9a41afb3340cf54b9745f845b552d802d607707cf04450761ef \
+                    size    67512394
 
-if {${build_arch} eq "i386"} {
-    checksums-append \
-                    ${stage0(ruststd)}${extract.suffix} \
+checksums-append \
+                    rust-std-${ruststd_version}-i686-apple-${os.platform}${extract.suffix} \
                     rmd160  2873bd63f74c1d225a0a3392bb8cfa5534679dc1 \
                     sha256  e454d4f5fd1e66f48d6ea8f102a96a15325cf969079f4485aab8e4486b96e2d8 \
-                    ${stage0(rustc)}${extract.suffix} \
+                    size 66640892 \
+                    rustc-${rustc_version}-i686-apple-${os.platform}${extract.suffix} \
                     rmd160  e4622e6082c4666e06bc173ee669f8198661d04a \
                     sha256  618b0dea1e1563d22cb58c1d54344c096e7a4e2d138a17413a349a3edc753745 \
-                    ${stage0(cargo)}${extract.suffix} \
+                    size 48081807 \
+                    cargo-${cargo_version}-i686-apple-${os.platform}${extract.suffix} \
                     rmd160  17458dce93eb7e644385bd82ecb511311765e5e6 \
-                    sha256  6b7a0a97e0d87cca7b7a5e090d168be6cdbb03298381bece33df493447e17e76
-} else {
-    checksums-append \
-                    ${stage0(ruststd)}${extract.suffix} \
+                    sha256  6b7a0a97e0d87cca7b7a5e090d168be6cdbb03298381bece33df493447e17e76 \
+                    size    3909191
+
+checksums-append \
+                    rust-std-${ruststd_version}-x86_64-apple-${os.platform}${extract.suffix} \
                     rmd160  5b2d5f9e7ed73112fc970d5d74eef392b5c490a8 \
                     sha256  c2859aeb763edc07ec289a929c66f269373de67908d3a9be069868a8c103c833 \
-                    ${stage0(rustc)}${extract.suffix} \
+                    size 67553850 \
+                    rustc-${rustc_version}-x86_64-apple-${os.platform}${extract.suffix} \
                     rmd160  f15d358053c4979a027299c68ba0dd8ca50df444 \
                     sha256  61d8774c6e348addc1e82fe598b5d007f30d3d8992d95f0530048236dedf4e0d \
-                    ${stage0(cargo)}${extract.suffix} \
+                    size 49076336 \
+                    cargo-${cargo_version}-x86_64-apple-${os.platform}${extract.suffix} \
                     rmd160  ac5eff8ae2489845f9761c275b03677a50309c1a \
-                    sha256  b6f7c662ea75a94f5a5e41c2fee95f09a5ba168429ac8cdd41f6ba2c78d1b07f
-}
-
-set stage0(dir)     ${worksrcpath}/build/stage0
+                    sha256  b6f7c662ea75a94f5a5e41c2fee95f09a5ba168429ac8cdd41f6ba2c78d1b07f \
+                    size    4215369
 
 pre-fetch {
     if {${os.platform} eq "darwin" && ${os.major} < 11} {
@@ -87,26 +104,73 @@
 }
 
 post-extract {
-    file mkdir ${stage0(dir)}
-    system "cp -r ${workpath}/${stage0(ruststd)}/rust-std-${rust_platform}/* ${stage0(dir)}"
-    system "cp -r ${workpath}/${stage0(rustc)}/rustc/* ${stage0(dir)}"
-    system "cp -r ${workpath}/${stage0(cargo)}/cargo/* ${stage0(dir)}"
+    foreach arch ${architectures} {
+        set rust_root     ${worksrcpath}/build/stage0-${arch}
+        set rust_platform ${arch}-apple-${os.platform}
+        file mkdir ${rust_root}
+        system "cp -r ${workpath}/rust-std-${ruststd_version}-${rust_platform}/rust-std-${rust_platform}/* ${rust_root}"
+        system "cp -r ${workpath}/rustc-${rustc_version}-${rust_platform}/rustc/* ${rust_root}"
+        system "cp -r ${workpath}/cargo-${cargo_version}-${rust_platform}/cargo/* ${rust_root}"
+    }
 }
 
-configure.args      --build=${rust_platform} \
-                    --enable-vendor \
+configure.args      --enable-vendor \
                     --default-linker=${configure.cc} \
                     --disable-codegen-tests \
                     --disable-docs \
                     --llvm-root=${prefix}/libexec/llvm-5.0 \
-                    --local-rust-root=${stage0(dir)} \
-                    --release-channel=stable \
+                    --release-channel=stable
+
+if {![variant_isset universal]} {
+    if {${build_arch} eq "i386"} {
+        set arch_name i686
+    } else {
+        set arch_name ${build_arch}
+    }
+    set rust_platform ${arch_name}-apple-${os.platform}
+    set rust_root     ${worksrcpath}/build/stage0-${arch_name}
+    configure.args-append \
+                   --build=${rust_platform} \
+                   --local-rust-root=${rust_root}
+} else {
+    foreach arch ${universal_archs} {
+        if {${arch} eq "i386"} {
+            set arch_name i686
+        } else {
+            set arch_name ${build_arch}
+        }
+        set rust_platform ${arch_name}-apple-${os.platform}
+        set rust_root     ${worksrcpath}/build/stage0-${arch_name}
+        lappend merger_configure_args(${arch}) \
+                  --build=${rust_platform} \
+                  --local-rust-root=${rust_root}
+    }
+}
+
+foreach arch ${architectures} {
+    set rust_platform ${arch}-apple-${os.platform}
+    configure.args-append \
                     --set=target.${rust_platform}.cc=${configure.cc} \
                     --set=target.${rust_platform}.cxx=${configure.cxx} \
                     --set=target.${rust_platform}.linker=${configure.cc}
+}
 
 configure.universal_args-delete --disable-dependency-tracking
 
+post-configure {
+    # the bootstrap call to rustc uses cc for the linker
+    # place config file in the HOME directory
+    # see https://trac.macports.org/wiki/UsingTheRightCompiler
+    xinstall -d -m 0755 ${workpath}/.home/.cargo
+    set config [open ${workpath}/.home/.cargo/config w]
+    foreach arch ${architectures} {
+        set rust_platform ${arch}-apple-${os.platform}
+        puts ${config} "\[target.${rust_platform}\]"
+        puts ${config} "linker = \"${configure.cc}\""
+    }
+    close ${config}
+}
+
 build.env           RUSTC_SAVE_ANALYSIS=api
 build.args          VERBOSE=1 BOOTSTRAP_ARGS="-v -j${build.jobs}"
 
@@ -115,8 +179,15 @@
 test.args           VERBOSE=1
 
 destroot.args       VERBOSE=1
-post-destroot {
-    if {${subport} eq ${name}} {
+if {${subport} eq ${name}} {
+    if {![variant_isset universal]} {
+        post-destroot {
+            if {${build_arch} eq "i386"} {
+                set arch_name i686
+            } else {
+                set arch_name ${build_arch}
+            }
+            set rust_platform ${arch_name}-apple-${os.platform}
         file copy \
             ${worksrcpath}/build/${rust_platform}/stage1-std/${rust_platform}/release/deps/save-analysis \
             ${destroot}${prefix}/lib/rustlib/${rust_platform}/analysis
@@ -125,6 +196,25 @@
         xinstall -m 644 ${worksrcpath}/src/etc/ctags.rust \
             ${destroot}${prefix}/share/${name}
     }
+    } else {
+        merger-post-destroot {
+            foreach arch ${universal_archs} {
+                if {${arch} eq "i386"} {
+                    set arch_name i686
+                } else {
+                    set arch_name ${arch}
+                }
+                set rust_platform ${arch_name}-apple-${os.platform}
+                file copy \
+                    ${worksrcpath}-${arch}/build/${rust_platform}/stage1-std/${rust_platform}/release/deps/save-analysis \
+                    ${destroot}-${arch}${prefix}/lib/rustlib/${rust_platform}/analysis
+
+                xinstall -d ${destroot}-${arch}${prefix}/share/${name}
+                xinstall -m 644 ${worksrcpath}-${arch}/src/etc/ctags.rust \
+                    ${destroot}-${arch}${prefix}/share/${name}
+            }
+        }
+    }
 }
 
 livecheck.type      regex
