Minimal rustc flags needed to compile a single file which depends on other crates, using compilations from a previous `cargo build` run?

First I'll explain what I think I need, in terms which should be easier to answer, and then I'll explain what I'm actually doing in case anyone can suggest a better approach to accomplish the same goal.

What I think I need:

Given a crate which depends on some other extern crates (i.e. it contains use statements), I want to know the easiest way to:

  • First run cargo build on my project, compiling all deps
  • Then run rustc ... on a single file in that crate, with the proper flags, such that that single file compiles.

I understand that using rustc requires a lot of flags. My hope was that by using cargo build first to compile everything, then I could just set a search path to -L target/debug/deps, and then specify some externs with --extern mycrate, etc. This isn't working though. Am I on the right track in trying to make this work, or is this too simple to work?

What I'm actually trying to do:

I'm trying to use rustc_interface::run_compiler() to play with the HIR of my multi-crate project. To do that, I need to set the right Options (which corresponds to rustc flags) to get the compiler to run. Once it runs, I want to generate HIR for each file and do some analytics on that. In particular I want to build a dependency graph of every type in my project, to see which types use which other types.

The code below is what I want to run. I am just asking what config would need to be to make this compile (I have omitted my attempts at setting up the config, but I described what I have tried already at the top of this question).

    rustc_interface::run_compiler(config, |compiler| {
        compiler.enter(|queries| {
            queries.global_ctxt().unwrap().enter(|tcx| {
                let hir = tcx.hir();
                for id in hir.items() {
                    // analyze...

I'm testing this on a simple two-crate project, where one crate has a single public struct, and the other crate uses the first crate's struct in its own struct. I can't get the code above to resolve the first crate when compiling the second.

I assumed this would be the most straightforward way to get access to an in-memory HIR tree. I know I can generate HIR output directly with e.g. rust-analyzer, but then I'm not sure how I can parse it back into a tree that I can easily do analytics on.

1 Like

Actually, I did a sanity check here and I see that I can run rustc with the args I described above, and my file compiles correctly:

rustc crates/beta/src/ --edition 2021 --crate-type lib -L target/debug/deps --extern alpha         

This works. (beta depends on alpha).

I wound up solving my problem by finding a way to parse cmd line args back into a rustc_session:config::Options struct. I was attempting to set the Options manually, but apparently I wasn't doing it correctly. I still don't know how to manually set the options, but I will just parse cmd line strings now since it clearly works better.

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.