From 7b026f0355a5394358e6a9758185db8eec850e0f Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <japaricious@gmail.com>
Date: Mon, 25 Jan 2016 16:23:31 -0500
Subject: [PATCH 1/4] add support for mips(el)-unknown-linux-musl

This target covers MIPS devices that run the trunk version of OpenWRT.

The x86_64-unknown-linux-musl target always links statically to C libraries. For
the mips(el)-unknown-linux-musl target, we opt for dynamic linking (like most of
other targets do) to keep binary size down.

As for the C compiler flags used in the build system, we use the same flags used
for the mips(el)-unknown-linux-gnu target.
---
 .gitmodules                                   |  3 ++-
 configure                                     |  2 +-
 mk/cfg/mips-unknown-linux-musl.mk             | 24 ++++++++++++++++++
 mk/cfg/mipsel-unknown-linux-musl.mk           | 24 ++++++++++++++++++
 src/liblibc                                   |  2 +-
 .../target/mips_unknown_linux_musl.rs         | 24 ++++++++++++++++++
 .../target/mipsel_unknown_linux_musl.rs       | 25 +++++++++++++++++++
 src/librustc_back/target/mod.rs               |  2 ++
 src/libstd/os/linux/raw.rs                    |  9 +++++++
 src/libstd/sys/common/libunwind.rs            |  5 ++--
 10 files changed, 115 insertions(+), 5 deletions(-)
 create mode 100644 mk/cfg/mips-unknown-linux-musl.mk
 create mode 100644 mk/cfg/mipsel-unknown-linux-musl.mk
 create mode 100644 src/librustc_back/target/mips_unknown_linux_musl.rs
 create mode 100644 src/librustc_back/target/mipsel_unknown_linux_musl.rs

diff --git a/.gitmodules b/.gitmodules
index eb033f5401d3c..1c0da89546915 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -16,4 +16,5 @@
 	url = https://github.com/rust-lang/rust-installer.git
 [submodule "src/liblibc"]
 	path = src/liblibc
-	url = https://github.com/rust-lang-nursery/libc.git
+	url = https://github.com/japaric/libc.git
+	branch = mips-musl
diff --git a/configure b/configure
index 2693e9bcd236a..fea864638f814 100755
--- a/configure
+++ b/configure
@@ -1178,7 +1178,7 @@ do
             ;;
 
 
-        *-musl)
+        x86_64-*-musl)
             if [ ! -f $CFG_MUSL_ROOT/lib/libc.a ]
             then
                 err "musl libc $CFG_MUSL_ROOT/lib/libc.a not found"
