Seems impossible to cross-compile using Unreal Engine's Native Toolchain

We have a cross-platform proprietary C++ library as our core technology that targets multiple platforms including Android, Linux, and Windows, and various game engines including Unreal Engine. On Windows, we use MSVC, on Android the Android NDK, and on Linux, we use Epic's Native Toolchain in order to support various Linux distributions. Even, for our non-Unreal builds as it provides us with a reliable approach to target as many distributions as possible without worrying about glibc compatibility.

From the documentation:

Unreal Engine's setup shell script (Setup.sh ) automatically downloads a native toolchain, which guarantees your compiler and linker to work with our codebase. With the native toolchain, you compile against a fixed sysroot (glibc at the very least), so that, for example, if you compile a game on Ubuntu 18.04, you will be able to start the binary on CentOS 7.

Now, we have been evaluating Rust for a while as it's going to address some of the challenges we face. However, we decided to gradually move components of our current C++ implementation to Rust. Now, considering that and the requirement for interoperability with Unreal's C++ we have no other way other than building our Rust codebase with the Unreal Toolchain compatibility in mind on Linux (x86-64 and AArch64 targets).

Now, simply adding the following to .cargo/config is not going to cut it:

[target.x86_64-unknown-linux-gnu]
ar = "../toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/llvm-ar"
linker = "../toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++"

[target.aarch64-unknown-linux-gnueabi]
ar = "../toolchains/linux-v20/aarch64-unknown-linux-gnueabi/bin/llvm-ar"
linker = "../toolchains/linux-v20/aarch64-unknown-linux-gnueabi/bin/clang++"

And, since the documentation is sparse, we are not sure how we can target multiple Epic Toolchain versions, e.g. v20, v21, and v22 in the same build (if even possible). Using CMake toolchain files we can easily achieve that in C++.

This is the error we get:

cargo build --target=x86_64-unknown-linux-gnu
   Compiling libc v0.2.149
