Integrating rustc into a standalone program

Hello!

So, I'm currently making a software which must be able to compile Rust code (that is partly/totally auto-generated). The software should be accessible even to non-coders, and thus should ideally not force the user to install Rustup as well as an entire Rust tool-chain on their machine.

So far, I use the internal rustc_... crates to do so. I also had to include the dynamic libraries librustc_driver, libstd and libLLVM, as the resulting binary required them. But, to compile your usual Rust source code, you need access to the core Rust crates, such as core, and most likely std, alloc, etc. Which are usually located in:

{RUSTUP_HOME}/toolchains/{CURRENT_TOOLCHAIN}/lib/rustlib/{TARGET}/lib

My question is the following: how would you go about this? Would you include all of the core Rust crates as well? Would you resort to asking the user to install Rust? Would you do something entirely different?

Also, how does this compare to the the way one would achieve this in C/C++, e.g. using libclang ?

Thanks in advance!

Write code to be compiled into files and use the command-line tool. That's how the Rust toolchain is designed, that's what RustC developers expect, that's how the toolchain works best.

Tighter integration, like linking libraries into the binary, just brings up all kinds of headaches. Apparently you experienced some of them already: Rust requires all kinds of helper tools and crates, you'd have to maintain all of them. Other headaches would be dealing with updates, each tiny bugfix in the toolchain would require a new version of your software. Unless you have a bigger development team you'll have a hard time to keep up with all this, let alone advance your own software.

Don't re-invent the wheel, invent the future :slight_smile:

P.S.: rustup ist just an installer, don't rely on that. Here on Debian/Ubuntu Rust works just fine, but no rustup anywhere. To find RustC, use ...

which rustc

... on Linux and MacOS. Windows has certainly something similar.

libclang-based tools typically require the user to have already installed a full dev environment.

Okay then, I guess the solution will to be suggest the user to let the software install Rust automatically (if it is not there already), with the option to go the Rust website.

Thanks for the replies!

1 Like

%SYSTEMROOT%\system32\where.exe

The other alternative is to search PATH yourself, although if you do, don't forget to account for PATHEXT.

1 Like

IIRC it is possible to have rustup do an unattended install, so that could be made painless, in principle.

But given that a virgin Rust install (i.e. no crates downloaded from crates.io yet) can already occupy a couple of GiB, it is not so straightforward whether an unattended install for other people is wise.

2 Likes

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.