Yes there is. The Rust compiler itself. The entire compiler is a library and rustc
is just a small crate that calls into the compiler. As an example there's the stupid-stats
crate. It runs the rust compiler to generate some statistics about the code.
All you need is to import the rustc
and rustc_driver
crates (with extern crate
) and implement the rustc_driver::CompilerCalls
trait for a type (lets call it MyDriver
). Then you can run rustc like this:
let args: Vec<_> = std::env::args().collect();
let mut my_driver = MyDriver::new();
rustc_driver::run_compiler(&args, &mut my_driver);
You need to make sure that the path to the standard library and core library is passed. In my case I added
"-L $HOME/.multirust/toolchains/nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib"
to the command line. You cannot simply add this to the args
vector, because $HOME
isn't parsed here. So you need some more code that extracts the $HOME
env var and builds your command.