How to use LLVM instead of MSVC++?


#1

Is it possible to use the LLVM toolchain with rustc to target to the specified CPU?

I’ve it installed, but running rustc with option -C target-cpu=powerpc64 tells 'powerpc64' is not a recognized processor for this target (ignoring processor). (I’m just targetting powerpc64 for checking if it works, since LLVM supports it, anyways.)

P.S.: I know LLVM doesn’t come with a linker, but in my case I’ll attempt to build my own. For now I’m just using the --crate-type=staticlib option for emmiting the default linker.


Tl; dr: How to specify a (default) toolchain to use within rustc, like LLVM?


#2

I don’t think you’ll get a lot of mileage trying to cross-compile on Windows using the MSVC toolchain, except for 32-bit x86.

If you want to kick the tires on this and you’re running Windows 10, your best bet is to install WSL and use the Linux Rust toolchain instead. There’s some good documentation on cross-compiling here.


#3

I should really say you shouldn’t expect to get good mileage on cross-compiling on Windows using either of the native Windows toolchains (msvc or gnu). Windows doesn’t support PowerPC 64 (AFAIK), so I’m guessing you want a Linux executable anyway, in which case your best bet is to use a Linux toolchain rather than a Windows to do the cross-compiling.


#4

I don’t think you’ll get a lot of mileage trying to cross-compile on Windows using the MSVC toolchain, except for 32-bit x86.

Actually I wanted to use the LLVM toolchain, but rustup someway uses the MSVC++ toolchain by default, so I wanted to know how I could replace the toolchain by LLVM.

If you want to kick the tires on this and you’re running Windows 10, your best bet is to install WSL 1 and use the Linux Rust toolchain instead. There’s some good documentation on cross-compiling here.

Running Linux won’t allow still me to use the LLVM toolchain, I guess, unless I don’t know how to specify it.


#5

My understanding is that Rust needs an external linker on Windows, and the only ones currently supported are MSVC and GNU/MinGW-w64.

Maybe in the future lld could be used, but that would be a future effort that’s not currently supported.


#6

A key point here is that, as far as I know, Rust always uses LLVM for code generation. They key difference between the MSVC and MinGW targets are related to ABI compatibility. In general you should always use the MSVC target if you’re building for Windows.


#7

When you use --target Rust already uses LLVM to generate code for that CPU. Adding the target in rustup is all you need for that. Where it breaks is a) the linker b) the system libraries.

Rust doesn’t know how to make an executable. It only knows how to make object files and static libraries. So it can easily make a static library for PowerPC, but to go from a static library to an actual executable or dynamic library, you need to somehow find PowerPC-compatible linker and PowerPC libc on Windows.


#8

You need to pass --target for cross-compilation. Which OS are you targeting? You can see a list of valid targets using rustup target list.