Rustc with manually built LLVM toolchain

Hello,

I’m absolutely new to rust but want to write my first lines to be compiled for a target which is not yet supported by LLVM upstream. So I built the LLVM fork which supports my target architecture, myself. Now I figure, I have to tell rustc to use the manually compiled toolchain instead of what it dragged in as dependencies on my OS (Debian). Whatever I tried so far, rustc --print target-list never showed the LLVM target from my manually built LLVM toolchain. What do I have to do in order to make rust use a different LLVM toolchain? Does it work that way at all? If not, could anybody provide any pointer which might help me what uses/calls/interfaces what and expects what being where?

Thanks a lot in advance!

mirko

1 Like

If you have LLVM with a custom target added you’ll have to build your own rustc and point to the custom LLVM toolchain during the build.

“Pointing” is done via specifying a path to llvm-config in the custom toolchain in llvm-config variable in config.toml (see config.toml.example for all the available configuration).

Beside that, you’ll need to register the new target in rustc's LLVM wrapper.
These PRs (https://github.com/rust-lang/rust/pull/41524, https://github.com/rust-lang/rust/pull/52787) show how it’s done (the librustc_llvm, rustllvm and librustc_target/spec parts specifically, the capi* part is not a first concern).

2 Likes

Thanks, that’s really helpful! Could you elaborate on details why rustc needs to be recompiled? I figured, if it uses LLVM it’s linked against respective libs dynamically and might just be able to pick up (new) targets provided by the LLVM backend? Obvously/Apparently that’s not the case - but why’s that?

1 Like

Because rustc statically links against its llvm by default. I think there was some work to dynamically link, especially to accommodate some Linux distros, but not sure the status of it (also it can be dangerous if you don’t dynamically link against a compatible version, and some other details)

1 Like

Dynamic linking LLVM works fine (I use it), but it’s not the default and you need to enable it. The config key is “link-shared”.

2 Likes

Thanks! I now managed to (statically) link rustc against my custom LLVM toolchain. However rustc didn’t list the new target until I manually patched it into it. That raises the question, why rustc needs to maintain its own hard-compiled list of targets rather than querying and using supported targets provided by LLVM?

1 Like

TargetOptions contains a lot of fields which can not be queried from LLVM, or are there to workaround known LLVM issues.

1 Like