error: linking with `/path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/mamadou/.cargo/bin:/opt/android/ant/bin:/home/mamadou/android-studio/jre/bin:/opt/android/sdk/ndk/25.1.8937393:/opt/android/sdk/extras/android/support:/opt/android/sdk/build-tools:/opt/android/sdk/platform-tools:/opt/android/sdk/tools:/home/mamadou/.local/lib64/perl5/bin:/usr/local/bin:/usr/local/sbin:/opt/android/ant/bin:/home/mamadou/android-studio/jre/bin:/opt/android/sdk/ndk/25.1.8937393:/opt/android/sdk/extras/android/support:/opt/android/sdk/build-tools:/opt/android/sdk/platform-tools:/opt/android/sdk/tools:/home/mamadou/.local/lib64/perl5/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin:/usr/lib/llvm/15/bin:/usr/lib/llvm/14/bin:/home/mamadou/dev/go/bin:/home/mamadou/dev/go/bin" VSLANG="1033" "/path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++" "-m64" "/tmp/rustc4P4uPA/symbols.o" "/path/to/foo/project/target/debug/build/libc-d285a08f0f1a6c27/build_script_build-d285a08f0f1a6c27.build_script_build.190a2ea6127d45cb-cgu.0.rcgu.o" "/path/to/foo/project/target/debug/build/libc-d285a08f0f1a6c27/build_script_build-d285a08f0f1a6c27.build_script_build.190a2ea6127d45cb-cgu.1.rcgu.o" "/path/to/foo/project/target/debug/build/libc-d285a08f0f1a6c27/build_script_build-d285a08f0f1a6c27.build_script_build.190a2ea6127d45cb-cgu.2.rcgu.o" "/path/to/foo/project/target/debug/build/libc-d285a08f0f1a6c27/build_script_build-d285a08f0f1a6c27.build_script_build.190a2ea6127d45cb-cgu.3.rcgu.o" "/path/to/foo/project/target/debug/build/libc-d285a08f0f1a6c27/build_script_build-d285a08f0f1a6c27.1efwchj2p1d2jmo1.rcgu.o" "-Wl,--as-needed" "-L" "/path/to/foo/project/target/debug/deps" "-L" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-64b7f85860d8e80f.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a6e3f3f120c06a9d.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-d9e2b538f14471de.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-19a8f7e0e3580341.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-42fee89bdf9b228e.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-34254f7cdcc0cc46.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-44ef65cfbf83fcb8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-c65acc9d349f7532.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-9c6ad0c05ee0b09a.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-decf62ffc2a6d6e8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-26adb3c50c2a21e1.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-0e893a92b0bf83dd.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-2ff912bc722ce8c9.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-5ec13f05f8d2bf50.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-2b93db426c5a26c2.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-26def7a8e3a3dff8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-cd2f2bc505f56f50.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-ec02dd343723da85.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-2f4d671a9b4d2563.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/path/to/foo/project/target/debug/build/libc-d285a08f0f1a6c27/build_script_build-d285a08f0f1a6c27" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/Scrt1.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0008002
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/Scrt1.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010001
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/Scrt1.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010002
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/crti.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010002
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/crti.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010001
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/crtbeginS.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010002
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/crtbeginS.o: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010001
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/libgcc_s.so.1: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010001
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/libgcc_s.so.1: unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010002
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/libgcc.a(_muldi3.o): unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010002
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: warning: /usr/lib/gcc/x86_64-pc-linux-gnu/13/libgcc.a(_muldi3.o): unsupported GNU_PROPERTY_TYPE (5) type: 0xc0010001
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: /lib64/libm.so.6: unknown type [0x13] section `.relr.dyn'
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: skipping incompatible /lib64/libm.so.6 when searching for /lib64/libm.so.6
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find /lib64/libm.so.6
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: /lib64/libmvec.so.1: unknown type [0x13] section `.relr.dyn'
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: skipping incompatible /lib64/libmvec.so.1 when searching for /lib64/libmvec.so.1
          /path/to/foo/project/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-ld: cannot find /lib64/libmvec.so.1
          clang++: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `libc` (build script) due to previous error

We have also tried to mess around with Cargo JSON targets but the source code does not show any options to specify the various options we have with CMake (later on this one).

We tried Xargo, cargo-build, and also github/japaric/rust-cross (the forum software does not allow more than two links :/) without any luck as it seems there is no easy way to have better control over the C/C++ compiler.

What do I mean by that? Let me provide the relevant portion of a CMake example toolchain in order to be able to target the Unreal Native Toolchain:

set(CMAKE_CROSSCOMPILING TRUE)

if (NOT DEFINED UE_ARCHITECTURE_TRIPLE)
    set(UE_ARCHITECTURE_TRIPLE "$ENV{UE_ARCHITECTURE_TRIPLE}")
endif ()

if (NOT DEFINED UE_TOOLCHAIN_BIN_DIR)
    set(UE_TOOLCHAIN_BIN_DIR "$ENV{UE_TOOLCHAIN_BIN_DIR}")
endif ()

if (NOT DEFINED UE_TOOLCHAIN_COMPILER_INCLUDE_DIR)
    set(UE_TOOLCHAIN_COMPILER_INCLUDE_DIR "$ENV{UE_TOOLCHAIN_COMPILER_INCLUDE_DIR}")
endif ()

if (NOT DEFINED UE_TOOLCHAIN_LIBCPP_INCLUDE_DIR)
    set(UE_TOOLCHAIN_LIBCPP_INCLUDE_DIR "$ENV{UE_TOOLCHAIN_LIBCPP_INCLUDE_DIR}")
endif ()

if (NOT DEFINED UE_TOOLCHAIN_SYSTEM_INCLUDE_DIR)
    set(UE_TOOLCHAIN_SYSTEM_INCLUDE_DIR "$ENV{UE_TOOLCHAIN_SYSTEM_INCLUDE_DIR}")
endif ()

if (NOT DEFINED BUILD_SHARED)
    set(BUILD_SHARED OFF)
endif ()

set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

set(UE_COMMON_COMPILER_FLAGS "-m64 -pipe")
set(UE_COMMON_COMPILER_FLAGS_DEBUG "${UE_COMMON_COMPILER_FLAGS}")
set(UE_COMMON_COMPILER_FLAGS_RELEASE "${UE_COMMON_COMPILER_FLAGS} -fdata-sections -ffunction-sections -fomit-frame-pointer")
set(UE_COMMON_C_FLAGS "-nostdinc -isystem\"${UE_TOOLCHAIN_COMPILER_INCLUDE_DIR}\" -isystem\"${UE_TOOLCHAIN_SYSTEM_INCLUDE_DIR}\"")
set(UE_COMMON_C_FLAGS_DEBUG "${UE_COMMON_COMPILER_FLAGS_DEBUG} ${UE_COMMON_C_FLAGS}")
set(UE_COMMON_C_FLAGS_RELEASE "${UE_COMMON_COMPILER_FLAGS_RELEASE} ${UE_COMMON_C_FLAGS}")
set(UE_COMMON_CXX_FLAGS "-fcxx-exceptions -fexceptions -frtti -nostdinc -nostdinc++ -isystem\"${UE_TOOLCHAIN_LIBCPP_INCLUDE_DIR}\" -isystem\"${UE_TOOLCHAIN_COMPILER_INCLUDE_DIR}\" -isystem\"${UE_TOOLCHAIN_SYSTEM_INCLUDE_DIR}\"")
set(UE_COMMON_CXX_FLAGS_DEBUG "${UE_COMMON_COMPILER_FLAGS_DEBUG} ${UE_COMMON_CXX_FLAGS}")
set(UE_COMMON_CXX_FLAGS_RELEASE "${UE_COMMON_COMPILER_FLAGS_RELEASE} ${UE_COMMON_CXX_FLAGS}")

set(UE_COMMON_LINKER_FLAGS "-fuse-ld=lld -nodefaultlibs -lc -lc++ -lc++abi -lm -m64 -stdlib=libc++")

# WORKAROUND
# -lc++ -lc++abi fails with BUILD_SHARED option enabled
if (BUILD_SHARED)
    # addressing older Unreal Engine versions link issues
    if (UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v11"
            OR UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v12"
            OR UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v13"
            OR UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v15"
            OR UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v16"
            OR UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v17"
            OR UE_TOOLCHAIN_VERSION_PREFIX MATCHES "v19")
        string(REPLACE "-lc++ -lc++abi" "" UE_COMMON_LINKER_FLAGS "${UE_COMMON_LINKER_FLAGS}")
    endif ()

    # and also removing the above flags causes the following warnings
    # clang++: warning: argument unused during compilation: '-nostdinc' [-Wunused-command-line-argument]
    # clang++: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
    string(REPLACE "-nostdinc -nostdinc++" "" UE_COMMON_CXX_FLAGS "${UE_COMMON_CXX_FLAGS}")
    string(REPLACE "-nostdinc -nostdinc++" "" UE_COMMON_CXX_FLAGS_DEBUG "${UE_COMMON_CXX_FLAGS_DEBUG}")
    string(REPLACE "-nostdinc -nostdinc++" "" UE_COMMON_CXX_FLAGS_RELEASE "${UE_COMMON_CXX_FLAGS_RELEASE}")
endif ()

# WORKAROUND
# this is necessary as a workaround for linker errors with some libs (e.g. fmt)
set(UE_COMMON_LINKER_FLAGS "${UE_COMMON_LINKER_FLAGS} -lgcc_s -lgcc")

set(UE_COMMON_LINKER_FLAGS_DEBUG "${UE_COMMON_LINKER_FLAGS}")
set(UE_COMMON_LINKER_FLAGS_RELEASE "${UE_COMMON_LINKER_FLAGS} -Wl,--gc-sections -Wl,--strip-all")

set(CMAKE_C_FLAGS_DEBUG_INIT "${UE_COMMON_C_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_RELEASE_INIT "${UE_COMMON_C_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${UE_COMMON_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${UE_COMMON_CXX_FLAGS_RELEASE}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "${UE_COMMON_LINKER_FLAGS_DEBUG}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT "${UE_COMMON_LINKER_FLAGS_RELEASE}")
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG_INIT "${UE_COMMON_LINKER_FLAGS_DEBUG}")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE_INIT "${UE_COMMON_LINKER_FLAGS_RELEASE}")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT "${UE_COMMON_LINKER_FLAGS_DEBUG}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE_INIT "${UE_COMMON_LINKER_FLAGS_RELEASE}")

set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 0)
set(CMAKE_LIBRARY_ARCHITECTURE "${UE_ARCHITECTURE_TRIPLE}")
set(CMAKE_C_COMPILER_TARGET "${UE_ARCHITECTURE_TRIPLE}")
set(CMAKE_CXX_COMPILER_TARGET "${UE_ARCHITECTURE_TRIPLE}")

set(CMAKE_SYSROOT "${UE_TOOLCHAIN_ROOT_DIR}")

# WORKAROUND
# this forces the linker to not link to system's glibc
# espcially on systems with newer glibc results in linker issues
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${UE_TOOLCHAIN_ROOT_DIR}" CACHE INTERNAL "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${UE_TOOLCHAIN_ROOT_DIR}" CACHE INTERNAL "" FORCE)

set(CMAKE_FIND_ROOT_PATH "${UE_TOOLCHAIN_ROOT_DIR}")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Due to LTO optimization being enabled,
# first prefer the LLVM Archiver,
# if not then GCC Archiver,
# otherwise the Binutils Archiver
if (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/llvm-ar")
    # LLVM Archiver
    set(CMAKE_AR "${UE_TOOLCHAIN_BIN_DIR}/llvm-ar")
elseif (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-gcc-ar")
    # GCC Archiver
    set(CMAKE_AR "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-gcc-ar")
else ()
    # Binutils Archiver
    set(CMAKE_AR "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-ar")
endif ()

set(CMAKE_ASM_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-as")

if (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/clang")
    set(CMAKE_C_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/clang")
elseif (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-gcc")
    set(CMAKE_C_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-gcc")
else ()
    set(CMAKE_C_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-cc")
endif ()

set(CMAKE_RANLIB "/bin/true")

set(CMAKE_C_COMPILER_AR "${CMAKE_AR}")
set(CMAKE_C_COMPILER_RANLIB "${CMAKE_RANLIB}")
set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_C_PREPROCESSOR "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-cpp")

if (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/clang++")
    set(CMAKE_CXX_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/clang++")
elseif (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-g++")
    set(CMAKE_CXX_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-g++")
else ()
    set(CMAKE_CXX_COMPILER "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-c++")
endif ()

set(CMAKE_CXX_COMPILER_AR "${CMAKE_AR}")
set(CMAKE_CXX_COMPILER_RANLIB "${CMAKE_RANLIB}")
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

set(CMAKE_LINKER "${CMAKE_CXX_COMPILER}")

# Prefer the Binutils nm over the GCC nm
if (EXISTS "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-nm")
    set(CMAKE_NM "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-nm")
else ()
    set(CMAKE_NM "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-gcc-nm")
endif ()

set(CMAKE_OBJCOPY "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-objcopy")
set(CMAKE_OBJDUMP "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-objdump")
set(CMAKE_STRIP "${UE_TOOLCHAIN_BIN_DIR}/${UE_ARCHITECTURE_TRIPLE}-strip")

Then for every toolchain/target combination we do something like this (e.g. v20/AArch64):

set(UE_ARCHITECTURE_TRIPLE  "aarch64-unknown-linux-gnueabi")

set(CMAKE_C_FLAGS           "-march=armv8-a")
set(CMAKE_CXX_FLAGS         "-march=armv8-a")
set(CMAKE_SYSTEM_PROCESSOR  "aarch64")

set(CMAKE_C_COMPILER_WORKS      ON)
set(CMAKE_CXX_COMPILER_WORKS    ON)

set(UE_TOOLCHAIN_FILE   "${CMAKE_CURRENT_LIST_DIR}/linux-toolchain-v20.cmake")

include("${UE_TOOLCHAIN_FILE}")

As you can see there are lots of options that need to be enabled/disabled in order to be able to target Unreal (e.g. -fuse-ld=lld -nodefaultlibs -lc -lc++ -lc++abi -lm -m64 -stdlib=libc++ for the linker flags and -fcxx-exceptions -fexceptions -frtti -nostdinc -nostdinc++ -isystem for the CXX flags, etc; some other hacks like -nostdinc -nostdinc++ for older versions are necessary; also CMAKE_POSITION_INDEPENDENT_CODE is necessary which I think is enabled by default in Rust).

So, I'm not even sure if such a thing is even possible with the current version of Rust and Cargo. This is a show-stopper for us in using Rust if we cannot link to the libc++ and glibc from the toolchain provided by Epic.

I'd appreciate any kind of help/direction I could get on this.

It's kinda hacky, but what about setting ar and/or linker to a shell script which takes arguments from rustc and massages them to work with your toolchain?

You could even implement a thing which selects the right toolchain version (e.g. it might scan your CMakeLists.txt file or an environment variable or whatever).

I haven't touched this unreal toolchain, but I have worked with yocto ones (for industrial automation). I'm not at my work computer now, so I can't check the exact flag I needed, but I do remember having to pass a sysroot flag, probably to gcc/clang which in turn would invoke the linker. I don't see that in your example code, where you just override ar and linker.

One difference is that I was building a program, not a dynamic library. But passing --sysroot to gcc/clang should matter in both cases.

(By the way, I very much doubt that changing ar is going to matter at all. Ar just creates an archive of object files. Not much platform specific there. But better safe than sorry.)

You should be able to pass flags to the linker like so:

[build]
rustflags = ["-C", "link-args=--sysroot=..."]

(etc for other flags)

Thank you for the responses.

@Michael-F-Bryan I have to try this out.

@Vorpal I did try that, but I've got the same results. I even tried to add more flags like my CMake toolchain but it did not work:

rustflags = ["-C", "link-args=-fuse-ld=lld -nodefaultlibs -lc -lc++ -lc++abi -lm -m64 -stdlib=libc++ -lgcc_s -lgcc --sysroot=/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu target-feature=+crt-static"]

OK, I've made some progress. It seems for not-so-obvious reasons Cargo ignores the rustflags settings inside the target section (how do I know? For example there's no sysroot in the error output). But, using the profiles feature I've managed some progress.

I added this to the top of Cargo.toml:

cargo-features = ["profile-rustflags"]

Then this inside .cargo/config.toml in a simple hello world project:

[target.aarch64-unknown-linux-gnu]
ar = "/path/to/toolchains/linux-v20/aarch64-unknown-linux-gnueabi/bin/llvm-ar"
linker = "/path/to/toolchains/linux-v20/aarch64-unknown-linux-gnueabi/bin/clang++"

[target.x86_64-unknown-linux-gnu]
ar = "/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/llvm-ar"
linker = "/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++"

[profile.dev]

[profile.release]

[profile.aarch64-unknown-linux-gnu-debug]
inherits = "dev"
rustflags = [
    "-C", "link-args=-fuse-ld=lld",
    "-C", "link-args=-nodefaultlibs",
    "-C", "link-args=-lc",
    "-C", "link-args=-lc++abi",
    "-C", "link-args=-lm",
    "-C", "link-args=-m64",
    "-C", "link-args=-stdlib=libc++",
    "-C", "link-args=-lgcc_s",
    "-C", "link-args=-lgcc",
    "-C", "link-args=--sysroot=/path/to/toolchains/linux-v20/aarch64-unknown-linux-gnu",
]

[profile.aarch64-unknown-linux-gnu-release]
inherits = "release"
rustflags = [
    "-C", "link-args=-fuse-ld=lld",
    "-C", "link-args=-nodefaultlibs",
    "-C", "link-args=-lc",
    "-C", "link-args=-lc++abi",
    "-C", "link-args=-lm",
    "-C", "link-args=-m64",
    "-C", "link-args=-stdlib=libc++",
    "-C", "link-args=-lgcc_s",
    "-C", "link-args=-lgcc",
    "-C", "link-args=--sysroot=/path/to/toolchains/linux-v20/aarch64-unknown-linux-gnu",
    "-C", "link-args=-Wl,--gc-sections -Wl,--strip-all",
]

[profile.x86-64-unknown-linux-gnu-debug]
inherits = "dev"
rustflags = [
    "-C", "link-args=-fuse-ld=lld",
    "-C", "link-args=-nodefaultlibs",
    "-C", "link-args=-lc",
    "-C", "link-args=-lc++abi",
    "-C", "link-args=-lm",
    "-C", "link-args=-m64",
    "-C", "link-args=-stdlib=libc++",
    "-C", "link-args=-lgcc_s",
    "-C", "link-args=-lgcc",
    "-C", "link-args=--sysroot=/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu",
]

[profile.x86-64-unknown-linux-gnu-release]
inherits = "release"
rustflags = [
    "-C", "link-args=-fuse-ld=lld",
    "-C", "link-args=-nodefaultlibs",
    "-C", "link-args=-lc",
    "-C", "link-args=-lc++abi",
    "-C", "link-args=-lm",
    "-C", "link-args=-m64",
    "-C", "link-args=-stdlib=libc++",
    "-C", "link-args=-lgcc_s",
    "-C", "link-args=-lgcc",
    "-C", "link-args=--sysroot=/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu",
    "-C", "link-args=-Wl,--gc-sections -Wl,--strip-all",
]

And, when I run the following, it builds:

$  cargo build --target x86_64-unknown-linux-gnu --profile x86-64-unknown-linux-gnu-release

But, when I do:

$ cargo build --target aarch64-unknown-linux-gnu --profile aarch64-unknown-linux-gnu-release
   Compiling libc v0.2.149
   Compiling current_platform v0.2.0
error: linking with `/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/mamadou/.cargo/bin:/opt/android/ant/bin:/home/mamadou/android-studio/jre/bin:/opt/android/sdk/ndk/25.1.8937393:/opt/android/sdk/extras/android/support:/opt/android/sdk/build-tools:/opt/android/sdk/platform-tools:/opt/android/sdk/tools:/home/mamadou/.local/lib64/perl5/bin:/usr/local/bin:/usr/local/sbin:/opt/android/ant/bin:/home/mamadou/android-studio/jre/bin:/opt/android/sdk/ndk/25.1.8937393:/opt/android/sdk/extras/android/support:/opt/android/sdk/build-tools:/opt/android/sdk/platform-tools:/opt/android/sdk/tools:/home/mamadou/.local/lib64/perl5/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin:/usr/lib/llvm/15/bin:/usr/lib/llvm/14/bin:/home/mamadou/dev/go/bin:/home/mamadou/dev/go/bin" VSLANG="1033" "/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++" "-m64" "/tmp/rustcbl2xk8/symbols.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/current_platform-d4e4608d9cb3b350/build_script_build-d4e4608d9cb3b350.build_script_build.5dd10f708cf28276-cgu.0.rcgu.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/current_platform-d4e4608d9cb3b350/build_script_build-d4e4608d9cb3b350.h44w5zohudneaai.rcgu.o" "-Wl,--as-needed" "-L" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/deps" "-L" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-64b7f85860d8e80f.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a6e3f3f120c06a9d.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-d9e2b538f14471de.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-19a8f7e0e3580341.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-42fee89bdf9b228e.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-34254f7cdcc0cc46.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-44ef65cfbf83fcb8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-c65acc9d349f7532.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-9c6ad0c05ee0b09a.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-decf62ffc2a6d6e8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-26adb3c50c2a21e1.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-0e893a92b0bf83dd.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-2ff912bc722ce8c9.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-5ec13f05f8d2bf50.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-2b93db426c5a26c2.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-26def7a8e3a3dff8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-cd2f2bc505f56f50.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-ec02dd343723da85.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-2f4d671a9b4d2563.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/current_platform-d4e4608d9cb3b350/build_script_build-d4e4608d9cb3b350" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-fuse-ld=lld" "-nodefaultlibs" "-lc" "-lc++abi" "-lm" "-m64" "-stdlib=libc++" "-lgcc_s" "-lgcc" "--sysroot=/path/to/toolchains/linux-v20/aarch64-unknown-linux-gnu" "-Wl,--gc-sections" "-Wl,--strip-all"
  = note: ld.lld: error: cannot open Scrt1.o: No such file or directory
          ld.lld: error: cannot open crti.o: No such file or directory
          ld.lld: error: unable to find library -lgcc_s
          ld.lld: error: unable to find library -lutil
          ld.lld: error: unable to find library -lrt
          ld.lld: error: unable to find library -lpthread
          ld.lld: error: unable to find library -lm
          ld.lld: error: unable to find library -ldl
          ld.lld: error: unable to find library -lc
          ld.lld: error: unable to find library -lc
          ld.lld: error: unable to find library -lc++abi
          ld.lld: error: unable to find library -lm
          ld.lld: error: unable to find library -lgcc_s
          ld.lld: error: cannot open crtn.o: No such file or directory
          clang++: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `current_platform` (build script) due to previous error
