Is it possible to cross-compile to a host/platform in which Rust codebase has no support for?

Let's suppose I want to cross-compile a Rust code to a host/platform in which Rust codebase has no support for, such as MorphOS.

Is it possible to do that?

I know that Cargo emit LLVM IR; I was thinking on convert the IR to C using llvm-cbe and compile the C code generated using an appropriate C compiler.

/\ Is this idea valid? Will it break compatibility, has it bugs or it will not work?

I'm not an expert on this, but in theory, most likely LLVM already has the codegen backend for your CPU architecture, you only need a linker (and of course, you must port/implement the libraries, I think at least core to make a no_std crate work)

2 Likes

@nerditation

Sorry for asking this, but I'm newbie yet introducing myself into the language, the libraries you said are crates? How could I port the libraries to an host in which Rust has no codebase support for?

The porting you said, is it compiling the core library with a cross-compiler (if the answer is yes, it's easy to do that with help of crossdev of Gentoo).

Generally no.

LLVM's IR is not portable. It's not like JVM bytecode. It's more like assembly. It encodes a lot of specific and implicit assumptions about the target platform.

In a very limited way — if you compile a static library for a close-enough other existing target (a similar PPC target), without using Rust's std or anything reliant on the target OS, and then use target's linker to make appropriate format of executable, then maaaybe the raw machine code from Rust will be close enough to make it work.

More realistically, if Rust doesn't already support some platform, but LLVM does, then you can add the target to Rust.

If you want to go through a C compiler, then mrustc may be easier.

2 Likes

Small correction: LLVM IR is very similar to JVM bytecode but it usually encodes a lot of specific and implicit assumptions about the target platform.

pNaCl and RenderScript are examples of use of LLVM IR in a target-agnostic fashion.

This being said rustc definitely is not like that and you couldn't use rustc-generated IR like that. PowerPC is supported target for rustc thus creating MorphOS port doesn't sound infeasible. But I don't think trying to deal with IR is good idea: both pNaCl and RenderScript are deprecated because IR from old version of LLVM couldn't be used in new version of LLVM and with “evergreen” Rust approach to compatibility… it just wouldn't work.

If you want to cross-compile for MorphOS then you would need to create Rust toolchain with MorphOS support. It may not be easy but all other ways are even more problematic.

1 Like

@kornel

More realistically, if Rust doesn't already support some platform, but LLVM does, then you can add the target to Rust.

If I add a new target to my local Rust environment using the tutorial you referenced in the link, will it only supports no_std development?

If you want to go through a C compiler, then mrust may be easier.

Could mrustc be a reasonable solution when cross-compiling to a host that LLVM does not support? I mean, a very very specific host that only GCC supports.

Does mrustc compile all the crates together with the code?

Custom target will typically be no_std, because to have std you have to port it first. That's quite a lot of extra work, obviously.

mrustc is not a full-featured Rust implementation. It supports only older versions of Rust, and doesn't have a borrow checker. It's intended to compile rustc proper, and not much beyond that. I don't know how well it handles uncommon targets.

If you use mrustc with a target not supported by rustc, you still have to port libstd to be able to use it. It uses the exact same libstd as rustc, just an older version.

If your target is only supported by GCC, you may want to try GitHub - rust-lang/rustc_codegen_gcc: libgccjit AOT codegen for rustc.

@kornel @bjorn3

Thanks for the clarifications.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.