diff --git a/mk/cfg/mips-unknown-linux-musl.mk b/mk/cfg/mips-unknown-linux-musl.mk
new file mode 100644
index 0000000000000..f7892bf041fed
--- /dev/null
+++ b/mk/cfg/mips-unknown-linux-musl.mk
@@ -0,0 +1,24 @@
+# mips-unknown-linux-musl configuration
+CC_mips-unknown-linux-musl=mips-openwrt-linux-gcc
+CXX_mips-unknown-linux-musl=mips-openwrt-linux-g++
+CPP_mips-unknown-linux-musl=mips-openwrt-linux-gcc -E
+AR_mips-unknown-linux-musl=mips-openwrt-linux-ar
+CFG_LIB_NAME_mips-unknown-linux-musl=lib$(1).so
+CFG_STATIC_LIB_NAME_mips-unknown-linux-musl=lib$(1).a
+CFG_LIB_GLOB_mips-unknown-linux-musl=lib$(1)-*.so
+CFG_LIB_DSYM_GLOB_mips-unknown-linux-musl=lib$(1)-*.dylib.dSYM
+CFG_JEMALLOC_CFLAGS_mips-unknown-linux-musl := -mips32r2 -msoft-float -mabi=32 $(CFLAGS)
+CFG_GCCISH_CFLAGS_mips-unknown-linux-musl := -Wall -g -fPIC -mips32r2 -msoft-float -mabi=32 $(CFLAGS)
+CFG_GCCISH_CXXFLAGS_mips-unknown-linux-musl := -fno-rtti $(CXXFLAGS)
+CFG_GCCISH_LINK_FLAGS_mips-unknown-linux-musl := -shared -fPIC -g -mips32r2 -msoft-float -mabi=32
+CFG_GCCISH_DEF_FLAG_mips-unknown-linux-musl := -Wl,--export-dynamic,--dynamic-list=
+CFG_LLC_FLAGS_mips-unknown-linux-musl :=
+CFG_INSTALL_NAME_mips-unknown-linux-musl =
+CFG_EXE_SUFFIX_mips-unknown-linux-musl =
+CFG_WINDOWSY_mips-unknown-linux-musl :=
+CFG_UNIXY_mips-unknown-linux-musl := 1
+CFG_LDPATH_mips-unknown-linux-musl :=
+CFG_RUN_mips-unknown-linux-musl=
+CFG_RUN_TARG_mips-unknown-linux-musl=
+RUSTC_FLAGS_mips-unknown-linux-musl := -C target-cpu=mips32r2 -C target-feature="+mips32r2" -C soft-float
+CFG_GNU_TRIPLE_mips-unknown-linux-musl := mips-unknown-linux-musl
diff --git a/mk/cfg/mipsel-unknown-linux-musl.mk b/mk/cfg/mipsel-unknown-linux-musl.mk
new file mode 100644
index 0000000000000..632423cc881e9
--- /dev/null
+++ b/mk/cfg/mipsel-unknown-linux-musl.mk
@@ -0,0 +1,24 @@
+# mipsel-unknown-linux-musl configuration
+CC_mipsel-unknown-linux-musl=mipsel-openwrt-linux-gcc
+CXX_mipsel-unknown-linux-musl=mipsel-openwrt-linux-g++
+CPP_mipsel-unknown-linux-musl=mipsel-openwrt-linux-gcc
+AR_mipsel-unknown-linux-musl=mipsel-openwrt-linux-ar
+CFG_LIB_NAME_mipsel-unknown-linux-musl=lib$(1).so
+CFG_STATIC_LIB_NAME_mipsel-unknown-linux-musl=lib$(1).a
+CFG_LIB_GLOB_mipsel-unknown-linux-musl=lib$(1)-*.so
+CFG_LIB_DSYM_GLOB_mipsel-unknown-linux-musl=lib$(1)-*.dylib.dSYM
+CFG_JEMALLOC_CFLAGS_mipsel-unknown-linux-musl := -mips32 -mabi=32 $(CFLAGS)
+CFG_GCCISH_CFLAGS_mipsel-unknown-linux-musl := -Wall -g -fPIC -mips32 -mabi=32 $(CFLAGS)
+CFG_GCCISH_CXXFLAGS_mipsel-unknown-linux-musl := -fno-rtti $(CXXFLAGS)
+CFG_GCCISH_LINK_FLAGS_mipsel-unknown-linux-musl := -shared -fPIC -g -mips32
+CFG_GCCISH_DEF_FLAG_mipsel-unknown-linux-musl := -Wl,--export-dynamic,--dynamic-list=
+CFG_LLC_FLAGS_mipsel-unknown-linux-musl :=
+CFG_INSTALL_NAME_mipsel-unknown-linux-musl =
+CFG_EXE_SUFFIX_mipsel-unknown-linux-musl :=
+CFG_WINDOWSY_mipsel-unknown-linux-musl :=
+CFG_UNIXY_mipsel-unknown-linux-musl := 1
+CFG_LDPATH_mipsel-unknown-linux-musl :=
+CFG_RUN_mipsel-unknown-linux-musl=
+CFG_RUN_TARG_mipsel-unknown-linux-musl=
+RUSTC_FLAGS_mipsel-unknown-linux-musl := -C target-cpu=mips32 -C target-feature="+mips32"
+CFG_GNU_TRIPLE_mipsel-unknown-linux-musl := mipsel-unknown-linux-musl
diff --git a/src/liblibc b/src/liblibc
index 91ff43c736de6..81c05bcddb05d 160000
--- a/src/liblibc
+++ b/src/liblibc
@@ -1 +1 @@
-Subproject commit 91ff43c736de664f8d3cd351e148c09cdea6731e
+Subproject commit 81c05bcddb05d0e5facf66f3945405bbf50b04d3
diff --git a/src/librustc_back/target/mips_unknown_linux_musl.rs b/src/librustc_back/target/mips_unknown_linux_musl.rs
new file mode 100644
index 0000000000000..21be075af8e1f
--- /dev/null
+++ b/src/librustc_back/target/mips_unknown_linux_musl.rs
@@ -0,0 +1,24 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use target::Target;
+
+pub fn target() -> Target {
+    Target {
+        llvm_target: "mips-unknown-linux-musl".to_string(),
+        target_endian: "big".to_string(),
+        target_pointer_width: "32".to_string(),
+        arch: "mips".to_string(),
+        target_os: "linux".to_string(),
+        target_env: "musl".to_string(),
+        target_vendor: "unknown".to_string(),
+        options: super::linux_base::opts()
+    }
+}
diff --git a/src/librustc_back/target/mipsel_unknown_linux_musl.rs b/src/librustc_back/target/mipsel_unknown_linux_musl.rs
new file mode 100644
index 0000000000000..11c9caf90373e
--- /dev/null
+++ b/src/librustc_back/target/mipsel_unknown_linux_musl.rs
@@ -0,0 +1,25 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use target::Target;
+
+pub fn target() -> Target {
+    Target {
+        llvm_target: "mipsel-unknown-linux-musl".to_string(),
+        target_endian: "little".to_string(),
+        target_pointer_width: "32".to_string(),
+        arch: "mips".to_string(),
+        target_os: "linux".to_string(),
+        target_env: "musl".to_string(),
+        target_vendor: "unknown".to_string(),
+
+        options: super::linux_base::opts()
+    }
+}
diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs
index 331ec4a82ac51..39f05ba645e87 100644
--- a/src/librustc_back/target/mod.rs
+++ b/src/librustc_back/target/mod.rs
@@ -420,6 +420,8 @@ impl Target {
             armv7_unknown_linux_gnueabihf,
             aarch64_unknown_linux_gnu,
             x86_64_unknown_linux_musl,
+            mips_unknown_linux_musl,
+            mipsel_unknown_linux_musl,
 
             i686_linux_android,
             arm_linux_androideabi,
diff --git a/src/libstd/os/linux/raw.rs b/src/libstd/os/linux/raw.rs
index 953d0917141d1..286823552c29b 100644
--- a/src/libstd/os/linux/raw.rs
+++ b/src/libstd/os/linux/raw.rs
@@ -93,11 +93,20 @@ mod arch {
     use os::raw::{c_long, c_ulong};
     use os::unix::raw::{gid_t, uid_t};
 
+    #[cfg(target_env = "musl")]
+    #[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = i64;
+    #[cfg(not(target_env = "musl"))]
     #[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = i32;
     #[stable(feature = "raw_ext", since = "1.1.0")] pub type blksize_t = i32;
+    #[cfg(target_env = "musl")]
+    #[stable(feature = "raw_ext", since = "1.1.0")] pub type ino_t = u64;
+    #[cfg(not(target_env = "musl"))]
     #[stable(feature = "raw_ext", since = "1.1.0")] pub type ino_t = u32;
     #[stable(feature = "raw_ext", since = "1.1.0")] pub type nlink_t = u32;
+    #[cfg(target_env = "musl")]
     #[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i32;
+    #[cfg(not(target_env = "musl"))]
+    #[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = i64;
     #[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i32;
 
     #[repr(C)]
diff --git a/src/libstd/sys/common/libunwind.rs b/src/libstd/sys/common/libunwind.rs
index 77d1eed96231d..179a27a2ec86c 100644
--- a/src/libstd/sys/common/libunwind.rs
+++ b/src/libstd/sys/common/libunwind.rs
@@ -101,9 +101,10 @@ pub type _Unwind_Exception_Cleanup_Fn =
                       exception: *mut _Unwind_Exception);
 
 #[cfg_attr(any(all(target_os = "linux", not(target_env = "musl")),
-               target_os = "freebsd"),
+               target_os = "freebsd",
+               all(target_os = "linux", target_env = "musl", not(target_arch = "x86_64"))),
            link(name = "gcc_s"))]