warning: build failed, waiting for other jobs to finish...
error: linking with `/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/mamadou/.cargo/bin:/opt/android/ant/bin:/home/mamadou/android-studio/jre/bin:/opt/android/sdk/ndk/25.1.8937393:/opt/android/sdk/extras/android/support:/opt/android/sdk/build-tools:/opt/android/sdk/platform-tools:/opt/android/sdk/tools:/home/mamadou/.local/lib64/perl5/bin:/usr/local/bin:/usr/local/sbin:/opt/android/ant/bin:/home/mamadou/android-studio/jre/bin:/opt/android/sdk/ndk/25.1.8937393:/opt/android/sdk/extras/android/support:/opt/android/sdk/build-tools:/opt/android/sdk/platform-tools:/opt/android/sdk/tools:/home/mamadou/.local/lib64/perl5/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/17/bin:/usr/lib/llvm/16/bin:/usr/lib/llvm/15/bin:/usr/lib/llvm/14/bin:/home/mamadou/dev/go/bin:/home/mamadou/dev/go/bin" VSLANG="1033" "/path/to/toolchains/linux-v20/x86_64-unknown-linux-gnu/bin/clang++" "-m64" "/tmp/rustckJMEF3/symbols.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/libc-c07f2a90dc20fad5/build_script_build-c07f2a90dc20fad5.build_script_build.6dcdae1df2e66ffe-cgu.0.rcgu.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/libc-c07f2a90dc20fad5/build_script_build-c07f2a90dc20fad5.build_script_build.6dcdae1df2e66ffe-cgu.1.rcgu.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/libc-c07f2a90dc20fad5/build_script_build-c07f2a90dc20fad5.build_script_build.6dcdae1df2e66ffe-cgu.2.rcgu.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/libc-c07f2a90dc20fad5/build_script_build-c07f2a90dc20fad5.build_script_build.6dcdae1df2e66ffe-cgu.3.rcgu.o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/libc-c07f2a90dc20fad5/build_script_build-c07f2a90dc20fad5.2p0wwrsyz8q4vlsd.rcgu.o" "-Wl,--as-needed" "-L" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/deps" "-L" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-64b7f85860d8e80f.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a6e3f3f120c06a9d.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-d9e2b538f14471de.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-19a8f7e0e3580341.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-42fee89bdf9b228e.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-34254f7cdcc0cc46.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-44ef65cfbf83fcb8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-c65acc9d349f7532.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-9c6ad0c05ee0b09a.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-decf62ffc2a6d6e8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-26adb3c50c2a21e1.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-0e893a92b0bf83dd.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-2ff912bc722ce8c9.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-5ec13f05f8d2bf50.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-2b93db426c5a26c2.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-26def7a8e3a3dff8.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-cd2f2bc505f56f50.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-ec02dd343723da85.rlib" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-2f4d671a9b4d2563.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/mamadou/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/mamadou/dev/rust/foo/target/aarch64-unknown-linux-gnu-release/build/libc-c07f2a90dc20fad5/build_script_build-c07f2a90dc20fad5" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-fuse-ld=lld" "-nodefaultlibs" "-lc" "-lc++abi" "-lm" "-m64" "-stdlib=libc++" "-lgcc_s" "-lgcc" "--sysroot=/path/to/toolchains/linux-v20/aarch64-unknown-linux-gnu" "-Wl,--gc-sections" "-Wl,--strip-all"
  = note: ld.lld: error: cannot open Scrt1.o: No such file or directory
          ld.lld: error: cannot open crti.o: No such file or directory
          ld.lld: error: unable to find library -lgcc_s
          ld.lld: error: unable to find library -lutil
          ld.lld: error: unable to find library -lrt
          ld.lld: error: unable to find library -lpthread
          ld.lld: error: unable to find library -lm
          ld.lld: error: unable to find library -ldl
          ld.lld: error: unable to find library -lc
          ld.lld: error: unable to find library -lc
          ld.lld: error: unable to find library -lc++abi
          ld.lld: error: unable to find library -lm
          ld.lld: error: unable to find library -lgcc_s
          ld.lld: error: cannot open crtn.o: No such file or directory
          clang++: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `libc` (build script) due to previous error

I'm not sure, why Cargo picks up the wrong linker path.