Skip to content

Commit

Permalink
Independence from the user toolchain (#32)
Browse files Browse the repository at this point in the history
## What Changed?

Sets the `SYSROOT` env variable for plugin execution so that we link
against the `std` lib that the tool was compiled against. This fixes the
"invalid metadata file" errors we were getting when tryin got run on
projects using a different toolchain version than the one we use.

Much of the "heavy lifting" is done by my `rustc_plugin` fork and not in
the prettiest way. A PR to push those changes upstream is coming.

## Why Does It Need To?

This creates a larger degree of independence from a user's toolchain.
Now a user of `dfpp` doesn't have to use the same toolchain version and
can e.g. use `stable`. It is now possible to run `cargo dfpp` straight
out of the box with no configuration on any project using "not quite
totally recent" rust versions.

Note that this only means the user no longer has to explicitly configure
their project to use the nightly toolchain that dfpp uses, but it is
still implicitly compiled against e.g. `nightly-2023-04-12`. As such
"missing feature annotations" can still happen, but this also gives us a
path forward to handle them but implicitly enabling them.

## Checklist

- [x] Above description has been filled out so that upon quash merge we
have a
  good record of what changed.
- [x] New functions, methods, types are documented. Old documentation is
updated
  if necessary
- [x] Documentation in Notion has been updated
- [x] Tests for new behaviors are provided
  - [x] New test suites (if any) ave been added to the CI tests (in
`.github/workflows/rust.yml`) either as compiler test or integration
test.
*Or* justification for their omission from CI has been provided in this
PR
    description.
  • Loading branch information
JustusAdam authored Sep 5, 2023
1 parent 8c5dfa1 commit d6130ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ rustc_private = true
#flowistry = { path = "../flowistry/crates/flowistry" }
flowistry = { git = "https://github.com/brownsys/flowistry", rev = "f3b27bb1e06a13c599ea56beca7aa047f7d291e5" }

#rustc_plugin = { git = "https://github.com/willcrichton/flowistry", rev = "1f469a4cce9f2e240ad14b5145669a8011d4e6b2" }
#rustc_plugin = { path = "../rustc_plugin/crates/rustc_plugin" }
#rustc_plugin = { git = "https://github.com/brownsys/flowistry", rev = "6e1de0459ec32bfd9ad046491a556410942d6ddc" }
#rustc_utils = { path = "../rustc_plugin/crates/rustc_utils"}
rustc_utils = "=0.6.0-nightly-2023-04-12"
rustc_plugin = "=0.6.0-nightly-2023-04-12"
Expand Down Expand Up @@ -71,5 +69,5 @@ debug = true

[replace]

"rustc_utils:0.6.0-nightly-2023-04-12" = { git = "https://github.com/JustusAdam/rustc_plugin", rev = "344ecb3bc056c641eaf9d06792571b3d81c8eb33" }
"rustc_plugin:0.6.0-nightly-2023-04-12" = { git = "https://github.com/JustusAdam/rustc_plugin", rev = "344ecb3bc056c641eaf9d06792571b3d81c8eb33" }
"rustc_utils:0.6.0-nightly-2023-04-12" = { git = "https://github.com/JustusAdam/rustc_plugin", rev = "a50617487e129623df946c42d554bd8080765096" }
"rustc_plugin:0.6.0-nightly-2023-04-12" = { git = "https://github.com/JustusAdam/rustc_plugin", rev = "a50617487e129623df946c42d554bd8080765096" }
22 changes: 16 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ fn add_link_search_path_for_compiler_binaries(s: impl std::fmt::Display) {
add_link_arg_for_compiler_binaries(format!("-Wl,-rpath,{s}"))
}

/// The "SYSROOT" path for the toolchain we're compiling against.
/// ($RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN)
fn rustup_toolchain_path() -> PathBuf {
let rustup_home = env::var("RUSTUP_HOME").unwrap();
let rustup_tc = env::var("RUSTUP_TOOLCHAIN").unwrap();
[&rustup_home, "toolchains", &rustup_tc]
.into_iter()
.collect()
}

/// Taken from Kani
/// (<https://github.com/model-checking/kani/blob/3d8ceddb0672e1dda6c186830f411c979bc132e2/kani-compiler/build.rs>)
/// this code links the rustc libraries directly with the compiled binaries.
pub fn link_rustc_lib() {
// Add rustup to the rpath in order to properly link with the correct rustc version.
let rustup_home = env::var("RUSTUP_HOME").unwrap();
let rustup_tc = env::var("RUSTUP_TOOLCHAIN").unwrap();
let rustup_lib = [&rustup_home, "toolchains", &rustup_tc, "lib"]
.into_iter()
.collect::<PathBuf>();
let mut rustup_lib = rustup_toolchain_path();
rustup_lib.push("lib");
add_link_search_path_for_compiler_binaries(rustup_lib.display());

// While we hard-code the above for development purposes, for a release/install we look
Expand Down Expand Up @@ -54,5 +61,8 @@ fn main() {
println!(
"cargo:rustc-env=BUILD_TIME={}",
chrono::Local::now().format("%a %b %e %T %Y")
)
);

let toolchain_path = rustup_toolchain_path();
println!("cargo:rustc-env=SYSROOT_PATH={}", toolchain_path.display());
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl rustc_plugin::RustcPlugin for DfppPlugin {
rustc_plugin::RustcPluginArgs {
args: Args::from_parseable(args.args).unwrap(),
filter: CrateFilter::OnlyWorkspace,
env: vec![("SYSROOT".into(), env!("SYSROOT_PATH").into())],
}
}

Expand Down

0 comments on commit d6130ae

Please sign in to comment.