-#[cfg_attr(all(target_os = "linux", target_env = "musl", not(test)),
+#[cfg_attr(all(target_os = "linux", target_env = "musl", target_arch = "x86_64", not(test)),
            link(name = "unwind", kind = "static"))]
 #[cfg_attr(any(target_os = "android", target_os = "openbsd"),
            link(name = "gcc"))]

From 146dfce803e85844cd14bb6967594efae4dcdb13 Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <japaricious@gmail.com>
Date: Sat, 30 Jan 2016 03:24:24 -0500
Subject: [PATCH 2/4] revert changes used for local testing

---
 .gitmodules                         | 3 +--
 mk/cfg/mips-unknown-linux-musl.mk   | 8 ++++----
 mk/cfg/mipsel-unknown-linux-musl.mk | 8 ++++----
 src/liblibc                         | 2 +-
 4 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/.gitmodules b/.gitmodules
index 1c0da89546915..eb033f5401d3c 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -16,5 +16,4 @@
 	url = https://github.com/rust-lang/rust-installer.git
 [submodule "src/liblibc"]
 	path = src/liblibc
-	url = https://github.com/japaric/libc.git
-	branch = mips-musl
+	url = https://github.com/rust-lang-nursery/libc.git
diff --git a/mk/cfg/mips-unknown-linux-musl.mk b/mk/cfg/mips-unknown-linux-musl.mk
index f7892bf041fed..e9a6891a1b2dd 100644
--- a/mk/cfg/mips-unknown-linux-musl.mk
+++ b/mk/cfg/mips-unknown-linux-musl.mk
@@ -1,8 +1,8 @@
 # mips-unknown-linux-musl configuration
