Build rustc properly for cross targets ( Linux > FreeBSD )

[cross posting from https://users.rust-lang.org/t/build-rustc-properly-for-cross-targets-linux-freebsd/38059]

I am using the following script to build rustc from source. All the build / link tools are LLVM/Clang ( 7.0 ). No GCC is used; That is intentional. The goal is to produce a Linux x64 ( Ubuntu 12.04 ) rustc that can build binaries for FreeBSD 32,64.

echo "[target.x86_64-unknown-freebsd]" >> $CONFIG_FILE_NAME

echo "cc = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/amd64-unknown-freebsd/bin/clang\"" >> $CONFIG_FILE_NAME
echo "cxx = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/amd64-unknown-freebsd/bin/clang++\"" >> $CONFIG_FILE_NAME
echo "ar = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/amd64-unknown-freebsd/bin/ar\"" >> $CONFIG_FILE_NAME
echo "ranlib = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/amd64-unknown-freebsd/bin/ranlib\"" >> $CONFIG_FILE_NAME
echo "linker = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/amd64-unknown-freebsd/bin/clang\"" >> $CONFIG_FILE_NAME

echo "[target.i686-unknown-freebsd]" >> $CONFIG_FILE_NAME

echo "cc = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/i386-unknown-freebsd/bin/clang\"" >> $CONFIG_FILE_NAME
echo "cxx = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/i386-unknown-freebsd/bin/clang++\"" >> $CONFIG_FILE_NAME
echo "ar = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/i386-unknown-freebsd/bin/ar\"" >> $CONFIG_FILE_NAME
echo "ranlib = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/i386-unknown-freebsd/bin/ranlib\"" >> $CONFIG_FILE_NAME
echo "linker = \"/compilers/${HOST_OS}/${HOST_OS_ID}/${HOST_OS_ARCH}/llvm/${LLVM_VERSION}/current/i386-unknown-freebsd/bin/clang\"" >> $CONFIG_FILE_NAME



export CARGO_TARGET_X86_64_UNKNOWN_FREEBSD_RUSTFLAGS="-C link-arg=-L/sysroots/llvm-${LLVM_VERSION}/freebsd/stable_11/current/stage/amd64/lib -C link-arg=-L/sysroots/llvm-${LLVM_VERSION}/freebsd/stable_11/current/stage/amd64/usr/lib"

export CARGO_TARGET_I686_UNKNOWN_FREEBSD_RUSTFLAGS="-C link-arg=-L/sysroots/llvm-${LLVM_VERSION}/freebsd/stable_11/current/stage/i386/lib -C link-arg=-L/sysroots/llvm-${LLVM_VERSION}/freebsd/stable_11/current/stage/i386/usr/lib"

"$X_PY_PATH" --src $SRC_DIR --config $CONFIG_FILE_NAME install 2>&1 | tee build.log 

config.template file used in the above script.

[llvm]
ninja = true
link-jobs = 1
version-suffix = "-rust"
release-debuginfo = true
use-libcxx = true
use-linker = "lld"

[rust]
optimize = true
debug = true
debuginfo-level = 2
debuginfo-level-rustc = 2
debuginfo-level-std = 2
debuginfo-level-tools = 2
channel = "dev"

[build]
extended = true
verbose = 1
target = ["x86_64-unknown-linux-gnu","x86_64-unknown-freebsd","i686-unknown-freebsd"]

Build / Install is successful. However when I build a sample project with the built rustc, I get linker errors about missing libc symbols.

.cargo/config in the sample app directory

[target.x86_64-unknown-freebsd]
linker = "/compilers/Linux/Ubuntu-12.04/x86_64/llvm/7.0/current/amd64-unknown-freebsd/bin/clang"
rustflags = ["-C","link-arg=--sysroot=/sysroot/pub/llvm-7.0/sysroot/stable_11/current/amd64-unknown-freebsd11.0","-C","link-arg=-L/sysroot/pub/llvm-7.0/freebsd/stable_11/current/stage/amd64/lib","-C","link-arg=-L/sysroot/pub/llvm-7.0/freebsd/stable_11/current/stage/amd64/usr/lib"]

(I have edited some internal paths. Clang arguments splitted to lines)

CARGO_HOME=/compilers/release/build/rust-build/release-1390/app-test/cargo_home cargo rustc -v --target=x86_64-unknown-freebsd


Compiling sample_app v0.1.0 (/compilers/release/build/rust-build/release-1390/app-test/sample_app)

     Running `rustc --edition=2018 --crate-name sample_app src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=0d6834f82a4f0c05 -C extra-filename=-0d6834f82a4f0c05 --out-dir /compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps --target x86_64-unknown-freebsd -C linker=/compilers/Linux/Ubuntu-12.04/x86_64/llvm/7.0/current/amd64-unknown-freebsd/bin/clang -C incremental=/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/incremental -L dependency=/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps -L dependency=/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/debug/deps -C link-arg=--sysroot/sysroot/pub/llvm-7.0/sysroot/stable_11/current/amd64-unknown-freebsd11.0 -C link-arg=-L/sysroot/pub/llvm-7.0/freebsd/stable_11/current/stage/amd64/lib -C link-arg=-L/sysroot/pub/llvm-7.0/freebsd/stable_11/current/stage/amd64/usr/lib`
error: linking with `/compilers/Linux/Ubuntu-12.04/x86_64/llvm/7.0/current/amd64-unknown-freebsd/bin/clang` failed: exit code: 1
  |
  = note: "/compilers/Linux/Ubuntu-12.04/x86_64/llvm/7.0/current/amd64-unknown-freebsd/bin/clang"
"-Wl,--as-needed"
"-Wl,-z,noexecstack"
"-m64"
"-L"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib" 
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.15e71f8pb1wtqg1v.rcgu.o"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.1nav2jwpt7gx0c3m.rcgu.o"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.2hjsno1klbt7giof.rcgu.o"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.2xl4xe4nd8tfwjsb.rcgu.o"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.l85hjo6csrn1a2x.rcgu.o"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.rqoev7k5qw6ys51.rcgu.o"
"-o"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps/sample_app-0d6834f82a4f0c05.rldu6mkoffyi8pu.rcgu.o"
"-Wl,--gc-sections"
"-pie"
"-Wl,-zrelro"
"-Wl,-znow"
"-nodefaultlibs"
"-L"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/x86_64-unknown-freebsd/debug/deps"
"-L"
"/compilers/release/build/rust-build/release-1390/app-test/sample_app/target/debug/deps"
"-L"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib"
"-Wl,--start-group"
"-Wl,-Bstatic"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libstd-a87d899a99486866.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libpanic_unwind-d6932a5953013889.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libhashbrown-5c967e335cf54e9c.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/librustc_std_workspace_alloc-06410e7395aaec04.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace-06f48baa18af63e1.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/librustc_demangle-cdd3cadcbc32f81f.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libunwind-837b33ebe062d582.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libcfg_if-1671030035446059.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/liblibc-a80606d63337bbc8.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/liballoc-d4b63a20a6fb4e0f.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/librustc_std_workspace_core-fcacba6645693a10.rlib"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libcore-f133cd0f95c72332.rlib"
"-Wl,--end-group"
"/compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libcompiler_builtins-d56f51d9a1c02edf.rlib"
"-Wl,-Bdynamic"
"-lexecinfo"
"-lpthread"
"-lgcc_s"
"-lc"
"-lm"
"-lrt"
"-lpthread"
"-lutil"
"-lutil"
"--sysroot=/sysroot/pub/llvm-7.0/sysroot/stable_11/current/amd64-unknown-freebsd11.0"
"-L/sysroot/pub/llvm-7.0/freebsd/stable_11/current/stage/amd64/lib"
"-L/sysroot/pub/llvm-7.0/freebsd/stable_11/current/stage/amd64/usr/lib"

  = note: ld.lld: error: undefined symbol: __errno_location
          >>> referenced by posix.c
          >>>               posix.o:(__rdos_backtrace_open) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by posix.c
          >>>               posix.o:(__rdos_backtrace_close) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by alloc.c
          >>>               alloc.o:(__rdos_backtrace_alloc) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by alloc.c
          >>>               alloc.o:(__rdos_backtrace_vector_grow) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by alloc.c
          >>>               alloc.o:(__rdos_backtrace_vector_finish) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by alloc.c
          >>>               alloc.o:(__rdos_backtrace_vector_release) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __fxstat
          >>> referenced by elf.c
          >>>               elf.o:(elf_add) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by elf.c
          >>>               elf.o:(elf_add) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by read.c
          >>>               read.o:(__rdos_backtrace_get_view) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4bee.rlib

          ld.lld: error: undefined symbol: __errno_location
          >>> referenced by read.c
          >>>               read.o:(__rdos_backtrace_get_view) in archive /compilers/release/build/rust-build/release-1390/install/Linux/Ubuntu-12.04/x86_64/rust/1.39.0/20200209/lib/rustlib/x86_64-unknown-freebsd/lib/libbacktrace_sys-79cc5d6ba22c4qbee.rlib
          clang-7: error: linker command failed with exit code 1 (use -v to see invocation)


error: aborting due to previous error