title: Cross-compile for MIPS MT7688 OpenWRT on OSX (mipsel-unknown-linux-musl)
body: <p>When I try to cross-compile a Cargo starter main.rs file for little-endian MIPS (MT7688 processor running OpenWRT) on OSX using rustc 1.32.0-nightly, I get the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>cargo build --target=mipsel-unknown-linux-musl
     Compiling led v0.1.0 (/Users/petrus/Projects/led)
  error: linking with `cc` failed: exit code: 1
    |
    = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.2kthj4han7xcr84g.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.440j3fk0cam0b0pu.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.482rers61msj2848.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.498udk2wtgjsw4ib.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.4uh6jdaux45o6bh1.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.5fq5qqfqkwjepod2.rcgu.o" "-o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.49n6u1xnbbs66t12.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps" "-L" "/Users/petrus/Projects/led/target/debug/deps" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libstd-27a0dffe96f2ad3e.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libpanic_unwind-fe8b80af161a561e.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcFd840X/libunwind-ecc019d8c6cb3f8a.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcFd840X/liblibc-5d68beb2e4661bda.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/liballoc-de565f8c0b8e0971.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/librustc_std_workspace_core-0a55bee6cc97e346.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcore-4075eb2b0854a330.rlib" "-Wl,--end-group" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcompiler_builtins-ecf284221505c8a3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc"
    = note: clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]
            ld: unknown option: --as-needed
            clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>Running with <code>RUSTFLAGS="-C linker=lld"</code> throws:</p>
  
  <pre class="lang-none prettyprint-override"><code>RUSTFLAGS="-C linker=lld" cargo build --target=mipsel-unknown-linux-musl
     Compiling led v0.1.0 (/Users/petrus/Projects/led)
  error: linker `lld` not found
    |
    = note: No such file or directory (os error 2)
  
  error: aborting due to previous error
  
  error: Could not compile `led`.
  
  To learn more, run the command again with --verbose.
  </code></pre>
  
  <p>Also tried with <code>RUSTFLAGS="-C linker=rust-lld"</code>, which throws:</p>
  
  <pre class="lang-none prettyprint-override"><code>rust-lld: error: unable to find library -lgcc_s
  rust-lld: error: unable to find library -lc
  </code></pre>
  
  <p>As I understand, I need to specify a linker toolchain, which I don't know how to procure. I also tried compiling with xargo and cross and dockcross/linux-mipsel, with no luck.</p>
  
  <p>I would build it on the device itself, but rustup won't install Rust on the MT7688. How do I get and specify the correct MIPS (little-endian) linker toolchain?</p>
  
  <p><strong>Update:</strong> I found a <a href="https://github.com/a3f/cross-gcc-hosted-on-macOS/releases" rel="noreferrer">pre-compiled MIPS GCC for macOS</a>, which throws `mipsel-elf/bin/ld: unrecognized option '-Wl,--as-needed' or  "error adding symbols: Bad value" when I try to specify mipsel-elf-gcc it as the linker:</p>
  
  <pre class="lang-none prettyprint-override"><code>rustc --target=mipsel-unknown-linux-musl -C linker=/Users/petrus/Projects/wallfly/respeaker/bin/gcc-mips/bin/mipsel-elf-gcc ./src/main.rs
  error: linking with `/Users/petrus/Projects/bin/gcc-mips/bin/mipsel-elf-gcc` failed: exit code: 1
    |
    = note: "/Users/petrus/Projects/bin/gcc-mips/bin/mipsel-elf-gcc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "main.main.7rcbfp3g-cgu.0.rcgu.o" "main.main.7rcbfp3g-cgu.1.rcgu.o" "main.main.7rcbfp3g-cgu.2.rcgu.o" "main.main.7rcbfp3g-cgu.3.rcgu.o" "main.main.7rcbfp3g-cgu.4.rcgu.o" "main.main.7rcbfp3g-cgu.5.rcgu.o" "-o" "main" "main.4s37gsrti678ik8u.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libstd-27a0dffe96f2ad3e.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libpanic_unwind-fe8b80af161a561e.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcLDTUyV/libunwind-ecc019d8c6cb3f8a.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcLDTUyV/liblibc-5d68beb2e4661bda.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/liballoc-de565f8c0b8e0971.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/librustc_std_workspace_core-0a55bee6cc97e346.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcore-4075eb2b0854a330.rlib" "-Wl,--end-group" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcompiler_builtins-ecf284221505c8a3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc"
    = note: /Users/petrus/Projects/bin/gcc-mips/bin/../lib/gcc/mipsel-elf/7.1.0/../../../../mipsel-elf/bin/ld: /Users/petrus/Projects/bin/gcc-mips/bin/../lib/gcc/mipsel-elf/7.1.0/crtbegin.o: relocation R_MIPS_HI16 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
            /Users/petrus/Projects/bin/gcc-mips/bin/../lib/gcc/mipsel-elf/7.1.0/crtbegin.o: error adding symbols: Bad value
            collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>Maybe I'm specifying it wrong?</p>