-CC_mips-unknown-linux-musl=mips-openwrt-linux-gcc
-CXX_mips-unknown-linux-musl=mips-openwrt-linux-g++
-CPP_mips-unknown-linux-musl=mips-openwrt-linux-gcc -E
-AR_mips-unknown-linux-musl=mips-openwrt-linux-ar
+CC_mips-unknown-linux-musl=mips-linux-musl-gcc
+CXX_mips-unknown-linux-musl=mips-linux-musl-g++
+CPP_mips-unknown-linux-musl=mips-linux-musl-gcc -E
+AR_mips-unknown-linux-musl=mips-linux-musl-ar
 CFG_LIB_NAME_mips-unknown-linux-musl=lib$(1).so
 CFG_STATIC_LIB_NAME_mips-unknown-linux-musl=lib$(1).a
 CFG_LIB_GLOB_mips-unknown-linux-musl=lib$(1)-*.so
diff --git a/mk/cfg/mipsel-unknown-linux-musl.mk b/mk/cfg/mipsel-unknown-linux-musl.mk
index 632423cc881e9..659c71596d3ff 100644
--- a/mk/cfg/mipsel-unknown-linux-musl.mk
+++ b/mk/cfg/mipsel-unknown-linux-musl.mk
@@ -1,8 +1,8 @@
 # mipsel-unknown-linux-musl configuration
-CC_mipsel-unknown-linux-musl=mipsel-openwrt-linux-gcc
-CXX_mipsel-unknown-linux-musl=mipsel-openwrt-linux-g++
-CPP_mipsel-unknown-linux-musl=mipsel-openwrt-linux-gcc
-AR_mipsel-unknown-linux-musl=mipsel-openwrt-linux-ar
+CC_mipsel-unknown-linux-musl=mipsel-linux-musl-gcc
+CXX_mipsel-unknown-linux-musl=mipsel-linux-musl-g++
+CPP_mipsel-unknown-linux-musl=mipsel-linux-musl-gcc
+AR_mipsel-unknown-linux-musl=mipsel-linux-musl-ar
 CFG_LIB_NAME_mipsel-unknown-linux-musl=lib$(1).so
 CFG_STATIC_LIB_NAME_mipsel-unknown-linux-musl=lib$(1).a
 CFG_LIB_GLOB_mipsel-unknown-linux-musl=lib$(1)-*.so
diff --git a/src/liblibc b/src/liblibc
index 81c05bcddb05d..53d11fc6ef10b 160000
--- a/src/liblibc
+++ b/src/liblibc
@@ -1 +1 @@
-Subproject commit 81c05bcddb05d0e5facf66f3945405bbf50b04d3
+Subproject commit 53d11fc6ef10b7c30e85e9f2ff1f67f81ec8636f

From 64ac041b1fb767982762ffca568a9fc3a81fcfbc Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <japaricious@gmail.com>
Date: Sat, 30 Jan 2016 03:28:37 -0500
Subject: [PATCH 3/4] rustc: set MIPS cpu/features in the compiler

cf #31303
---
 mk/cfg/mips-unknown-linux-musl.mk                     | 2 +-
 mk/cfg/mipsel-unknown-linux-musl.mk                   | 2 +-
 src/librustc_back/target/mips_unknown_linux_musl.rs   | 8 ++++++--
 src/librustc_back/target/mipsel_unknown_linux_musl.rs | 9 ++++++---
 4 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/mk/cfg/mips-unknown-linux-musl.mk b/mk/cfg/mips-unknown-linux-musl.mk
index e9a6891a1b2dd..33528b986f6e5 100644
--- a/mk/cfg/mips-unknown-linux-musl.mk
+++ b/mk/cfg/mips-unknown-linux-musl.mk
@@ -20,5 +20,5 @@ CFG_UNIXY_mips-unknown-linux-musl := 1
 CFG_LDPATH_mips-unknown-linux-musl :=
 CFG_RUN_mips-unknown-linux-musl=
 CFG_RUN_TARG_mips-unknown-linux-musl=
-RUSTC_FLAGS_mips-unknown-linux-musl := -C target-cpu=mips32r2 -C target-feature="+mips32r2" -C soft-float
+RUSTC_FLAGS_mips-unknown-linux-musl :=
 CFG_GNU_TRIPLE_mips-unknown-linux-musl := mips-unknown-linux-musl
diff --git a/mk/cfg/mipsel-unknown-linux-musl.mk b/mk/cfg/mipsel-unknown-linux-musl.mk
index 659c71596d3ff..db836b81c5fd5 100644
--- a/mk/cfg/mipsel-unknown-linux-musl.mk
+++ b/mk/cfg/mipsel-unknown-linux-musl.mk
@@ -20,5 +20,5 @@ CFG_UNIXY_mipsel-unknown-linux-musl := 1
 CFG_LDPATH_mipsel-unknown-linux-musl :=
 CFG_RUN_mipsel-unknown-linux-musl=
 CFG_RUN_TARG_mipsel-unknown-linux-musl=
-RUSTC_FLAGS_mipsel-unknown-linux-musl := -C target-cpu=mips32 -C target-feature="+mips32"
+RUSTC_FLAGS_mipsel-unknown-linux-musl :=
 CFG_GNU_TRIPLE_mipsel-unknown-linux-musl := mipsel-unknown-linux-musl
diff --git a/src/librustc_back/target/mips_unknown_linux_musl.rs b/src/librustc_back/target/mips_unknown_linux_musl.rs
index 21be075af8e1f..050fb91aa1903 100644
--- a/src/librustc_back/target/mips_unknown_linux_musl.rs
+++ b/src/librustc_back/target/mips_unknown_linux_musl.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use target::Target;
+use target::{Target, TargetOptions};
 
 pub fn target() -> Target {
     Target {
@@ -19,6 +19,10 @@ pub fn target() -> Target {
         target_os: "linux".to_string(),
         target_env: "musl".to_string(),
         target_vendor: "unknown".to_string(),
-        options: super::linux_base::opts()
+        options: TargetOptions {
+            cpu: "mips32r2".to_string(),
+            features: "+mips32r2,+soft-float".to_string(),
+            ..super::linux_base::opts()
+        }
     }
 }
diff --git a/src/librustc_back/target/mipsel_unknown_linux_musl.rs b/src/librustc_back/target/mipsel_unknown_linux_musl.rs
index 11c9caf90373e..383a0d891ca7c 100644
--- a/src/librustc_back/target/mipsel_unknown_linux_musl.rs
+++ b/src/librustc_back/target/mipsel_unknown_linux_musl.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use target::Target;
+use target::{Target, TargetOptions};
 
 pub fn target() -> Target {
     Target {
@@ -19,7 +19,10 @@ pub fn target() -> Target {
         target_os: "linux".to_string(),
         target_env: "musl".to_string(),
         target_vendor: "unknown".to_string(),
-
-        options: super::linux_base::opts()
+        options: TargetOptions {
+            cpu: "mips32".to_string(),
+            features: "+mips32".to_string(),
+            ..super::linux_base::opts()
+        }
     }
 }

From 27127dbc238a5c2abffa53517fd67d1165765d3f Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <japaricious@gmail.com>
Date: Sun, 31 Jan 2016 07:18:30 -0500
Subject: [PATCH 4/4] update libc submodule

fixes failed test (std::os::raw::tests::unix) in x86_64-musl target
---
 src/liblibc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/liblibc b/src/liblibc
index 53d11fc6ef10b..30f70baa6cc1b 160000
--- a/src/liblibc
+++ b/src/liblibc
@@ -1 +1 @@
-Subproject commit 53d11fc6ef10b7c30e85e9f2ff1f67f81ec8636f
+Subproject commit 30f70baa6cc1ba3ddebb55b988fafbad0c0cc810